blob: 2a4369aeb1eefeea61befb3d2cc22f16fbed543e [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 Crond18b5f82020-11-25 16:46:05 +01001484static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1485 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001486 uint8_t *data,
1487 size_t data_size,
1488 size_t *data_length )
1489{
Ronald Crond18b5f82020-11-25 16:46:05 +01001490 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001491 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001492 memcpy( data, key_buffer, key_buffer_size );
1493 memset( data + key_buffer_size, 0,
1494 data_size - key_buffer_size );
1495 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001496 return( PSA_SUCCESS );
1497}
1498
Ronald Cron7285cda2020-11-26 14:46:37 +01001499psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001500 const psa_key_attributes_t *attributes,
1501 const uint8_t *key_buffer, size_t key_buffer_size,
1502 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503{
Ronald Crond18b5f82020-11-25 16:46:05 +01001504 psa_key_type_t type = attributes->core.type;
1505
Gilles Peskine5d309672019-07-12 23:47:28 +02001506#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1507 const psa_drv_se_t *drv;
1508 psa_drv_se_context_t *drv_context;
1509#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1510
Ronald Cron9486f9d2020-11-26 13:36:23 +01001511#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Crond18b5f82020-11-25 16:46:05 +01001512 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001513 {
1514 if( ( drv->key_management == NULL ) ||
1515 ( drv->key_management->p_export == NULL ) )
1516 return( PSA_ERROR_NOT_SUPPORTED );
1517
1518 return( drv->key_management->p_export(
Ronald Crond18b5f82020-11-25 16:46:05 +01001519 drv_context,
1520 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cron9486f9d2020-11-26 13:36:23 +01001521 data, data_size, data_length ) );
1522 }
1523#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1524
Ronald Crond18b5f82020-11-25 16:46:05 +01001525 if( key_type_is_raw_bytes( type ) ||
1526 PSA_KEY_TYPE_IS_RSA( type ) ||
1527 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001528 {
1529 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001530 key_buffer, key_buffer_size,
1531 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001532 }
1533 else
1534 {
1535 /* This shouldn't happen in the reference implementation, but
1536 it is valid for a special-purpose implementation to omit
1537 support for exporting certain key types. */
1538 return( PSA_ERROR_NOT_SUPPORTED );
1539 }
1540}
1541
1542psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1543 uint8_t *data,
1544 size_t data_size,
1545 size_t *data_length )
1546{
1547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1548 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1549 psa_key_slot_t *slot;
1550
1551 /* Set the key to empty now, so that even when there are errors, we always
1552 * set data_length to a value between 0 and data_size. On error, setting
1553 * the key to empty is a good choice because an empty key representation is
1554 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001555 *data_length = 0;
1556
Ronald Cron9486f9d2020-11-26 13:36:23 +01001557 /* Export requires the EXPORT flag. There is an exception for public keys,
1558 * which don't require any flag, but
1559 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1560 */
1561 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1562 PSA_KEY_USAGE_EXPORT, 0 );
1563 if( status != PSA_SUCCESS )
1564 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001565
Gilles Peskinef9168942019-09-12 19:20:29 +02001566 /* Reject a zero-length output buffer now, since this can never be a
1567 * valid key representation. This way we know that data must be a valid
1568 * pointer and we can do things like memset(data, ..., data_size). */
1569 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001570 {
1571 status = PSA_ERROR_BUFFER_TOO_SMALL;
1572 goto exit;
1573 }
1574
Ronald Crond18b5f82020-11-25 16:46:05 +01001575 psa_key_attributes_t attributes = {
1576 .core = slot->attr
1577 };
1578 status = psa_export_key_internal( &attributes,
1579 slot->key.data, slot->key.bytes,
1580 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001581
1582exit:
1583 unlock_status = psa_unlock_key_slot( slot );
1584
1585 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1586}
1587
Ronald Cron7285cda2020-11-26 14:46:37 +01001588psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001589 const psa_key_attributes_t *attributes,
1590 const uint8_t *key_buffer,
1591 size_t key_buffer_size,
1592 uint8_t *data,
1593 size_t data_size,
1594 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001595{
Ronald Crond18b5f82020-11-25 16:46:05 +01001596 psa_key_type_t type = attributes->core.type;
1597 psa_key_lifetime_t lifetime = attributes->core.lifetime;
1598
Ronald Cron9486f9d2020-11-26 13:36:23 +01001599#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1600 const psa_drv_se_t *drv;
1601 psa_drv_se_context_t *drv_context;
1602#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef9168942019-09-12 19:20:29 +02001603
Gilles Peskine5d309672019-07-12 23:47:28 +02001604#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Crond18b5f82020-11-25 16:46:05 +01001605 if( psa_get_se_driver( lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001606 {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001607 if( ( drv->key_management == NULL ) ||
1608 ( drv->key_management->p_export_public == NULL ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001609 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5d309672019-07-12 23:47:28 +02001610
Ronald Cron9486f9d2020-11-26 13:36:23 +01001611 return( drv->key_management->p_export_public(
Ronald Crond18b5f82020-11-25 16:46:05 +01001612 drv_context,
1613 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cron9486f9d2020-11-26 13:36:23 +01001614 data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001615 }
Ronald Cron9486f9d2020-11-26 13:36:23 +01001616 else
1617#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Ronald Crond18b5f82020-11-25 16:46:05 +01001618 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001619 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001620 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001621 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001622 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001623 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001624 key_buffer, key_buffer_size,
1625 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001626 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001627
Steven Cooreman560c28a2020-07-24 23:20:24 +02001628 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001629 * so conversion is needed. Try the accelerators first. */
Ronald Cron84cc9942020-11-25 14:30:05 +01001630 psa_status_t status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001631 attributes, key_buffer, key_buffer_size,
Ronald Cron84cc9942020-11-25 14:30:05 +01001632 data, data_size, data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +02001633
1634 if( status != PSA_ERROR_NOT_SUPPORTED ||
Ronald Crond18b5f82020-11-25 16:46:05 +01001635 psa_key_lifetime_is_external( lifetime ) )
Steven Cooremanb9b84422020-10-14 14:39:20 +02001636 return( status );
1637
Ronald Crond18b5f82020-11-25 16:46:05 +01001638 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001639 {
John Durkop0e005192020-10-31 22:06:54 -07001640#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1641 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001642 mbedtls_rsa_context *rsa = NULL;
Ronald Crond18b5f82020-11-25 16:46:05 +01001643 status = mbedtls_psa_rsa_load_representation( type,
1644 key_buffer,
1645 key_buffer_size,
Ronald Cron90857082020-11-25 14:58:33 +01001646 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001647 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001648 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001649
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001650 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1651 rsa,
1652 data,
1653 data_size,
1654 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001655
Steven Cooremana2371e52020-07-28 14:30:39 +02001656 mbedtls_rsa_free( rsa );
1657 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001658
Steven Cooreman560c28a2020-07-24 23:20:24 +02001659 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001660#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001661 /* We don't know how to convert a private RSA key to public. */
1662 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001663#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1664 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001665 }
1666 else
Moran Pekera998bc62018-04-16 18:16:20 +03001667 {
John Durkop6ba40d12020-11-10 08:50:04 -08001668#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1669 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001670 mbedtls_ecp_keypair *ecp = NULL;
Ronald Crond18b5f82020-11-25 16:46:05 +01001671 status = mbedtls_psa_ecp_load_representation( type,
1672 key_buffer,
1673 key_buffer_size,
Ronald Cron90857082020-11-25 14:58:33 +01001674 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001675 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001676 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001677
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001678 status = mbedtls_psa_ecp_export_key(
1679 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
Ronald Crond18b5f82020-11-25 16:46:05 +01001680 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) ),
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001681 ecp,
1682 data,
1683 data_size,
1684 data_length );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001685
Steven Cooremana2371e52020-07-28 14:30:39 +02001686 mbedtls_ecp_keypair_free( ecp );
1687 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001688 return( status );
1689#else
1690 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001691 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001692#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1693 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001694 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001695 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001696 else
1697 {
1698 /* This shouldn't happen in the reference implementation, but
1699 it is valid for a special-purpose implementation to omit
1700 support for exporting certain key types. */
1701 return( PSA_ERROR_NOT_SUPPORTED );
1702 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703}
Ronald Crond18b5f82020-11-25 16:46:05 +01001704
Ronald Croncf56a0a2020-08-04 09:51:30 +02001705psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001706 uint8_t *data,
1707 size_t data_size,
1708 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001709{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001711 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001712 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001713
1714 /* Set the key to empty now, so that even when there are errors, we always
1715 * set data_length to a value between 0 and data_size. On error, setting
1716 * the key to empty is a good choice because an empty key representation is
1717 * unlikely to be accepted anywhere. */
1718 *data_length = 0;
1719
1720 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001721 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001722 if( status != PSA_SUCCESS )
1723 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001724
Ronald Cron9486f9d2020-11-26 13:36:23 +01001725 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1726 {
1727 status = PSA_ERROR_INVALID_ARGUMENT;
1728 goto exit;
1729 }
1730
1731 /* Reject a zero-length output buffer now, since this can never be a
1732 * valid key representation. This way we know that data must be a valid
1733 * pointer and we can do things like memset(data, ..., data_size). */
1734 if( data_size == 0 )
1735 {
1736 status = PSA_ERROR_BUFFER_TOO_SMALL;
1737 goto exit;
1738 }
1739
Ronald Crond18b5f82020-11-25 16:46:05 +01001740 psa_key_attributes_t attributes = {
1741 .core = slot->attr
1742 };
Ronald Cron9486f9d2020-11-26 13:36:23 +01001743 status = psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001744 &attributes, slot->key.data, slot->key.bytes,
1745 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001746
1747exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001748 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001749
Ronald Cron5c522922020-11-14 16:35:34 +01001750 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001751}
1752
Gilles Peskine91e8c332019-08-02 19:19:39 +02001753#if defined(static_assert)
1754static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1755 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001756static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001757 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001758static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001759 "One or more key attribute flag is listed as both internal-only and external-only" );
1760#endif
1761
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001762/** Validate that a key policy is internally well-formed.
1763 *
1764 * This function only rejects invalid policies. It does not validate the
1765 * consistency of the policy with respect to other attributes of the key
1766 * such as the key type.
1767 */
1768static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001769{
1770 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001771 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 PSA_KEY_USAGE_ENCRYPT |
1773 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001774 PSA_KEY_USAGE_SIGN_HASH |
1775 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1777 return( PSA_ERROR_INVALID_ARGUMENT );
1778
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 return( PSA_SUCCESS );
1780}
1781
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001782/** Validate the internal consistency of key attributes.
1783 *
1784 * This function only rejects invalid attribute values. If does not
1785 * validate the consistency of the attributes with any key data that may
1786 * be involved in the creation of the key.
1787 *
1788 * Call this function early in the key creation process.
1789 *
1790 * \param[in] attributes Key attributes for the new key.
1791 * \param[out] p_drv On any return, the driver for the key, if any.
1792 * NULL for a transparent key.
1793 *
1794 */
1795static psa_status_t psa_validate_key_attributes(
1796 const psa_key_attributes_t *attributes,
1797 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001798{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001799 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001800 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001801 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001802
Ronald Cron54b90082020-10-29 15:26:43 +01001803 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001804 if( status != PSA_SUCCESS )
1805 return( status );
1806
Ronald Crond2ed4812020-07-17 16:11:30 +02001807 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001808 if( status != PSA_SUCCESS )
1809 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001810
Ronald Cron65f38a32020-10-23 17:11:13 +02001811 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1812 {
1813 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1814 return( PSA_ERROR_INVALID_ARGUMENT );
1815 }
1816 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001817 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001818 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001819 if( status != PSA_SUCCESS )
1820 return( status );
1821 }
1822
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001823 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001824 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001825 return( status );
1826
1827 /* Refuse to create overly large keys.
1828 * Note that this doesn't trigger on import if the attributes don't
1829 * explicitly specify a size (so psa_get_key_bits returns 0), so
1830 * psa_import_key() needs its own checks. */
1831 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1832 return( PSA_ERROR_NOT_SUPPORTED );
1833
Gilles Peskine91e8c332019-08-02 19:19:39 +02001834 /* Reject invalid flags. These should not be reachable through the API. */
1835 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1836 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1837 return( PSA_ERROR_INVALID_ARGUMENT );
1838
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001839 return( PSA_SUCCESS );
1840}
1841
Gilles Peskine4747d192019-04-17 15:05:45 +02001842/** Prepare a key slot to receive key material.
1843 *
1844 * This function allocates a key slot and sets its metadata.
1845 *
1846 * If this function fails, call psa_fail_key_creation().
1847 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001848 * This function is intended to be used as follows:
1849 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001850 * it with the specified attributes, and in case of a volatile key assign it
1851 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001852 * -# Populate the slot with the key material.
1853 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1854 * In case of failure at any step, stop the sequence and call
1855 * psa_fail_key_creation().
1856 *
Ronald Cron5c522922020-11-14 16:35:34 +01001857 * On success, the key slot is locked. It is the responsibility of the caller
1858 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001859 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001860 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001861 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 * \param[out] p_slot On success, a pointer to the prepared slot.
1863 * \param[out] p_drv On any return, the driver for the key, if any.
1864 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001865 *
1866 * \retval #PSA_SUCCESS
1867 * The key slot is ready to receive key material.
1868 * \return If this function fails, the key slot is an invalid state.
1869 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 */
1871static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001872 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001873 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001874 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001875 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001876{
1877 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001878 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 psa_key_slot_t *slot;
1880
Gilles Peskinedf179142019-07-15 22:02:14 +02001881 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 *p_drv = NULL;
1883
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001884 status = psa_validate_key_attributes( attributes, p_drv );
1885 if( status != PSA_SUCCESS )
1886 return( status );
1887
Ronald Cronc4d1b512020-07-31 11:26:37 +02001888 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 if( status != PSA_SUCCESS )
1890 return( status );
1891 slot = *p_slot;
1892
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001893 /* We're storing the declared bit-size of the key. It's up to each
1894 * creation mechanism to verify that this information is correct.
1895 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001896 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001897 * is optional (import, copy). In case of a volatile key, assign it the
1898 * volatile key identifier associated to the slot returned to contain its
1899 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001900
1901 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001902 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1903 {
1904#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1905 slot->attr.id = volatile_key_id;
1906#else
1907 slot->attr.id.key_id = volatile_key_id;
1908#endif
1909 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001910
Gilles Peskine91e8c332019-08-02 19:19:39 +02001911 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001912 * external-only flags, query `attributes`. Thanks to the check
1913 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001914 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001915 * may have set. */
1916 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001917
Gilles Peskinecbaff462019-07-12 23:46:04 +02001918#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001919 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001920 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001921 * create the key file in internal storage, create the
1922 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001923 * persistent data. This is done by starting a transaction that will
1924 * encompass these three actions.
1925 * For registering a volatile key, we just need to find an appropriate
1926 * slot number inside the SE. Since the key is designated volatile, creating
1927 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001928 /* The first thing to do is to find a slot number for the new key.
1929 * We save the slot number in persistent storage as part of the
1930 * transaction data. It will be needed to recover if the power
1931 * fails during the key creation process, to clean up on the secure
1932 * element side after restarting. Obtaining a slot number from the
1933 * secure element driver updates its persistent state, but we do not yet
1934 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001935 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001936 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001937 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001938 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001939 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001940 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001941 if( status != PSA_SUCCESS )
1942 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001943
1944 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001945 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001946 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1947 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001948 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001949 psa_crypto_transaction.key.id = slot->attr.id;
1950 status = psa_crypto_save_transaction( );
1951 if( status != PSA_SUCCESS )
1952 {
1953 (void) psa_crypto_stop_transaction( );
1954 return( status );
1955 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001956 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001957
1958 status = psa_copy_key_material_into_slot(
1959 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001960 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001961
1962 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1963 {
1964 /* Key registration only makes sense with a secure element. */
1965 return( PSA_ERROR_INVALID_ARGUMENT );
1966 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001967#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1968
Ronald Cronc4d1b512020-07-31 11:26:37 +02001969 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001970}
Gilles Peskine4747d192019-04-17 15:05:45 +02001971
1972/** Finalize the creation of a key once its key material has been set.
1973 *
1974 * This entails writing the key to persistent storage.
1975 *
1976 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001977 * See the documentation of psa_start_key_creation() for the intended use
1978 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001979 *
Ronald Cron5c522922020-11-14 16:35:34 +01001980 * If the finalization succeeds, the function unlocks the key slot (it was
1981 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1982 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001983 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001984 * \param[in,out] slot Pointer to the slot with key material.
1985 * \param[in] driver The secure element driver for the key,
1986 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001987 * \param[out] key On success, identifier of the key. Note that the
1988 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001989 *
1990 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001991 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001992 * \return If this function fails, the key slot is an invalid state.
1993 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001994 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001995static psa_status_t psa_finish_key_creation(
1996 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001997 psa_se_drv_table_entry_t *driver,
1998 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001999{
2000 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002001 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002002 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002003
2004#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002005 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002006 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002007#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2008 if( driver != NULL )
2009 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002010 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002011 psa_key_slot_number_t slot_number =
2012 psa_key_slot_get_slot_number( slot ) ;
2013
Gilles Peskineb46bef22019-07-30 21:32:04 +02002014#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01002015 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02002016 sizeof( data.slot_number ),
2017 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002018#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01002019 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002020 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002021 (uint8_t*) &data,
2022 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002023 }
2024 else
2025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2026 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002027 /* Key material is saved in export representation in the slot, so
2028 * just pass the slot buffer for storage. */
2029 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002030 slot->key.data,
2031 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002032 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002033 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002034#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2035
Gilles Peskinecbaff462019-07-12 23:46:04 +02002036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002037 /* Finish the transaction for a key creation. This does not
2038 * happen when registering an existing key. Detect this case
2039 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002040 * creation of a persistent key in a secure element requires a transaction,
2041 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002042 if( driver != NULL &&
2043 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002044 {
2045 status = psa_save_se_persistent_data( driver );
2046 if( status != PSA_SUCCESS )
2047 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002048 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002049 return( status );
2050 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002051 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002052 }
2053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2054
Ronald Cron50972942020-11-14 11:28:25 +01002055 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002056 {
2057 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002058 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002059 if( status != PSA_SUCCESS )
2060 *key = MBEDTLS_SVC_KEY_ID_INIT;
2061 }
Ronald Cron50972942020-11-14 11:28:25 +01002062
Gilles Peskine4747d192019-04-17 15:05:45 +02002063 return( status );
2064}
2065
2066/** Abort the creation of a key.
2067 *
2068 * You may call this function after calling psa_start_key_creation(),
2069 * or after psa_finish_key_creation() fails. In other circumstances, this
2070 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002071 * See the documentation of psa_start_key_creation() for the intended use
2072 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002073 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002074 * \param[in,out] slot Pointer to the slot with key material.
2075 * \param[in] driver The secure element driver for the key,
2076 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002077 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002078static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002080{
Gilles Peskine011e4282019-06-26 18:34:38 +02002081 (void) driver;
2082
Gilles Peskine4747d192019-04-17 15:05:45 +02002083 if( slot == NULL )
2084 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002085
2086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002087 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002088 * element, and the failure happened later (when saving metadata
2089 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002090 * element.
2091 * https://github.com/ARMmbed/mbed-crypto/issues/217
2092 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002093
Gilles Peskined7729582019-08-05 15:55:54 +02002094 /* Abort the ongoing transaction if any (there may not be one if
2095 * the creation process failed before starting one, or if the
2096 * key creation is a registration of a key in a secure element).
2097 * Earlier functions must already have done what it takes to undo any
2098 * partial creation. All that's left is to update the transaction data
2099 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002100 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002101#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2102
Gilles Peskine4747d192019-04-17 15:05:45 +02002103 psa_wipe_key_slot( slot );
2104}
2105
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002106/** Validate optional attributes during key creation.
2107 *
2108 * Some key attributes are optional during key creation. If they are
2109 * specified in the attributes structure, check that they are consistent
2110 * with the data in the slot.
2111 *
2112 * This function should be called near the end of key creation, after
2113 * the slot in memory is fully populated but before saving persistent data.
2114 */
2115static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002116 const psa_key_slot_t *slot,
2117 const psa_key_attributes_t *attributes )
2118{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002119 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002121 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002122 return( PSA_ERROR_INVALID_ARGUMENT );
2123 }
2124
2125 if( attributes->domain_parameters_size != 0 )
2126 {
John Durkop0e005192020-10-31 22:06:54 -07002127#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2128 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002129 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002130 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002131 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002132 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002134
Ronald Cron90857082020-11-25 14:58:33 +01002135 psa_status_t status = mbedtls_psa_rsa_load_representation(
2136 slot->attr.type,
2137 slot->key.data,
2138 slot->key.bytes,
2139 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002140 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002141 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002142
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002143 mbedtls_mpi_init( &actual );
2144 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002145 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002146 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002147 mbedtls_rsa_free( rsa );
2148 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002149 if( ret != 0 )
2150 goto rsa_exit;
2151 ret = mbedtls_mpi_read_binary( &required,
2152 attributes->domain_parameters,
2153 attributes->domain_parameters_size );
2154 if( ret != 0 )
2155 goto rsa_exit;
2156 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2157 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2158 rsa_exit:
2159 mbedtls_mpi_free( &actual );
2160 mbedtls_mpi_free( &required );
2161 if( ret != 0)
2162 return( mbedtls_to_psa_error( ret ) );
2163 }
2164 else
John Durkop9814fa22020-11-04 12:28:15 -08002165#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2166 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002167 {
2168 return( PSA_ERROR_INVALID_ARGUMENT );
2169 }
2170 }
2171
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002172 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002173 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002174 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002175 return( PSA_ERROR_INVALID_ARGUMENT );
2176 }
2177
2178 return( PSA_SUCCESS );
2179}
2180
Gilles Peskine4747d192019-04-17 15:05:45 +02002181psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002182 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002183 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002184 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002185{
2186 psa_status_t status;
2187 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002188 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002189
Ronald Cron81709fc2020-11-14 12:10:32 +01002190 *key = MBEDTLS_SVC_KEY_ID_INIT;
2191
Gilles Peskine0f84d622019-09-12 19:03:13 +02002192 /* Reject zero-length symmetric keys (including raw data key objects).
2193 * This also rejects any key which might be encoded as an empty string,
2194 * which is never valid. */
2195 if( data_length == 0 )
2196 return( PSA_ERROR_INVALID_ARGUMENT );
2197
Gilles Peskinedf179142019-07-15 22:02:14 +02002198 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002199 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002200 if( status != PSA_SUCCESS )
2201 goto exit;
2202
Gilles Peskine5d309672019-07-12 23:47:28 +02002203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2204 if( driver != NULL )
2205 {
2206 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002207 /* The driver should set the number of key bits, however in
2208 * case it doesn't, we initialize bits to an invalid value. */
2209 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002210 if( drv->key_management == NULL ||
2211 drv->key_management->p_import == NULL )
2212 {
2213 status = PSA_ERROR_NOT_SUPPORTED;
2214 goto exit;
2215 }
2216 status = drv->key_management->p_import(
2217 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002218 psa_key_slot_get_slot_number( slot ),
2219 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002220 if( status != PSA_SUCCESS )
2221 goto exit;
2222 if( bits > PSA_MAX_KEY_BITS )
2223 {
2224 status = PSA_ERROR_NOT_SUPPORTED;
2225 goto exit;
2226 }
2227 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002228 }
2229 else
2230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002231 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2232 {
2233 /* Importing a key with external lifetime through the driver wrapper
2234 * interface is not yet supported. Return as if this was an invalid
2235 * lifetime. */
2236 status = PSA_ERROR_INVALID_ARGUMENT;
2237 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002238 }
2239 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002240 {
2241 status = psa_import_key_into_slot( slot, data, data_length );
2242 if( status != PSA_SUCCESS )
2243 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002244 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002245 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002246 if( status != PSA_SUCCESS )
2247 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002248
Ronald Cron81709fc2020-11-14 12:10:32 +01002249 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002250exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002251 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002252 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002253
Gilles Peskine4747d192019-04-17 15:05:45 +02002254 return( status );
2255}
2256
Gilles Peskined7729582019-08-05 15:55:54 +02002257#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2258psa_status_t mbedtls_psa_register_se_key(
2259 const psa_key_attributes_t *attributes )
2260{
2261 psa_status_t status;
2262 psa_key_slot_t *slot = NULL;
2263 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002264 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002265
2266 /* Leaving attributes unspecified is not currently supported.
2267 * It could make sense to query the key type and size from the
2268 * secure element, but not all secure elements support this
2269 * and the driver HAL doesn't currently support it. */
2270 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2271 return( PSA_ERROR_NOT_SUPPORTED );
2272 if( psa_get_key_bits( attributes ) == 0 )
2273 return( PSA_ERROR_NOT_SUPPORTED );
2274
2275 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002276 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002277 if( status != PSA_SUCCESS )
2278 goto exit;
2279
Ronald Cron81709fc2020-11-14 12:10:32 +01002280 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002281
2282exit:
2283 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002284 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002285
Gilles Peskined7729582019-08-05 15:55:54 +02002286 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002287 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002288 return( status );
2289}
2290#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2291
Gilles Peskinef603c712019-01-19 13:40:11 +01002292static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002293 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002294{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002295 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002296 source->key.data,
2297 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002298 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002299 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002300
Steven Cooreman398aee52020-10-13 14:35:45 +02002301 target->attr.type = source->attr.type;
2302 target->attr.bits = source->attr.bits;
2303
2304 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002305}
2306
Ronald Croncf56a0a2020-08-04 09:51:30 +02002307psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002308 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002309 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002310{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002312 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002313 psa_key_slot_t *source_slot = NULL;
2314 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002315 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002316 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002317
Ronald Cron81709fc2020-11-14 12:10:32 +01002318 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2319
Ronald Cron5c522922020-11-14 16:35:34 +01002320 status = psa_get_and_lock_transparent_key_slot_with_policy(
2321 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002322 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002323 goto exit;
2324
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002325 status = psa_validate_optional_attributes( source_slot,
2326 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002327 if( status != PSA_SUCCESS )
2328 goto exit;
2329
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002330 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002331 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002332 if( status != PSA_SUCCESS )
2333 goto exit;
2334
Ronald Cron81709fc2020-11-14 12:10:32 +01002335 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2336 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002337 if( status != PSA_SUCCESS )
2338 goto exit;
2339
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2341 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002342 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002343 /* Copying to a secure element is not implemented yet. */
2344 status = PSA_ERROR_NOT_SUPPORTED;
2345 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002346 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002347#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002348
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002349 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002350 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002351 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002352
Ronald Cron81709fc2020-11-14 12:10:32 +01002353 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002354exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002355 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002356 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002357
Ronald Cron5c522922020-11-14 16:35:34 +01002358 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002359
Ronald Cron5c522922020-11-14 16:35:34 +01002360 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002361}
2362
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002363
2364
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002365/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002366/* Message digests */
2367/****************************************************************/
2368
John Durkop07cc04a2020-11-16 22:08:34 -08002369#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002370 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2371 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002372 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002373static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002374{
2375 switch( alg )
2376 {
John Durkopee4e6602020-11-27 08:48:46 -08002377#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002378 case PSA_ALG_MD2:
2379 return( &mbedtls_md2_info );
2380#endif
John Durkopee4e6602020-11-27 08:48:46 -08002381#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002382 case PSA_ALG_MD4:
2383 return( &mbedtls_md4_info );
2384#endif
John Durkopee4e6602020-11-27 08:48:46 -08002385#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002386 case PSA_ALG_MD5:
2387 return( &mbedtls_md5_info );
2388#endif
John Durkopee4e6602020-11-27 08:48:46 -08002389#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002390 case PSA_ALG_RIPEMD160:
2391 return( &mbedtls_ripemd160_info );
2392#endif
John Durkopee4e6602020-11-27 08:48:46 -08002393#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002394 case PSA_ALG_SHA_1:
2395 return( &mbedtls_sha1_info );
2396#endif
John Durkopee4e6602020-11-27 08:48:46 -08002397#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002398 case PSA_ALG_SHA_224:
2399 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002400#endif
2401#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002402 case PSA_ALG_SHA_256:
2403 return( &mbedtls_sha256_info );
2404#endif
John Durkopee4e6602020-11-27 08:48:46 -08002405#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002406 case PSA_ALG_SHA_384:
2407 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002408#endif
John Durkopee4e6602020-11-27 08:48:46 -08002409#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002410 case PSA_ALG_SHA_512:
2411 return( &mbedtls_sha512_info );
2412#endif
2413 default:
2414 return( NULL );
2415 }
2416}
John Durkop07cc04a2020-11-16 22:08:34 -08002417#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002418 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2419 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2420 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002421
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2423{
2424 switch( operation->alg )
2425 {
Gilles Peskine81736312018-06-26 15:04:31 +02002426 case 0:
2427 /* The object has (apparently) been initialized but it is not
2428 * in use. It's ok to call abort on such an object, and there's
2429 * nothing to do. */
2430 break;
John Durkopee4e6602020-11-27 08:48:46 -08002431#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432 case PSA_ALG_MD2:
2433 mbedtls_md2_free( &operation->ctx.md2 );
2434 break;
2435#endif
John Durkopee4e6602020-11-27 08:48:46 -08002436#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002437 case PSA_ALG_MD4:
2438 mbedtls_md4_free( &operation->ctx.md4 );
2439 break;
2440#endif
John Durkopee4e6602020-11-27 08:48:46 -08002441#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002442 case PSA_ALG_MD5:
2443 mbedtls_md5_free( &operation->ctx.md5 );
2444 break;
2445#endif
John Durkopee4e6602020-11-27 08:48:46 -08002446#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002447 case PSA_ALG_RIPEMD160:
2448 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2449 break;
2450#endif
John Durkopee4e6602020-11-27 08:48:46 -08002451#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002452 case PSA_ALG_SHA_1:
2453 mbedtls_sha1_free( &operation->ctx.sha1 );
2454 break;
2455#endif
John Durkop6ca23272020-12-03 06:01:32 -08002456#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002457 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002458 mbedtls_sha256_free( &operation->ctx.sha256 );
2459 break;
2460#endif
2461#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002462 case PSA_ALG_SHA_256:
2463 mbedtls_sha256_free( &operation->ctx.sha256 );
2464 break;
2465#endif
John Durkop6ca23272020-12-03 06:01:32 -08002466#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002467 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002468 mbedtls_sha512_free( &operation->ctx.sha512 );
2469 break;
2470#endif
2471#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002472 case PSA_ALG_SHA_512:
2473 mbedtls_sha512_free( &operation->ctx.sha512 );
2474 break;
2475#endif
2476 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002477 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478 }
2479 operation->alg = 0;
2480 return( PSA_SUCCESS );
2481}
2482
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002483psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002484 psa_algorithm_t alg )
2485{
Janos Follath24eed8d2019-11-22 13:21:35 +00002486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002487
2488 /* A context must be freshly initialized before it can be set up. */
2489 if( operation->alg != 0 )
2490 {
2491 return( PSA_ERROR_BAD_STATE );
2492 }
2493
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 switch( alg )
2495 {
John Durkopee4e6602020-11-27 08:48:46 -08002496#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497 case PSA_ALG_MD2:
2498 mbedtls_md2_init( &operation->ctx.md2 );
2499 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2500 break;
2501#endif
John Durkopee4e6602020-11-27 08:48:46 -08002502#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503 case PSA_ALG_MD4:
2504 mbedtls_md4_init( &operation->ctx.md4 );
2505 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2506 break;
2507#endif
John Durkopee4e6602020-11-27 08:48:46 -08002508#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509 case PSA_ALG_MD5:
2510 mbedtls_md5_init( &operation->ctx.md5 );
2511 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2512 break;
2513#endif
John Durkopee4e6602020-11-27 08:48:46 -08002514#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002515 case PSA_ALG_RIPEMD160:
2516 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2517 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2518 break;
2519#endif
John Durkopee4e6602020-11-27 08:48:46 -08002520#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521 case PSA_ALG_SHA_1:
2522 mbedtls_sha1_init( &operation->ctx.sha1 );
2523 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2524 break;
2525#endif
John Durkopee4e6602020-11-27 08:48:46 -08002526#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002527 case PSA_ALG_SHA_224:
2528 mbedtls_sha256_init( &operation->ctx.sha256 );
2529 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2530 break;
John Durkopee4e6602020-11-27 08:48:46 -08002531#endif
2532#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533 case PSA_ALG_SHA_256:
2534 mbedtls_sha256_init( &operation->ctx.sha256 );
2535 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2536 break;
2537#endif
John Durkopee4e6602020-11-27 08:48:46 -08002538#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002539 case PSA_ALG_SHA_384:
2540 mbedtls_sha512_init( &operation->ctx.sha512 );
2541 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2542 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002543#endif
John Durkopee4e6602020-11-27 08:48:46 -08002544#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002545 case PSA_ALG_SHA_512:
2546 mbedtls_sha512_init( &operation->ctx.sha512 );
2547 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2548 break;
2549#endif
2550 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002551 return( PSA_ALG_IS_HASH( alg ) ?
2552 PSA_ERROR_NOT_SUPPORTED :
2553 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554 }
2555 if( ret == 0 )
2556 operation->alg = alg;
2557 else
2558 psa_hash_abort( operation );
2559 return( mbedtls_to_psa_error( ret ) );
2560}
2561
2562psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2563 const uint8_t *input,
2564 size_t input_length )
2565{
Janos Follath24eed8d2019-11-22 13:21:35 +00002566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002567
2568 /* Don't require hash implementations to behave correctly on a
2569 * zero-length input, which may have an invalid pointer. */
2570 if( input_length == 0 )
2571 return( PSA_SUCCESS );
2572
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 switch( operation->alg )
2574 {
John Durkopee4e6602020-11-27 08:48:46 -08002575#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 case PSA_ALG_MD2:
2577 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2578 input, input_length );
2579 break;
2580#endif
John Durkopee4e6602020-11-27 08:48:46 -08002581#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002582 case PSA_ALG_MD4:
2583 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2584 input, input_length );
2585 break;
2586#endif
John Durkopee4e6602020-11-27 08:48:46 -08002587#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 case PSA_ALG_MD5:
2589 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2590 input, input_length );
2591 break;
2592#endif
John Durkopee4e6602020-11-27 08:48:46 -08002593#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 case PSA_ALG_RIPEMD160:
2595 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2596 input, input_length );
2597 break;
2598#endif
John Durkopee4e6602020-11-27 08:48:46 -08002599#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002600 case PSA_ALG_SHA_1:
2601 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2602 input, input_length );
2603 break;
2604#endif
John Durkop6ca23272020-12-03 06:01:32 -08002605#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002606 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002607 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2608 input, input_length );
2609 break;
2610#endif
2611#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002612 case PSA_ALG_SHA_256:
2613 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2614 input, input_length );
2615 break;
2616#endif
John Durkop6ca23272020-12-03 06:01:32 -08002617#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002618 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002619 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2620 input, input_length );
2621 break;
2622#endif
2623#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 case PSA_ALG_SHA_512:
2625 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2626 input, input_length );
2627 break;
2628#endif
2629 default:
John Durkopee4e6602020-11-27 08:48:46 -08002630 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002631 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002632 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002633
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002634 if( ret != 0 )
2635 psa_hash_abort( operation );
2636 return( mbedtls_to_psa_error( ret ) );
2637}
2638
2639psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2640 uint8_t *hash,
2641 size_t hash_size,
2642 size_t *hash_length )
2643{
itayzafrir40835d42018-08-02 13:14:17 +03002644 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002646 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002647
2648 /* Fill the output buffer with something that isn't a valid hash
2649 * (barring an attack on the hash and deliberately-crafted input),
2650 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002651 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002652 /* If hash_size is 0 then hash may be NULL and then the
2653 * call to memset would have undefined behavior. */
2654 if( hash_size != 0 )
2655 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002656
2657 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002658 {
2659 status = PSA_ERROR_BUFFER_TOO_SMALL;
2660 goto exit;
2661 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002662
2663 switch( operation->alg )
2664 {
John Durkopee4e6602020-11-27 08:48:46 -08002665#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002666 case PSA_ALG_MD2:
2667 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2668 break;
2669#endif
John Durkopee4e6602020-11-27 08:48:46 -08002670#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002671 case PSA_ALG_MD4:
2672 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2673 break;
2674#endif
John Durkopee4e6602020-11-27 08:48:46 -08002675#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002676 case PSA_ALG_MD5:
2677 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2678 break;
2679#endif
John Durkopee4e6602020-11-27 08:48:46 -08002680#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002681 case PSA_ALG_RIPEMD160:
2682 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2683 break;
2684#endif
John Durkopee4e6602020-11-27 08:48:46 -08002685#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002686 case PSA_ALG_SHA_1:
2687 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2688 break;
2689#endif
John Durkop6ca23272020-12-03 06:01:32 -08002690#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002691 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002692 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2693 break;
2694#endif
2695#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002696 case PSA_ALG_SHA_256:
2697 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2698 break;
2699#endif
John Durkop6ca23272020-12-03 06:01:32 -08002700#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002701 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002702 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2703 break;
2704#endif
2705#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002706 case PSA_ALG_SHA_512:
2707 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2708 break;
2709#endif
2710 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002711 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002712 }
itayzafrir40835d42018-08-02 13:14:17 +03002713 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002714
itayzafrir40835d42018-08-02 13:14:17 +03002715exit:
2716 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002718 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719 return( psa_hash_abort( operation ) );
2720 }
2721 else
2722 {
2723 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002724 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002725 }
2726}
2727
Gilles Peskine2d277862018-06-18 15:41:12 +02002728psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2729 const uint8_t *hash,
2730 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002731{
2732 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2733 size_t actual_hash_length;
2734 psa_status_t status = psa_hash_finish( operation,
2735 actual_hash, sizeof( actual_hash ),
2736 &actual_hash_length );
2737 if( status != PSA_SUCCESS )
2738 return( status );
2739 if( actual_hash_length != hash_length )
2740 return( PSA_ERROR_INVALID_SIGNATURE );
2741 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2742 return( PSA_ERROR_INVALID_SIGNATURE );
2743 return( PSA_SUCCESS );
2744}
2745
Gilles Peskine0a749c82019-11-28 19:33:58 +01002746psa_status_t psa_hash_compute( psa_algorithm_t alg,
2747 const uint8_t *input, size_t input_length,
2748 uint8_t *hash, size_t hash_size,
2749 size_t *hash_length )
2750{
2751 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2753
2754 *hash_length = hash_size;
2755 status = psa_hash_setup( &operation, alg );
2756 if( status != PSA_SUCCESS )
2757 goto exit;
2758 status = psa_hash_update( &operation, input, input_length );
2759 if( status != PSA_SUCCESS )
2760 goto exit;
2761 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2762 if( status != PSA_SUCCESS )
2763 goto exit;
2764
2765exit:
2766 if( status == PSA_SUCCESS )
2767 status = psa_hash_abort( &operation );
2768 else
2769 psa_hash_abort( &operation );
2770 return( status );
2771}
2772
2773psa_status_t psa_hash_compare( psa_algorithm_t alg,
2774 const uint8_t *input, size_t input_length,
2775 const uint8_t *hash, size_t hash_length )
2776{
2777 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2779
2780 status = psa_hash_setup( &operation, alg );
2781 if( status != PSA_SUCCESS )
2782 goto exit;
2783 status = psa_hash_update( &operation, input, input_length );
2784 if( status != PSA_SUCCESS )
2785 goto exit;
2786 status = psa_hash_verify( &operation, hash, hash_length );
2787 if( status != PSA_SUCCESS )
2788 goto exit;
2789
2790exit:
2791 if( status == PSA_SUCCESS )
2792 status = psa_hash_abort( &operation );
2793 else
2794 psa_hash_abort( &operation );
2795 return( status );
2796}
2797
Gilles Peskineeb35d782019-01-22 17:56:16 +01002798psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2799 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002800{
2801 if( target_operation->alg != 0 )
2802 return( PSA_ERROR_BAD_STATE );
2803
2804 switch( source_operation->alg )
2805 {
2806 case 0:
2807 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002808#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002809 case PSA_ALG_MD2:
2810 mbedtls_md2_clone( &target_operation->ctx.md2,
2811 &source_operation->ctx.md2 );
2812 break;
2813#endif
John Durkopee4e6602020-11-27 08:48:46 -08002814#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002815 case PSA_ALG_MD4:
2816 mbedtls_md4_clone( &target_operation->ctx.md4,
2817 &source_operation->ctx.md4 );
2818 break;
2819#endif
John Durkopee4e6602020-11-27 08:48:46 -08002820#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002821 case PSA_ALG_MD5:
2822 mbedtls_md5_clone( &target_operation->ctx.md5,
2823 &source_operation->ctx.md5 );
2824 break;
2825#endif
John Durkopee4e6602020-11-27 08:48:46 -08002826#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002827 case PSA_ALG_RIPEMD160:
2828 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2829 &source_operation->ctx.ripemd160 );
2830 break;
2831#endif
John Durkopee4e6602020-11-27 08:48:46 -08002832#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002833 case PSA_ALG_SHA_1:
2834 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2835 &source_operation->ctx.sha1 );
2836 break;
2837#endif
John Durkop6ca23272020-12-03 06:01:32 -08002838#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002839 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002840 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2841 &source_operation->ctx.sha256 );
2842 break;
2843#endif
2844#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002845 case PSA_ALG_SHA_256:
2846 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2847 &source_operation->ctx.sha256 );
2848 break;
2849#endif
John Durkop6ca23272020-12-03 06:01:32 -08002850#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002851 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002852 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2853 &source_operation->ctx.sha512 );
2854 break;
2855#endif
2856#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002857 case PSA_ALG_SHA_512:
2858 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2859 &source_operation->ctx.sha512 );
2860 break;
2861#endif
2862 default:
2863 return( PSA_ERROR_NOT_SUPPORTED );
2864 }
2865
2866 target_operation->alg = source_operation->alg;
2867 return( PSA_SUCCESS );
2868}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002869
2870
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002871/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872/* MAC */
2873/****************************************************************/
2874
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002875static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 psa_algorithm_t alg,
2877 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002878 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002879 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002882 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002884 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002885 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002886
Gilles Peskine8c9def32018-02-08 10:02:12 +01002887 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2888 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002889 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002891 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002892 mode = MBEDTLS_MODE_STREAM;
2893 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 case PSA_ALG_CTR:
2895 mode = MBEDTLS_MODE_CTR;
2896 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002897 case PSA_ALG_CFB:
2898 mode = MBEDTLS_MODE_CFB;
2899 break;
2900 case PSA_ALG_OFB:
2901 mode = MBEDTLS_MODE_OFB;
2902 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002903 case PSA_ALG_ECB_NO_PADDING:
2904 mode = MBEDTLS_MODE_ECB;
2905 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002906 case PSA_ALG_CBC_NO_PADDING:
2907 mode = MBEDTLS_MODE_CBC;
2908 break;
2909 case PSA_ALG_CBC_PKCS7:
2910 mode = MBEDTLS_MODE_CBC;
2911 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002912 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002913 mode = MBEDTLS_MODE_CCM;
2914 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002915 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916 mode = MBEDTLS_MODE_GCM;
2917 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002918 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2919 mode = MBEDTLS_MODE_CHACHAPOLY;
2920 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 default:
2922 return( NULL );
2923 }
2924 }
2925 else if( alg == PSA_ALG_CMAC )
2926 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 else
2928 return( NULL );
2929
2930 switch( key_type )
2931 {
2932 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002933 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 break;
2935 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002936 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2937 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002939 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002941 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002942 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2943 * but two-key Triple-DES is functionally three-key Triple-DES
2944 * with K1=K3, so that's how we present it to mbedtls. */
2945 if( key_bits == 128 )
2946 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947 break;
2948 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002949 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 break;
2951 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002952 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002954 case PSA_KEY_TYPE_CHACHA20:
2955 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2956 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 default:
2958 return( NULL );
2959 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002960 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002961 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962
Jaeden Amero23bbb752018-06-26 14:16:54 +01002963 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2964 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965}
2966
John Durkop6ba40d12020-11-10 08:50:04 -08002967#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002968static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002969{
Gilles Peskine2d277862018-06-18 15:41:12 +02002970 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002971 {
2972 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002973 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002974 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002975 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002976 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002977 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002978 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002979 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002980 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002981 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002982 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002983 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002984 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002985 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002986 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002987 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002988 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002989 return( 128 );
2990 default:
2991 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002992 }
2993}
John Durkop07cc04a2020-11-16 22:08:34 -08002994#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002995
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996/* Initialize the MAC operation structure. Once this function has been
2997 * called, psa_mac_abort can run and will do the right thing. */
2998static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2999 psa_algorithm_t alg )
3000{
3001 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3002
3003 operation->alg = alg;
3004 operation->key_set = 0;
3005 operation->iv_set = 0;
3006 operation->iv_required = 0;
3007 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003008 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003009
3010#if defined(MBEDTLS_CMAC_C)
3011 if( alg == PSA_ALG_CMAC )
3012 {
3013 operation->iv_required = 0;
3014 mbedtls_cipher_init( &operation->ctx.cmac );
3015 status = PSA_SUCCESS;
3016 }
3017 else
3018#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003019#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003020 if( PSA_ALG_IS_HMAC( operation->alg ) )
3021 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003022 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3023 operation->ctx.hmac.hash_ctx.alg = 0;
3024 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003025 }
3026 else
John Durkopd0321952020-10-29 21:37:36 -07003027#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003028 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003029 if( ! PSA_ALG_IS_MAC( alg ) )
3030 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003031 }
3032
3033 if( status != PSA_SUCCESS )
3034 memset( operation, 0, sizeof( *operation ) );
3035 return( status );
3036}
3037
John Durkop6ba40d12020-11-10 08:50:04 -08003038#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003039static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3040{
Gilles Peskine3f108122018-12-07 18:14:53 +01003041 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003042 return( psa_hash_abort( &hmac->hash_ctx ) );
3043}
John Durkop6ba40d12020-11-10 08:50:04 -08003044#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003045
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3047{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003048 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003050 /* The object has (apparently) been initialized but it is not
3051 * in use. It's ok to call abort on such an object, and there's
3052 * nothing to do. */
3053 return( PSA_SUCCESS );
3054 }
3055 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003057 if( operation->alg == PSA_ALG_CMAC )
3058 {
3059 mbedtls_cipher_free( &operation->ctx.cmac );
3060 }
3061 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003063#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 if( PSA_ALG_IS_HMAC( operation->alg ) )
3065 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003066 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003067 }
3068 else
John Durkopd0321952020-10-29 21:37:36 -07003069#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003070 {
3071 /* Sanity check (shouldn't happen: operation->alg should
3072 * always have been initialized to a valid value). */
3073 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074 }
Moran Peker41deec42018-04-04 15:43:05 +03003075
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076 operation->alg = 0;
3077 operation->key_set = 0;
3078 operation->iv_set = 0;
3079 operation->iv_required = 0;
3080 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003081 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003082
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003084
3085bad_state:
3086 /* If abort is called on an uninitialized object, we can't trust
3087 * anything. Wipe the object in case it contains confidential data.
3088 * This may result in a memory leak if a pointer gets overwritten,
3089 * but it's too late to do anything about this. */
3090 memset( operation, 0, sizeof( *operation ) );
3091 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092}
3093
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003094#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003095static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003096 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003097 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003098 const mbedtls_cipher_info_t *cipher_info )
3099{
Janos Follath24eed8d2019-11-22 13:21:35 +00003100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003101
3102 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003103
3104 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3105 if( ret != 0 )
3106 return( ret );
3107
3108 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003109 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003110 key_bits );
3111 return( ret );
3112}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003113#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003114
John Durkop6ba40d12020-11-10 08:50:04 -08003115#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003116static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3117 const uint8_t *key,
3118 size_t key_length,
3119 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003120{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003121 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003123 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003124 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003125 psa_status_t status;
3126
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003127 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3128 * overflow below. This should never trigger if the hash algorithm
3129 * is implemented correctly. */
3130 /* The size checks against the ipad and opad buffers cannot be written
3131 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3132 * because that triggers -Wlogical-op on GCC 7.3. */
3133 if( block_size > sizeof( ipad ) )
3134 return( PSA_ERROR_NOT_SUPPORTED );
3135 if( block_size > sizeof( hmac->opad ) )
3136 return( PSA_ERROR_NOT_SUPPORTED );
3137 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003138 return( PSA_ERROR_NOT_SUPPORTED );
3139
Gilles Peskined223b522018-06-11 18:12:58 +02003140 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003141 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003142 status = psa_hash_compute( hash_alg, key, key_length,
3143 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003144 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003145 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003146 }
Gilles Peskine96889972018-07-12 17:07:03 +02003147 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3148 * but it is permitted. It is common when HMAC is used in HKDF, for
3149 * example. Don't call `memcpy` in the 0-length because `key` could be
3150 * an invalid pointer which would make the behavior undefined. */
3151 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003152 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003153
Gilles Peskined223b522018-06-11 18:12:58 +02003154 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3155 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003156 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003157 ipad[i] ^= 0x36;
3158 memset( ipad + key_length, 0x36, block_size - key_length );
3159
3160 /* Copy the key material from ipad to opad, flipping the requisite bits,
3161 * and filling the rest of opad with the requisite constant. */
3162 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003163 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3164 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003165
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003167 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003168 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003169
Gilles Peskine01126fa2018-07-12 17:04:55 +02003170 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003171
3172cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003173 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003174
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003175 return( status );
3176}
John Durkop6ba40d12020-11-10 08:50:04 -08003177#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003178
Gilles Peskine89167cb2018-07-08 20:12:23 +02003179static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003180 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003181 psa_algorithm_t alg,
3182 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003183{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003185 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003186 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003187 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003188 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003189 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003190 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003191 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003193 /* A context must be freshly initialized before it can be set up. */
3194 if( operation->alg != 0 )
3195 {
3196 return( PSA_ERROR_BAD_STATE );
3197 }
3198
Gilles Peskined911eb72018-08-14 15:18:45 +02003199 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003200 if( status != PSA_SUCCESS )
3201 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003202 if( is_sign )
3203 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204
Ronald Cron5c522922020-11-14 16:35:34 +01003205 status = psa_get_and_lock_transparent_key_slot_with_policy(
3206 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003207 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003208 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003209 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003210
Gilles Peskine8c9def32018-02-08 10:02:12 +01003211#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003212 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003213 {
3214 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003215 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003216 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003218 if( cipher_info == NULL )
3219 {
3220 status = PSA_ERROR_NOT_SUPPORTED;
3221 goto exit;
3222 }
3223 operation->mac_size = cipher_info->block_size;
3224 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3225 status = mbedtls_to_psa_error( ret );
3226 }
3227 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003228#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003229#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003230 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003231 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003232 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003233 if( hash_alg == 0 )
3234 {
3235 status = PSA_ERROR_NOT_SUPPORTED;
3236 goto exit;
3237 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003238
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003239 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003240 /* Sanity check. This shouldn't fail on a valid configuration. */
3241 if( operation->mac_size == 0 ||
3242 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3243 {
3244 status = PSA_ERROR_NOT_SUPPORTED;
3245 goto exit;
3246 }
3247
Gilles Peskine8e338702019-07-30 20:06:31 +02003248 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003249 {
3250 status = PSA_ERROR_INVALID_ARGUMENT;
3251 goto exit;
3252 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003253
Gilles Peskine01126fa2018-07-12 17:04:55 +02003254 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003255 slot->key.data,
3256 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003257 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003258 }
3259 else
John Durkopd0321952020-10-29 21:37:36 -07003260#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003261 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003262 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003263 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003264 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003265
Gilles Peskined911eb72018-08-14 15:18:45 +02003266 if( truncated == 0 )
3267 {
3268 /* The "normal" case: untruncated algorithm. Nothing to do. */
3269 }
3270 else if( truncated < 4 )
3271 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003272 /* A very short MAC is too short for security since it can be
3273 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3274 * so we make this our minimum, even though 32 bits is still
3275 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003276 status = PSA_ERROR_NOT_SUPPORTED;
3277 }
3278 else if( truncated > operation->mac_size )
3279 {
3280 /* It's impossible to "truncate" to a larger length. */
3281 status = PSA_ERROR_INVALID_ARGUMENT;
3282 }
3283 else
3284 operation->mac_size = truncated;
3285
Gilles Peskinefbfac682018-07-08 20:51:54 +02003286exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003287 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003288 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003289 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003290 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003291 else
3292 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003293 operation->key_set = 1;
3294 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003295
Ronald Cron5c522922020-11-14 16:35:34 +01003296 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003297
Ronald Cron5c522922020-11-14 16:35:34 +01003298 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003299}
3300
Gilles Peskine89167cb2018-07-08 20:12:23 +02003301psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003302 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003303 psa_algorithm_t alg )
3304{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003305 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003306}
3307
3308psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003309 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003310 psa_algorithm_t alg )
3311{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003312 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003313}
3314
Gilles Peskine8c9def32018-02-08 10:02:12 +01003315psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3316 const uint8_t *input,
3317 size_t input_length )
3318{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003319 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003320 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003321 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003322 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003323 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003324 operation->has_input = 1;
3325
Gilles Peskine8c9def32018-02-08 10:02:12 +01003326#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003327 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003328 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003329 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3330 input, input_length );
3331 status = mbedtls_to_psa_error( ret );
3332 }
3333 else
3334#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003336 if( PSA_ALG_IS_HMAC( operation->alg ) )
3337 {
3338 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3339 input_length );
3340 }
3341 else
John Durkopd0321952020-10-29 21:37:36 -07003342#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003343 {
3344 /* This shouldn't happen if `operation` was initialized by
3345 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003346 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003347 }
3348
Gilles Peskinefbfac682018-07-08 20:51:54 +02003349 if( status != PSA_SUCCESS )
3350 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003351 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003352}
3353
John Durkop6ba40d12020-11-10 08:50:04 -08003354#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003355static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3356 uint8_t *mac,
3357 size_t mac_size )
3358{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003359 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003360 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3361 size_t hash_size = 0;
3362 size_t block_size = psa_get_hash_block_size( hash_alg );
3363 psa_status_t status;
3364
Gilles Peskine01126fa2018-07-12 17:04:55 +02003365 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3366 if( status != PSA_SUCCESS )
3367 return( status );
3368 /* From here on, tmp needs to be wiped. */
3369
3370 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3371 if( status != PSA_SUCCESS )
3372 goto exit;
3373
3374 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3375 if( status != PSA_SUCCESS )
3376 goto exit;
3377
3378 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3379 if( status != PSA_SUCCESS )
3380 goto exit;
3381
Gilles Peskined911eb72018-08-14 15:18:45 +02003382 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3383 if( status != PSA_SUCCESS )
3384 goto exit;
3385
3386 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003387
3388exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003389 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003390 return( status );
3391}
John Durkop6ba40d12020-11-10 08:50:04 -08003392#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003393
mohammad16036df908f2018-04-02 08:34:15 -07003394static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003395 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003396 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003397{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003398 if( ! operation->key_set )
3399 return( PSA_ERROR_BAD_STATE );
3400 if( operation->iv_required && ! operation->iv_set )
3401 return( PSA_ERROR_BAD_STATE );
3402
Gilles Peskine8c9def32018-02-08 10:02:12 +01003403 if( mac_size < operation->mac_size )
3404 return( PSA_ERROR_BUFFER_TOO_SMALL );
3405
Gilles Peskine8c9def32018-02-08 10:02:12 +01003406#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003407 if( operation->alg == PSA_ALG_CMAC )
3408 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003409 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003410 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3411 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003412 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003413 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003414 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003416 else
3417#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003418#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003419 if( PSA_ALG_IS_HMAC( operation->alg ) )
3420 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003421 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003422 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003423 }
3424 else
John Durkopd0321952020-10-29 21:37:36 -07003425#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003426 {
3427 /* This shouldn't happen if `operation` was initialized by
3428 * a setup function. */
3429 return( PSA_ERROR_BAD_STATE );
3430 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003431}
3432
Gilles Peskineacd4be32018-07-08 19:56:25 +02003433psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3434 uint8_t *mac,
3435 size_t mac_size,
3436 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003437{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003438 psa_status_t status;
3439
Jaeden Amero252ef282019-02-15 14:05:35 +00003440 if( operation->alg == 0 )
3441 {
3442 return( PSA_ERROR_BAD_STATE );
3443 }
3444
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003445 /* Fill the output buffer with something that isn't a valid mac
3446 * (barring an attack on the mac and deliberately-crafted input),
3447 * in case the caller doesn't check the return status properly. */
3448 *mac_length = mac_size;
3449 /* If mac_size is 0 then mac may be NULL and then the
3450 * call to memset would have undefined behavior. */
3451 if( mac_size != 0 )
3452 memset( mac, '!', mac_size );
3453
Gilles Peskine89167cb2018-07-08 20:12:23 +02003454 if( ! operation->is_sign )
3455 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003456 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003457 }
mohammad16036df908f2018-04-02 08:34:15 -07003458
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003459 status = psa_mac_finish_internal( operation, mac, mac_size );
3460
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003461 if( status == PSA_SUCCESS )
3462 {
3463 status = psa_mac_abort( operation );
3464 if( status == PSA_SUCCESS )
3465 *mac_length = operation->mac_size;
3466 else
3467 memset( mac, '!', mac_size );
3468 }
3469 else
3470 psa_mac_abort( operation );
3471 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003472}
3473
Gilles Peskineacd4be32018-07-08 19:56:25 +02003474psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3475 const uint8_t *mac,
3476 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003477{
Gilles Peskine828ed142018-06-18 23:25:51 +02003478 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003479 psa_status_t status;
3480
Jaeden Amero252ef282019-02-15 14:05:35 +00003481 if( operation->alg == 0 )
3482 {
3483 return( PSA_ERROR_BAD_STATE );
3484 }
3485
Gilles Peskine89167cb2018-07-08 20:12:23 +02003486 if( operation->is_sign )
3487 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003488 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003489 }
3490 if( operation->mac_size != mac_length )
3491 {
3492 status = PSA_ERROR_INVALID_SIGNATURE;
3493 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003494 }
mohammad16036df908f2018-04-02 08:34:15 -07003495
3496 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003497 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003498 if( status != PSA_SUCCESS )
3499 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003500
3501 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3502 status = PSA_ERROR_INVALID_SIGNATURE;
3503
3504cleanup:
3505 if( status == PSA_SUCCESS )
3506 status = psa_mac_abort( operation );
3507 else
3508 psa_mac_abort( operation );
3509
Gilles Peskine3f108122018-12-07 18:14:53 +01003510 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003511
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003512 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003513}
3514
3515
Gilles Peskine20035e32018-02-03 22:44:14 +01003516
Gilles Peskine20035e32018-02-03 22:44:14 +01003517/****************************************************************/
3518/* Asymmetric cryptography */
3519/****************************************************************/
3520
John Durkop6ba40d12020-11-10 08:50:04 -08003521#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003523/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003524 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003525static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3526 size_t hash_length,
3527 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003528{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003530 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003531 *md_alg = mbedtls_md_get_type( md_info );
3532
3533 /* The Mbed TLS RSA module uses an unsigned int for hash length
3534 * parameters. Validate that it fits so that we don't risk an
3535 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003536#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003537 if( hash_length > UINT_MAX )
3538 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003539#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003540
John Durkop0e005192020-10-31 22:06:54 -07003541#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003542 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3543 * must be correct. */
3544 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3545 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003546 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003547 if( md_info == NULL )
3548 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003549 if( mbedtls_md_get_size( md_info ) != hash_length )
3550 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003551 }
John Durkop0e005192020-10-31 22:06:54 -07003552#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003553
John Durkop0e005192020-10-31 22:06:54 -07003554#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003555 /* PSS requires a hash internally. */
3556 if( PSA_ALG_IS_RSA_PSS( alg ) )
3557 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003558 if( md_info == NULL )
3559 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003560 }
John Durkop0e005192020-10-31 22:06:54 -07003561#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003562
Gilles Peskine61b91d42018-06-08 16:09:36 +02003563 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003564}
3565
Gilles Peskine2b450e32018-06-27 15:42:46 +02003566static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3567 psa_algorithm_t alg,
3568 const uint8_t *hash,
3569 size_t hash_length,
3570 uint8_t *signature,
3571 size_t signature_size,
3572 size_t *signature_length )
3573{
3574 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003576 mbedtls_md_type_t md_alg;
3577
3578 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3579 if( status != PSA_SUCCESS )
3580 return( status );
3581
Gilles Peskine630a18a2018-06-29 17:49:35 +02003582 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003583 return( PSA_ERROR_BUFFER_TOO_SMALL );
3584
John Durkop0e005192020-10-31 22:06:54 -07003585#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003586 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3587 {
3588 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3589 MBEDTLS_MD_NONE );
3590 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003591 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003592 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003593 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003594 md_alg,
3595 (unsigned int) hash_length,
3596 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003597 signature );
3598 }
3599 else
John Durkop0e005192020-10-31 22:06:54 -07003600#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3601#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003602 if( PSA_ALG_IS_RSA_PSS( alg ) )
3603 {
3604 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3605 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003606 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003607 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003608 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003609 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003610 (unsigned int) hash_length,
3611 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003612 signature );
3613 }
3614 else
John Durkop0e005192020-10-31 22:06:54 -07003615#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003616 {
3617 return( PSA_ERROR_INVALID_ARGUMENT );
3618 }
3619
3620 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003621 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003622 return( mbedtls_to_psa_error( ret ) );
3623}
3624
3625static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3626 psa_algorithm_t alg,
3627 const uint8_t *hash,
3628 size_t hash_length,
3629 const uint8_t *signature,
3630 size_t signature_length )
3631{
3632 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003634 mbedtls_md_type_t md_alg;
3635
3636 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3637 if( status != PSA_SUCCESS )
3638 return( status );
3639
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003640 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3641 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003642
John Durkop0e005192020-10-31 22:06:54 -07003643#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003644 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3645 {
3646 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3647 MBEDTLS_MD_NONE );
3648 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003649 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003650 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003651 MBEDTLS_RSA_PUBLIC,
3652 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003653 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003654 hash,
3655 signature );
3656 }
3657 else
John Durkop0e005192020-10-31 22:06:54 -07003658#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3659#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003660 if( PSA_ALG_IS_RSA_PSS( alg ) )
3661 {
3662 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3663 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003664 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003665 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003666 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003667 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003668 (unsigned int) hash_length,
3669 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003670 signature );
3671 }
3672 else
John Durkop0e005192020-10-31 22:06:54 -07003673#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003674 {
3675 return( PSA_ERROR_INVALID_ARGUMENT );
3676 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003677
3678 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3679 * the rest of the signature is invalid". This has little use in
3680 * practice and PSA doesn't report this distinction. */
3681 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3682 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003683 return( mbedtls_to_psa_error( ret ) );
3684}
John Durkop6ba40d12020-11-10 08:50:04 -08003685#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3686 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003687
John Durkop6ba40d12020-11-10 08:50:04 -08003688#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3689 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003690/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3691 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3692 * (even though these functions don't modify it). */
3693static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3694 psa_algorithm_t alg,
3695 const uint8_t *hash,
3696 size_t hash_length,
3697 uint8_t *signature,
3698 size_t signature_size,
3699 size_t *signature_length )
3700{
Janos Follath24eed8d2019-11-22 13:21:35 +00003701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003702 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003703 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003704 mbedtls_mpi_init( &r );
3705 mbedtls_mpi_init( &s );
3706
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003707 if( signature_size < 2 * curve_bytes )
3708 {
3709 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3710 goto cleanup;
3711 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003712
John Durkop0ea39e02020-10-13 19:58:20 -07003713#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003714 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3715 {
3716 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3717 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3718 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003719 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3720 &ecp->d, hash,
3721 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003722 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003723 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003724 }
3725 else
John Durkop0ea39e02020-10-13 19:58:20 -07003726#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003727 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003728 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003729 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3730 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003731 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003732 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003733 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003734
3735 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3736 signature,
3737 curve_bytes ) );
3738 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3739 signature + curve_bytes,
3740 curve_bytes ) );
3741
3742cleanup:
3743 mbedtls_mpi_free( &r );
3744 mbedtls_mpi_free( &s );
3745 if( ret == 0 )
3746 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003747 return( mbedtls_to_psa_error( ret ) );
3748}
3749
3750static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3751 const uint8_t *hash,
3752 size_t hash_length,
3753 const uint8_t *signature,
3754 size_t signature_length )
3755{
Janos Follath24eed8d2019-11-22 13:21:35 +00003756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003757 mbedtls_mpi r, s;
3758 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3759 mbedtls_mpi_init( &r );
3760 mbedtls_mpi_init( &s );
3761
3762 if( signature_length != 2 * curve_bytes )
3763 return( PSA_ERROR_INVALID_SIGNATURE );
3764
3765 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3766 signature,
3767 curve_bytes ) );
3768 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3769 signature + curve_bytes,
3770 curve_bytes ) );
3771
Steven Cooremanacda8342020-07-24 23:09:52 +02003772 /* Check whether the public part is loaded. If not, load it. */
3773 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3774 {
3775 MBEDTLS_MPI_CHK(
3776 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003777 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003778 }
3779
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003780 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3781 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003782
3783cleanup:
3784 mbedtls_mpi_free( &r );
3785 mbedtls_mpi_free( &s );
3786 return( mbedtls_to_psa_error( ret ) );
3787}
John Durkop6ba40d12020-11-10 08:50:04 -08003788#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3789 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003790
Ronald Croncf56a0a2020-08-04 09:51:30 +02003791psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003792 psa_algorithm_t alg,
3793 const uint8_t *hash,
3794 size_t hash_length,
3795 uint8_t *signature,
3796 size_t signature_size,
3797 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003798{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003801 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003802
3803 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003804 /* Immediately reject a zero-length signature buffer. This guarantees
3805 * that signature must be a valid pointer. (On the other hand, the hash
3806 * buffer can in principle be empty since it doesn't actually have
3807 * to be a hash.) */
3808 if( signature_size == 0 )
3809 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003810
Ronald Cron5c522922020-11-14 16:35:34 +01003811 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3812 PSA_KEY_USAGE_SIGN_HASH,
3813 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003814 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003815 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003816 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003817 {
3818 status = PSA_ERROR_INVALID_ARGUMENT;
3819 goto exit;
3820 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003821
Steven Cooremancd84cb42020-07-16 20:28:36 +02003822 /* Try any of the available accelerators first */
3823 status = psa_driver_wrapper_sign_hash( slot,
3824 alg,
3825 hash,
3826 hash_length,
3827 signature,
3828 signature_size,
3829 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003830 if( status != PSA_ERROR_NOT_SUPPORTED ||
3831 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003832 goto exit;
3833
Steven Cooreman7a250572020-07-17 16:43:05 +02003834 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003835#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3836 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003837 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003838 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003839 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003840
Ronald Cron90857082020-11-25 14:58:33 +01003841 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3842 slot->key.data,
3843 slot->key.bytes,
3844 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003845 if( status != PSA_SUCCESS )
3846 goto exit;
3847
Steven Cooremana2371e52020-07-28 14:30:39 +02003848 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003849 alg,
3850 hash, hash_length,
3851 signature, signature_size,
3852 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003853
Steven Cooremana2371e52020-07-28 14:30:39 +02003854 mbedtls_rsa_free( rsa );
3855 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003856 }
3857 else
John Durkop6ba40d12020-11-10 08:50:04 -08003858#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3859 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003860 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003861 {
John Durkop9814fa22020-11-04 12:28:15 -08003862#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3863 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003864 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003865#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003866 PSA_ALG_IS_ECDSA( alg )
3867#else
3868 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3869#endif
3870 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003871 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003872 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003873 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3874 slot->key.data,
3875 slot->key.bytes,
3876 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003877 if( status != PSA_SUCCESS )
3878 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003879 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003880 alg,
3881 hash, hash_length,
3882 signature, signature_size,
3883 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003884 mbedtls_ecp_keypair_free( ecp );
3885 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003886 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003887 else
John Durkop9814fa22020-11-04 12:28:15 -08003888#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3889 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003890 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003891 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003892 }
itayzafrir5c753392018-05-08 11:18:38 +03003893 }
3894 else
itayzafrir5c753392018-05-08 11:18:38 +03003895 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003896 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003897 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003898
3899exit:
3900 /* Fill the unused part of the output buffer (the whole buffer on error,
3901 * the trailing part on success) with something that isn't a valid mac
3902 * (barring an attack on the mac and deliberately-crafted input),
3903 * in case the caller doesn't check the return status properly. */
3904 if( status == PSA_SUCCESS )
3905 memset( signature + *signature_length, '!',
3906 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003907 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003908 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003909 /* If signature_size is 0 then we have nothing to do. We must not call
3910 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003911
Ronald Cron5c522922020-11-14 16:35:34 +01003912 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003913
Ronald Cron5c522922020-11-14 16:35:34 +01003914 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003915}
3916
Ronald Croncf56a0a2020-08-04 09:51:30 +02003917psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003918 psa_algorithm_t alg,
3919 const uint8_t *hash,
3920 size_t hash_length,
3921 const uint8_t *signature,
3922 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003923{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003925 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003926 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003927
Ronald Cron5c522922020-11-14 16:35:34 +01003928 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3929 PSA_KEY_USAGE_VERIFY_HASH,
3930 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003931 if( status != PSA_SUCCESS )
3932 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003933
Steven Cooreman55ae2172020-07-17 19:46:15 +02003934 /* Try any of the available accelerators first */
3935 status = psa_driver_wrapper_verify_hash( slot,
3936 alg,
3937 hash,
3938 hash_length,
3939 signature,
3940 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003941 if( status != PSA_ERROR_NOT_SUPPORTED ||
3942 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003943 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003944
John Durkop6ba40d12020-11-10 08:50:04 -08003945#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3946 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003947 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003949 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003950
Ronald Cron90857082020-11-25 14:58:33 +01003951 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3952 slot->key.data,
3953 slot->key.bytes,
3954 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003955 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003956 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003957
Steven Cooremana2371e52020-07-28 14:30:39 +02003958 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003959 alg,
3960 hash, hash_length,
3961 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003962 mbedtls_rsa_free( rsa );
3963 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003964 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965 }
3966 else
John Durkop6ba40d12020-11-10 08:50:04 -08003967#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3968 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003969 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003970 {
John Durkop9814fa22020-11-04 12:28:15 -08003971#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3972 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003973 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003974 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003975 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003976 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3977 slot->key.data,
3978 slot->key.bytes,
3979 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003980 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003981 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003982 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003983 hash, hash_length,
3984 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003985 mbedtls_ecp_keypair_free( ecp );
3986 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003987 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003988 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003989 else
John Durkop9814fa22020-11-04 12:28:15 -08003990#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3991 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003992 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003993 status = PSA_ERROR_INVALID_ARGUMENT;
3994 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003995 }
itayzafrir5c753392018-05-08 11:18:38 +03003996 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003997 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003998 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003999 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004000 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004001
4002exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004003 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004004
Ronald Cron5c522922020-11-14 16:35:34 +01004005 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004006}
4007
John Durkop0e005192020-10-31 22:06:54 -07004008#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004009static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4010 mbedtls_rsa_context *rsa )
4011{
4012 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4013 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4014 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4015 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4016}
John Durkop0e005192020-10-31 22:06:54 -07004017#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004018
Ronald Croncf56a0a2020-08-04 09:51:30 +02004019psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004020 psa_algorithm_t alg,
4021 const uint8_t *input,
4022 size_t input_length,
4023 const uint8_t *salt,
4024 size_t salt_length,
4025 uint8_t *output,
4026 size_t output_size,
4027 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004029 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004030 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004031 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004032
Darryl Green5cc689a2018-07-24 15:34:10 +01004033 (void) input;
4034 (void) input_length;
4035 (void) salt;
4036 (void) output;
4037 (void) output_size;
4038
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004039 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004041 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4042 return( PSA_ERROR_INVALID_ARGUMENT );
4043
Ronald Cron5c522922020-11-14 16:35:34 +01004044 status = psa_get_and_lock_transparent_key_slot_with_policy(
4045 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004046 if( status != PSA_SUCCESS )
4047 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004048 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4049 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004050 {
4051 status = PSA_ERROR_INVALID_ARGUMENT;
4052 goto exit;
4053 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054
John Durkop6ba40d12020-11-10 08:50:04 -08004055#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4056 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004057 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004059 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004060 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4061 slot->key.data,
4062 slot->key.bytes,
4063 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004064 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004065 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004066
4067 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004068 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004069 status = PSA_ERROR_BUFFER_TOO_SMALL;
4070 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004071 }
John Durkop0e005192020-10-31 22:06:54 -07004072#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004073 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004075 status = mbedtls_to_psa_error(
4076 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004077 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004078 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004079 MBEDTLS_RSA_PUBLIC,
4080 input_length,
4081 input,
4082 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 }
4084 else
John Durkop0e005192020-10-31 22:06:54 -07004085#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4086#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004087 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004089 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004090 status = mbedtls_to_psa_error(
4091 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004092 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004093 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004094 MBEDTLS_RSA_PUBLIC,
4095 salt, salt_length,
4096 input_length,
4097 input,
4098 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099 }
4100 else
John Durkop0e005192020-10-31 22:06:54 -07004101#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004102 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004103 status = PSA_ERROR_INVALID_ARGUMENT;
4104 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004106rsa_exit:
4107 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004108 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004109
Steven Cooremana2371e52020-07-28 14:30:39 +02004110 mbedtls_rsa_free( rsa );
4111 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004112 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004113 else
John Durkop9814fa22020-11-04 12:28:15 -08004114#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004115 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004117 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004118 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004119
4120exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004121 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004122
Ronald Cron5c522922020-11-14 16:35:34 +01004123 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004124}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125
Ronald Croncf56a0a2020-08-04 09:51:30 +02004126psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004127 psa_algorithm_t alg,
4128 const uint8_t *input,
4129 size_t input_length,
4130 const uint8_t *salt,
4131 size_t salt_length,
4132 uint8_t *output,
4133 size_t output_size,
4134 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004137 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004138 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004139
Darryl Green5cc689a2018-07-24 15:34:10 +01004140 (void) input;
4141 (void) input_length;
4142 (void) salt;
4143 (void) output;
4144 (void) output_size;
4145
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004146 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004148 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4149 return( PSA_ERROR_INVALID_ARGUMENT );
4150
Ronald Cron5c522922020-11-14 16:35:34 +01004151 status = psa_get_and_lock_transparent_key_slot_with_policy(
4152 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004153 if( status != PSA_SUCCESS )
4154 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004155 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004156 {
4157 status = PSA_ERROR_INVALID_ARGUMENT;
4158 goto exit;
4159 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160
John Durkop6ba40d12020-11-10 08:50:04 -08004161#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4162 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004163 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004165 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004166 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4167 slot->key.data,
4168 slot->key.bytes,
4169 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004170 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004171 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172
Steven Cooremana2371e52020-07-28 14:30:39 +02004173 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004174 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004175 status = PSA_ERROR_INVALID_ARGUMENT;
4176 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004177 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004178
John Durkop0e005192020-10-31 22:06:54 -07004179#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004180 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004182 status = mbedtls_to_psa_error(
4183 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004184 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004185 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004186 MBEDTLS_RSA_PRIVATE,
4187 output_length,
4188 input,
4189 output,
4190 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191 }
4192 else
John Durkop0e005192020-10-31 22:06:54 -07004193#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4194#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004195 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004197 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004198 status = mbedtls_to_psa_error(
4199 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004200 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004201 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004202 MBEDTLS_RSA_PRIVATE,
4203 salt, salt_length,
4204 output_length,
4205 input,
4206 output,
4207 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004208 }
4209 else
John Durkop0e005192020-10-31 22:06:54 -07004210#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004211 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004212 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004214
Steven Cooreman4fed4552020-08-03 14:46:03 +02004215rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004216 mbedtls_rsa_free( rsa );
4217 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004219 else
John Durkop6ba40d12020-11-10 08:50:04 -08004220#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4221 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004222 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004223 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004225
4226exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004227 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004228
Ronald Cron5c522922020-11-14 16:35:34 +01004229 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004230}
Gilles Peskine20035e32018-02-03 22:44:14 +01004231
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004232
4233
mohammad1603503973b2018-03-12 15:59:30 +02004234/****************************************************************/
4235/* Symmetric cryptography */
4236/****************************************************************/
4237
Gilles Peskinee553c652018-06-04 16:22:46 +02004238static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004239 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004240 psa_algorithm_t alg,
4241 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004242{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004244 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004245 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004246 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004247 size_t key_bits;
4248 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004249 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4250 PSA_KEY_USAGE_ENCRYPT :
4251 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004252
Steven Cooremand3feccd2020-09-01 15:56:14 +02004253 /* A context must be freshly initialized before it can be set up. */
4254 if( operation->alg != 0 )
4255 return( PSA_ERROR_BAD_STATE );
4256
Steven Cooremana07b9972020-09-10 14:54:14 +02004257 /* The requested algorithm must be one that can be processed by cipher. */
4258 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004259 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004260
Steven Cooremanef8575e2020-09-11 11:44:50 +02004261 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004262 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004263 if( status != PSA_SUCCESS )
4264 goto exit;
4265
4266 /* Initialize the operation struct members, except for alg. The alg member
4267 * is used to indicate to psa_cipher_abort that there are resources to free,
4268 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004269 operation->key_set = 0;
4270 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004271 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004272 operation->iv_size = 0;
4273 operation->block_size = 0;
4274 if( alg == PSA_ALG_ECB_NO_PADDING )
4275 operation->iv_required = 0;
4276 else
4277 operation->iv_required = 1;
4278
Steven Cooremana07b9972020-09-10 14:54:14 +02004279 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004280 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004281 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004282 slot,
4283 alg );
4284 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004285 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004286 slot,
4287 alg );
4288
Steven Cooremanef8575e2020-09-11 11:44:50 +02004289 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004290 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004291 /* Once the driver context is initialised, it needs to be freed using
4292 * psa_cipher_abort. Indicate this through setting alg. */
4293 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004294 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004295
Steven Cooremand3feccd2020-09-01 15:56:14 +02004296 if( status != PSA_ERROR_NOT_SUPPORTED ||
4297 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4298 goto exit;
4299
Steven Cooremana07b9972020-09-10 14:54:14 +02004300 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004301 * available for the given algorithm & key. */
4302 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004303
Steven Cooremana07b9972020-09-10 14:54:14 +02004304 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004305 * psa_cipher_abort. Indicate there is something to be freed through setting
4306 * alg, and indicate the operation is being done using mbedtls crypto through
4307 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004308 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004309 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004310
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004311 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004312 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004313 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004314 {
4315 status = PSA_ERROR_NOT_SUPPORTED;
4316 goto exit;
4317 }
mohammad1603503973b2018-03-12 15:59:30 +02004318
mohammad1603503973b2018-03-12 15:59:30 +02004319 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004320 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004321 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004322
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004323#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004324 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004325 {
4326 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004327 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004328 memcpy( keys, slot->key.data, 16 );
4329 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004330 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4331 keys,
4332 192, cipher_operation );
4333 }
4334 else
4335#endif
4336 {
4337 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004338 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004339 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004340 }
Moran Peker41deec42018-04-04 15:43:05 +03004341 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004342 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004343
mohammad16038481e742018-03-18 13:57:31 +02004344#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004345 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004346 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004347 case PSA_ALG_CBC_NO_PADDING:
4348 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4349 MBEDTLS_PADDING_NONE );
4350 break;
4351 case PSA_ALG_CBC_PKCS7:
4352 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4353 MBEDTLS_PADDING_PKCS7 );
4354 break;
4355 default:
4356 /* The algorithm doesn't involve padding. */
4357 ret = 0;
4358 break;
4359 }
4360 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004361 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004362#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4363
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004364 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004365 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004366 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4367 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004368 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004369 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004370 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004371#if defined(MBEDTLS_CHACHA20_C)
4372 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004373 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004374 operation->iv_size = 12;
4375#endif
mohammad1603503973b2018-03-12 15:59:30 +02004376
Steven Cooreman7df02922020-09-09 15:28:49 +02004377 status = PSA_SUCCESS;
4378
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004379exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004380 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004381 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004382 if( status == PSA_SUCCESS )
4383 {
4384 /* Update operation flags for both driver and software implementations */
4385 operation->key_set = 1;
4386 }
4387 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004388 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004389
Ronald Cron5c522922020-11-14 16:35:34 +01004390 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004391
Ronald Cron5c522922020-11-14 16:35:34 +01004392 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004393}
4394
Gilles Peskinefe119512018-07-08 21:39:34 +02004395psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004396 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004397 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004398{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004399 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004400}
4401
Gilles Peskinefe119512018-07-08 21:39:34 +02004402psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004403 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004404 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004405{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004406 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004407}
4408
Gilles Peskinefe119512018-07-08 21:39:34 +02004409psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004410 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004411 size_t iv_size,
4412 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004413{
itayzafrir534bd7c2018-08-02 13:56:32 +03004414 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004416 if( operation->iv_set || ! operation->iv_required )
4417 {
4418 return( PSA_ERROR_BAD_STATE );
4419 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004420
Steven Cooremanef8575e2020-09-11 11:44:50 +02004421 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004422 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004423 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004424 iv,
4425 iv_size,
4426 iv_length );
4427 goto exit;
4428 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004429
Moran Peker41deec42018-04-04 15:43:05 +03004430 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004431 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004432 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004433 goto exit;
4434 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004435 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004436 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004437 if( ret != 0 )
4438 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004439 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004440 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004441 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004442
mohammad16038481e742018-03-18 13:57:31 +02004443 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004444 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004445
Moran Peker395db872018-05-31 14:07:14 +03004446exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004447 if( status == PSA_SUCCESS )
4448 operation->iv_set = 1;
4449 else
Moran Peker395db872018-05-31 14:07:14 +03004450 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004451 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004452}
4453
Gilles Peskinefe119512018-07-08 21:39:34 +02004454psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004455 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004456 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004457{
itayzafrir534bd7c2018-08-02 13:56:32 +03004458 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004460 if( operation->iv_set || ! operation->iv_required )
4461 {
4462 return( PSA_ERROR_BAD_STATE );
4463 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004464
Steven Cooremanef8575e2020-09-11 11:44:50 +02004465 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004466 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004467 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004468 iv,
4469 iv_length );
4470 goto exit;
4471 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004472
Moran Pekera28258c2018-05-29 16:25:04 +03004473 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004474 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004475 status = PSA_ERROR_INVALID_ARGUMENT;
4476 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004477 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004478 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4479 status = mbedtls_to_psa_error( ret );
4480exit:
4481 if( status == PSA_SUCCESS )
4482 operation->iv_set = 1;
4483 else
mohammad1603503973b2018-03-12 15:59:30 +02004484 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004485 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004486}
4487
Steven Cooreman177deba2020-09-07 17:14:14 +02004488/* Process input for which the algorithm is set to ECB mode. This requires
4489 * manual processing, since the PSA API is defined as being able to process
4490 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4491 * underlying mbedtls_cipher_update only takes full blocks. */
4492static psa_status_t psa_cipher_update_ecb_internal(
4493 mbedtls_cipher_context_t *ctx,
4494 const uint8_t *input,
4495 size_t input_length,
4496 uint8_t *output,
4497 size_t output_size,
4498 size_t *output_length )
4499{
4500 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4501 size_t block_size = ctx->cipher_info->block_size;
4502 size_t internal_output_length = 0;
4503 *output_length = 0;
4504
4505 if( input_length == 0 )
4506 {
4507 status = PSA_SUCCESS;
4508 goto exit;
4509 }
4510
4511 if( ctx->unprocessed_len > 0 )
4512 {
4513 /* Fill up to block size, and run the block if there's a full one. */
4514 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4515
4516 if( input_length < bytes_to_copy )
4517 bytes_to_copy = input_length;
4518
4519 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4520 input, bytes_to_copy );
4521 input_length -= bytes_to_copy;
4522 input += bytes_to_copy;
4523 ctx->unprocessed_len += bytes_to_copy;
4524
4525 if( ctx->unprocessed_len == block_size )
4526 {
4527 status = mbedtls_to_psa_error(
4528 mbedtls_cipher_update( ctx,
4529 ctx->unprocessed_data,
4530 block_size,
4531 output, &internal_output_length ) );
4532
4533 if( status != PSA_SUCCESS )
4534 goto exit;
4535
4536 output += internal_output_length;
4537 output_size -= internal_output_length;
4538 *output_length += internal_output_length;
4539 ctx->unprocessed_len = 0;
4540 }
4541 }
4542
4543 while( input_length >= block_size )
4544 {
4545 /* Run all full blocks we have, one by one */
4546 status = mbedtls_to_psa_error(
4547 mbedtls_cipher_update( ctx, input,
4548 block_size,
4549 output, &internal_output_length ) );
4550
4551 if( status != PSA_SUCCESS )
4552 goto exit;
4553
4554 input_length -= block_size;
4555 input += block_size;
4556
4557 output += internal_output_length;
4558 output_size -= internal_output_length;
4559 *output_length += internal_output_length;
4560 }
4561
4562 if( input_length > 0 )
4563 {
4564 /* Save unprocessed bytes for later processing */
4565 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4566 input, input_length );
4567 ctx->unprocessed_len += input_length;
4568 }
4569
4570 status = PSA_SUCCESS;
4571
4572exit:
4573 return( status );
4574}
4575
Gilles Peskinee553c652018-06-04 16:22:46 +02004576psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4577 const uint8_t *input,
4578 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004579 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004580 size_t output_size,
4581 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004582{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004584 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004585 if( operation->alg == 0 )
4586 {
4587 return( PSA_ERROR_BAD_STATE );
4588 }
4589 if( operation->iv_required && ! operation->iv_set )
4590 {
4591 return( PSA_ERROR_BAD_STATE );
4592 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004593
Steven Cooremanef8575e2020-09-11 11:44:50 +02004594 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004595 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004596 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004597 input,
4598 input_length,
4599 output,
4600 output_size,
4601 output_length );
4602 goto exit;
4603 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004604
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004605 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004606 {
4607 /* Take the unprocessed partial block left over from previous
4608 * update calls, if any, plus the input to this call. Remove
4609 * the last partial block, if any. You get the data that will be
4610 * output in this call. */
4611 expected_output_size =
4612 ( operation->ctx.cipher.unprocessed_len + input_length )
4613 / operation->block_size * operation->block_size;
4614 }
4615 else
4616 {
4617 expected_output_size = input_length;
4618 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004619
Gilles Peskine89d789c2018-06-04 17:17:16 +02004620 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004621 {
4622 status = PSA_ERROR_BUFFER_TOO_SMALL;
4623 goto exit;
4624 }
mohammad160382759612018-03-12 18:16:40 +02004625
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004626 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4627 {
4628 /* mbedtls_cipher_update has an API inconsistency: it will only
4629 * process a single block at a time in ECB mode. Abstract away that
4630 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004631 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4632 input,
4633 input_length,
4634 output,
4635 output_size,
4636 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004637 }
4638 else
4639 {
4640 status = mbedtls_to_psa_error(
4641 mbedtls_cipher_update( &operation->ctx.cipher, input,
4642 input_length, output, output_length ) );
4643 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004644exit:
4645 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004646 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004647 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004648}
4649
Gilles Peskinee553c652018-06-04 16:22:46 +02004650psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4651 uint8_t *output,
4652 size_t output_size,
4653 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004654{
David Saadab4ecc272019-02-14 13:48:10 +02004655 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004656 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004657 if( operation->alg == 0 )
4658 {
4659 return( PSA_ERROR_BAD_STATE );
4660 }
4661 if( operation->iv_required && ! operation->iv_set )
4662 {
4663 return( PSA_ERROR_BAD_STATE );
4664 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004665
Steven Cooremanef8575e2020-09-11 11:44:50 +02004666 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004667 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004668 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004669 output,
4670 output_size,
4671 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004672 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004673 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004674
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004675 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004676 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004677 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004678 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004679 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004680 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004681 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004682 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004683 }
4684
Steven Cooremanef8575e2020-09-11 11:44:50 +02004685 status = mbedtls_to_psa_error(
4686 mbedtls_cipher_finish( &operation->ctx.cipher,
4687 temp_output_buffer,
4688 output_length ) );
4689 if( status != PSA_SUCCESS )
4690 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004691
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004692 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004693 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004694 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004695 memcpy( output, temp_output_buffer, *output_length );
4696 else
Janos Follath315b51c2018-07-09 16:04:51 +01004697 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004698
Steven Cooremanef8575e2020-09-11 11:44:50 +02004699exit:
4700 if( operation->mbedtls_in_use == 1 )
4701 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004702
Steven Cooremanef8575e2020-09-11 11:44:50 +02004703 if( status == PSA_SUCCESS )
4704 return( psa_cipher_abort( operation ) );
4705 else
4706 {
4707 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004708 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004709
Steven Cooremanef8575e2020-09-11 11:44:50 +02004710 return( status );
4711 }
mohammad1603503973b2018-03-12 15:59:30 +02004712}
4713
Gilles Peskinee553c652018-06-04 16:22:46 +02004714psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4715{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004716 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004717 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004718 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004719 * in use. It's ok to call abort on such an object, and there's
4720 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004721 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004722 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004723
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004724 /* Sanity check (shouldn't happen: operation->alg should
4725 * always have been initialized to a valid value). */
4726 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4727 return( PSA_ERROR_BAD_STATE );
4728
Steven Cooremanef8575e2020-09-11 11:44:50 +02004729 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004730 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004731 else
4732 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004733
Moran Peker41deec42018-04-04 15:43:05 +03004734 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004735 operation->key_set = 0;
4736 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004737 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004738 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004739 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004740 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004741
Moran Peker395db872018-05-31 14:07:14 +03004742 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004743}
4744
Gilles Peskinea0655c32018-04-30 17:06:50 +02004745
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004746
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004747
mohammad16035955c982018-04-26 00:53:03 +03004748/****************************************************************/
4749/* AEAD */
4750/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004751
Gilles Peskineedf9a652018-08-17 18:11:56 +02004752typedef struct
4753{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004754 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004755 const mbedtls_cipher_info_t *cipher_info;
4756 union
4757 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004758 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004759#if defined(MBEDTLS_CCM_C)
4760 mbedtls_ccm_context ccm;
4761#endif /* MBEDTLS_CCM_C */
4762#if defined(MBEDTLS_GCM_C)
4763 mbedtls_gcm_context gcm;
4764#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004765#if defined(MBEDTLS_CHACHAPOLY_C)
4766 mbedtls_chachapoly_context chachapoly;
4767#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004768 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004769 psa_algorithm_t core_alg;
4770 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004771 uint8_t tag_length;
4772} aead_operation_t;
4773
Ronald Cronf95a2b12020-10-22 15:24:49 +02004774#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4775
Gilles Peskine30a9e412019-01-14 18:36:12 +01004776static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004777{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004778 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004779 {
4780#if defined(MBEDTLS_CCM_C)
4781 case PSA_ALG_CCM:
4782 mbedtls_ccm_free( &operation->ctx.ccm );
4783 break;
4784#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004785#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004786 case PSA_ALG_GCM:
4787 mbedtls_gcm_free( &operation->ctx.gcm );
4788 break;
4789#endif /* MBEDTLS_GCM_C */
4790 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004791
Ronald Cron5c522922020-11-14 16:35:34 +01004792 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004793}
4794
4795static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004796 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004797 psa_key_usage_t usage,
4798 psa_algorithm_t alg )
4799{
4800 psa_status_t status;
4801 size_t key_bits;
4802 mbedtls_cipher_id_t cipher_id;
4803
Ronald Cron5c522922020-11-14 16:35:34 +01004804 status = psa_get_and_lock_transparent_key_slot_with_policy(
4805 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004806 if( status != PSA_SUCCESS )
4807 return( status );
4808
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004809 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004810
4811 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004812 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004813 &cipher_id );
4814 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004815 {
4816 status = PSA_ERROR_NOT_SUPPORTED;
4817 goto cleanup;
4818 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004819
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004820 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004821 {
4822#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004823 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4824 operation->core_alg = PSA_ALG_CCM;
4825 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004826 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4827 * The call to mbedtls_ccm_encrypt_and_tag or
4828 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004829 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004830 {
4831 status = PSA_ERROR_INVALID_ARGUMENT;
4832 goto cleanup;
4833 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004834 mbedtls_ccm_init( &operation->ctx.ccm );
4835 status = mbedtls_to_psa_error(
4836 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004837 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004838 (unsigned int) key_bits ) );
4839 if( status != 0 )
4840 goto cleanup;
4841 break;
4842#endif /* MBEDTLS_CCM_C */
4843
4844#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004845 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4846 operation->core_alg = PSA_ALG_GCM;
4847 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004848 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4849 * The call to mbedtls_gcm_crypt_and_tag or
4850 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004851 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004852 {
4853 status = PSA_ERROR_INVALID_ARGUMENT;
4854 goto cleanup;
4855 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004856 mbedtls_gcm_init( &operation->ctx.gcm );
4857 status = mbedtls_to_psa_error(
4858 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004859 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004860 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004861 if( status != 0 )
4862 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004863 break;
4864#endif /* MBEDTLS_GCM_C */
4865
Gilles Peskine26869f22019-05-06 15:25:00 +02004866#if defined(MBEDTLS_CHACHAPOLY_C)
4867 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4868 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4869 operation->full_tag_length = 16;
4870 /* We only support the default tag length. */
4871 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004872 {
4873 status = PSA_ERROR_NOT_SUPPORTED;
4874 goto cleanup;
4875 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004876 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4877 status = mbedtls_to_psa_error(
4878 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004879 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004880 if( status != 0 )
4881 goto cleanup;
4882 break;
4883#endif /* MBEDTLS_CHACHAPOLY_C */
4884
Gilles Peskineedf9a652018-08-17 18:11:56 +02004885 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004886 status = PSA_ERROR_NOT_SUPPORTED;
4887 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004888 }
4889
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004890 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4891 {
4892 status = PSA_ERROR_INVALID_ARGUMENT;
4893 goto cleanup;
4894 }
4895 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004896
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897 return( PSA_SUCCESS );
4898
4899cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004900 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004901 return( status );
4902}
4903
Ronald Croncf56a0a2020-08-04 09:51:30 +02004904psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004905 psa_algorithm_t alg,
4906 const uint8_t *nonce,
4907 size_t nonce_length,
4908 const uint8_t *additional_data,
4909 size_t additional_data_length,
4910 const uint8_t *plaintext,
4911 size_t plaintext_length,
4912 uint8_t *ciphertext,
4913 size_t ciphertext_size,
4914 size_t *ciphertext_length )
4915{
mohammad16035955c982018-04-26 00:53:03 +03004916 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004917 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004918 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004919
mohammad1603f08a5502018-06-03 15:05:47 +03004920 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004921
Ronald Croncf56a0a2020-08-04 09:51:30 +02004922 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004923 if( status != PSA_SUCCESS )
4924 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004925
Gilles Peskineedf9a652018-08-17 18:11:56 +02004926 /* For all currently supported modes, the tag is at the end of the
4927 * ciphertext. */
4928 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4929 {
4930 status = PSA_ERROR_BUFFER_TOO_SMALL;
4931 goto exit;
4932 }
4933 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004934
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004935#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004936 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004937 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004938 status = mbedtls_to_psa_error(
4939 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4940 MBEDTLS_GCM_ENCRYPT,
4941 plaintext_length,
4942 nonce, nonce_length,
4943 additional_data, additional_data_length,
4944 plaintext, ciphertext,
4945 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004946 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004947 else
4948#endif /* MBEDTLS_GCM_C */
4949#if defined(MBEDTLS_CCM_C)
4950 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004951 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004952 status = mbedtls_to_psa_error(
4953 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4954 plaintext_length,
4955 nonce, nonce_length,
4956 additional_data,
4957 additional_data_length,
4958 plaintext, ciphertext,
4959 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004960 }
mohammad16035c8845f2018-05-09 05:40:09 -07004961 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004962#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004963#if defined(MBEDTLS_CHACHAPOLY_C)
4964 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4965 {
4966 if( nonce_length != 12 || operation.tag_length != 16 )
4967 {
4968 status = PSA_ERROR_NOT_SUPPORTED;
4969 goto exit;
4970 }
4971 status = mbedtls_to_psa_error(
4972 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4973 plaintext_length,
4974 nonce,
4975 additional_data,
4976 additional_data_length,
4977 plaintext,
4978 ciphertext,
4979 tag ) );
4980 }
4981 else
4982#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004983 {
mohammad1603554faad2018-06-03 15:07:38 +03004984 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004985 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004986
Gilles Peskineedf9a652018-08-17 18:11:56 +02004987 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4988 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004989
Gilles Peskineedf9a652018-08-17 18:11:56 +02004990exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004991 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004992 if( status == PSA_SUCCESS )
4993 *ciphertext_length = plaintext_length + operation.tag_length;
4994 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004995}
4996
Gilles Peskineee652a32018-06-01 19:23:52 +02004997/* Locate the tag in a ciphertext buffer containing the encrypted data
4998 * followed by the tag. Return the length of the part preceding the tag in
4999 * *plaintext_length. This is the size of the plaintext in modes where
5000 * the encrypted data has the same size as the plaintext, such as
5001 * CCM and GCM. */
5002static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5003 const uint8_t *ciphertext,
5004 size_t ciphertext_length,
5005 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005006 const uint8_t **p_tag )
5007{
5008 size_t payload_length;
5009 if( tag_length > ciphertext_length )
5010 return( PSA_ERROR_INVALID_ARGUMENT );
5011 payload_length = ciphertext_length - tag_length;
5012 if( payload_length > plaintext_size )
5013 return( PSA_ERROR_BUFFER_TOO_SMALL );
5014 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005015 return( PSA_SUCCESS );
5016}
5017
Ronald Croncf56a0a2020-08-04 09:51:30 +02005018psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005019 psa_algorithm_t alg,
5020 const uint8_t *nonce,
5021 size_t nonce_length,
5022 const uint8_t *additional_data,
5023 size_t additional_data_length,
5024 const uint8_t *ciphertext,
5025 size_t ciphertext_length,
5026 uint8_t *plaintext,
5027 size_t plaintext_size,
5028 size_t *plaintext_length )
5029{
mohammad16035955c982018-04-26 00:53:03 +03005030 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005031 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005032 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005033
Gilles Peskineee652a32018-06-01 19:23:52 +02005034 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005035
Ronald Croncf56a0a2020-08-04 09:51:30 +02005036 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005037 if( status != PSA_SUCCESS )
5038 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005039
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005040 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5041 ciphertext, ciphertext_length,
5042 plaintext_size, &tag );
5043 if( status != PSA_SUCCESS )
5044 goto exit;
5045
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005046#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005047 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005048 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005049 status = mbedtls_to_psa_error(
5050 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5051 ciphertext_length - operation.tag_length,
5052 nonce, nonce_length,
5053 additional_data,
5054 additional_data_length,
5055 tag, operation.tag_length,
5056 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005057 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005058 else
5059#endif /* MBEDTLS_GCM_C */
5060#if defined(MBEDTLS_CCM_C)
5061 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005062 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005063 status = mbedtls_to_psa_error(
5064 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5065 ciphertext_length - operation.tag_length,
5066 nonce, nonce_length,
5067 additional_data,
5068 additional_data_length,
5069 ciphertext, plaintext,
5070 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005071 }
mohammad160339574652018-06-01 04:39:53 -07005072 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005073#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005074#if defined(MBEDTLS_CHACHAPOLY_C)
5075 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5076 {
5077 if( nonce_length != 12 || operation.tag_length != 16 )
5078 {
5079 status = PSA_ERROR_NOT_SUPPORTED;
5080 goto exit;
5081 }
5082 status = mbedtls_to_psa_error(
5083 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5084 ciphertext_length - operation.tag_length,
5085 nonce,
5086 additional_data,
5087 additional_data_length,
5088 tag,
5089 ciphertext,
5090 plaintext ) );
5091 }
5092 else
5093#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005094 {
mohammad1603554faad2018-06-03 15:07:38 +03005095 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005096 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005097
Gilles Peskineedf9a652018-08-17 18:11:56 +02005098 if( status != PSA_SUCCESS && plaintext_size != 0 )
5099 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005100
Gilles Peskineedf9a652018-08-17 18:11:56 +02005101exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005102 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005103 if( status == PSA_SUCCESS )
5104 *plaintext_length = ciphertext_length - operation.tag_length;
5105 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005106}
5107
Gilles Peskinea0655c32018-04-30 17:06:50 +02005108
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005109
Gilles Peskine20035e32018-02-03 22:44:14 +01005110/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005111/* Generators */
5112/****************************************************************/
5113
John Durkop07cc04a2020-11-16 22:08:34 -08005114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5115 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5116 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5117#define AT_LEAST_ONE_BUILTIN_KDF
5118#endif
5119
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120#define HKDF_STATE_INIT 0 /* no input yet */
5121#define HKDF_STATE_STARTED 1 /* got salt */
5122#define HKDF_STATE_KEYED 2 /* got key */
5123#define HKDF_STATE_OUTPUT 3 /* output started */
5124
Gilles Peskinecbe66502019-05-16 16:59:18 +02005125static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5129 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005130 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005132}
5133
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005135{
5136 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005138 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005139 {
5140 /* The object has (apparently) been initialized but it is not
5141 * in use. It's ok to call abort on such an object, and there's
5142 * nothing to do. */
5143 }
5144 else
John Durkopd0321952020-10-29 21:37:36 -07005145#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005146 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005147 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 mbedtls_free( operation->ctx.hkdf.info );
5149 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005150 }
John Durkop07cc04a2020-11-16 22:08:34 -08005151 else
5152#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5153#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5154 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5155 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005157 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005158 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005159 if( operation->ctx.tls12_prf.seed != NULL )
5160 {
5161 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5162 operation->ctx.tls12_prf.seed_length );
5163 mbedtls_free( operation->ctx.tls12_prf.seed );
5164 }
5165
5166 if( operation->ctx.tls12_prf.label != NULL )
5167 {
5168 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5169 operation->ctx.tls12_prf.label_length );
5170 mbedtls_free( operation->ctx.tls12_prf.label );
5171 }
5172
5173 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5174
5175 /* We leave the fields Ai and output_block to be erased safely by the
5176 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005177 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005178 else
John Durkop07cc04a2020-11-16 22:08:34 -08005179#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5180 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005181 {
5182 status = PSA_ERROR_BAD_STATE;
5183 }
Janos Follath083036a2019-06-11 10:22:26 +01005184 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005185 return( status );
5186}
5187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005189 size_t *capacity)
5190{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005192 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005194 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005195 }
5196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005198 return( PSA_SUCCESS );
5199}
5200
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005201psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202 size_t capacity )
5203{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209 return( PSA_SUCCESS );
5210}
5211
John Durkopd0321952020-10-29 21:37:36 -07005212#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005215static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005216 psa_algorithm_t hash_alg,
5217 uint8_t *output,
5218 size_t output_length )
5219{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005220 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005221 psa_status_t status;
5222
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5224 return( PSA_ERROR_BAD_STATE );
5225 hkdf->state = HKDF_STATE_OUTPUT;
5226
Gilles Peskinebef7f142018-07-12 17:22:21 +02005227 while( output_length != 0 )
5228 {
5229 /* Copy what remains of the current block */
5230 uint8_t n = hash_length - hkdf->offset_in_block;
5231 if( n > output_length )
5232 n = (uint8_t) output_length;
5233 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5234 output += n;
5235 output_length -= n;
5236 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005237 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005238 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005239 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005240 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005242 * object was corrupted or if this function is called directly
5243 * inside the library. */
5244 if( hkdf->block_number == 0xff )
5245 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005246
5247 /* We need a new block */
5248 ++hkdf->block_number;
5249 hkdf->offset_in_block = 0;
5250 status = psa_hmac_setup_internal( &hkdf->hmac,
5251 hkdf->prk, hash_length,
5252 hash_alg );
5253 if( status != PSA_SUCCESS )
5254 return( status );
5255 if( hkdf->block_number != 1 )
5256 {
5257 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5258 hkdf->output_block,
5259 hash_length );
5260 if( status != PSA_SUCCESS )
5261 return( status );
5262 }
5263 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5264 hkdf->info,
5265 hkdf->info_length );
5266 if( status != PSA_SUCCESS )
5267 return( status );
5268 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5269 &hkdf->block_number, 1 );
5270 if( status != PSA_SUCCESS )
5271 return( status );
5272 status = psa_hmac_finish_internal( &hkdf->hmac,
5273 hkdf->output_block,
5274 sizeof( hkdf->output_block ) );
5275 if( status != PSA_SUCCESS )
5276 return( status );
5277 }
5278
5279 return( PSA_SUCCESS );
5280}
John Durkop07cc04a2020-11-16 22:08:34 -08005281#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005282
John Durkop07cc04a2020-11-16 22:08:34 -08005283#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5284 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005285static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5286 psa_tls12_prf_key_derivation_t *tls12_prf,
5287 psa_algorithm_t alg )
5288{
5289 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005290 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5292 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005293
Janos Follath7742fee2019-06-17 12:58:10 +01005294 /* We can't be wanting more output after block 0xff, otherwise
5295 * the capacity check in psa_key_derivation_output_bytes() would have
5296 * prevented this call. It could happen only if the operation
5297 * object was corrupted or if this function is called directly
5298 * inside the library. */
5299 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005300 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005301
5302 /* We need a new block */
5303 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005304 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005305
5306 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5307 *
5308 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5309 *
5310 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5311 * HMAC_hash(secret, A(2) + seed) +
5312 * HMAC_hash(secret, A(3) + seed) + ...
5313 *
5314 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005315 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005316 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005318 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005319 * is currently extracted as `output_block` and where i is
5320 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005321 */
5322
Janos Follathea29bfb2019-06-19 12:21:20 +01005323 /* Save the hash context before using it, to preserve the hash state with
5324 * only the inner padding in it. We need this, because inner padding depends
5325 * on the key (secret in the RFC's terminology). */
5326 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5327 if( status != PSA_SUCCESS )
5328 goto cleanup;
5329
5330 /* Calculate A(i) where i = tls12_prf->block_number. */
5331 if( tls12_prf->block_number == 1 )
5332 {
5333 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5334 * the variable seed and in this instance means it in the context of the
5335 * P_hash function, where seed = label + seed.) */
5336 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5337 tls12_prf->label, tls12_prf->label_length );
5338 if( status != PSA_SUCCESS )
5339 goto cleanup;
5340 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5341 tls12_prf->seed, tls12_prf->seed_length );
5342 if( status != PSA_SUCCESS )
5343 goto cleanup;
5344 }
5345 else
5346 {
5347 /* A(i) = HMAC_hash(secret, A(i-1)) */
5348 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5349 tls12_prf->Ai, hash_length );
5350 if( status != PSA_SUCCESS )
5351 goto cleanup;
5352 }
5353
5354 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5355 tls12_prf->Ai, hash_length );
5356 if( status != PSA_SUCCESS )
5357 goto cleanup;
5358 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5359 if( status != PSA_SUCCESS )
5360 goto cleanup;
5361
5362 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5363 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5364 tls12_prf->Ai, hash_length );
5365 if( status != PSA_SUCCESS )
5366 goto cleanup;
5367 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5368 tls12_prf->label, tls12_prf->label_length );
5369 if( status != PSA_SUCCESS )
5370 goto cleanup;
5371 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5372 tls12_prf->seed, tls12_prf->seed_length );
5373 if( status != PSA_SUCCESS )
5374 goto cleanup;
5375 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5376 tls12_prf->output_block, hash_length );
5377 if( status != PSA_SUCCESS )
5378 goto cleanup;
5379 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5380 if( status != PSA_SUCCESS )
5381 goto cleanup;
5382
Janos Follath7742fee2019-06-17 12:58:10 +01005383
5384cleanup:
5385
Janos Follathea29bfb2019-06-19 12:21:20 +01005386 cleanup_status = psa_hash_abort( &backup );
5387 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5388 status = cleanup_status;
5389
5390 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005391}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005392
Janos Follath844eb0e2019-06-19 12:10:49 +01005393static psa_status_t psa_key_derivation_tls12_prf_read(
5394 psa_tls12_prf_key_derivation_t *tls12_prf,
5395 psa_algorithm_t alg,
5396 uint8_t *output,
5397 size_t output_length )
5398{
5399 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005400 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005401 psa_status_t status;
5402 uint8_t offset, length;
5403
5404 while( output_length != 0 )
5405 {
5406 /* Check if we have fully processed the current block. */
5407 if( tls12_prf->left_in_block == 0 )
5408 {
5409 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5410 alg );
5411 if( status != PSA_SUCCESS )
5412 return( status );
5413
5414 continue;
5415 }
5416
5417 if( tls12_prf->left_in_block > output_length )
5418 length = (uint8_t) output_length;
5419 else
5420 length = tls12_prf->left_in_block;
5421
5422 offset = hash_length - tls12_prf->left_in_block;
5423 memcpy( output, tls12_prf->output_block + offset, length );
5424 output += length;
5425 output_length -= length;
5426 tls12_prf->left_in_block -= length;
5427 }
5428
5429 return( PSA_SUCCESS );
5430}
John Durkop07cc04a2020-11-16 22:08:34 -08005431#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5432 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005433
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005434psa_status_t psa_key_derivation_output_bytes(
5435 psa_key_derivation_operation_t *operation,
5436 uint8_t *output,
5437 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005438{
5439 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005443 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005445 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005446 }
5447
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005451 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005453 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005454 goto exit;
5455 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005457 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005459 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005460 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5461 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005463 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005464 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005465 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005467
John Durkopd0321952020-10-29 21:37:36 -07005468#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005469 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005470 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005473 output, output_length );
5474 }
Janos Follathadbec812019-06-14 11:05:39 +01005475 else
John Durkop07cc04a2020-11-16 22:08:34 -08005476#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5477#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5478 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005479 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005480 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005481 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005483 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005484 output_length );
5485 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005486 else
John Durkop07cc04a2020-11-16 22:08:34 -08005487#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5488 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489 {
5490 return( PSA_ERROR_BAD_STATE );
5491 }
5492
5493exit:
5494 if( status != PSA_SUCCESS )
5495 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005496 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 * This allows us to differentiate between exhausted operations and
5498 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5499 * operations. */
5500 psa_algorithm_t alg = operation->alg;
5501 psa_key_derivation_abort( operation );
5502 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005503 memset( output, '!', output_length );
5504 }
5505 return( status );
5506}
5507
Gilles Peskine08542d82018-07-19 17:05:42 +02005508#if defined(MBEDTLS_DES_C)
5509static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5510{
5511 if( data_size >= 8 )
5512 mbedtls_des_key_set_parity( data );
5513 if( data_size >= 16 )
5514 mbedtls_des_key_set_parity( data + 8 );
5515 if( data_size >= 24 )
5516 mbedtls_des_key_set_parity( data + 16 );
5517}
5518#endif /* MBEDTLS_DES_C */
5519
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005520static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005521 psa_key_slot_t *slot,
5522 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005524{
5525 uint8_t *data = NULL;
5526 size_t bytes = PSA_BITS_TO_BYTES( bits );
5527 psa_status_t status;
5528
Gilles Peskine8e338702019-07-30 20:06:31 +02005529 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530 return( PSA_ERROR_INVALID_ARGUMENT );
5531 if( bits % 8 != 0 )
5532 return( PSA_ERROR_INVALID_ARGUMENT );
5533 data = mbedtls_calloc( 1, bytes );
5534 if( data == NULL )
5535 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005538 if( status != PSA_SUCCESS )
5539 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005540#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005541 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005542 psa_des_set_key_parity( data, bytes );
5543#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005545
5546exit:
5547 mbedtls_free( data );
5548 return( status );
5549}
5550
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005551psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005552 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005553 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005554{
5555 psa_status_t status;
5556 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005557 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005558
Ronald Cron81709fc2020-11-14 12:10:32 +01005559 *key = MBEDTLS_SVC_KEY_ID_INIT;
5560
Gilles Peskine0f84d622019-09-12 19:03:13 +02005561 /* Reject any attempt to create a zero-length key so that we don't
5562 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5563 if( psa_get_key_bits( attributes ) == 0 )
5564 return( PSA_ERROR_INVALID_ARGUMENT );
5565
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005566 if( ! operation->can_output_key )
5567 return( PSA_ERROR_NOT_PERMITTED );
5568
Ronald Cron81709fc2020-11-14 12:10:32 +01005569 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5570 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005571#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5572 if( driver != NULL )
5573 {
5574 /* Deriving a key in a secure element is not implemented yet. */
5575 status = PSA_ERROR_NOT_SUPPORTED;
5576 }
5577#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005578 if( status == PSA_SUCCESS )
5579 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005580 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005581 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005583 }
5584 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005585 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005586 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005587 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005588
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005589 return( status );
5590}
5591
Gilles Peskineeab56e42018-07-12 17:12:33 +02005592
5593
5594/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005595/* Key derivation */
5596/****************************************************************/
5597
John Durkop07cc04a2020-11-16 22:08:34 -08005598#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005599static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005600 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005601 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005602{
John Durkop07cc04a2020-11-16 22:08:34 -08005603 int is_kdf_alg_supported;
5604
Janos Follath5fe19732019-06-20 15:09:30 +01005605 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5606 * initialisers for this union leave some bytes unspecified.) */
5607 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5608
Gilles Peskine969c5d62019-01-16 15:53:06 +01005609 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005610#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005611 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5612 is_kdf_alg_supported = 1;
5613 else
5614#endif
5615#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5616 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5617 is_kdf_alg_supported = 1;
5618 else
5619#endif
5620#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5621 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5622 is_kdf_alg_supported = 1;
5623 else
5624#endif
5625 is_kdf_alg_supported = 0;
5626
5627 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005628 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005629 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005630 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005631 if( hash_size == 0 )
5632 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005633 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5634 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005635 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005636 {
5637 return( PSA_ERROR_NOT_SUPPORTED );
5638 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005640 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005641 }
John Durkop07cc04a2020-11-16 22:08:34 -08005642
5643 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005644}
John Durkop07cc04a2020-11-16 22:08:34 -08005645#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005646
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005648 psa_algorithm_t alg )
5649{
5650 psa_status_t status;
5651
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005653 return( PSA_ERROR_BAD_STATE );
5654
Gilles Peskine6843c292019-01-18 16:44:49 +01005655 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5656 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005657#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005658 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005659 {
5660 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005661 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005662 }
5663 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5664 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005666 }
John Durkop07cc04a2020-11-16 22:08:34 -08005667#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005668 else
5669 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005670
5671 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005672 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005673 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005674}
5675
John Durkopd0321952020-10-29 21:37:36 -07005676#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005677static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678 psa_algorithm_t hash_alg,
5679 psa_key_derivation_step_t step,
5680 const uint8_t *data,
5681 size_t data_length )
5682{
5683 psa_status_t status;
5684 switch( step )
5685 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005686 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005687 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005688 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005689 status = psa_hmac_setup_internal( &hkdf->hmac,
5690 data, data_length,
5691 hash_alg );
5692 if( status != PSA_SUCCESS )
5693 return( status );
5694 hkdf->state = HKDF_STATE_STARTED;
5695 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005696 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005697 /* If no salt was provided, use an empty salt. */
5698 if( hkdf->state == HKDF_STATE_INIT )
5699 {
5700 status = psa_hmac_setup_internal( &hkdf->hmac,
5701 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005702 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005703 if( status != PSA_SUCCESS )
5704 return( status );
5705 hkdf->state = HKDF_STATE_STARTED;
5706 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005707 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005708 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005709 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5710 data, data_length );
5711 if( status != PSA_SUCCESS )
5712 return( status );
5713 status = psa_hmac_finish_internal( &hkdf->hmac,
5714 hkdf->prk,
5715 sizeof( hkdf->prk ) );
5716 if( status != PSA_SUCCESS )
5717 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005718 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005719 hkdf->block_number = 0;
5720 hkdf->state = HKDF_STATE_KEYED;
5721 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005722 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005723 if( hkdf->state == HKDF_STATE_OUTPUT )
5724 return( PSA_ERROR_BAD_STATE );
5725 if( hkdf->info_set )
5726 return( PSA_ERROR_BAD_STATE );
5727 hkdf->info_length = data_length;
5728 if( data_length != 0 )
5729 {
5730 hkdf->info = mbedtls_calloc( 1, data_length );
5731 if( hkdf->info == NULL )
5732 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5733 memcpy( hkdf->info, data, data_length );
5734 }
5735 hkdf->info_set = 1;
5736 return( PSA_SUCCESS );
5737 default:
5738 return( PSA_ERROR_INVALID_ARGUMENT );
5739 }
5740}
John Durkop07cc04a2020-11-16 22:08:34 -08005741#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005742
John Durkop07cc04a2020-11-16 22:08:34 -08005743#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5744 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005745static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5746 const uint8_t *data,
5747 size_t data_length )
5748{
5749 if( prf->state != TLS12_PRF_STATE_INIT )
5750 return( PSA_ERROR_BAD_STATE );
5751
Janos Follathd6dce9f2019-07-04 09:11:38 +01005752 if( data_length != 0 )
5753 {
5754 prf->seed = mbedtls_calloc( 1, data_length );
5755 if( prf->seed == NULL )
5756 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005757
Janos Follathd6dce9f2019-07-04 09:11:38 +01005758 memcpy( prf->seed, data, data_length );
5759 prf->seed_length = data_length;
5760 }
Janos Follathf08e2652019-06-13 09:05:41 +01005761
5762 prf->state = TLS12_PRF_STATE_SEED_SET;
5763
5764 return( PSA_SUCCESS );
5765}
5766
Janos Follath81550542019-06-13 14:26:34 +01005767static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5768 psa_algorithm_t hash_alg,
5769 const uint8_t *data,
5770 size_t data_length )
5771{
5772 psa_status_t status;
5773 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5774 return( PSA_ERROR_BAD_STATE );
5775
5776 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5777 if( status != PSA_SUCCESS )
5778 return( status );
5779
5780 prf->state = TLS12_PRF_STATE_KEY_SET;
5781
5782 return( PSA_SUCCESS );
5783}
5784
Janos Follath63028dd2019-06-13 09:15:47 +01005785static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5786 const uint8_t *data,
5787 size_t data_length )
5788{
5789 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5790 return( PSA_ERROR_BAD_STATE );
5791
Janos Follathd6dce9f2019-07-04 09:11:38 +01005792 if( data_length != 0 )
5793 {
5794 prf->label = mbedtls_calloc( 1, data_length );
5795 if( prf->label == NULL )
5796 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005797
Janos Follathd6dce9f2019-07-04 09:11:38 +01005798 memcpy( prf->label, data, data_length );
5799 prf->label_length = data_length;
5800 }
Janos Follath63028dd2019-06-13 09:15:47 +01005801
5802 prf->state = TLS12_PRF_STATE_LABEL_SET;
5803
5804 return( PSA_SUCCESS );
5805}
5806
Janos Follathb03233e2019-06-11 15:30:30 +01005807static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5808 psa_algorithm_t hash_alg,
5809 psa_key_derivation_step_t step,
5810 const uint8_t *data,
5811 size_t data_length )
5812{
Janos Follathb03233e2019-06-11 15:30:30 +01005813 switch( step )
5814 {
Janos Follathf08e2652019-06-13 09:05:41 +01005815 case PSA_KEY_DERIVATION_INPUT_SEED:
5816 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005817 case PSA_KEY_DERIVATION_INPUT_SECRET:
5818 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005819 case PSA_KEY_DERIVATION_INPUT_LABEL:
5820 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005821 default:
5822 return( PSA_ERROR_INVALID_ARGUMENT );
5823 }
5824}
John Durkop07cc04a2020-11-16 22:08:34 -08005825#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5826 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5827
5828#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5829static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5830 psa_tls12_prf_key_derivation_t *prf,
5831 psa_algorithm_t hash_alg,
5832 const uint8_t *data,
5833 size_t data_length )
5834{
5835 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005836 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005837 uint8_t *cur = pms;
5838
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005839 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005840 return( PSA_ERROR_INVALID_ARGUMENT );
5841
5842 /* Quoting RFC 4279, Section 2:
5843 *
5844 * The premaster secret is formed as follows: if the PSK is N octets
5845 * long, concatenate a uint16 with the value N, N zero octets, a second
5846 * uint16 with the value N, and the PSK itself.
5847 */
5848
5849 *cur++ = ( data_length >> 8 ) & 0xff;
5850 *cur++ = ( data_length >> 0 ) & 0xff;
5851 memset( cur, 0, data_length );
5852 cur += data_length;
5853 *cur++ = pms[0];
5854 *cur++ = pms[1];
5855 memcpy( cur, data, data_length );
5856 cur += data_length;
5857
5858 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5859
5860 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5861 return( status );
5862}
Janos Follath6660f0e2019-06-17 08:44:03 +01005863
5864static psa_status_t psa_tls12_prf_psk_to_ms_input(
5865 psa_tls12_prf_key_derivation_t *prf,
5866 psa_algorithm_t hash_alg,
5867 psa_key_derivation_step_t step,
5868 const uint8_t *data,
5869 size_t data_length )
5870{
5871 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005872 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005873 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5874 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005875 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005876
5877 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5878}
John Durkop07cc04a2020-11-16 22:08:34 -08005879#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005880
Gilles Peskineb8965192019-09-24 16:21:10 +02005881/** Check whether the given key type is acceptable for the given
5882 * input step of a key derivation.
5883 *
5884 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5885 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5886 * Both secret and non-secret inputs can alternatively have the type
5887 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5888 * that the input was passed as a buffer rather than via a key object.
5889 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005890static int psa_key_derivation_check_input_type(
5891 psa_key_derivation_step_t step,
5892 psa_key_type_t key_type )
5893{
5894 switch( step )
5895 {
5896 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005897 if( key_type == PSA_KEY_TYPE_DERIVE )
5898 return( PSA_SUCCESS );
5899 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005900 return( PSA_SUCCESS );
5901 break;
5902 case PSA_KEY_DERIVATION_INPUT_LABEL:
5903 case PSA_KEY_DERIVATION_INPUT_SALT:
5904 case PSA_KEY_DERIVATION_INPUT_INFO:
5905 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005906 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5907 return( PSA_SUCCESS );
5908 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005909 return( PSA_SUCCESS );
5910 break;
5911 }
5912 return( PSA_ERROR_INVALID_ARGUMENT );
5913}
5914
Janos Follathb80a94e2019-06-12 15:54:46 +01005915static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005916 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005917 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005918 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005919 const uint8_t *data,
5920 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005921{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005922 psa_status_t status;
5923 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5924
5925 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005926 if( status != PSA_SUCCESS )
5927 goto exit;
5928
John Durkopd0321952020-10-29 21:37:36 -07005929#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005930 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005931 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005932 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005933 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005934 step, data, data_length );
5935 }
John Durkop07cc04a2020-11-16 22:08:34 -08005936 else
5937#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5938#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5939 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005940 {
Janos Follathb03233e2019-06-11 15:30:30 +01005941 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5942 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5943 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005944 }
John Durkop07cc04a2020-11-16 22:08:34 -08005945 else
5946#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5947#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5948 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005949 {
5950 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5951 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5952 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005953 }
5954 else
John Durkop07cc04a2020-11-16 22:08:34 -08005955#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005956 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005957 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005958 return( PSA_ERROR_BAD_STATE );
5959 }
5960
Gilles Peskine224b0d62019-09-23 18:13:17 +02005961exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005962 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005963 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005964 return( status );
5965}
5966
Janos Follath51f4a0f2019-06-14 11:35:55 +01005967psa_status_t psa_key_derivation_input_bytes(
5968 psa_key_derivation_operation_t *operation,
5969 psa_key_derivation_step_t step,
5970 const uint8_t *data,
5971 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005972{
Gilles Peskineb8965192019-09-24 16:21:10 +02005973 return( psa_key_derivation_input_internal( operation, step,
5974 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005975 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005976}
5977
Janos Follath51f4a0f2019-06-14 11:35:55 +01005978psa_status_t psa_key_derivation_input_key(
5979 psa_key_derivation_operation_t *operation,
5980 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005981 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005982{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005983 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005984 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005985 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005986
Ronald Cron5c522922020-11-14 16:35:34 +01005987 status = psa_get_and_lock_transparent_key_slot_with_policy(
5988 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005989 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005990 {
5991 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005992 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005993 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005994
5995 /* Passing a key object as a SECRET input unlocks the permission
5996 * to output to a key object. */
5997 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5998 operation->can_output_key = 1;
5999
Ronald Cronf95a2b12020-10-22 15:24:49 +02006000 status = psa_key_derivation_input_internal( operation,
6001 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006002 slot->key.data,
6003 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006004
Ronald Cron5c522922020-11-14 16:35:34 +01006005 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006006
Ronald Cron5c522922020-11-14 16:35:34 +01006007 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006008}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006009
6010
6011
6012/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006013/* Key agreement */
6014/****************************************************************/
6015
John Durkop6ba40d12020-11-10 08:50:04 -08006016#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006017static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6018 size_t peer_key_length,
6019 const mbedtls_ecp_keypair *our_key,
6020 uint8_t *shared_secret,
6021 size_t shared_secret_size,
6022 size_t *shared_secret_length )
6023{
Steven Cooremand4867872020-08-05 16:31:39 +02006024 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006025 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006026 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006027 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006028 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006029 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006030
Ronald Cron90857082020-11-25 14:58:33 +01006031 status = mbedtls_psa_ecp_load_representation(
6032 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6033 peer_key,
6034 peer_key_length,
6035 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006036 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006037 goto exit;
6038
Jaeden Amero97271b32019-01-10 19:38:51 +00006039 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006040 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006041 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006042 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006043 status = mbedtls_to_psa_error(
6044 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6045 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006046 goto exit;
6047
Jaeden Amero97271b32019-01-10 19:38:51 +00006048 status = mbedtls_to_psa_error(
6049 mbedtls_ecdh_calc_secret( &ecdh,
6050 shared_secret_length,
6051 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006052 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006053 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006054 if( status != PSA_SUCCESS )
6055 goto exit;
6056 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6057 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006058
6059exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006060 if( status != PSA_SUCCESS )
6061 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006062 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006063 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006064 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006065
Jaeden Amero97271b32019-01-10 19:38:51 +00006066 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006067}
John Durkop6ba40d12020-11-10 08:50:04 -08006068#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006069
Gilles Peskine01d718c2018-09-18 12:01:02 +02006070#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6071
Gilles Peskine0216fe12019-04-11 21:23:21 +02006072static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6073 psa_key_slot_t *private_key,
6074 const uint8_t *peer_key,
6075 size_t peer_key_length,
6076 uint8_t *shared_secret,
6077 size_t shared_secret_size,
6078 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006079{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006080 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006081 {
John Durkop6ba40d12020-11-10 08:50:04 -08006082#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006083 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006084 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006085 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006086 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01006087 psa_status_t status = mbedtls_psa_ecp_load_representation(
6088 private_key->attr.type,
6089 private_key->key.data,
6090 private_key->key.bytes,
6091 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006092 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006093 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006094 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006095 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006096 shared_secret, shared_secret_size,
6097 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006098 mbedtls_ecp_keypair_free( ecp );
6099 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006100 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006101#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006102 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006103 (void) private_key;
6104 (void) peer_key;
6105 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006106 (void) shared_secret;
6107 (void) shared_secret_size;
6108 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006109 return( PSA_ERROR_NOT_SUPPORTED );
6110 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006111}
6112
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006113/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006114 * to potentially free embedded data structures and wipe confidential data.
6115 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006116static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006117 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006118 psa_key_slot_t *private_key,
6119 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006120 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006121{
6122 psa_status_t status;
6123 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6124 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006125 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006126
6127 /* Step 1: run the secret agreement algorithm to generate the shared
6128 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006129 status = psa_key_agreement_raw_internal( ka_alg,
6130 private_key,
6131 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006132 shared_secret,
6133 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006134 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006135 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006136 goto exit;
6137
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006138 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006139 * the shared secret. A shared secret is permitted wherever a key
6140 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006141 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006142 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006143 shared_secret,
6144 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006145exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006146 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006147 return( status );
6148}
6149
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006150psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006151 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006152 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006153 const uint8_t *peer_key,
6154 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006155{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006157 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006158 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006159
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006160 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006161 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006162 status = psa_get_and_lock_transparent_key_slot_with_policy(
6163 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006164 if( status != PSA_SUCCESS )
6165 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006166 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006167 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006168 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006169 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006170 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006171 else
6172 {
6173 /* If a private key has been added as SECRET, we allow the derived
6174 * key material to be used as a key in PSA Crypto. */
6175 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6176 operation->can_output_key = 1;
6177 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006178
Ronald Cron5c522922020-11-14 16:35:34 +01006179 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006180
Ronald Cron5c522922020-11-14 16:35:34 +01006181 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006182}
6183
Gilles Peskinebe697d82019-05-16 18:00:41 +02006184psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006185 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006186 const uint8_t *peer_key,
6187 size_t peer_key_length,
6188 uint8_t *output,
6189 size_t output_size,
6190 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006191{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006192 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006193 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006194 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006195
6196 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6197 {
6198 status = PSA_ERROR_INVALID_ARGUMENT;
6199 goto exit;
6200 }
Ronald Cron5c522922020-11-14 16:35:34 +01006201 status = psa_get_and_lock_transparent_key_slot_with_policy(
6202 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006203 if( status != PSA_SUCCESS )
6204 goto exit;
6205
6206 status = psa_key_agreement_raw_internal( alg, slot,
6207 peer_key, peer_key_length,
6208 output, output_size,
6209 output_length );
6210
6211exit:
6212 if( status != PSA_SUCCESS )
6213 {
6214 /* If an error happens and is not handled properly, the output
6215 * may be used as a key to protect sensitive data. Arrange for such
6216 * a key to be random, which is likely to result in decryption or
6217 * verification errors. This is better than filling the buffer with
6218 * some constant data such as zeros, which would result in the data
6219 * being protected with a reproducible, easily knowable key.
6220 */
6221 psa_generate_random( output, output_size );
6222 *output_length = output_size;
6223 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006224
Ronald Cron5c522922020-11-14 16:35:34 +01006225 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006226
Ronald Cron5c522922020-11-14 16:35:34 +01006227 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006228}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006229
6230
Gilles Peskine30524eb2020-11-13 17:02:26 +01006231
Gilles Peskine86a440b2018-11-12 18:39:40 +01006232/****************************************************************/
6233/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006234/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006235
Gilles Peskine30524eb2020-11-13 17:02:26 +01006236/** Initialize the PSA random generator.
6237 */
6238static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6239{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006240#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6241 memset( rng, 0, sizeof( *rng ) );
6242#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6243
Gilles Peskine30524eb2020-11-13 17:02:26 +01006244 /* Set default configuration if
6245 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6246 if( rng->entropy_init == NULL )
6247 rng->entropy_init = mbedtls_entropy_init;
6248 if( rng->entropy_free == NULL )
6249 rng->entropy_free = mbedtls_entropy_free;
6250
6251 rng->entropy_init( &rng->entropy );
6252#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6253 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6254 /* The PSA entropy injection feature depends on using NV seed as an entropy
6255 * source. Add NV seed as an entropy source for PSA entropy injection. */
6256 mbedtls_entropy_add_source( &rng->entropy,
6257 mbedtls_nv_seed_poll, NULL,
6258 MBEDTLS_ENTROPY_BLOCK_SIZE,
6259 MBEDTLS_ENTROPY_SOURCE_STRONG );
6260#endif
6261
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006262 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006263#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006264}
6265
6266/** Deinitialize the PSA random generator.
6267 */
6268static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6269{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006270#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6271 memset( rng, 0, sizeof( *rng ) );
6272#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006273 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006274 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006275#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006276}
6277
6278/** Seed the PSA random generator.
6279 */
6280static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6281{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006282#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6283 /* Do nothing: the external RNG seeds itself. */
6284 (void) rng;
6285 return( PSA_SUCCESS );
6286#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006287 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006288 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6289 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006290 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006291#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006292}
6293
Gilles Peskinee59236f2018-01-27 23:32:46 +01006294psa_status_t psa_generate_random( uint8_t *output,
6295 size_t output_size )
6296{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006297 GUARD_MODULE_INITIALIZED;
6298
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006299#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6300
6301 size_t output_length = 0;
6302 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6303 output, output_size,
6304 &output_length );
6305 if( status != PSA_SUCCESS )
6306 return( status );
6307 /* Breaking up a request into smaller chunks is currently not supported
6308 * for the extrernal RNG interface. */
6309 if( output_length != output_size )
6310 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6311 return( PSA_SUCCESS );
6312
6313#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6314
Gilles Peskine71ddab92021-01-04 21:01:07 +01006315 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006316 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006317 size_t request_size =
6318 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6319 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6320 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006321 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6322 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006323 if( ret != 0 )
6324 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006325 output_size -= request_size;
6326 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006327 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006328 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006329#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006330}
6331
Gilles Peskine8814fc42020-12-14 15:33:44 +01006332/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006333 *
6334 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6335 * `psa_generate_random(...)`. The state parameter is ignored since the
6336 * PSA API doesn't support passing an explicit state.
6337 *
6338 * In the non-external case, psa_generate_random() calls an
6339 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6340 * and semantics as mbedtls_psa_get_random(). As an optimization,
6341 * instead of doing this back-and-forth between the PSA API and the
6342 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6343 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006344 */
6345#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6346int mbedtls_psa_get_random( void *p_rng,
6347 unsigned char *output,
6348 size_t output_size )
6349{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006350 /* This function takes a pointer to the RNG state because that's what
6351 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6352 * its own state internally and doesn't let the caller access that state.
6353 * So we just ignore the state parameter, and in practice we'll pass
6354 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006355 (void) p_rng;
6356 psa_status_t status = psa_generate_random( output, output_size );
6357 if( status == PSA_SUCCESS )
6358 return( 0 );
6359 else
6360 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6361}
6362#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6363
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006364#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6365#include "mbedtls/entropy_poll.h"
6366
Gilles Peskine7228da22019-07-15 11:06:15 +02006367psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006368 size_t seed_size )
6369{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006370 if( global_data.initialized )
6371 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006372
6373 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6374 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6375 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6376 return( PSA_ERROR_INVALID_ARGUMENT );
6377
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006378 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006379}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006380#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006381
John Durkop07cc04a2020-11-16 22:08:34 -08006382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006383static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6384 size_t domain_parameters_size,
6385 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006386{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006387 size_t i;
6388 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006389
Gilles Peskinee56e8782019-04-26 17:34:02 +02006390 if( domain_parameters_size == 0 )
6391 {
6392 *exponent = 65537;
6393 return( PSA_SUCCESS );
6394 }
6395
6396 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6397 * support values that fit in a 32-bit integer, which is larger than
6398 * int on just about every platform anyway. */
6399 if( domain_parameters_size > sizeof( acc ) )
6400 return( PSA_ERROR_NOT_SUPPORTED );
6401 for( i = 0; i < domain_parameters_size; i++ )
6402 acc = ( acc << 8 ) | domain_parameters[i];
6403 if( acc > INT_MAX )
6404 return( PSA_ERROR_NOT_SUPPORTED );
6405 *exponent = acc;
6406 return( PSA_SUCCESS );
6407}
John Durkop07cc04a2020-11-16 22:08:34 -08006408#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006409
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006410static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006411 psa_key_slot_t *slot, size_t bits,
6412 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413{
Gilles Peskine8e338702019-07-30 20:06:31 +02006414 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006415
Gilles Peskinee56e8782019-04-26 17:34:02 +02006416 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006417 return( PSA_ERROR_INVALID_ARGUMENT );
6418
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419 if( key_type_is_raw_bytes( type ) )
6420 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006421 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006422
6423 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006424 if( status != PSA_SUCCESS )
6425 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006426
6427 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006428 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6429 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006430 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006431
Ronald Cronea0f8a62020-11-25 17:52:23 +01006432 status = psa_generate_random( slot->key.data,
6433 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006434 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006435 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006436
6437 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006438#if defined(MBEDTLS_DES_C)
6439 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006440 psa_des_set_key_parity( slot->key.data,
6441 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006442#endif /* MBEDTLS_DES_C */
6443 }
6444 else
6445
John Durkop07cc04a2020-11-16 22:08:34 -08006446#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006447 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006448 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006449 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006451 int exponent;
6452 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006453 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6454 return( PSA_ERROR_NOT_SUPPORTED );
6455 /* Accept only byte-aligned keys, for the same reasons as
6456 * in psa_import_rsa_key(). */
6457 if( bits % 8 != 0 )
6458 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006459 status = psa_read_rsa_exponent( domain_parameters,
6460 domain_parameters_size,
6461 &exponent );
6462 if( status != PSA_SUCCESS )
6463 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006464 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6465 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006466 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006467 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468 (unsigned int) bits,
6469 exponent );
6470 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006471 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006472
6473 /* Make sure to always have an export representation available */
6474 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6475
Steven Cooreman75b74362020-07-28 14:30:13 +02006476 status = psa_allocate_buffer_to_slot( slot, bytes );
6477 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006478 {
6479 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006480 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006481 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006482
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006483 status = mbedtls_psa_rsa_export_key( type,
6484 &rsa,
6485 slot->key.data,
6486 bytes,
6487 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006488 mbedtls_rsa_free( &rsa );
6489 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006490 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006491 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006492 }
6493 else
John Durkop07cc04a2020-11-16 22:08:34 -08006494#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006495
John Durkop9814fa22020-11-04 12:28:15 -08006496#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006497 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006499 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006500 mbedtls_ecp_group_id grp_id =
6501 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006502 const mbedtls_ecp_curve_info *curve_info =
6503 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006504 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006506 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006507 return( PSA_ERROR_NOT_SUPPORTED );
6508 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6509 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006510 mbedtls_ecp_keypair_init( &ecp );
6511 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006512 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006513 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006514 if( ret != 0 )
6515 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006516 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006517 return( mbedtls_to_psa_error( ret ) );
6518 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006519
6520
6521 /* Make sure to always have an export representation available */
6522 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006523 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6524 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006525 {
6526 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006527 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006528 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006529
6530 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006531 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006532
6533 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006534 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006535 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006536 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006537 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006538 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539 }
6540 else
John Durkop9814fa22020-11-04 12:28:15 -08006541#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006542 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006544 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006545
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006546 return( PSA_SUCCESS );
6547}
Darryl Green0c6575a2018-11-07 16:05:30 +00006548
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006549psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006550 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006551{
6552 psa_status_t status;
6553 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006554 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006555
Ronald Cron81709fc2020-11-14 12:10:32 +01006556 *key = MBEDTLS_SVC_KEY_ID_INIT;
6557
Gilles Peskine0f84d622019-09-12 19:03:13 +02006558 /* Reject any attempt to create a zero-length key so that we don't
6559 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6560 if( psa_get_key_bits( attributes ) == 0 )
6561 return( PSA_ERROR_INVALID_ARGUMENT );
6562
Ronald Cron81709fc2020-11-14 12:10:32 +01006563 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6564 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006565 if( status != PSA_SUCCESS )
6566 goto exit;
6567
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006568 status = psa_driver_wrapper_generate_key( attributes,
6569 slot );
6570 if( status != PSA_ERROR_NOT_SUPPORTED ||
6571 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6572 goto exit;
6573
6574 status = psa_generate_key_internal(
6575 slot, attributes->core.bits,
6576 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006577
6578exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006579 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006580 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006581 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006582 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006583
Darryl Green0c6575a2018-11-07 16:05:30 +00006584 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006585}
6586
6587
Gilles Peskinee59236f2018-01-27 23:32:46 +01006588
6589/****************************************************************/
6590/* Module setup */
6591/****************************************************************/
6592
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006593#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006594psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6595 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6596 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6597{
6598 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6599 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006600 global_data.rng.entropy_init = entropy_init;
6601 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006602 return( PSA_SUCCESS );
6603}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006604#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006605
Gilles Peskinee59236f2018-01-27 23:32:46 +01006606void mbedtls_psa_crypto_free( void )
6607{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006608 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006609 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6610 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006611 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006612 }
6613 /* Wipe all remaining data, including configuration.
6614 * In particular, this sets all state indicator to the value
6615 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006616 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006617#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006618 /* Unregister all secure element drivers, so that we restart from
6619 * a pristine state. */
6620 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006621#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006622}
6623
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006624#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6625/** Recover a transaction that was interrupted by a power failure.
6626 *
6627 * This function is called during initialization, before psa_crypto_init()
6628 * returns. If this function returns a failure status, the initialization
6629 * fails.
6630 */
6631static psa_status_t psa_crypto_recover_transaction(
6632 const psa_crypto_transaction_t *transaction )
6633{
6634 switch( transaction->unknown.type )
6635 {
6636 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6637 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006638 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006639 * is implemented.
6640 * https://github.com/ARMmbed/mbed-crypto/issues/218
6641 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006642 default:
6643 /* We found an unsupported transaction in the storage.
6644 * We don't know what state the storage is in. Give up. */
6645 return( PSA_ERROR_STORAGE_FAILURE );
6646 }
6647}
6648#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6649
Gilles Peskinee59236f2018-01-27 23:32:46 +01006650psa_status_t psa_crypto_init( void )
6651{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006652 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006653
Gilles Peskinec6b69072018-11-20 21:42:52 +01006654 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006655 if( global_data.initialized != 0 )
6656 return( PSA_SUCCESS );
6657
Gilles Peskine30524eb2020-11-13 17:02:26 +01006658 /* Initialize and seed the random generator. */
6659 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006660 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006661 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006662 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006663 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006664 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006665
Gilles Peskine66fb1262018-12-10 16:29:04 +01006666 status = psa_initialize_key_slots( );
6667 if( status != PSA_SUCCESS )
6668 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006669
Gilles Peskined9348f22019-10-01 15:22:29 +02006670#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6671 status = psa_init_all_se_drivers( );
6672 if( status != PSA_SUCCESS )
6673 goto exit;
6674#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6675
Gilles Peskine4b734222019-07-24 15:56:31 +02006676#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006677 status = psa_crypto_load_transaction( );
6678 if( status == PSA_SUCCESS )
6679 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006680 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6681 if( status != PSA_SUCCESS )
6682 goto exit;
6683 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006684 }
6685 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6686 {
6687 /* There's no transaction to complete. It's all good. */
6688 status = PSA_SUCCESS;
6689 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006690#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006691
Gilles Peskinec6b69072018-11-20 21:42:52 +01006692 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006693 global_data.initialized = 1;
6694
6695exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006696 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006697 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006698 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006699}
6700
6701#endif /* MBEDTLS_PSA_CRYPTO_C */