blob: 5463a5f1e9c84d6e9b1159e4b26216f53a1ffca4 [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
Gilles Peskineb46bef22019-07-30 21:32:04 +0200536/** Return the size of the key in the given slot, in bits.
537 *
538 * \param[in] slot A key slot.
539 *
540 * \return The key size in bits, read from the metadata in the slot.
541 */
542static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
543{
544 return( slot->attr.bits );
545}
546
Steven Cooreman75b74362020-07-28 14:30:13 +0200547/** Try to allocate a buffer to an empty key slot.
548 *
549 * \param[in,out] slot Key slot to attach buffer to.
550 * \param[in] buffer_length Requested size of the buffer.
551 *
552 * \retval #PSA_SUCCESS
553 * The buffer has been successfully allocated.
554 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
555 * Not enough memory was available for allocation.
556 * \retval #PSA_ERROR_ALREADY_EXISTS
557 * Trying to allocate a buffer to a non-empty key slot.
558 */
559static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
560 size_t buffer_length )
561{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100562 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200563 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200564
Ronald Cronea0f8a62020-11-25 17:52:23 +0100565 slot->key.data = mbedtls_calloc( 1, buffer_length );
566 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200567 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200568
Ronald Cronea0f8a62020-11-25 17:52:23 +0100569 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200570 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200571}
572
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200573psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
574 const uint8_t* data,
575 size_t data_length )
576{
577 psa_status_t status = psa_allocate_buffer_to_slot( slot,
578 data_length );
579 if( status != PSA_SUCCESS )
580 return( status );
581
Ronald Cronea0f8a62020-11-25 17:52:23 +0100582 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200583 return( PSA_SUCCESS );
584}
585
Ronald Crona0fe59f2020-11-29 11:14:53 +0100586psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100587 const psa_key_attributes_t *attributes,
588 const uint8_t *data, size_t data_length,
589 uint8_t *key_buffer, size_t key_buffer_size,
590 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
593 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200595 /* zero-length keys are never supported. */
596 if( data_length == 0 )
597 return( PSA_ERROR_NOT_SUPPORTED );
598
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100599 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200602
Steven Cooreman75b74362020-07-28 14:30:13 +0200603 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
604 if( data_length > SIZE_MAX / 8 )
605 return( PSA_ERROR_NOT_SUPPORTED );
606
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200607 /* Enforce a size limit, and in particular ensure that the bit
608 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100609 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200610 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100612 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200614 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200615
Ronald Crondd04d422020-11-28 15:14:42 +0100616 /* Copy the key material. */
617 memcpy( key_buffer, data, data_length );
618 *key_buffer_length = data_length;
619 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Steven Cooreman40120f62020-10-29 11:42:22 +0100621 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100623 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200624 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100625 status = psa_driver_wrapper_import_key( attributes,
Ronald Cron83282872020-11-22 14:02:39 +0100626 data, data_length,
Ronald Crondd04d422020-11-28 15:14:42 +0100627 key_buffer,
628 key_buffer_size,
629 key_buffer_length,
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100630 bits );
631 if( status != PSA_ERROR_NOT_SUPPORTED )
632 return( status );
Ronald Cron83282872020-11-22 14:02:39 +0100633
Ronald Crondd04d422020-11-28 15:14:42 +0100634 mbedtls_platform_zeroize( key_buffer, key_buffer_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200635
636 /* Key format is not supported by any accelerator, try software fallback
637 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100640 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200641 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 return( mbedtls_psa_ecp_import_key( attributes,
643 data, data_length,
644 key_buffer, key_buffer_size,
645 key_buffer_length,
646 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100647 }
John Durkop9814fa22020-11-04 12:28:15 -0800648#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
649 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700650#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
651 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100652 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200653 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100654 return( mbedtls_psa_rsa_import_key( attributes,
655 data, data_length,
656 key_buffer, key_buffer_size,
657 key_buffer_length,
658 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200659 }
John Durkop9814fa22020-11-04 12:28:15 -0800660#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100662 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100663
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100665}
666
Gilles Peskinef603c712019-01-19 13:40:11 +0100667/** Calculate the intersection of two algorithm usage policies.
668 *
669 * Return 0 (which allows no operation) on incompatibility.
670 */
671static psa_algorithm_t psa_key_policy_algorithm_intersection(
672 psa_algorithm_t alg1,
673 psa_algorithm_t alg2 )
674{
Gilles Peskine549ea862019-05-22 11:45:59 +0200675 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100676 if( alg1 == alg2 )
677 return( alg1 );
678 /* If the policies are from the same hash-and-sign family, check
679 * if one is a wildcard. If so the other has the specific algorithm. */
680 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
681 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
682 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
683 {
684 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
685 return( alg2 );
686 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
687 return( alg1 );
688 }
689 /* If the policies are incompatible, allow nothing. */
690 return( 0 );
691}
692
Gilles Peskined6f371b2019-05-10 19:33:38 +0200693static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
694 psa_algorithm_t requested_alg )
695{
Gilles Peskine549ea862019-05-22 11:45:59 +0200696 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200697 if( requested_alg == policy_alg )
698 return( 1 );
699 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200700 * and requested_alg is the same hash-and-sign family with any hash,
701 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200702 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
703 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
704 {
705 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
706 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
707 }
Steven Cooremance48e852020-10-05 16:02:45 +0200708 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200709 * a key derivation with that key agreement should also be allowed. This
710 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200711 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
712 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
713 {
714 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
715 policy_alg );
716 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200717 /* If it isn't permitted, it's forbidden. */
718 return( 0 );
719}
720
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100721/** Test whether a policy permits an algorithm.
722 *
723 * The caller must test usage flags separately.
724 */
725static int psa_key_policy_permits( const psa_key_policy_t *policy,
726 psa_algorithm_t alg )
727{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200728 return( psa_key_algorithm_permits( policy->alg, alg ) ||
729 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100730}
731
Gilles Peskinef603c712019-01-19 13:40:11 +0100732/** Restrict a key policy based on a constraint.
733 *
734 * \param[in,out] policy The policy to restrict.
735 * \param[in] constraint The policy constraint to apply.
736 *
737 * \retval #PSA_SUCCESS
738 * \c *policy contains the intersection of the original value of
739 * \c *policy and \c *constraint.
740 * \retval #PSA_ERROR_INVALID_ARGUMENT
741 * \c *policy and \c *constraint are incompatible.
742 * \c *policy is unchanged.
743 */
744static psa_status_t psa_restrict_key_policy(
745 psa_key_policy_t *policy,
746 const psa_key_policy_t *constraint )
747{
748 psa_algorithm_t intersection_alg =
749 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200750 psa_algorithm_t intersection_alg2 =
751 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
753 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200754 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
755 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100756 policy->usage &= constraint->usage;
757 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200758 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100759 return( PSA_SUCCESS );
760}
761
Ronald Cron5c522922020-11-14 16:35:34 +0100762/** Get the description of a key given its identifier and policy constraints
763 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200764 *
Ronald Cron5c522922020-11-14 16:35:34 +0100765 * The key must have allow all the usage flags set in \p usage. If \p alg is
766 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100767 *
Ronald Cron5c522922020-11-14 16:35:34 +0100768 * In case of a persistent key, the function loads the description of the key
769 * into a key slot if not already done.
770 *
771 * On success, the returned key slot is locked. It is the responsibility of
772 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200773 */
Ronald Cron5c522922020-11-14 16:35:34 +0100774static psa_status_t psa_get_and_lock_key_slot_with_policy(
775 mbedtls_svc_key_id_t key,
776 psa_key_slot_t **p_slot,
777 psa_key_usage_t usage,
778 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100779{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
781 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100782
Ronald Cron5c522922020-11-14 16:35:34 +0100783 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100784 if( status != PSA_SUCCESS )
785 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200786 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100787
788 /* Enforce that usage policy for the key slot contains all the flags
789 * required by the usage parameter. There is one exception: public
790 * keys can always be exported, so we treat public key objects as
791 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200792 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200794
795 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200796 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200797 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100798
799 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200800 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200801 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100802
Darryl Green06fd18d2018-07-16 11:21:11 +0100803 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200804
805error:
806 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100807 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200808
809 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100810}
Darryl Green940d72c2018-07-13 13:18:51 +0100811
Ronald Cron5c522922020-11-14 16:35:34 +0100812/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200813 *
814 * A transparent key is a key for which the key material is directly
815 * available, as opposed to a key in a secure element.
816 *
Ronald Cron5c522922020-11-14 16:35:34 +0100817 * This is a temporary function to use instead of
818 * psa_get_and_lock_key_slot_with_policy() until secure element support is
819 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200820 *
Ronald Cron5c522922020-11-14 16:35:34 +0100821 * On success, the returned key slot is locked. It is the responsibility of the
822 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200823 */
824#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100825static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
826 mbedtls_svc_key_id_t key,
827 psa_key_slot_t **p_slot,
828 psa_key_usage_t usage,
829 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200830{
Ronald Cron5c522922020-11-14 16:35:34 +0100831 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
832 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200833 if( status != PSA_SUCCESS )
834 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200835
Gilles Peskineadad8132019-07-25 11:31:23 +0200836 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200837 {
Ronald Cron5c522922020-11-14 16:35:34 +0100838 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200839 *p_slot = NULL;
840 return( PSA_ERROR_NOT_SUPPORTED );
841 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200842
Gilles Peskine28f8f302019-07-24 13:30:31 +0200843 return( PSA_SUCCESS );
844}
845#else /* MBEDTLS_PSA_CRYPTO_SE_C */
846/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100847#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
848 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200849#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
850
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100851/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100852static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000853{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100854 /* Data pointer will always be either a valid pointer or NULL in an
855 * initialized slot, so we can just free it. */
856 if( slot->key.data != NULL )
857 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
858
859 mbedtls_free( slot->key.data );
860 slot->key.data = NULL;
861 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000862
863 return( PSA_SUCCESS );
864}
865
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100866/** Completely wipe a slot in memory, including its policy.
867 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100868psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100869{
870 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100871
872 /*
873 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100874 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100875 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
876 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100877 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100878 */
Ronald Cron5c522922020-11-14 16:35:34 +0100879 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100880 {
881#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100882 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100883#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100884 status = PSA_ERROR_CORRUPTION_DETECTED;
885 }
886
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200887 /* Multipart operations may still be using the key. This is safe
888 * because all multipart operation objects are independent from
889 * the key slot: if they need to access the key after the setup
890 * phase, they have a copy of the key. Note that this means that
891 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100892 /* At this point, key material and other type-specific content has
893 * been wiped. Clear remaining metadata. We can call memset and not
894 * zeroize because the metadata is not particularly sensitive. */
895 memset( slot, 0, sizeof( *slot ) );
896 return( status );
897}
898
Ronald Croncf56a0a2020-08-04 09:51:30 +0200899psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100900{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100901 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200902 psa_status_t status; /* status of the last operation */
903 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200904#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
905 psa_se_drv_table_entry_t *driver;
906#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907
Ronald Croncf56a0a2020-08-04 09:51:30 +0200908 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200909 return( PSA_SUCCESS );
910
Ronald Cronf2911112020-10-29 17:51:10 +0100911 /*
Ronald Cron19daca92020-11-10 18:08:03 +0100912 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +0100913 * key, this will load the key description from persistent memory if not
914 * done yet. We cannot avoid this loading as without it we don't know if
915 * the key is operated by an SE or not and this information is needed by
916 * the current implementation.
917 */
Ronald Cron5c522922020-11-14 16:35:34 +0100918 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200919 if( status != PSA_SUCCESS )
920 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200921
Ronald Cronf2911112020-10-29 17:51:10 +0100922 /*
923 * If the key slot containing the key description is under access by the
924 * library (apart from the present access), the key cannot be destroyed
925 * yet. For the time being, just return in error. Eventually (to be
926 * implemented), the key should be destroyed when all accesses have
927 * stopped.
928 */
Ronald Cron5c522922020-11-14 16:35:34 +0100929 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +0100930 {
Ronald Cron5c522922020-11-14 16:35:34 +0100931 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +0100932 return( PSA_ERROR_GENERIC_ERROR );
933 }
934
Gilles Peskine354f7672019-07-12 23:46:38 +0200935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200936 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +0200937 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200938 {
Gilles Peskine60450a42019-07-25 11:32:45 +0200939 /* For a key in a secure element, we need to do three things:
940 * remove the key file in internal storage, destroy the
941 * key inside the secure element, and update the driver's
942 * persistent data. Start a transaction that will encompass these
943 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +0200944 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +0200945 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +0100946 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +0200947 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +0200948 status = psa_crypto_save_transaction( );
949 if( status != PSA_SUCCESS )
950 {
Gilles Peskine66be51c2019-07-25 18:02:52 +0200951 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200952 /* We should still try to destroy the key in the secure
953 * element and the key metadata in storage. This is especially
954 * important if the error is that the storage is full.
955 * But how to do it exactly without risking an inconsistent
956 * state after a reset?
957 * https://github.com/ARMmbed/mbed-crypto/issues/215
958 */
959 overall_status = status;
960 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +0200961 }
962
Ronald Cronea0f8a62020-11-25 17:52:23 +0100963 status = psa_destroy_se_key( driver,
964 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200965 if( overall_status == PSA_SUCCESS )
966 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +0200967 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200968#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
969
Darryl Greend49a4992018-06-18 17:27:26 +0100970#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +0200971 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +0100972 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200973 status = psa_destroy_persistent_key( slot->attr.id );
974 if( overall_status == PSA_SUCCESS )
975 overall_status = status;
976
Gilles Peskine9ce31c42019-08-13 15:14:20 +0200977 /* TODO: other slots may have a copy of the same key. We should
978 * invalidate them.
979 * https://github.com/ARMmbed/mbed-crypto/issues/214
980 */
Darryl Greend49a4992018-06-18 17:27:26 +0100981 }
982#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +0200983
Gilles Peskinefc762652019-07-22 19:30:34 +0200984#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
985 if( driver != NULL )
986 {
Gilles Peskine725f22a2019-07-25 11:31:48 +0200987 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200988 if( overall_status == PSA_SUCCESS )
989 overall_status = status;
990 status = psa_crypto_stop_transaction( );
991 if( overall_status == PSA_SUCCESS )
992 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +0200993 }
994#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
995
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200996#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
997exit:
998#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100999 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001000 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1001 if( overall_status == PSA_SUCCESS )
1002 overall_status = status;
1003 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004}
1005
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001006void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001007{
Gilles Peskineb699f072019-04-26 16:06:02 +02001008 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001009 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001010}
1011
Gilles Peskineb699f072019-04-26 16:06:02 +02001012psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1013 psa_key_type_t type,
1014 const uint8_t *data,
1015 size_t data_length )
1016{
1017 uint8_t *copy = NULL;
1018
1019 if( data_length != 0 )
1020 {
1021 copy = mbedtls_calloc( 1, data_length );
1022 if( copy == NULL )
1023 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1024 memcpy( copy, data, data_length );
1025 }
1026 /* After this point, this function is guaranteed to succeed, so it
1027 * can start modifying `*attributes`. */
1028
1029 if( attributes->domain_parameters != NULL )
1030 {
1031 mbedtls_free( attributes->domain_parameters );
1032 attributes->domain_parameters = NULL;
1033 attributes->domain_parameters_size = 0;
1034 }
1035
1036 attributes->domain_parameters = copy;
1037 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001038 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001039 return( PSA_SUCCESS );
1040}
1041
1042psa_status_t psa_get_key_domain_parameters(
1043 const psa_key_attributes_t *attributes,
1044 uint8_t *data, size_t data_size, size_t *data_length )
1045{
1046 if( attributes->domain_parameters_size > data_size )
1047 return( PSA_ERROR_BUFFER_TOO_SMALL );
1048 *data_length = attributes->domain_parameters_size;
1049 if( attributes->domain_parameters_size != 0 )
1050 memcpy( data, attributes->domain_parameters,
1051 attributes->domain_parameters_size );
1052 return( PSA_SUCCESS );
1053}
1054
John Durkop07cc04a2020-11-16 22:08:34 -08001055#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001056 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001057static psa_status_t psa_get_rsa_public_exponent(
1058 const mbedtls_rsa_context *rsa,
1059 psa_key_attributes_t *attributes )
1060{
1061 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001063 uint8_t *buffer = NULL;
1064 size_t buflen;
1065 mbedtls_mpi_init( &mpi );
1066
1067 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1068 if( ret != 0 )
1069 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001070 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1071 {
1072 /* It's the default value, which is reported as an empty string,
1073 * so there's nothing to do. */
1074 goto exit;
1075 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001076
1077 buflen = mbedtls_mpi_size( &mpi );
1078 buffer = mbedtls_calloc( 1, buflen );
1079 if( buffer == NULL )
1080 {
1081 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1082 goto exit;
1083 }
1084 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1085 if( ret != 0 )
1086 goto exit;
1087 attributes->domain_parameters = buffer;
1088 attributes->domain_parameters_size = buflen;
1089
1090exit:
1091 mbedtls_mpi_free( &mpi );
1092 if( ret != 0 )
1093 mbedtls_free( buffer );
1094 return( mbedtls_to_psa_error( ret ) );
1095}
John Durkop07cc04a2020-11-16 22:08:34 -08001096#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001097 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001098
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001099/** Retrieve all the publicly-accessible attributes of a key.
1100 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001101psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001102 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001103{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001105 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001106 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001107
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001108 psa_reset_key_attributes( attributes );
1109
Ronald Cron5c522922020-11-14 16:35:34 +01001110 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001111 if( status != PSA_SUCCESS )
1112 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001113
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001114 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001115 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1116 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1117
1118#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1119 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001120 psa_set_key_slot_number( attributes,
1121 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001122#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001123
Gilles Peskine8e338702019-07-30 20:06:31 +02001124 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001125 {
John Durkop07cc04a2020-11-16 22:08:34 -08001126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001127 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001128 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001129 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001130#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001131 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001132 * is not yet implemented.
1133 * https://github.com/ARMmbed/mbed-crypto/issues/216
1134 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001135 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001136 break;
1137#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001138 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001139 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001140
Ronald Cron90857082020-11-25 14:58:33 +01001141 status = mbedtls_psa_rsa_load_representation(
1142 slot->attr.type,
1143 slot->key.data,
1144 slot->key.bytes,
1145 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001146 if( status != PSA_SUCCESS )
1147 break;
1148
Steven Cooremana2371e52020-07-28 14:30:39 +02001149 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001150 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001151 mbedtls_rsa_free( rsa );
1152 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001153 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001154 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001155#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001156 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001157 default:
1158 /* Nothing else to do. */
1159 break;
1160 }
1161
1162 if( status != PSA_SUCCESS )
1163 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001164
Ronald Cron5c522922020-11-14 16:35:34 +01001165 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001166
Ronald Cron5c522922020-11-14 16:35:34 +01001167 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168}
1169
Gilles Peskinec8000c02019-08-02 20:15:51 +02001170#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1171psa_status_t psa_get_key_slot_number(
1172 const psa_key_attributes_t *attributes,
1173 psa_key_slot_number_t *slot_number )
1174{
1175 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1176 {
1177 *slot_number = attributes->slot_number;
1178 return( PSA_SUCCESS );
1179 }
1180 else
1181 return( PSA_ERROR_INVALID_ARGUMENT );
1182}
1183#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1184
Ronald Crond18b5f82020-11-25 16:46:05 +01001185static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1186 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001187 uint8_t *data,
1188 size_t data_size,
1189 size_t *data_length )
1190{
Ronald Crond18b5f82020-11-25 16:46:05 +01001191 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001192 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001193 memcpy( data, key_buffer, key_buffer_size );
1194 memset( data + key_buffer_size, 0,
1195 data_size - key_buffer_size );
1196 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001197 return( PSA_SUCCESS );
1198}
1199
Ronald Cron7285cda2020-11-26 14:46:37 +01001200psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001201 const psa_key_attributes_t *attributes,
1202 const uint8_t *key_buffer, size_t key_buffer_size,
1203 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204{
Ronald Crond18b5f82020-11-25 16:46:05 +01001205 psa_key_type_t type = attributes->core.type;
1206
Ronald Crond18b5f82020-11-25 16:46:05 +01001207 if( key_type_is_raw_bytes( type ) ||
1208 PSA_KEY_TYPE_IS_RSA( type ) ||
1209 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001210 {
1211 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001212 key_buffer, key_buffer_size,
1213 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001214 }
1215 else
1216 {
1217 /* This shouldn't happen in the reference implementation, but
1218 it is valid for a special-purpose implementation to omit
1219 support for exporting certain key types. */
1220 return( PSA_ERROR_NOT_SUPPORTED );
1221 }
1222}
1223
1224psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1225 uint8_t *data,
1226 size_t data_size,
1227 size_t *data_length )
1228{
1229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1231 psa_key_slot_t *slot;
1232
1233 /* Set the key to empty now, so that even when there are errors, we always
1234 * set data_length to a value between 0 and data_size. On error, setting
1235 * the key to empty is a good choice because an empty key representation is
1236 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001237 *data_length = 0;
1238
Ronald Cron9486f9d2020-11-26 13:36:23 +01001239 /* Export requires the EXPORT flag. There is an exception for public keys,
1240 * which don't require any flag, but
1241 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1242 */
1243 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1244 PSA_KEY_USAGE_EXPORT, 0 );
1245 if( status != PSA_SUCCESS )
1246 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001247
Gilles Peskinef9168942019-09-12 19:20:29 +02001248 /* Reject a zero-length output buffer now, since this can never be a
1249 * valid key representation. This way we know that data must be a valid
1250 * pointer and we can do things like memset(data, ..., data_size). */
1251 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001252 {
1253 status = PSA_ERROR_BUFFER_TOO_SMALL;
1254 goto exit;
1255 }
1256
Ronald Crond18b5f82020-11-25 16:46:05 +01001257 psa_key_attributes_t attributes = {
1258 .core = slot->attr
1259 };
Ronald Cron67227982020-11-26 15:16:05 +01001260 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001261 slot->key.data, slot->key.bytes,
1262 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001263
1264exit:
1265 unlock_status = psa_unlock_key_slot( slot );
1266
1267 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1268}
1269
Ronald Cron7285cda2020-11-26 14:46:37 +01001270psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001271 const psa_key_attributes_t *attributes,
1272 const uint8_t *key_buffer,
1273 size_t key_buffer_size,
1274 uint8_t *data,
1275 size_t data_size,
1276 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001277{
Ronald Crond18b5f82020-11-25 16:46:05 +01001278 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001279
Ronald Crond18b5f82020-11-25 16:46:05 +01001280 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001281 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001282 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001283 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001284 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001285 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001286 key_buffer, key_buffer_size,
1287 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001288 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001289
Ronald Crond18b5f82020-11-25 16:46:05 +01001290 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001291 {
John Durkop0e005192020-10-31 22:06:54 -07001292#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1293 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001294 return( mbedtls_psa_rsa_export_public_key( attributes,
1295 key_buffer,
1296 key_buffer_size,
1297 data,
1298 data_size,
1299 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001300#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001301 /* We don't know how to convert a private RSA key to public. */
1302 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001303#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1304 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001305 }
1306 else
Moran Pekera998bc62018-04-16 18:16:20 +03001307 {
John Durkop6ba40d12020-11-10 08:50:04 -08001308#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1309 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001310 return( mbedtls_psa_ecp_export_public_key( attributes,
1311 key_buffer,
1312 key_buffer_size,
1313 data,
1314 data_size,
1315 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001316#else
1317 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001318 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001319#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1320 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001321 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001322 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001323 else
1324 {
1325 /* This shouldn't happen in the reference implementation, but
1326 it is valid for a special-purpose implementation to omit
1327 support for exporting certain key types. */
1328 return( PSA_ERROR_NOT_SUPPORTED );
1329 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330}
Ronald Crond18b5f82020-11-25 16:46:05 +01001331
Ronald Croncf56a0a2020-08-04 09:51:30 +02001332psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001333 uint8_t *data,
1334 size_t data_size,
1335 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001336{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001338 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001339 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001340
1341 /* Set the key to empty now, so that even when there are errors, we always
1342 * set data_length to a value between 0 and data_size. On error, setting
1343 * the key to empty is a good choice because an empty key representation is
1344 * unlikely to be accepted anywhere. */
1345 *data_length = 0;
1346
1347 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001348 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001349 if( status != PSA_SUCCESS )
1350 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001351
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1353 {
1354 status = PSA_ERROR_INVALID_ARGUMENT;
1355 goto exit;
1356 }
1357
1358 /* Reject a zero-length output buffer now, since this can never be a
1359 * valid key representation. This way we know that data must be a valid
1360 * pointer and we can do things like memset(data, ..., data_size). */
1361 if( data_size == 0 )
1362 {
1363 status = PSA_ERROR_BUFFER_TOO_SMALL;
1364 goto exit;
1365 }
1366
Ronald Crond18b5f82020-11-25 16:46:05 +01001367 psa_key_attributes_t attributes = {
1368 .core = slot->attr
1369 };
Ronald Cron67227982020-11-26 15:16:05 +01001370 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001371 &attributes, slot->key.data, slot->key.bytes,
1372 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001373
1374exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001375 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001376
Ronald Cron5c522922020-11-14 16:35:34 +01001377 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001378}
1379
Gilles Peskine91e8c332019-08-02 19:19:39 +02001380#if defined(static_assert)
1381static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1382 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001383static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001384 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001385static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001386 "One or more key attribute flag is listed as both internal-only and external-only" );
1387#endif
1388
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001389/** Validate that a key policy is internally well-formed.
1390 *
1391 * This function only rejects invalid policies. It does not validate the
1392 * consistency of the policy with respect to other attributes of the key
1393 * such as the key type.
1394 */
1395static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001396{
1397 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001398 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001399 PSA_KEY_USAGE_ENCRYPT |
1400 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001401 PSA_KEY_USAGE_SIGN_HASH |
1402 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001403 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1404 return( PSA_ERROR_INVALID_ARGUMENT );
1405
Gilles Peskine4747d192019-04-17 15:05:45 +02001406 return( PSA_SUCCESS );
1407}
1408
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001409/** Validate the internal consistency of key attributes.
1410 *
1411 * This function only rejects invalid attribute values. If does not
1412 * validate the consistency of the attributes with any key data that may
1413 * be involved in the creation of the key.
1414 *
1415 * Call this function early in the key creation process.
1416 *
1417 * \param[in] attributes Key attributes for the new key.
1418 * \param[out] p_drv On any return, the driver for the key, if any.
1419 * NULL for a transparent key.
1420 *
1421 */
1422static psa_status_t psa_validate_key_attributes(
1423 const psa_key_attributes_t *attributes,
1424 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001425{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001426 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001427 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001428 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001429
Ronald Cron54b90082020-10-29 15:26:43 +01001430 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001431 if( status != PSA_SUCCESS )
1432 return( status );
1433
Ronald Crond2ed4812020-07-17 16:11:30 +02001434 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001435 if( status != PSA_SUCCESS )
1436 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001437
Ronald Cron65f38a32020-10-23 17:11:13 +02001438 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1439 {
1440 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1441 return( PSA_ERROR_INVALID_ARGUMENT );
1442 }
1443 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001444 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001445 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001446 if( status != PSA_SUCCESS )
1447 return( status );
1448 }
1449
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001450 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001451 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001452 return( status );
1453
1454 /* Refuse to create overly large keys.
1455 * Note that this doesn't trigger on import if the attributes don't
1456 * explicitly specify a size (so psa_get_key_bits returns 0), so
1457 * psa_import_key() needs its own checks. */
1458 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1459 return( PSA_ERROR_NOT_SUPPORTED );
1460
Gilles Peskine91e8c332019-08-02 19:19:39 +02001461 /* Reject invalid flags. These should not be reachable through the API. */
1462 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1463 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1464 return( PSA_ERROR_INVALID_ARGUMENT );
1465
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001466 return( PSA_SUCCESS );
1467}
1468
Gilles Peskine4747d192019-04-17 15:05:45 +02001469/** Prepare a key slot to receive key material.
1470 *
1471 * This function allocates a key slot and sets its metadata.
1472 *
1473 * If this function fails, call psa_fail_key_creation().
1474 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001475 * This function is intended to be used as follows:
1476 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001477 * it with the specified attributes, and in case of a volatile key assign it
1478 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001479 * -# Populate the slot with the key material.
1480 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1481 * In case of failure at any step, stop the sequence and call
1482 * psa_fail_key_creation().
1483 *
Ronald Cron5c522922020-11-14 16:35:34 +01001484 * On success, the key slot is locked. It is the responsibility of the caller
1485 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001486 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001487 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001488 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001489 * \param[out] p_slot On success, a pointer to the prepared slot.
1490 * \param[out] p_drv On any return, the driver for the key, if any.
1491 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001492 *
1493 * \retval #PSA_SUCCESS
1494 * The key slot is ready to receive key material.
1495 * \return If this function fails, the key slot is an invalid state.
1496 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001497 */
1498static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001499 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001500 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001501 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001502 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001503{
1504 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001505 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001506 psa_key_slot_t *slot;
1507
Gilles Peskinedf179142019-07-15 22:02:14 +02001508 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001509 *p_drv = NULL;
1510
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001511 status = psa_validate_key_attributes( attributes, p_drv );
1512 if( status != PSA_SUCCESS )
1513 return( status );
1514
Ronald Cronc4d1b512020-07-31 11:26:37 +02001515 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001516 if( status != PSA_SUCCESS )
1517 return( status );
1518 slot = *p_slot;
1519
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001520 /* We're storing the declared bit-size of the key. It's up to each
1521 * creation mechanism to verify that this information is correct.
1522 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001523 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001524 * is optional (import, copy). In case of a volatile key, assign it the
1525 * volatile key identifier associated to the slot returned to contain its
1526 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001527
1528 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001529 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1530 {
1531#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1532 slot->attr.id = volatile_key_id;
1533#else
1534 slot->attr.id.key_id = volatile_key_id;
1535#endif
1536 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001537
Gilles Peskine91e8c332019-08-02 19:19:39 +02001538 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001539 * external-only flags, query `attributes`. Thanks to the check
1540 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001541 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001542 * may have set. */
1543 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001544
Gilles Peskinecbaff462019-07-12 23:46:04 +02001545#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001546 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001547 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001548 * create the key file in internal storage, create the
1549 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001550 * persistent data. This is done by starting a transaction that will
1551 * encompass these three actions.
1552 * For registering a volatile key, we just need to find an appropriate
1553 * slot number inside the SE. Since the key is designated volatile, creating
1554 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001555 /* The first thing to do is to find a slot number for the new key.
1556 * We save the slot number in persistent storage as part of the
1557 * transaction data. It will be needed to recover if the power
1558 * fails during the key creation process, to clean up on the secure
1559 * element side after restarting. Obtaining a slot number from the
1560 * secure element driver updates its persistent state, but we do not yet
1561 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001562 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001563 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001564 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001565 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001566 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001567 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001568 if( status != PSA_SUCCESS )
1569 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001570
1571 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001572 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001573 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1574 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001575 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001576 psa_crypto_transaction.key.id = slot->attr.id;
1577 status = psa_crypto_save_transaction( );
1578 if( status != PSA_SUCCESS )
1579 {
1580 (void) psa_crypto_stop_transaction( );
1581 return( status );
1582 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001583 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001584
1585 status = psa_copy_key_material_into_slot(
1586 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001587 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001588
1589 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1590 {
1591 /* Key registration only makes sense with a secure element. */
1592 return( PSA_ERROR_INVALID_ARGUMENT );
1593 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001594#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1595
Ronald Cronc4d1b512020-07-31 11:26:37 +02001596 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001597}
Gilles Peskine4747d192019-04-17 15:05:45 +02001598
1599/** Finalize the creation of a key once its key material has been set.
1600 *
1601 * This entails writing the key to persistent storage.
1602 *
1603 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001604 * See the documentation of psa_start_key_creation() for the intended use
1605 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 *
Ronald Cron5c522922020-11-14 16:35:34 +01001607 * If the finalization succeeds, the function unlocks the key slot (it was
1608 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1609 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001610 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001611 * \param[in,out] slot Pointer to the slot with key material.
1612 * \param[in] driver The secure element driver for the key,
1613 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001614 * \param[out] key On success, identifier of the key. Note that the
1615 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001616 *
1617 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001618 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001619 * \return If this function fails, the key slot is an invalid state.
1620 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001622static psa_status_t psa_finish_key_creation(
1623 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001624 psa_se_drv_table_entry_t *driver,
1625 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001626{
1627 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001628 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001629 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001630
1631#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001632 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001634#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1635 if( driver != NULL )
1636 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001637 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001638 psa_key_slot_number_t slot_number =
1639 psa_key_slot_get_slot_number( slot ) ;
1640
Gilles Peskineb46bef22019-07-30 21:32:04 +02001641#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001642 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001643 sizeof( data.slot_number ),
1644 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001645#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001646 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001647 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001648 (uint8_t*) &data,
1649 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001650 }
1651 else
1652#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1653 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001654 /* Key material is saved in export representation in the slot, so
1655 * just pass the slot buffer for storage. */
1656 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001657 slot->key.data,
1658 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001659 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001660 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001661#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1662
Gilles Peskinecbaff462019-07-12 23:46:04 +02001663#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001664 /* Finish the transaction for a key creation. This does not
1665 * happen when registering an existing key. Detect this case
1666 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001667 * creation of a persistent key in a secure element requires a transaction,
1668 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001669 if( driver != NULL &&
1670 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001671 {
1672 status = psa_save_se_persistent_data( driver );
1673 if( status != PSA_SUCCESS )
1674 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001675 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001676 return( status );
1677 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001678 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001679 }
1680#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1681
Ronald Cron50972942020-11-14 11:28:25 +01001682 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001683 {
1684 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001685 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001686 if( status != PSA_SUCCESS )
1687 *key = MBEDTLS_SVC_KEY_ID_INIT;
1688 }
Ronald Cron50972942020-11-14 11:28:25 +01001689
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 return( status );
1691}
1692
1693/** Abort the creation of a key.
1694 *
1695 * You may call this function after calling psa_start_key_creation(),
1696 * or after psa_finish_key_creation() fails. In other circumstances, this
1697 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001698 * See the documentation of psa_start_key_creation() for the intended use
1699 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001700 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001701 * \param[in,out] slot Pointer to the slot with key material.
1702 * \param[in] driver The secure element driver for the key,
1703 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001704 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001705static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001706 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001707{
Gilles Peskine011e4282019-06-26 18:34:38 +02001708 (void) driver;
1709
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 if( slot == NULL )
1711 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001712
1713#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001714 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001715 * element, and the failure happened later (when saving metadata
1716 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001717 * element.
1718 * https://github.com/ARMmbed/mbed-crypto/issues/217
1719 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001720
Gilles Peskined7729582019-08-05 15:55:54 +02001721 /* Abort the ongoing transaction if any (there may not be one if
1722 * the creation process failed before starting one, or if the
1723 * key creation is a registration of a key in a secure element).
1724 * Earlier functions must already have done what it takes to undo any
1725 * partial creation. All that's left is to update the transaction data
1726 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001727 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001728#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1729
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 psa_wipe_key_slot( slot );
1731}
1732
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001733/** Validate optional attributes during key creation.
1734 *
1735 * Some key attributes are optional during key creation. If they are
1736 * specified in the attributes structure, check that they are consistent
1737 * with the data in the slot.
1738 *
1739 * This function should be called near the end of key creation, after
1740 * the slot in memory is fully populated but before saving persistent data.
1741 */
1742static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001743 const psa_key_slot_t *slot,
1744 const psa_key_attributes_t *attributes )
1745{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001746 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001747 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001748 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001749 return( PSA_ERROR_INVALID_ARGUMENT );
1750 }
1751
1752 if( attributes->domain_parameters_size != 0 )
1753 {
John Durkop0e005192020-10-31 22:06:54 -07001754#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1755 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001756 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001757 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001758 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001759 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001761
Ronald Cron90857082020-11-25 14:58:33 +01001762 psa_status_t status = mbedtls_psa_rsa_load_representation(
1763 slot->attr.type,
1764 slot->key.data,
1765 slot->key.bytes,
1766 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001767 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001768 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001769
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001770 mbedtls_mpi_init( &actual );
1771 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001772 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001774 mbedtls_rsa_free( rsa );
1775 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001776 if( ret != 0 )
1777 goto rsa_exit;
1778 ret = mbedtls_mpi_read_binary( &required,
1779 attributes->domain_parameters,
1780 attributes->domain_parameters_size );
1781 if( ret != 0 )
1782 goto rsa_exit;
1783 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1784 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1785 rsa_exit:
1786 mbedtls_mpi_free( &actual );
1787 mbedtls_mpi_free( &required );
1788 if( ret != 0)
1789 return( mbedtls_to_psa_error( ret ) );
1790 }
1791 else
John Durkop9814fa22020-11-04 12:28:15 -08001792#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1793 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001794 {
1795 return( PSA_ERROR_INVALID_ARGUMENT );
1796 }
1797 }
1798
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001799 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001800 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001801 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 return( PSA_ERROR_INVALID_ARGUMENT );
1803 }
1804
1805 return( PSA_SUCCESS );
1806}
1807
Gilles Peskine4747d192019-04-17 15:05:45 +02001808psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001809 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001810 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001811 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001812{
1813 psa_status_t status;
1814 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001815 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001816
Ronald Cron81709fc2020-11-14 12:10:32 +01001817 *key = MBEDTLS_SVC_KEY_ID_INIT;
1818
Gilles Peskine0f84d622019-09-12 19:03:13 +02001819 /* Reject zero-length symmetric keys (including raw data key objects).
1820 * This also rejects any key which might be encoded as an empty string,
1821 * which is never valid. */
1822 if( data_length == 0 )
1823 return( PSA_ERROR_INVALID_ARGUMENT );
1824
Gilles Peskinedf179142019-07-15 22:02:14 +02001825 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001826 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001827 if( status != PSA_SUCCESS )
1828 goto exit;
1829
Gilles Peskine5d309672019-07-12 23:47:28 +02001830#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1831 if( driver != NULL )
1832 {
1833 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001834 /* The driver should set the number of key bits, however in
1835 * case it doesn't, we initialize bits to an invalid value. */
1836 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001837 if( drv->key_management == NULL ||
1838 drv->key_management->p_import == NULL )
1839 {
1840 status = PSA_ERROR_NOT_SUPPORTED;
1841 goto exit;
1842 }
1843 status = drv->key_management->p_import(
1844 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01001845 psa_key_slot_get_slot_number( slot ),
1846 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001847 if( status != PSA_SUCCESS )
1848 goto exit;
1849 if( bits > PSA_MAX_KEY_BITS )
1850 {
1851 status = PSA_ERROR_NOT_SUPPORTED;
1852 goto exit;
1853 }
1854 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001855 }
1856 else
1857#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01001858 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
1859 {
1860 /* Importing a key with external lifetime through the driver wrapper
1861 * interface is not yet supported. Return as if this was an invalid
1862 * lifetime. */
1863 status = PSA_ERROR_INVALID_ARGUMENT;
1864 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001865 }
1866 else
Gilles Peskine5d309672019-07-12 23:47:28 +02001867 {
Ronald Crondd04d422020-11-28 15:14:42 +01001868 status = psa_allocate_buffer_to_slot( slot, data_length );
1869 if( status != PSA_SUCCESS )
1870 goto exit;
1871
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001872 size_t bits = slot->attr.bits;
1873 status = psa_import_key_into_slot( attributes,
1874 data, data_length,
Ronald Crondd04d422020-11-28 15:14:42 +01001875 slot->key.data,
1876 slot->key.bytes,
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001877 &slot->key.bytes, &bits );
Gilles Peskine5d309672019-07-12 23:47:28 +02001878 if( status != PSA_SUCCESS )
1879 goto exit;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001880
1881 if( slot->attr.bits == 0 )
1882 slot->attr.bits = (psa_key_bits_t) bits;
1883 else if( bits != slot->attr.bits )
1884 {
1885 status = PSA_ERROR_INVALID_ARGUMENT;
1886 goto exit;
1887 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001888 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001889
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001890 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001891 if( status != PSA_SUCCESS )
1892 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893
Ronald Cron81709fc2020-11-14 12:10:32 +01001894 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001895exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001897 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001898
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 return( status );
1900}
1901
Gilles Peskined7729582019-08-05 15:55:54 +02001902#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1903psa_status_t mbedtls_psa_register_se_key(
1904 const psa_key_attributes_t *attributes )
1905{
1906 psa_status_t status;
1907 psa_key_slot_t *slot = NULL;
1908 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001910
1911 /* Leaving attributes unspecified is not currently supported.
1912 * It could make sense to query the key type and size from the
1913 * secure element, but not all secure elements support this
1914 * and the driver HAL doesn't currently support it. */
1915 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1916 return( PSA_ERROR_NOT_SUPPORTED );
1917 if( psa_get_key_bits( attributes ) == 0 )
1918 return( PSA_ERROR_NOT_SUPPORTED );
1919
1920 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001921 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001922 if( status != PSA_SUCCESS )
1923 goto exit;
1924
Ronald Cron81709fc2020-11-14 12:10:32 +01001925 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001926
1927exit:
1928 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001929 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001930
Gilles Peskined7729582019-08-05 15:55:54 +02001931 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001932 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001933 return( status );
1934}
1935#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1936
Gilles Peskinef603c712019-01-19 13:40:11 +01001937static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001938 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001939{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001940 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001941 source->key.data,
1942 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001943 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001944 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001945
Steven Cooreman398aee52020-10-13 14:35:45 +02001946 target->attr.type = source->attr.type;
1947 target->attr.bits = source->attr.bits;
1948
1949 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950}
1951
Ronald Croncf56a0a2020-08-04 09:51:30 +02001952psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001953 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001954 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001955{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001957 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001958 psa_key_slot_t *source_slot = NULL;
1959 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001960 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001961 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001962
Ronald Cron81709fc2020-11-14 12:10:32 +01001963 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1964
Ronald Cron5c522922020-11-14 16:35:34 +01001965 status = psa_get_and_lock_transparent_key_slot_with_policy(
1966 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001967 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968 goto exit;
1969
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001970 status = psa_validate_optional_attributes( source_slot,
1971 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001972 if( status != PSA_SUCCESS )
1973 goto exit;
1974
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001975 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001976 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Ronald Cron81709fc2020-11-14 12:10:32 +01001980 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
1981 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001985#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1986 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001987 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001988 /* Copying to a secure element is not implemented yet. */
1989 status = PSA_ERROR_NOT_SUPPORTED;
1990 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001991 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001992#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001993
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001995 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001997
Ronald Cron81709fc2020-11-14 12:10:32 +01001998 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002001 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002002
Ronald Cron5c522922020-11-14 16:35:34 +01002003 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002004
Ronald Cron5c522922020-11-14 16:35:34 +01002005 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002006}
2007
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002008
2009
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002010/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002011/* Message digests */
2012/****************************************************************/
2013
John Durkop07cc04a2020-11-16 22:08:34 -08002014#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002015 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2016 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002017 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002018static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002019{
2020 switch( alg )
2021 {
John Durkopee4e6602020-11-27 08:48:46 -08002022#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002023 case PSA_ALG_MD2:
2024 return( &mbedtls_md2_info );
2025#endif
John Durkopee4e6602020-11-27 08:48:46 -08002026#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002027 case PSA_ALG_MD4:
2028 return( &mbedtls_md4_info );
2029#endif
John Durkopee4e6602020-11-27 08:48:46 -08002030#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002031 case PSA_ALG_MD5:
2032 return( &mbedtls_md5_info );
2033#endif
John Durkopee4e6602020-11-27 08:48:46 -08002034#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002035 case PSA_ALG_RIPEMD160:
2036 return( &mbedtls_ripemd160_info );
2037#endif
John Durkopee4e6602020-11-27 08:48:46 -08002038#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002039 case PSA_ALG_SHA_1:
2040 return( &mbedtls_sha1_info );
2041#endif
John Durkopee4e6602020-11-27 08:48:46 -08002042#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002043 case PSA_ALG_SHA_224:
2044 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002045#endif
2046#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002047 case PSA_ALG_SHA_256:
2048 return( &mbedtls_sha256_info );
2049#endif
John Durkopee4e6602020-11-27 08:48:46 -08002050#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002051 case PSA_ALG_SHA_384:
2052 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002053#endif
John Durkopee4e6602020-11-27 08:48:46 -08002054#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002055 case PSA_ALG_SHA_512:
2056 return( &mbedtls_sha512_info );
2057#endif
2058 default:
2059 return( NULL );
2060 }
2061}
John Durkop07cc04a2020-11-16 22:08:34 -08002062#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002063 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2064 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2065 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002066
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2068{
2069 switch( operation->alg )
2070 {
Gilles Peskine81736312018-06-26 15:04:31 +02002071 case 0:
2072 /* The object has (apparently) been initialized but it is not
2073 * in use. It's ok to call abort on such an object, and there's
2074 * nothing to do. */
2075 break;
John Durkopee4e6602020-11-27 08:48:46 -08002076#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002077 case PSA_ALG_MD2:
2078 mbedtls_md2_free( &operation->ctx.md2 );
2079 break;
2080#endif
John Durkopee4e6602020-11-27 08:48:46 -08002081#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082 case PSA_ALG_MD4:
2083 mbedtls_md4_free( &operation->ctx.md4 );
2084 break;
2085#endif
John Durkopee4e6602020-11-27 08:48:46 -08002086#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002087 case PSA_ALG_MD5:
2088 mbedtls_md5_free( &operation->ctx.md5 );
2089 break;
2090#endif
John Durkopee4e6602020-11-27 08:48:46 -08002091#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002092 case PSA_ALG_RIPEMD160:
2093 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2094 break;
2095#endif
John Durkopee4e6602020-11-27 08:48:46 -08002096#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002097 case PSA_ALG_SHA_1:
2098 mbedtls_sha1_free( &operation->ctx.sha1 );
2099 break;
2100#endif
John Durkop6ca23272020-12-03 06:01:32 -08002101#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002102 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002103 mbedtls_sha256_free( &operation->ctx.sha256 );
2104 break;
2105#endif
2106#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107 case PSA_ALG_SHA_256:
2108 mbedtls_sha256_free( &operation->ctx.sha256 );
2109 break;
2110#endif
John Durkop6ca23272020-12-03 06:01:32 -08002111#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002113 mbedtls_sha512_free( &operation->ctx.sha512 );
2114 break;
2115#endif
2116#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117 case PSA_ALG_SHA_512:
2118 mbedtls_sha512_free( &operation->ctx.sha512 );
2119 break;
2120#endif
2121 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002122 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 }
2124 operation->alg = 0;
2125 return( PSA_SUCCESS );
2126}
2127
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002128psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 psa_algorithm_t alg )
2130{
Janos Follath24eed8d2019-11-22 13:21:35 +00002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002132
2133 /* A context must be freshly initialized before it can be set up. */
2134 if( operation->alg != 0 )
2135 {
2136 return( PSA_ERROR_BAD_STATE );
2137 }
2138
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002139 switch( alg )
2140 {
John Durkopee4e6602020-11-27 08:48:46 -08002141#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002142 case PSA_ALG_MD2:
2143 mbedtls_md2_init( &operation->ctx.md2 );
2144 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2145 break;
2146#endif
John Durkopee4e6602020-11-27 08:48:46 -08002147#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002148 case PSA_ALG_MD4:
2149 mbedtls_md4_init( &operation->ctx.md4 );
2150 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2151 break;
2152#endif
John Durkopee4e6602020-11-27 08:48:46 -08002153#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154 case PSA_ALG_MD5:
2155 mbedtls_md5_init( &operation->ctx.md5 );
2156 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2157 break;
2158#endif
John Durkopee4e6602020-11-27 08:48:46 -08002159#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002160 case PSA_ALG_RIPEMD160:
2161 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2162 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2163 break;
2164#endif
John Durkopee4e6602020-11-27 08:48:46 -08002165#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002166 case PSA_ALG_SHA_1:
2167 mbedtls_sha1_init( &operation->ctx.sha1 );
2168 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2169 break;
2170#endif
John Durkopee4e6602020-11-27 08:48:46 -08002171#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172 case PSA_ALG_SHA_224:
2173 mbedtls_sha256_init( &operation->ctx.sha256 );
2174 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2175 break;
John Durkopee4e6602020-11-27 08:48:46 -08002176#endif
2177#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178 case PSA_ALG_SHA_256:
2179 mbedtls_sha256_init( &operation->ctx.sha256 );
2180 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2181 break;
2182#endif
John Durkopee4e6602020-11-27 08:48:46 -08002183#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184 case PSA_ALG_SHA_384:
2185 mbedtls_sha512_init( &operation->ctx.sha512 );
2186 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2187 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002188#endif
John Durkopee4e6602020-11-27 08:48:46 -08002189#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190 case PSA_ALG_SHA_512:
2191 mbedtls_sha512_init( &operation->ctx.sha512 );
2192 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2193 break;
2194#endif
2195 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002196 return( PSA_ALG_IS_HASH( alg ) ?
2197 PSA_ERROR_NOT_SUPPORTED :
2198 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199 }
2200 if( ret == 0 )
2201 operation->alg = alg;
2202 else
2203 psa_hash_abort( operation );
2204 return( mbedtls_to_psa_error( ret ) );
2205}
2206
2207psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2208 const uint8_t *input,
2209 size_t input_length )
2210{
Janos Follath24eed8d2019-11-22 13:21:35 +00002211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002212
2213 /* Don't require hash implementations to behave correctly on a
2214 * zero-length input, which may have an invalid pointer. */
2215 if( input_length == 0 )
2216 return( PSA_SUCCESS );
2217
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002218 switch( operation->alg )
2219 {
John Durkopee4e6602020-11-27 08:48:46 -08002220#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221 case PSA_ALG_MD2:
2222 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2223 input, input_length );
2224 break;
2225#endif
John Durkopee4e6602020-11-27 08:48:46 -08002226#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227 case PSA_ALG_MD4:
2228 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2229 input, input_length );
2230 break;
2231#endif
John Durkopee4e6602020-11-27 08:48:46 -08002232#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233 case PSA_ALG_MD5:
2234 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2235 input, input_length );
2236 break;
2237#endif
John Durkopee4e6602020-11-27 08:48:46 -08002238#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 case PSA_ALG_RIPEMD160:
2240 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2241 input, input_length );
2242 break;
2243#endif
John Durkopee4e6602020-11-27 08:48:46 -08002244#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245 case PSA_ALG_SHA_1:
2246 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2247 input, input_length );
2248 break;
2249#endif
John Durkop6ca23272020-12-03 06:01:32 -08002250#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002252 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2253 input, input_length );
2254 break;
2255#endif
2256#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 case PSA_ALG_SHA_256:
2258 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2259 input, input_length );
2260 break;
2261#endif
John Durkop6ca23272020-12-03 06:01:32 -08002262#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002264 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2265 input, input_length );
2266 break;
2267#endif
2268#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 case PSA_ALG_SHA_512:
2270 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2271 input, input_length );
2272 break;
2273#endif
2274 default:
John Durkopee4e6602020-11-27 08:48:46 -08002275 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002276 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002278
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 if( ret != 0 )
2280 psa_hash_abort( operation );
2281 return( mbedtls_to_psa_error( ret ) );
2282}
2283
2284psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2285 uint8_t *hash,
2286 size_t hash_size,
2287 size_t *hash_length )
2288{
itayzafrir40835d42018-08-02 13:14:17 +03002289 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002291 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292
2293 /* Fill the output buffer with something that isn't a valid hash
2294 * (barring an attack on the hash and deliberately-crafted input),
2295 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002296 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002297 /* If hash_size is 0 then hash may be NULL and then the
2298 * call to memset would have undefined behavior. */
2299 if( hash_size != 0 )
2300 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301
2302 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002303 {
2304 status = PSA_ERROR_BUFFER_TOO_SMALL;
2305 goto exit;
2306 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002307
2308 switch( operation->alg )
2309 {
John Durkopee4e6602020-11-27 08:48:46 -08002310#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 case PSA_ALG_MD2:
2312 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2313 break;
2314#endif
John Durkopee4e6602020-11-27 08:48:46 -08002315#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 case PSA_ALG_MD4:
2317 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2318 break;
2319#endif
John Durkopee4e6602020-11-27 08:48:46 -08002320#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 case PSA_ALG_MD5:
2322 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2323 break;
2324#endif
John Durkopee4e6602020-11-27 08:48:46 -08002325#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_RIPEMD160:
2327 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2328 break;
2329#endif
John Durkopee4e6602020-11-27 08:48:46 -08002330#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_1:
2332 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2333 break;
2334#endif
John Durkop6ca23272020-12-03 06:01:32 -08002335#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002337 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2338 break;
2339#endif
2340#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 case PSA_ALG_SHA_256:
2342 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2343 break;
2344#endif
John Durkop6ca23272020-12-03 06:01:32 -08002345#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002347 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2348 break;
2349#endif
2350#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 case PSA_ALG_SHA_512:
2352 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2353 break;
2354#endif
2355 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002356 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357 }
itayzafrir40835d42018-08-02 13:14:17 +03002358 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359
itayzafrir40835d42018-08-02 13:14:17 +03002360exit:
2361 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002363 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364 return( psa_hash_abort( operation ) );
2365 }
2366 else
2367 {
2368 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002369 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 }
2371}
2372
Gilles Peskine2d277862018-06-18 15:41:12 +02002373psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2374 const uint8_t *hash,
2375 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376{
2377 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2378 size_t actual_hash_length;
2379 psa_status_t status = psa_hash_finish( operation,
2380 actual_hash, sizeof( actual_hash ),
2381 &actual_hash_length );
2382 if( status != PSA_SUCCESS )
2383 return( status );
2384 if( actual_hash_length != hash_length )
2385 return( PSA_ERROR_INVALID_SIGNATURE );
2386 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2387 return( PSA_ERROR_INVALID_SIGNATURE );
2388 return( PSA_SUCCESS );
2389}
2390
Gilles Peskine0a749c82019-11-28 19:33:58 +01002391psa_status_t psa_hash_compute( psa_algorithm_t alg,
2392 const uint8_t *input, size_t input_length,
2393 uint8_t *hash, size_t hash_size,
2394 size_t *hash_length )
2395{
2396 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2398
2399 *hash_length = hash_size;
2400 status = psa_hash_setup( &operation, alg );
2401 if( status != PSA_SUCCESS )
2402 goto exit;
2403 status = psa_hash_update( &operation, input, input_length );
2404 if( status != PSA_SUCCESS )
2405 goto exit;
2406 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2407 if( status != PSA_SUCCESS )
2408 goto exit;
2409
2410exit:
2411 if( status == PSA_SUCCESS )
2412 status = psa_hash_abort( &operation );
2413 else
2414 psa_hash_abort( &operation );
2415 return( status );
2416}
2417
2418psa_status_t psa_hash_compare( psa_algorithm_t alg,
2419 const uint8_t *input, size_t input_length,
2420 const uint8_t *hash, size_t hash_length )
2421{
2422 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2424
2425 status = psa_hash_setup( &operation, alg );
2426 if( status != PSA_SUCCESS )
2427 goto exit;
2428 status = psa_hash_update( &operation, input, input_length );
2429 if( status != PSA_SUCCESS )
2430 goto exit;
2431 status = psa_hash_verify( &operation, hash, hash_length );
2432 if( status != PSA_SUCCESS )
2433 goto exit;
2434
2435exit:
2436 if( status == PSA_SUCCESS )
2437 status = psa_hash_abort( &operation );
2438 else
2439 psa_hash_abort( &operation );
2440 return( status );
2441}
2442
Gilles Peskineeb35d782019-01-22 17:56:16 +01002443psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2444 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002445{
2446 if( target_operation->alg != 0 )
2447 return( PSA_ERROR_BAD_STATE );
2448
2449 switch( source_operation->alg )
2450 {
2451 case 0:
2452 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002453#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002454 case PSA_ALG_MD2:
2455 mbedtls_md2_clone( &target_operation->ctx.md2,
2456 &source_operation->ctx.md2 );
2457 break;
2458#endif
John Durkopee4e6602020-11-27 08:48:46 -08002459#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002460 case PSA_ALG_MD4:
2461 mbedtls_md4_clone( &target_operation->ctx.md4,
2462 &source_operation->ctx.md4 );
2463 break;
2464#endif
John Durkopee4e6602020-11-27 08:48:46 -08002465#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002466 case PSA_ALG_MD5:
2467 mbedtls_md5_clone( &target_operation->ctx.md5,
2468 &source_operation->ctx.md5 );
2469 break;
2470#endif
John Durkopee4e6602020-11-27 08:48:46 -08002471#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002472 case PSA_ALG_RIPEMD160:
2473 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2474 &source_operation->ctx.ripemd160 );
2475 break;
2476#endif
John Durkopee4e6602020-11-27 08:48:46 -08002477#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002478 case PSA_ALG_SHA_1:
2479 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2480 &source_operation->ctx.sha1 );
2481 break;
2482#endif
John Durkop6ca23272020-12-03 06:01:32 -08002483#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002484 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002485 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2486 &source_operation->ctx.sha256 );
2487 break;
2488#endif
2489#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002490 case PSA_ALG_SHA_256:
2491 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2492 &source_operation->ctx.sha256 );
2493 break;
2494#endif
John Durkop6ca23272020-12-03 06:01:32 -08002495#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002496 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002497 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2498 &source_operation->ctx.sha512 );
2499 break;
2500#endif
2501#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002502 case PSA_ALG_SHA_512:
2503 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2504 &source_operation->ctx.sha512 );
2505 break;
2506#endif
2507 default:
2508 return( PSA_ERROR_NOT_SUPPORTED );
2509 }
2510
2511 target_operation->alg = source_operation->alg;
2512 return( PSA_SUCCESS );
2513}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514
2515
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517/* MAC */
2518/****************************************************************/
2519
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002520static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521 psa_algorithm_t alg,
2522 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002523 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002524 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002527 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002529 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002530 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002531
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2533 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002534 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002536 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 mode = MBEDTLS_MODE_STREAM;
2538 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 case PSA_ALG_CTR:
2540 mode = MBEDTLS_MODE_CTR;
2541 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002542 case PSA_ALG_CFB:
2543 mode = MBEDTLS_MODE_CFB;
2544 break;
2545 case PSA_ALG_OFB:
2546 mode = MBEDTLS_MODE_OFB;
2547 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002548 case PSA_ALG_ECB_NO_PADDING:
2549 mode = MBEDTLS_MODE_ECB;
2550 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002551 case PSA_ALG_CBC_NO_PADDING:
2552 mode = MBEDTLS_MODE_CBC;
2553 break;
2554 case PSA_ALG_CBC_PKCS7:
2555 mode = MBEDTLS_MODE_CBC;
2556 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002557 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 mode = MBEDTLS_MODE_CCM;
2559 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002560 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 mode = MBEDTLS_MODE_GCM;
2562 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002563 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2564 mode = MBEDTLS_MODE_CHACHAPOLY;
2565 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 default:
2567 return( NULL );
2568 }
2569 }
2570 else if( alg == PSA_ALG_CMAC )
2571 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 else
2573 return( NULL );
2574
2575 switch( key_type )
2576 {
2577 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002578 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579 break;
2580 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002581 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2582 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002584 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002586 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002587 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2588 * but two-key Triple-DES is functionally three-key Triple-DES
2589 * with K1=K3, so that's how we present it to mbedtls. */
2590 if( key_bits == 128 )
2591 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592 break;
2593 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002594 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595 break;
2596 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002597 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002599 case PSA_KEY_TYPE_CHACHA20:
2600 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2601 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602 default:
2603 return( NULL );
2604 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002605 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002606 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607
Jaeden Amero23bbb752018-06-26 14:16:54 +01002608 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2609 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610}
2611
John Durkop6ba40d12020-11-10 08:50:04 -08002612#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002613static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002614{
Gilles Peskine2d277862018-06-18 15:41:12 +02002615 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002616 {
2617 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002618 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002619 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002620 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002621 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002622 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002623 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002624 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002625 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002626 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002627 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002628 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002629 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002630 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002631 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002632 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002633 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002634 return( 128 );
2635 default:
2636 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002637 }
2638}
John Durkop07cc04a2020-11-16 22:08:34 -08002639#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002640
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641/* Initialize the MAC operation structure. Once this function has been
2642 * called, psa_mac_abort can run and will do the right thing. */
2643static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2644 psa_algorithm_t alg )
2645{
2646 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2647
2648 operation->alg = alg;
2649 operation->key_set = 0;
2650 operation->iv_set = 0;
2651 operation->iv_required = 0;
2652 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002653 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654
2655#if defined(MBEDTLS_CMAC_C)
2656 if( alg == PSA_ALG_CMAC )
2657 {
2658 operation->iv_required = 0;
2659 mbedtls_cipher_init( &operation->ctx.cmac );
2660 status = PSA_SUCCESS;
2661 }
2662 else
2663#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002664#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002665 if( PSA_ALG_IS_HMAC( operation->alg ) )
2666 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002667 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2668 operation->ctx.hmac.hash_ctx.alg = 0;
2669 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002670 }
2671 else
John Durkopd0321952020-10-29 21:37:36 -07002672#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002673 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002674 if( ! PSA_ALG_IS_MAC( alg ) )
2675 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676 }
2677
2678 if( status != PSA_SUCCESS )
2679 memset( operation, 0, sizeof( *operation ) );
2680 return( status );
2681}
2682
John Durkop6ba40d12020-11-10 08:50:04 -08002683#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002684static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2685{
Gilles Peskine3f108122018-12-07 18:14:53 +01002686 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002687 return( psa_hash_abort( &hmac->hash_ctx ) );
2688}
John Durkop6ba40d12020-11-10 08:50:04 -08002689#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002690
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2692{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002693 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002695 /* The object has (apparently) been initialized but it is not
2696 * in use. It's ok to call abort on such an object, and there's
2697 * nothing to do. */
2698 return( PSA_SUCCESS );
2699 }
2700 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002702 if( operation->alg == PSA_ALG_CMAC )
2703 {
2704 mbedtls_cipher_free( &operation->ctx.cmac );
2705 }
2706 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002708#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002709 if( PSA_ALG_IS_HMAC( operation->alg ) )
2710 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002711 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002712 }
2713 else
John Durkopd0321952020-10-29 21:37:36 -07002714#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002715 {
2716 /* Sanity check (shouldn't happen: operation->alg should
2717 * always have been initialized to a valid value). */
2718 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 }
Moran Peker41deec42018-04-04 15:43:05 +03002720
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721 operation->alg = 0;
2722 operation->key_set = 0;
2723 operation->iv_set = 0;
2724 operation->iv_required = 0;
2725 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002726 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002727
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002729
2730bad_state:
2731 /* If abort is called on an uninitialized object, we can't trust
2732 * anything. Wipe the object in case it contains confidential data.
2733 * This may result in a memory leak if a pointer gets overwritten,
2734 * but it's too late to do anything about this. */
2735 memset( operation, 0, sizeof( *operation ) );
2736 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737}
2738
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002739#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002740static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002741 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002742 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002743 const mbedtls_cipher_info_t *cipher_info )
2744{
Janos Follath24eed8d2019-11-22 13:21:35 +00002745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746
2747 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748
2749 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2750 if( ret != 0 )
2751 return( ret );
2752
2753 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002754 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002755 key_bits );
2756 return( ret );
2757}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002758#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002759
John Durkop6ba40d12020-11-10 08:50:04 -08002760#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002761static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2762 const uint8_t *key,
2763 size_t key_length,
2764 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002766 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002767 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002768 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002769 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770 psa_status_t status;
2771
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002772 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2773 * overflow below. This should never trigger if the hash algorithm
2774 * is implemented correctly. */
2775 /* The size checks against the ipad and opad buffers cannot be written
2776 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2777 * because that triggers -Wlogical-op on GCC 7.3. */
2778 if( block_size > sizeof( ipad ) )
2779 return( PSA_ERROR_NOT_SUPPORTED );
2780 if( block_size > sizeof( hmac->opad ) )
2781 return( PSA_ERROR_NOT_SUPPORTED );
2782 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002783 return( PSA_ERROR_NOT_SUPPORTED );
2784
Gilles Peskined223b522018-06-11 18:12:58 +02002785 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002787 status = psa_hash_compute( hash_alg, key, key_length,
2788 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002790 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002791 }
Gilles Peskine96889972018-07-12 17:07:03 +02002792 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2793 * but it is permitted. It is common when HMAC is used in HKDF, for
2794 * example. Don't call `memcpy` in the 0-length because `key` could be
2795 * an invalid pointer which would make the behavior undefined. */
2796 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002797 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002798
Gilles Peskined223b522018-06-11 18:12:58 +02002799 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2800 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002802 ipad[i] ^= 0x36;
2803 memset( ipad + key_length, 0x36, block_size - key_length );
2804
2805 /* Copy the key material from ipad to opad, flipping the requisite bits,
2806 * and filling the rest of opad with the requisite constant. */
2807 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002808 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2809 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002810
Gilles Peskine01126fa2018-07-12 17:04:55 +02002811 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002812 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002813 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002814
Gilles Peskine01126fa2018-07-12 17:04:55 +02002815 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002816
2817cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002818 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002819
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002820 return( status );
2821}
John Durkop6ba40d12020-11-10 08:50:04 -08002822#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002823
Gilles Peskine89167cb2018-07-08 20:12:23 +02002824static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002825 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002826 psa_algorithm_t alg,
2827 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002830 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002831 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002833 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002834 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002835 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002836 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002838 /* A context must be freshly initialized before it can be set up. */
2839 if( operation->alg != 0 )
2840 {
2841 return( PSA_ERROR_BAD_STATE );
2842 }
2843
Gilles Peskined911eb72018-08-14 15:18:45 +02002844 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002845 if( status != PSA_SUCCESS )
2846 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002847 if( is_sign )
2848 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849
Ronald Cron5c522922020-11-14 16:35:34 +01002850 status = psa_get_and_lock_transparent_key_slot_with_policy(
2851 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002852 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002853 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002854 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002855
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002857 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 {
2859 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002860 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002861 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 if( cipher_info == NULL )
2864 {
2865 status = PSA_ERROR_NOT_SUPPORTED;
2866 goto exit;
2867 }
2868 operation->mac_size = cipher_info->block_size;
2869 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2870 status = mbedtls_to_psa_error( ret );
2871 }
2872 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002874#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002875 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002877 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002878 if( hash_alg == 0 )
2879 {
2880 status = PSA_ERROR_NOT_SUPPORTED;
2881 goto exit;
2882 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002883
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002884 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002885 /* Sanity check. This shouldn't fail on a valid configuration. */
2886 if( operation->mac_size == 0 ||
2887 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2888 {
2889 status = PSA_ERROR_NOT_SUPPORTED;
2890 goto exit;
2891 }
2892
Gilles Peskine8e338702019-07-30 20:06:31 +02002893 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002894 {
2895 status = PSA_ERROR_INVALID_ARGUMENT;
2896 goto exit;
2897 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002898
Gilles Peskine01126fa2018-07-12 17:04:55 +02002899 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002900 slot->key.data,
2901 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002902 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002903 }
2904 else
John Durkopd0321952020-10-29 21:37:36 -07002905#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002907 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002908 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002910
Gilles Peskined911eb72018-08-14 15:18:45 +02002911 if( truncated == 0 )
2912 {
2913 /* The "normal" case: untruncated algorithm. Nothing to do. */
2914 }
2915 else if( truncated < 4 )
2916 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002917 /* A very short MAC is too short for security since it can be
2918 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2919 * so we make this our minimum, even though 32 bits is still
2920 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002921 status = PSA_ERROR_NOT_SUPPORTED;
2922 }
2923 else if( truncated > operation->mac_size )
2924 {
2925 /* It's impossible to "truncate" to a larger length. */
2926 status = PSA_ERROR_INVALID_ARGUMENT;
2927 }
2928 else
2929 operation->mac_size = truncated;
2930
Gilles Peskinefbfac682018-07-08 20:51:54 +02002931exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002932 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002934 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002936 else
2937 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002938 operation->key_set = 1;
2939 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002940
Ronald Cron5c522922020-11-14 16:35:34 +01002941 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002942
Ronald Cron5c522922020-11-14 16:35:34 +01002943 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944}
2945
Gilles Peskine89167cb2018-07-08 20:12:23 +02002946psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002947 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002948 psa_algorithm_t alg )
2949{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002950 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002951}
2952
2953psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002954 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002955 psa_algorithm_t alg )
2956{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002957 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002958}
2959
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2961 const uint8_t *input,
2962 size_t input_length )
2963{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002964 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002966 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002968 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969 operation->has_input = 1;
2970
Gilles Peskine8c9def32018-02-08 10:02:12 +01002971#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002972 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002973 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002974 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2975 input, input_length );
2976 status = mbedtls_to_psa_error( ret );
2977 }
2978 else
2979#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002980#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002981 if( PSA_ALG_IS_HMAC( operation->alg ) )
2982 {
2983 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2984 input_length );
2985 }
2986 else
John Durkopd0321952020-10-29 21:37:36 -07002987#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002988 {
2989 /* This shouldn't happen if `operation` was initialized by
2990 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002991 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002992 }
2993
Gilles Peskinefbfac682018-07-08 20:51:54 +02002994 if( status != PSA_SUCCESS )
2995 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002996 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997}
2998
John Durkop6ba40d12020-11-10 08:50:04 -08002999#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003000static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3001 uint8_t *mac,
3002 size_t mac_size )
3003{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003004 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003005 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3006 size_t hash_size = 0;
3007 size_t block_size = psa_get_hash_block_size( hash_alg );
3008 psa_status_t status;
3009
Gilles Peskine01126fa2018-07-12 17:04:55 +02003010 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3011 if( status != PSA_SUCCESS )
3012 return( status );
3013 /* From here on, tmp needs to be wiped. */
3014
3015 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3016 if( status != PSA_SUCCESS )
3017 goto exit;
3018
3019 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3020 if( status != PSA_SUCCESS )
3021 goto exit;
3022
3023 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3024 if( status != PSA_SUCCESS )
3025 goto exit;
3026
Gilles Peskined911eb72018-08-14 15:18:45 +02003027 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3028 if( status != PSA_SUCCESS )
3029 goto exit;
3030
3031 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003032
3033exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003034 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003035 return( status );
3036}
John Durkop6ba40d12020-11-10 08:50:04 -08003037#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003038
mohammad16036df908f2018-04-02 08:34:15 -07003039static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003040 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003041 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003043 if( ! operation->key_set )
3044 return( PSA_ERROR_BAD_STATE );
3045 if( operation->iv_required && ! operation->iv_set )
3046 return( PSA_ERROR_BAD_STATE );
3047
Gilles Peskine8c9def32018-02-08 10:02:12 +01003048 if( mac_size < operation->mac_size )
3049 return( PSA_ERROR_BUFFER_TOO_SMALL );
3050
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003052 if( operation->alg == PSA_ALG_CMAC )
3053 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003054 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003055 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3056 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003057 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003058 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003059 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003060 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003061 else
3062#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 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003067 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003068 }
3069 else
John Durkopd0321952020-10-29 21:37:36 -07003070#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003071 {
3072 /* This shouldn't happen if `operation` was initialized by
3073 * a setup function. */
3074 return( PSA_ERROR_BAD_STATE );
3075 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076}
3077
Gilles Peskineacd4be32018-07-08 19:56:25 +02003078psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3079 uint8_t *mac,
3080 size_t mac_size,
3081 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003082{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003083 psa_status_t status;
3084
Jaeden Amero252ef282019-02-15 14:05:35 +00003085 if( operation->alg == 0 )
3086 {
3087 return( PSA_ERROR_BAD_STATE );
3088 }
3089
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003090 /* Fill the output buffer with something that isn't a valid mac
3091 * (barring an attack on the mac and deliberately-crafted input),
3092 * in case the caller doesn't check the return status properly. */
3093 *mac_length = mac_size;
3094 /* If mac_size is 0 then mac may be NULL and then the
3095 * call to memset would have undefined behavior. */
3096 if( mac_size != 0 )
3097 memset( mac, '!', mac_size );
3098
Gilles Peskine89167cb2018-07-08 20:12:23 +02003099 if( ! operation->is_sign )
3100 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003101 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003102 }
mohammad16036df908f2018-04-02 08:34:15 -07003103
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003104 status = psa_mac_finish_internal( operation, mac, mac_size );
3105
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003106 if( status == PSA_SUCCESS )
3107 {
3108 status = psa_mac_abort( operation );
3109 if( status == PSA_SUCCESS )
3110 *mac_length = operation->mac_size;
3111 else
3112 memset( mac, '!', mac_size );
3113 }
3114 else
3115 psa_mac_abort( operation );
3116 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003117}
3118
Gilles Peskineacd4be32018-07-08 19:56:25 +02003119psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3120 const uint8_t *mac,
3121 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003122{
Gilles Peskine828ed142018-06-18 23:25:51 +02003123 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003124 psa_status_t status;
3125
Jaeden Amero252ef282019-02-15 14:05:35 +00003126 if( operation->alg == 0 )
3127 {
3128 return( PSA_ERROR_BAD_STATE );
3129 }
3130
Gilles Peskine89167cb2018-07-08 20:12:23 +02003131 if( operation->is_sign )
3132 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003133 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003134 }
3135 if( operation->mac_size != mac_length )
3136 {
3137 status = PSA_ERROR_INVALID_SIGNATURE;
3138 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003139 }
mohammad16036df908f2018-04-02 08:34:15 -07003140
3141 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003142 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003143 if( status != PSA_SUCCESS )
3144 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003145
3146 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3147 status = PSA_ERROR_INVALID_SIGNATURE;
3148
3149cleanup:
3150 if( status == PSA_SUCCESS )
3151 status = psa_mac_abort( operation );
3152 else
3153 psa_mac_abort( operation );
3154
Gilles Peskine3f108122018-12-07 18:14:53 +01003155 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003156
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003157 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003158}
3159
3160
Gilles Peskine20035e32018-02-03 22:44:14 +01003161
Gilles Peskine20035e32018-02-03 22:44:14 +01003162/****************************************************************/
3163/* Asymmetric cryptography */
3164/****************************************************************/
3165
John Durkop6ba40d12020-11-10 08:50:04 -08003166#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3167 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003168/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003169 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003170static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3171 size_t hash_length,
3172 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003173{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003174 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003175 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003176 *md_alg = mbedtls_md_get_type( md_info );
3177
3178 /* The Mbed TLS RSA module uses an unsigned int for hash length
3179 * parameters. Validate that it fits so that we don't risk an
3180 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003181#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003182 if( hash_length > UINT_MAX )
3183 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003184#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003185
John Durkop0e005192020-10-31 22:06:54 -07003186#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003187 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3188 * must be correct. */
3189 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3190 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003191 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003192 if( md_info == NULL )
3193 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003194 if( mbedtls_md_get_size( md_info ) != hash_length )
3195 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003196 }
John Durkop0e005192020-10-31 22:06:54 -07003197#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003198
John Durkop0e005192020-10-31 22:06:54 -07003199#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003200 /* PSS requires a hash internally. */
3201 if( PSA_ALG_IS_RSA_PSS( alg ) )
3202 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003203 if( md_info == NULL )
3204 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003205 }
John Durkop0e005192020-10-31 22:06:54 -07003206#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003207
Gilles Peskine61b91d42018-06-08 16:09:36 +02003208 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003209}
3210
Gilles Peskine2b450e32018-06-27 15:42:46 +02003211static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3212 psa_algorithm_t alg,
3213 const uint8_t *hash,
3214 size_t hash_length,
3215 uint8_t *signature,
3216 size_t signature_size,
3217 size_t *signature_length )
3218{
3219 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003221 mbedtls_md_type_t md_alg;
3222
3223 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3224 if( status != PSA_SUCCESS )
3225 return( status );
3226
Gilles Peskine630a18a2018-06-29 17:49:35 +02003227 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003228 return( PSA_ERROR_BUFFER_TOO_SMALL );
3229
John Durkop0e005192020-10-31 22:06:54 -07003230#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003231 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3232 {
3233 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3234 MBEDTLS_MD_NONE );
3235 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003236 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003237 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003238 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003239 md_alg,
3240 (unsigned int) hash_length,
3241 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003242 signature );
3243 }
3244 else
John Durkop0e005192020-10-31 22:06:54 -07003245#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3246#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003247 if( PSA_ALG_IS_RSA_PSS( alg ) )
3248 {
3249 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3250 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003251 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003252 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003253 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003254 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003255 (unsigned int) hash_length,
3256 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003257 signature );
3258 }
3259 else
John Durkop0e005192020-10-31 22:06:54 -07003260#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003261 {
3262 return( PSA_ERROR_INVALID_ARGUMENT );
3263 }
3264
3265 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003266 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003267 return( mbedtls_to_psa_error( ret ) );
3268}
3269
3270static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3271 psa_algorithm_t alg,
3272 const uint8_t *hash,
3273 size_t hash_length,
3274 const uint8_t *signature,
3275 size_t signature_length )
3276{
3277 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003279 mbedtls_md_type_t md_alg;
3280
3281 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3282 if( status != PSA_SUCCESS )
3283 return( status );
3284
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003285 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3286 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003287
John Durkop0e005192020-10-31 22:06:54 -07003288#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003289 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3290 {
3291 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3292 MBEDTLS_MD_NONE );
3293 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003294 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003295 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003296 MBEDTLS_RSA_PUBLIC,
3297 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003298 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003299 hash,
3300 signature );
3301 }
3302 else
John Durkop0e005192020-10-31 22:06:54 -07003303#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3304#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003305 if( PSA_ALG_IS_RSA_PSS( alg ) )
3306 {
3307 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3308 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003309 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003310 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003311 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003312 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003313 (unsigned int) hash_length,
3314 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003315 signature );
3316 }
3317 else
John Durkop0e005192020-10-31 22:06:54 -07003318#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003319 {
3320 return( PSA_ERROR_INVALID_ARGUMENT );
3321 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003322
3323 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3324 * the rest of the signature is invalid". This has little use in
3325 * practice and PSA doesn't report this distinction. */
3326 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3327 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003328 return( mbedtls_to_psa_error( ret ) );
3329}
John Durkop6ba40d12020-11-10 08:50:04 -08003330#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3331 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003332
John Durkop6ba40d12020-11-10 08:50:04 -08003333#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3334 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003335/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3336 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3337 * (even though these functions don't modify it). */
3338static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3339 psa_algorithm_t alg,
3340 const uint8_t *hash,
3341 size_t hash_length,
3342 uint8_t *signature,
3343 size_t signature_size,
3344 size_t *signature_length )
3345{
Janos Follath24eed8d2019-11-22 13:21:35 +00003346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003347 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003348 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003349 mbedtls_mpi_init( &r );
3350 mbedtls_mpi_init( &s );
3351
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003352 if( signature_size < 2 * curve_bytes )
3353 {
3354 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3355 goto cleanup;
3356 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003357
John Durkop0ea39e02020-10-13 19:58:20 -07003358#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003359 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3360 {
3361 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3362 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3363 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003364 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3365 &ecp->d, hash,
3366 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003367 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003368 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003369 }
3370 else
John Durkop0ea39e02020-10-13 19:58:20 -07003371#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003372 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003373 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003374 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3375 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003376 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003377 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003379
3380 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3381 signature,
3382 curve_bytes ) );
3383 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3384 signature + curve_bytes,
3385 curve_bytes ) );
3386
3387cleanup:
3388 mbedtls_mpi_free( &r );
3389 mbedtls_mpi_free( &s );
3390 if( ret == 0 )
3391 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003392 return( mbedtls_to_psa_error( ret ) );
3393}
3394
3395static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3396 const uint8_t *hash,
3397 size_t hash_length,
3398 const uint8_t *signature,
3399 size_t signature_length )
3400{
Janos Follath24eed8d2019-11-22 13:21:35 +00003401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003402 mbedtls_mpi r, s;
3403 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3404 mbedtls_mpi_init( &r );
3405 mbedtls_mpi_init( &s );
3406
3407 if( signature_length != 2 * curve_bytes )
3408 return( PSA_ERROR_INVALID_SIGNATURE );
3409
3410 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3411 signature,
3412 curve_bytes ) );
3413 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3414 signature + curve_bytes,
3415 curve_bytes ) );
3416
Steven Cooremanacda8342020-07-24 23:09:52 +02003417 /* Check whether the public part is loaded. If not, load it. */
3418 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3419 {
3420 MBEDTLS_MPI_CHK(
3421 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003422 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003423 }
3424
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003425 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3426 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003427
3428cleanup:
3429 mbedtls_mpi_free( &r );
3430 mbedtls_mpi_free( &s );
3431 return( mbedtls_to_psa_error( ret ) );
3432}
John Durkop6ba40d12020-11-10 08:50:04 -08003433#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3434 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003435
Ronald Croncf56a0a2020-08-04 09:51:30 +02003436psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003437 psa_algorithm_t alg,
3438 const uint8_t *hash,
3439 size_t hash_length,
3440 uint8_t *signature,
3441 size_t signature_size,
3442 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003443{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003445 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003446 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003447
3448 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003449 /* Immediately reject a zero-length signature buffer. This guarantees
3450 * that signature must be a valid pointer. (On the other hand, the hash
3451 * buffer can in principle be empty since it doesn't actually have
3452 * to be a hash.) */
3453 if( signature_size == 0 )
3454 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003455
Ronald Cron5c522922020-11-14 16:35:34 +01003456 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3457 PSA_KEY_USAGE_SIGN_HASH,
3458 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003459 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003460 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003461 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003462 {
3463 status = PSA_ERROR_INVALID_ARGUMENT;
3464 goto exit;
3465 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003466
Steven Cooremancd84cb42020-07-16 20:28:36 +02003467 /* Try any of the available accelerators first */
3468 status = psa_driver_wrapper_sign_hash( slot,
3469 alg,
3470 hash,
3471 hash_length,
3472 signature,
3473 signature_size,
3474 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003475 if( status != PSA_ERROR_NOT_SUPPORTED ||
3476 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003477 goto exit;
3478
Steven Cooreman7a250572020-07-17 16:43:05 +02003479 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003480#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3481 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003482 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003483 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003484 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003485
Ronald Cron90857082020-11-25 14:58:33 +01003486 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3487 slot->key.data,
3488 slot->key.bytes,
3489 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003490 if( status != PSA_SUCCESS )
3491 goto exit;
3492
Steven Cooremana2371e52020-07-28 14:30:39 +02003493 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003494 alg,
3495 hash, hash_length,
3496 signature, signature_size,
3497 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003498
Steven Cooremana2371e52020-07-28 14:30:39 +02003499 mbedtls_rsa_free( rsa );
3500 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003501 }
3502 else
John Durkop6ba40d12020-11-10 08:50:04 -08003503#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3504 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003505 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003506 {
John Durkop9814fa22020-11-04 12:28:15 -08003507#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3508 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003509 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003510#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003511 PSA_ALG_IS_ECDSA( alg )
3512#else
3513 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3514#endif
3515 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003516 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003517 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003518 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3519 slot->key.data,
3520 slot->key.bytes,
3521 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003522 if( status != PSA_SUCCESS )
3523 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003524 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003525 alg,
3526 hash, hash_length,
3527 signature, signature_size,
3528 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003529 mbedtls_ecp_keypair_free( ecp );
3530 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003531 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003532 else
John Durkop9814fa22020-11-04 12:28:15 -08003533#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3534 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003535 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003536 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003537 }
itayzafrir5c753392018-05-08 11:18:38 +03003538 }
3539 else
itayzafrir5c753392018-05-08 11:18:38 +03003540 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003541 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003542 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003543
3544exit:
3545 /* Fill the unused part of the output buffer (the whole buffer on error,
3546 * the trailing part on success) with something that isn't a valid mac
3547 * (barring an attack on the mac and deliberately-crafted input),
3548 * in case the caller doesn't check the return status properly. */
3549 if( status == PSA_SUCCESS )
3550 memset( signature + *signature_length, '!',
3551 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003552 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003553 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003554 /* If signature_size is 0 then we have nothing to do. We must not call
3555 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003556
Ronald Cron5c522922020-11-14 16:35:34 +01003557 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003558
Ronald Cron5c522922020-11-14 16:35:34 +01003559 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003560}
3561
Ronald Croncf56a0a2020-08-04 09:51:30 +02003562psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003563 psa_algorithm_t alg,
3564 const uint8_t *hash,
3565 size_t hash_length,
3566 const uint8_t *signature,
3567 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003568{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003570 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003571 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003572
Ronald Cron5c522922020-11-14 16:35:34 +01003573 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3574 PSA_KEY_USAGE_VERIFY_HASH,
3575 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003576 if( status != PSA_SUCCESS )
3577 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003578
Steven Cooreman55ae2172020-07-17 19:46:15 +02003579 /* Try any of the available accelerators first */
3580 status = psa_driver_wrapper_verify_hash( slot,
3581 alg,
3582 hash,
3583 hash_length,
3584 signature,
3585 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003586 if( status != PSA_ERROR_NOT_SUPPORTED ||
3587 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003588 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003589
John Durkop6ba40d12020-11-10 08:50:04 -08003590#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3591 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003592 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003593 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003594 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003595
Ronald Cron90857082020-11-25 14:58:33 +01003596 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3597 slot->key.data,
3598 slot->key.bytes,
3599 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003600 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003601 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003602
Steven Cooremana2371e52020-07-28 14:30:39 +02003603 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003604 alg,
3605 hash, hash_length,
3606 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003607 mbedtls_rsa_free( rsa );
3608 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003609 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 }
3611 else
John Durkop6ba40d12020-11-10 08:50:04 -08003612#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3613 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003614 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003615 {
John Durkop9814fa22020-11-04 12:28:15 -08003616#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3617 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003618 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003619 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003620 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003621 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3622 slot->key.data,
3623 slot->key.bytes,
3624 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003625 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003626 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003627 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003628 hash, hash_length,
3629 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003630 mbedtls_ecp_keypair_free( ecp );
3631 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003632 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003633 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003634 else
John Durkop9814fa22020-11-04 12:28:15 -08003635#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3636 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003637 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003638 status = PSA_ERROR_INVALID_ARGUMENT;
3639 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003640 }
itayzafrir5c753392018-05-08 11:18:38 +03003641 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003642 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003643 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003644 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003645 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003646
3647exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003648 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003649
Ronald Cron5c522922020-11-14 16:35:34 +01003650 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003651}
3652
John Durkop0e005192020-10-31 22:06:54 -07003653#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003654static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3655 mbedtls_rsa_context *rsa )
3656{
3657 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3658 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3659 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3660 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3661}
John Durkop0e005192020-10-31 22:06:54 -07003662#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003663
Ronald Croncf56a0a2020-08-04 09:51:30 +02003664psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003665 psa_algorithm_t alg,
3666 const uint8_t *input,
3667 size_t input_length,
3668 const uint8_t *salt,
3669 size_t salt_length,
3670 uint8_t *output,
3671 size_t output_size,
3672 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003673{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003675 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003676 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003677
Darryl Green5cc689a2018-07-24 15:34:10 +01003678 (void) input;
3679 (void) input_length;
3680 (void) salt;
3681 (void) output;
3682 (void) output_size;
3683
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003684 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003686 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3687 return( PSA_ERROR_INVALID_ARGUMENT );
3688
Ronald Cron5c522922020-11-14 16:35:34 +01003689 status = psa_get_and_lock_transparent_key_slot_with_policy(
3690 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003691 if( status != PSA_SUCCESS )
3692 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003693 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3694 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003695 {
3696 status = PSA_ERROR_INVALID_ARGUMENT;
3697 goto exit;
3698 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699
John Durkop6ba40d12020-11-10 08:50:04 -08003700#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3701 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003702 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003704 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003705 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3706 slot->key.data,
3707 slot->key.bytes,
3708 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003709 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003710 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003711
3712 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003713 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003714 status = PSA_ERROR_BUFFER_TOO_SMALL;
3715 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003716 }
John Durkop0e005192020-10-31 22:06:54 -07003717#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003718 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003720 status = mbedtls_to_psa_error(
3721 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003722 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003723 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003724 MBEDTLS_RSA_PUBLIC,
3725 input_length,
3726 input,
3727 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 }
3729 else
John Durkop0e005192020-10-31 22:06:54 -07003730#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3731#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003732 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003734 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003735 status = mbedtls_to_psa_error(
3736 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003737 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003738 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003739 MBEDTLS_RSA_PUBLIC,
3740 salt, salt_length,
3741 input_length,
3742 input,
3743 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744 }
3745 else
John Durkop0e005192020-10-31 22:06:54 -07003746#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003748 status = PSA_ERROR_INVALID_ARGUMENT;
3749 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003750 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003751rsa_exit:
3752 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003753 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003754
Steven Cooremana2371e52020-07-28 14:30:39 +02003755 mbedtls_rsa_free( rsa );
3756 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003757 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003758 else
John Durkop9814fa22020-11-04 12:28:15 -08003759#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003760 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003761 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003762 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003763 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003764
3765exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003766 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003767
Ronald Cron5c522922020-11-14 16:35:34 +01003768 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003769}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770
Ronald Croncf56a0a2020-08-04 09:51:30 +02003771psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003772 psa_algorithm_t alg,
3773 const uint8_t *input,
3774 size_t input_length,
3775 const uint8_t *salt,
3776 size_t salt_length,
3777 uint8_t *output,
3778 size_t output_size,
3779 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003780{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003782 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003783 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003784
Darryl Green5cc689a2018-07-24 15:34:10 +01003785 (void) input;
3786 (void) input_length;
3787 (void) salt;
3788 (void) output;
3789 (void) output_size;
3790
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003791 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003792
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003793 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3794 return( PSA_ERROR_INVALID_ARGUMENT );
3795
Ronald Cron5c522922020-11-14 16:35:34 +01003796 status = psa_get_and_lock_transparent_key_slot_with_policy(
3797 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003798 if( status != PSA_SUCCESS )
3799 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003800 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003801 {
3802 status = PSA_ERROR_INVALID_ARGUMENT;
3803 goto exit;
3804 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805
John Durkop6ba40d12020-11-10 08:50:04 -08003806#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3807 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003808 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003810 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003811 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3812 slot->key.data,
3813 slot->key.bytes,
3814 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003815 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003816 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003817
Steven Cooremana2371e52020-07-28 14:30:39 +02003818 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003819 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003820 status = PSA_ERROR_INVALID_ARGUMENT;
3821 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003822 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003823
John Durkop0e005192020-10-31 22:06:54 -07003824#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003825 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003827 status = mbedtls_to_psa_error(
3828 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003829 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003830 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003831 MBEDTLS_RSA_PRIVATE,
3832 output_length,
3833 input,
3834 output,
3835 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003836 }
3837 else
John Durkop0e005192020-10-31 22:06:54 -07003838#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3839#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003840 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003842 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003843 status = mbedtls_to_psa_error(
3844 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003845 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003846 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003847 MBEDTLS_RSA_PRIVATE,
3848 salt, salt_length,
3849 output_length,
3850 input,
3851 output,
3852 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853 }
3854 else
John Durkop0e005192020-10-31 22:06:54 -07003855#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003857 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003859
Steven Cooreman4fed4552020-08-03 14:46:03 +02003860rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003861 mbedtls_rsa_free( rsa );
3862 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003863 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864 else
John Durkop6ba40d12020-11-10 08:50:04 -08003865#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3866 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003868 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003870
3871exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003872 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003873
Ronald Cron5c522922020-11-14 16:35:34 +01003874 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003875}
Gilles Peskine20035e32018-02-03 22:44:14 +01003876
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003877
3878
mohammad1603503973b2018-03-12 15:59:30 +02003879/****************************************************************/
3880/* Symmetric cryptography */
3881/****************************************************************/
3882
Gilles Peskinee553c652018-06-04 16:22:46 +02003883static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003884 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003885 psa_algorithm_t alg,
3886 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003887{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003888 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003889 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003890 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003891 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003892 size_t key_bits;
3893 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003894 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3895 PSA_KEY_USAGE_ENCRYPT :
3896 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003897
Steven Cooremand3feccd2020-09-01 15:56:14 +02003898 /* A context must be freshly initialized before it can be set up. */
3899 if( operation->alg != 0 )
3900 return( PSA_ERROR_BAD_STATE );
3901
Steven Cooremana07b9972020-09-10 14:54:14 +02003902 /* The requested algorithm must be one that can be processed by cipher. */
3903 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003904 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003905
Steven Cooremanef8575e2020-09-11 11:44:50 +02003906 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003907 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003908 if( status != PSA_SUCCESS )
3909 goto exit;
3910
3911 /* Initialize the operation struct members, except for alg. The alg member
3912 * is used to indicate to psa_cipher_abort that there are resources to free,
3913 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003914 operation->key_set = 0;
3915 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003916 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003917 operation->iv_size = 0;
3918 operation->block_size = 0;
3919 if( alg == PSA_ALG_ECB_NO_PADDING )
3920 operation->iv_required = 0;
3921 else
3922 operation->iv_required = 1;
3923
Steven Cooremana07b9972020-09-10 14:54:14 +02003924 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003925 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003926 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003927 slot,
3928 alg );
3929 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003930 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003931 slot,
3932 alg );
3933
Steven Cooremanef8575e2020-09-11 11:44:50 +02003934 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003935 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003936 /* Once the driver context is initialised, it needs to be freed using
3937 * psa_cipher_abort. Indicate this through setting alg. */
3938 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003939 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003940
Steven Cooremand3feccd2020-09-01 15:56:14 +02003941 if( status != PSA_ERROR_NOT_SUPPORTED ||
3942 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3943 goto exit;
3944
Steven Cooremana07b9972020-09-10 14:54:14 +02003945 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003946 * available for the given algorithm & key. */
3947 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003948
Steven Cooremana07b9972020-09-10 14:54:14 +02003949 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003950 * psa_cipher_abort. Indicate there is something to be freed through setting
3951 * alg, and indicate the operation is being done using mbedtls crypto through
3952 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003953 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003954 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003955
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003956 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003957 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003958 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003959 {
3960 status = PSA_ERROR_NOT_SUPPORTED;
3961 goto exit;
3962 }
mohammad1603503973b2018-03-12 15:59:30 +02003963
mohammad1603503973b2018-03-12 15:59:30 +02003964 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003965 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003966 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003967
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003968#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003969 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003970 {
3971 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003972 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003973 memcpy( keys, slot->key.data, 16 );
3974 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003975 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3976 keys,
3977 192, cipher_operation );
3978 }
3979 else
3980#endif
3981 {
3982 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003983 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003984 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003985 }
Moran Peker41deec42018-04-04 15:43:05 +03003986 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003987 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003988
mohammad16038481e742018-03-18 13:57:31 +02003989#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003990 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003991 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003992 case PSA_ALG_CBC_NO_PADDING:
3993 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3994 MBEDTLS_PADDING_NONE );
3995 break;
3996 case PSA_ALG_CBC_PKCS7:
3997 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3998 MBEDTLS_PADDING_PKCS7 );
3999 break;
4000 default:
4001 /* The algorithm doesn't involve padding. */
4002 ret = 0;
4003 break;
4004 }
4005 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004006 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004007#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4008
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004009 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004010 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004011 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4012 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004013 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004014 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004015 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004016#if defined(MBEDTLS_CHACHA20_C)
4017 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004018 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004019 operation->iv_size = 12;
4020#endif
mohammad1603503973b2018-03-12 15:59:30 +02004021
Steven Cooreman7df02922020-09-09 15:28:49 +02004022 status = PSA_SUCCESS;
4023
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004024exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004025 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004026 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004027 if( status == PSA_SUCCESS )
4028 {
4029 /* Update operation flags for both driver and software implementations */
4030 operation->key_set = 1;
4031 }
4032 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004033 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004034
Ronald Cron5c522922020-11-14 16:35:34 +01004035 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004036
Ronald Cron5c522922020-11-14 16:35:34 +01004037 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004038}
4039
Gilles Peskinefe119512018-07-08 21:39:34 +02004040psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004041 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004042 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004043{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004044 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004045}
4046
Gilles Peskinefe119512018-07-08 21:39:34 +02004047psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004048 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004049 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004050{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004051 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004052}
4053
Gilles Peskinefe119512018-07-08 21:39:34 +02004054psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004055 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004056 size_t iv_size,
4057 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004058{
itayzafrir534bd7c2018-08-02 13:56:32 +03004059 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004061 if( operation->iv_set || ! operation->iv_required )
4062 {
4063 return( PSA_ERROR_BAD_STATE );
4064 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004065
Steven Cooremanef8575e2020-09-11 11:44:50 +02004066 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004067 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004068 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004069 iv,
4070 iv_size,
4071 iv_length );
4072 goto exit;
4073 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004074
Moran Peker41deec42018-04-04 15:43:05 +03004075 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004076 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004077 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004078 goto exit;
4079 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004080 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004081 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004082 if( ret != 0 )
4083 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004084 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004085 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004086 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004087
mohammad16038481e742018-03-18 13:57:31 +02004088 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004089 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004090
Moran Peker395db872018-05-31 14:07:14 +03004091exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004092 if( status == PSA_SUCCESS )
4093 operation->iv_set = 1;
4094 else
Moran Peker395db872018-05-31 14:07:14 +03004095 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004096 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004097}
4098
Gilles Peskinefe119512018-07-08 21:39:34 +02004099psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004100 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004101 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004102{
itayzafrir534bd7c2018-08-02 13:56:32 +03004103 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004105 if( operation->iv_set || ! operation->iv_required )
4106 {
4107 return( PSA_ERROR_BAD_STATE );
4108 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004109
Steven Cooremanef8575e2020-09-11 11:44:50 +02004110 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004111 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004112 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004113 iv,
4114 iv_length );
4115 goto exit;
4116 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Moran Pekera28258c2018-05-29 16:25:04 +03004118 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004119 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004120 status = PSA_ERROR_INVALID_ARGUMENT;
4121 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004122 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004123 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4124 status = mbedtls_to_psa_error( ret );
4125exit:
4126 if( status == PSA_SUCCESS )
4127 operation->iv_set = 1;
4128 else
mohammad1603503973b2018-03-12 15:59:30 +02004129 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004130 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004131}
4132
Steven Cooreman177deba2020-09-07 17:14:14 +02004133/* Process input for which the algorithm is set to ECB mode. This requires
4134 * manual processing, since the PSA API is defined as being able to process
4135 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4136 * underlying mbedtls_cipher_update only takes full blocks. */
4137static psa_status_t psa_cipher_update_ecb_internal(
4138 mbedtls_cipher_context_t *ctx,
4139 const uint8_t *input,
4140 size_t input_length,
4141 uint8_t *output,
4142 size_t output_size,
4143 size_t *output_length )
4144{
4145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4146 size_t block_size = ctx->cipher_info->block_size;
4147 size_t internal_output_length = 0;
4148 *output_length = 0;
4149
4150 if( input_length == 0 )
4151 {
4152 status = PSA_SUCCESS;
4153 goto exit;
4154 }
4155
4156 if( ctx->unprocessed_len > 0 )
4157 {
4158 /* Fill up to block size, and run the block if there's a full one. */
4159 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4160
4161 if( input_length < bytes_to_copy )
4162 bytes_to_copy = input_length;
4163
4164 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4165 input, bytes_to_copy );
4166 input_length -= bytes_to_copy;
4167 input += bytes_to_copy;
4168 ctx->unprocessed_len += bytes_to_copy;
4169
4170 if( ctx->unprocessed_len == block_size )
4171 {
4172 status = mbedtls_to_psa_error(
4173 mbedtls_cipher_update( ctx,
4174 ctx->unprocessed_data,
4175 block_size,
4176 output, &internal_output_length ) );
4177
4178 if( status != PSA_SUCCESS )
4179 goto exit;
4180
4181 output += internal_output_length;
4182 output_size -= internal_output_length;
4183 *output_length += internal_output_length;
4184 ctx->unprocessed_len = 0;
4185 }
4186 }
4187
4188 while( input_length >= block_size )
4189 {
4190 /* Run all full blocks we have, one by one */
4191 status = mbedtls_to_psa_error(
4192 mbedtls_cipher_update( ctx, input,
4193 block_size,
4194 output, &internal_output_length ) );
4195
4196 if( status != PSA_SUCCESS )
4197 goto exit;
4198
4199 input_length -= block_size;
4200 input += block_size;
4201
4202 output += internal_output_length;
4203 output_size -= internal_output_length;
4204 *output_length += internal_output_length;
4205 }
4206
4207 if( input_length > 0 )
4208 {
4209 /* Save unprocessed bytes for later processing */
4210 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4211 input, input_length );
4212 ctx->unprocessed_len += input_length;
4213 }
4214
4215 status = PSA_SUCCESS;
4216
4217exit:
4218 return( status );
4219}
4220
Gilles Peskinee553c652018-06-04 16:22:46 +02004221psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4222 const uint8_t *input,
4223 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004224 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004225 size_t output_size,
4226 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004227{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004229 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004230 if( operation->alg == 0 )
4231 {
4232 return( PSA_ERROR_BAD_STATE );
4233 }
4234 if( operation->iv_required && ! operation->iv_set )
4235 {
4236 return( PSA_ERROR_BAD_STATE );
4237 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004238
Steven Cooremanef8575e2020-09-11 11:44:50 +02004239 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004240 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004241 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004242 input,
4243 input_length,
4244 output,
4245 output_size,
4246 output_length );
4247 goto exit;
4248 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004249
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004250 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004251 {
4252 /* Take the unprocessed partial block left over from previous
4253 * update calls, if any, plus the input to this call. Remove
4254 * the last partial block, if any. You get the data that will be
4255 * output in this call. */
4256 expected_output_size =
4257 ( operation->ctx.cipher.unprocessed_len + input_length )
4258 / operation->block_size * operation->block_size;
4259 }
4260 else
4261 {
4262 expected_output_size = input_length;
4263 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004264
Gilles Peskine89d789c2018-06-04 17:17:16 +02004265 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004266 {
4267 status = PSA_ERROR_BUFFER_TOO_SMALL;
4268 goto exit;
4269 }
mohammad160382759612018-03-12 18:16:40 +02004270
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004271 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4272 {
4273 /* mbedtls_cipher_update has an API inconsistency: it will only
4274 * process a single block at a time in ECB mode. Abstract away that
4275 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004276 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4277 input,
4278 input_length,
4279 output,
4280 output_size,
4281 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004282 }
4283 else
4284 {
4285 status = mbedtls_to_psa_error(
4286 mbedtls_cipher_update( &operation->ctx.cipher, input,
4287 input_length, output, output_length ) );
4288 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004289exit:
4290 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004291 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004292 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004293}
4294
Gilles Peskinee553c652018-06-04 16:22:46 +02004295psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4296 uint8_t *output,
4297 size_t output_size,
4298 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004299{
David Saadab4ecc272019-02-14 13:48:10 +02004300 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004301 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004302 if( operation->alg == 0 )
4303 {
4304 return( PSA_ERROR_BAD_STATE );
4305 }
4306 if( operation->iv_required && ! operation->iv_set )
4307 {
4308 return( PSA_ERROR_BAD_STATE );
4309 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004310
Steven Cooremanef8575e2020-09-11 11:44:50 +02004311 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004312 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004313 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004314 output,
4315 output_size,
4316 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004317 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004318 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004319
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004320 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004321 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004322 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004323 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004324 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004325 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004326 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004327 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004328 }
4329
Steven Cooremanef8575e2020-09-11 11:44:50 +02004330 status = mbedtls_to_psa_error(
4331 mbedtls_cipher_finish( &operation->ctx.cipher,
4332 temp_output_buffer,
4333 output_length ) );
4334 if( status != PSA_SUCCESS )
4335 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004336
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004337 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004338 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004339 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004340 memcpy( output, temp_output_buffer, *output_length );
4341 else
Janos Follath315b51c2018-07-09 16:04:51 +01004342 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004343
Steven Cooremanef8575e2020-09-11 11:44:50 +02004344exit:
4345 if( operation->mbedtls_in_use == 1 )
4346 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004347
Steven Cooremanef8575e2020-09-11 11:44:50 +02004348 if( status == PSA_SUCCESS )
4349 return( psa_cipher_abort( operation ) );
4350 else
4351 {
4352 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004353 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004354
Steven Cooremanef8575e2020-09-11 11:44:50 +02004355 return( status );
4356 }
mohammad1603503973b2018-03-12 15:59:30 +02004357}
4358
Gilles Peskinee553c652018-06-04 16:22:46 +02004359psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4360{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004361 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004362 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004363 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004364 * in use. It's ok to call abort on such an object, and there's
4365 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004366 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004367 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004368
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004369 /* Sanity check (shouldn't happen: operation->alg should
4370 * always have been initialized to a valid value). */
4371 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4372 return( PSA_ERROR_BAD_STATE );
4373
Steven Cooremanef8575e2020-09-11 11:44:50 +02004374 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004375 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004376 else
4377 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004378
Moran Peker41deec42018-04-04 15:43:05 +03004379 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004380 operation->key_set = 0;
4381 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004382 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004383 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004384 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004385 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004386
Moran Peker395db872018-05-31 14:07:14 +03004387 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004388}
4389
Gilles Peskinea0655c32018-04-30 17:06:50 +02004390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004391
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004392
mohammad16035955c982018-04-26 00:53:03 +03004393/****************************************************************/
4394/* AEAD */
4395/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004396
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397typedef struct
4398{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004399 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004400 const mbedtls_cipher_info_t *cipher_info;
4401 union
4402 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004403 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404#if defined(MBEDTLS_CCM_C)
4405 mbedtls_ccm_context ccm;
4406#endif /* MBEDTLS_CCM_C */
4407#if defined(MBEDTLS_GCM_C)
4408 mbedtls_gcm_context gcm;
4409#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004410#if defined(MBEDTLS_CHACHAPOLY_C)
4411 mbedtls_chachapoly_context chachapoly;
4412#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004413 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004414 psa_algorithm_t core_alg;
4415 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004416 uint8_t tag_length;
4417} aead_operation_t;
4418
Ronald Cronf95a2b12020-10-22 15:24:49 +02004419#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4420
Gilles Peskine30a9e412019-01-14 18:36:12 +01004421static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004422{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004423 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004424 {
4425#if defined(MBEDTLS_CCM_C)
4426 case PSA_ALG_CCM:
4427 mbedtls_ccm_free( &operation->ctx.ccm );
4428 break;
4429#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004430#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004431 case PSA_ALG_GCM:
4432 mbedtls_gcm_free( &operation->ctx.gcm );
4433 break;
4434#endif /* MBEDTLS_GCM_C */
4435 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004436
Ronald Cron5c522922020-11-14 16:35:34 +01004437 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004438}
4439
4440static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004441 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004442 psa_key_usage_t usage,
4443 psa_algorithm_t alg )
4444{
4445 psa_status_t status;
4446 size_t key_bits;
4447 mbedtls_cipher_id_t cipher_id;
4448
Ronald Cron5c522922020-11-14 16:35:34 +01004449 status = psa_get_and_lock_transparent_key_slot_with_policy(
4450 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004451 if( status != PSA_SUCCESS )
4452 return( status );
4453
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004454 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004455
4456 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004457 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004458 &cipher_id );
4459 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004460 {
4461 status = PSA_ERROR_NOT_SUPPORTED;
4462 goto cleanup;
4463 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004464
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004465 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004466 {
4467#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004468 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4469 operation->core_alg = PSA_ALG_CCM;
4470 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004471 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4472 * The call to mbedtls_ccm_encrypt_and_tag or
4473 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004474 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004475 {
4476 status = PSA_ERROR_INVALID_ARGUMENT;
4477 goto cleanup;
4478 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004479 mbedtls_ccm_init( &operation->ctx.ccm );
4480 status = mbedtls_to_psa_error(
4481 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004482 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004483 (unsigned int) key_bits ) );
4484 if( status != 0 )
4485 goto cleanup;
4486 break;
4487#endif /* MBEDTLS_CCM_C */
4488
4489#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004490 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4491 operation->core_alg = PSA_ALG_GCM;
4492 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004493 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4494 * The call to mbedtls_gcm_crypt_and_tag or
4495 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004496 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004497 {
4498 status = PSA_ERROR_INVALID_ARGUMENT;
4499 goto cleanup;
4500 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004501 mbedtls_gcm_init( &operation->ctx.gcm );
4502 status = mbedtls_to_psa_error(
4503 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004504 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004505 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004506 if( status != 0 )
4507 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004508 break;
4509#endif /* MBEDTLS_GCM_C */
4510
Gilles Peskine26869f22019-05-06 15:25:00 +02004511#if defined(MBEDTLS_CHACHAPOLY_C)
4512 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4513 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4514 operation->full_tag_length = 16;
4515 /* We only support the default tag length. */
4516 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004517 {
4518 status = PSA_ERROR_NOT_SUPPORTED;
4519 goto cleanup;
4520 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004521 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4522 status = mbedtls_to_psa_error(
4523 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004524 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004525 if( status != 0 )
4526 goto cleanup;
4527 break;
4528#endif /* MBEDTLS_CHACHAPOLY_C */
4529
Gilles Peskineedf9a652018-08-17 18:11:56 +02004530 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004531 status = PSA_ERROR_NOT_SUPPORTED;
4532 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004533 }
4534
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004535 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4536 {
4537 status = PSA_ERROR_INVALID_ARGUMENT;
4538 goto cleanup;
4539 }
4540 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004541
Gilles Peskineedf9a652018-08-17 18:11:56 +02004542 return( PSA_SUCCESS );
4543
4544cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004545 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004546 return( status );
4547}
4548
Ronald Croncf56a0a2020-08-04 09:51:30 +02004549psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004550 psa_algorithm_t alg,
4551 const uint8_t *nonce,
4552 size_t nonce_length,
4553 const uint8_t *additional_data,
4554 size_t additional_data_length,
4555 const uint8_t *plaintext,
4556 size_t plaintext_length,
4557 uint8_t *ciphertext,
4558 size_t ciphertext_size,
4559 size_t *ciphertext_length )
4560{
mohammad16035955c982018-04-26 00:53:03 +03004561 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004562 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004563 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004564
mohammad1603f08a5502018-06-03 15:05:47 +03004565 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004566
Ronald Croncf56a0a2020-08-04 09:51:30 +02004567 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004568 if( status != PSA_SUCCESS )
4569 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004570
Gilles Peskineedf9a652018-08-17 18:11:56 +02004571 /* For all currently supported modes, the tag is at the end of the
4572 * ciphertext. */
4573 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4574 {
4575 status = PSA_ERROR_BUFFER_TOO_SMALL;
4576 goto exit;
4577 }
4578 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004579
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004580#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004581 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004582 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004583 status = mbedtls_to_psa_error(
4584 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4585 MBEDTLS_GCM_ENCRYPT,
4586 plaintext_length,
4587 nonce, nonce_length,
4588 additional_data, additional_data_length,
4589 plaintext, ciphertext,
4590 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004591 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004592 else
4593#endif /* MBEDTLS_GCM_C */
4594#if defined(MBEDTLS_CCM_C)
4595 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004596 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004597 status = mbedtls_to_psa_error(
4598 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4599 plaintext_length,
4600 nonce, nonce_length,
4601 additional_data,
4602 additional_data_length,
4603 plaintext, ciphertext,
4604 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004605 }
mohammad16035c8845f2018-05-09 05:40:09 -07004606 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004607#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004608#if defined(MBEDTLS_CHACHAPOLY_C)
4609 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4610 {
4611 if( nonce_length != 12 || operation.tag_length != 16 )
4612 {
4613 status = PSA_ERROR_NOT_SUPPORTED;
4614 goto exit;
4615 }
4616 status = mbedtls_to_psa_error(
4617 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4618 plaintext_length,
4619 nonce,
4620 additional_data,
4621 additional_data_length,
4622 plaintext,
4623 ciphertext,
4624 tag ) );
4625 }
4626 else
4627#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004628 {
mohammad1603554faad2018-06-03 15:07:38 +03004629 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004630 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004631
Gilles Peskineedf9a652018-08-17 18:11:56 +02004632 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4633 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004634
Gilles Peskineedf9a652018-08-17 18:11:56 +02004635exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004636 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004637 if( status == PSA_SUCCESS )
4638 *ciphertext_length = plaintext_length + operation.tag_length;
4639 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004640}
4641
Gilles Peskineee652a32018-06-01 19:23:52 +02004642/* Locate the tag in a ciphertext buffer containing the encrypted data
4643 * followed by the tag. Return the length of the part preceding the tag in
4644 * *plaintext_length. This is the size of the plaintext in modes where
4645 * the encrypted data has the same size as the plaintext, such as
4646 * CCM and GCM. */
4647static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4648 const uint8_t *ciphertext,
4649 size_t ciphertext_length,
4650 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004651 const uint8_t **p_tag )
4652{
4653 size_t payload_length;
4654 if( tag_length > ciphertext_length )
4655 return( PSA_ERROR_INVALID_ARGUMENT );
4656 payload_length = ciphertext_length - tag_length;
4657 if( payload_length > plaintext_size )
4658 return( PSA_ERROR_BUFFER_TOO_SMALL );
4659 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004660 return( PSA_SUCCESS );
4661}
4662
Ronald Croncf56a0a2020-08-04 09:51:30 +02004663psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004664 psa_algorithm_t alg,
4665 const uint8_t *nonce,
4666 size_t nonce_length,
4667 const uint8_t *additional_data,
4668 size_t additional_data_length,
4669 const uint8_t *ciphertext,
4670 size_t ciphertext_length,
4671 uint8_t *plaintext,
4672 size_t plaintext_size,
4673 size_t *plaintext_length )
4674{
mohammad16035955c982018-04-26 00:53:03 +03004675 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004676 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004677 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004678
Gilles Peskineee652a32018-06-01 19:23:52 +02004679 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004680
Ronald Croncf56a0a2020-08-04 09:51:30 +02004681 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004682 if( status != PSA_SUCCESS )
4683 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004684
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004685 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4686 ciphertext, ciphertext_length,
4687 plaintext_size, &tag );
4688 if( status != PSA_SUCCESS )
4689 goto exit;
4690
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004691#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004692 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004693 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004694 status = mbedtls_to_psa_error(
4695 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4696 ciphertext_length - operation.tag_length,
4697 nonce, nonce_length,
4698 additional_data,
4699 additional_data_length,
4700 tag, operation.tag_length,
4701 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004702 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004703 else
4704#endif /* MBEDTLS_GCM_C */
4705#if defined(MBEDTLS_CCM_C)
4706 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004707 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004708 status = mbedtls_to_psa_error(
4709 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4710 ciphertext_length - operation.tag_length,
4711 nonce, nonce_length,
4712 additional_data,
4713 additional_data_length,
4714 ciphertext, plaintext,
4715 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004716 }
mohammad160339574652018-06-01 04:39:53 -07004717 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004718#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004719#if defined(MBEDTLS_CHACHAPOLY_C)
4720 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4721 {
4722 if( nonce_length != 12 || operation.tag_length != 16 )
4723 {
4724 status = PSA_ERROR_NOT_SUPPORTED;
4725 goto exit;
4726 }
4727 status = mbedtls_to_psa_error(
4728 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4729 ciphertext_length - operation.tag_length,
4730 nonce,
4731 additional_data,
4732 additional_data_length,
4733 tag,
4734 ciphertext,
4735 plaintext ) );
4736 }
4737 else
4738#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004739 {
mohammad1603554faad2018-06-03 15:07:38 +03004740 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004741 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004742
Gilles Peskineedf9a652018-08-17 18:11:56 +02004743 if( status != PSA_SUCCESS && plaintext_size != 0 )
4744 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004745
Gilles Peskineedf9a652018-08-17 18:11:56 +02004746exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004747 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004748 if( status == PSA_SUCCESS )
4749 *plaintext_length = ciphertext_length - operation.tag_length;
4750 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004751}
4752
Gilles Peskinea0655c32018-04-30 17:06:50 +02004753
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004754
Gilles Peskine20035e32018-02-03 22:44:14 +01004755/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756/* Generators */
4757/****************************************************************/
4758
John Durkop07cc04a2020-11-16 22:08:34 -08004759#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4760 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4761 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4762#define AT_LEAST_ONE_BUILTIN_KDF
4763#endif
4764
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004765#define HKDF_STATE_INIT 0 /* no input yet */
4766#define HKDF_STATE_STARTED 1 /* got salt */
4767#define HKDF_STATE_KEYED 2 /* got key */
4768#define HKDF_STATE_OUTPUT 3 /* output started */
4769
Gilles Peskinecbe66502019-05-16 16:59:18 +02004770static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004772{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4774 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004775 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004777}
4778
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004780{
4781 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004783 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004784 {
4785 /* The object has (apparently) been initialized but it is not
4786 * in use. It's ok to call abort on such an object, and there's
4787 * nothing to do. */
4788 }
4789 else
John Durkopd0321952020-10-29 21:37:36 -07004790#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004791 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004792 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 mbedtls_free( operation->ctx.hkdf.info );
4794 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004795 }
John Durkop07cc04a2020-11-16 22:08:34 -08004796 else
4797#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4798#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4799 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4800 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004802 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004803 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004804 if( operation->ctx.tls12_prf.seed != NULL )
4805 {
4806 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4807 operation->ctx.tls12_prf.seed_length );
4808 mbedtls_free( operation->ctx.tls12_prf.seed );
4809 }
4810
4811 if( operation->ctx.tls12_prf.label != NULL )
4812 {
4813 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4814 operation->ctx.tls12_prf.label_length );
4815 mbedtls_free( operation->ctx.tls12_prf.label );
4816 }
4817
4818 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4819
4820 /* We leave the fields Ai and output_block to be erased safely by the
4821 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004822 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004823 else
John Durkop07cc04a2020-11-16 22:08:34 -08004824#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4825 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004826 {
4827 status = PSA_ERROR_BAD_STATE;
4828 }
Janos Follath083036a2019-06-11 10:22:26 +01004829 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004830 return( status );
4831}
4832
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004834 size_t *capacity)
4835{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004837 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004839 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004840 }
4841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004843 return( PSA_SUCCESS );
4844}
4845
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004847 size_t capacity )
4848{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004850 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004852 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004854 return( PSA_SUCCESS );
4855}
4856
John Durkopd0321952020-10-29 21:37:36 -07004857#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004859 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004860static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004861 psa_algorithm_t hash_alg,
4862 uint8_t *output,
4863 size_t output_length )
4864{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004865 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004866 psa_status_t status;
4867
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004868 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4869 return( PSA_ERROR_BAD_STATE );
4870 hkdf->state = HKDF_STATE_OUTPUT;
4871
Gilles Peskinebef7f142018-07-12 17:22:21 +02004872 while( output_length != 0 )
4873 {
4874 /* Copy what remains of the current block */
4875 uint8_t n = hash_length - hkdf->offset_in_block;
4876 if( n > output_length )
4877 n = (uint8_t) output_length;
4878 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4879 output += n;
4880 output_length -= n;
4881 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004882 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004883 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004884 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004885 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004887 * object was corrupted or if this function is called directly
4888 * inside the library. */
4889 if( hkdf->block_number == 0xff )
4890 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004891
4892 /* We need a new block */
4893 ++hkdf->block_number;
4894 hkdf->offset_in_block = 0;
4895 status = psa_hmac_setup_internal( &hkdf->hmac,
4896 hkdf->prk, hash_length,
4897 hash_alg );
4898 if( status != PSA_SUCCESS )
4899 return( status );
4900 if( hkdf->block_number != 1 )
4901 {
4902 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4903 hkdf->output_block,
4904 hash_length );
4905 if( status != PSA_SUCCESS )
4906 return( status );
4907 }
4908 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4909 hkdf->info,
4910 hkdf->info_length );
4911 if( status != PSA_SUCCESS )
4912 return( status );
4913 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4914 &hkdf->block_number, 1 );
4915 if( status != PSA_SUCCESS )
4916 return( status );
4917 status = psa_hmac_finish_internal( &hkdf->hmac,
4918 hkdf->output_block,
4919 sizeof( hkdf->output_block ) );
4920 if( status != PSA_SUCCESS )
4921 return( status );
4922 }
4923
4924 return( PSA_SUCCESS );
4925}
John Durkop07cc04a2020-11-16 22:08:34 -08004926#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004927
John Durkop07cc04a2020-11-16 22:08:34 -08004928#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4929 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004930static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4931 psa_tls12_prf_key_derivation_t *tls12_prf,
4932 psa_algorithm_t alg )
4933{
4934 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004935 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004936 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4937 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004938
Janos Follath7742fee2019-06-17 12:58:10 +01004939 /* We can't be wanting more output after block 0xff, otherwise
4940 * the capacity check in psa_key_derivation_output_bytes() would have
4941 * prevented this call. It could happen only if the operation
4942 * object was corrupted or if this function is called directly
4943 * inside the library. */
4944 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004945 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004946
4947 /* We need a new block */
4948 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004949 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004950
4951 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4952 *
4953 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4954 *
4955 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4956 * HMAC_hash(secret, A(2) + seed) +
4957 * HMAC_hash(secret, A(3) + seed) + ...
4958 *
4959 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004960 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004961 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004962 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004963 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004964 * is currently extracted as `output_block` and where i is
4965 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004966 */
4967
Janos Follathea29bfb2019-06-19 12:21:20 +01004968 /* Save the hash context before using it, to preserve the hash state with
4969 * only the inner padding in it. We need this, because inner padding depends
4970 * on the key (secret in the RFC's terminology). */
4971 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4972 if( status != PSA_SUCCESS )
4973 goto cleanup;
4974
4975 /* Calculate A(i) where i = tls12_prf->block_number. */
4976 if( tls12_prf->block_number == 1 )
4977 {
4978 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4979 * the variable seed and in this instance means it in the context of the
4980 * P_hash function, where seed = label + seed.) */
4981 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4982 tls12_prf->label, tls12_prf->label_length );
4983 if( status != PSA_SUCCESS )
4984 goto cleanup;
4985 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4986 tls12_prf->seed, tls12_prf->seed_length );
4987 if( status != PSA_SUCCESS )
4988 goto cleanup;
4989 }
4990 else
4991 {
4992 /* A(i) = HMAC_hash(secret, A(i-1)) */
4993 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4994 tls12_prf->Ai, hash_length );
4995 if( status != PSA_SUCCESS )
4996 goto cleanup;
4997 }
4998
4999 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5000 tls12_prf->Ai, hash_length );
5001 if( status != PSA_SUCCESS )
5002 goto cleanup;
5003 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5004 if( status != PSA_SUCCESS )
5005 goto cleanup;
5006
5007 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5008 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5009 tls12_prf->Ai, hash_length );
5010 if( status != PSA_SUCCESS )
5011 goto cleanup;
5012 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5013 tls12_prf->label, tls12_prf->label_length );
5014 if( status != PSA_SUCCESS )
5015 goto cleanup;
5016 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5017 tls12_prf->seed, tls12_prf->seed_length );
5018 if( status != PSA_SUCCESS )
5019 goto cleanup;
5020 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5021 tls12_prf->output_block, hash_length );
5022 if( status != PSA_SUCCESS )
5023 goto cleanup;
5024 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5025 if( status != PSA_SUCCESS )
5026 goto cleanup;
5027
Janos Follath7742fee2019-06-17 12:58:10 +01005028
5029cleanup:
5030
Janos Follathea29bfb2019-06-19 12:21:20 +01005031 cleanup_status = psa_hash_abort( &backup );
5032 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5033 status = cleanup_status;
5034
5035 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005036}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005037
Janos Follath844eb0e2019-06-19 12:10:49 +01005038static psa_status_t psa_key_derivation_tls12_prf_read(
5039 psa_tls12_prf_key_derivation_t *tls12_prf,
5040 psa_algorithm_t alg,
5041 uint8_t *output,
5042 size_t output_length )
5043{
5044 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005045 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005046 psa_status_t status;
5047 uint8_t offset, length;
5048
5049 while( output_length != 0 )
5050 {
5051 /* Check if we have fully processed the current block. */
5052 if( tls12_prf->left_in_block == 0 )
5053 {
5054 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5055 alg );
5056 if( status != PSA_SUCCESS )
5057 return( status );
5058
5059 continue;
5060 }
5061
5062 if( tls12_prf->left_in_block > output_length )
5063 length = (uint8_t) output_length;
5064 else
5065 length = tls12_prf->left_in_block;
5066
5067 offset = hash_length - tls12_prf->left_in_block;
5068 memcpy( output, tls12_prf->output_block + offset, length );
5069 output += length;
5070 output_length -= length;
5071 tls12_prf->left_in_block -= length;
5072 }
5073
5074 return( PSA_SUCCESS );
5075}
John Durkop07cc04a2020-11-16 22:08:34 -08005076#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5077 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005078
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005079psa_status_t psa_key_derivation_output_bytes(
5080 psa_key_derivation_operation_t *operation,
5081 uint8_t *output,
5082 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005083{
5084 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005086
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005088 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005089 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005090 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005091 }
5092
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005094 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005096 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005097 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005098 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005099 goto exit;
5100 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005102 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005104 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005105 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5106 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005108 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005109 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005110 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112
John Durkopd0321952020-10-29 21:37:36 -07005113#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005114 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005115 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005116 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005117 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005118 output, output_length );
5119 }
Janos Follathadbec812019-06-14 11:05:39 +01005120 else
John Durkop07cc04a2020-11-16 22:08:34 -08005121#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5122#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5123 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005124 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005125 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005126 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005128 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005129 output_length );
5130 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005131 else
John Durkop07cc04a2020-11-16 22:08:34 -08005132#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5133 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005134 {
5135 return( PSA_ERROR_BAD_STATE );
5136 }
5137
5138exit:
5139 if( status != PSA_SUCCESS )
5140 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005141 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 * This allows us to differentiate between exhausted operations and
5143 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5144 * operations. */
5145 psa_algorithm_t alg = operation->alg;
5146 psa_key_derivation_abort( operation );
5147 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005148 memset( output, '!', output_length );
5149 }
5150 return( status );
5151}
5152
Gilles Peskine08542d82018-07-19 17:05:42 +02005153#if defined(MBEDTLS_DES_C)
5154static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5155{
5156 if( data_size >= 8 )
5157 mbedtls_des_key_set_parity( data );
5158 if( data_size >= 16 )
5159 mbedtls_des_key_set_parity( data + 8 );
5160 if( data_size >= 24 )
5161 mbedtls_des_key_set_parity( data + 16 );
5162}
5163#endif /* MBEDTLS_DES_C */
5164
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005165static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005166 psa_key_slot_t *slot,
5167 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005169{
5170 uint8_t *data = NULL;
5171 size_t bytes = PSA_BITS_TO_BYTES( bits );
5172 psa_status_t status;
5173
Gilles Peskine8e338702019-07-30 20:06:31 +02005174 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005175 return( PSA_ERROR_INVALID_ARGUMENT );
5176 if( bits % 8 != 0 )
5177 return( PSA_ERROR_INVALID_ARGUMENT );
5178 data = mbedtls_calloc( 1, bytes );
5179 if( data == NULL )
5180 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005183 if( status != PSA_SUCCESS )
5184 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005185#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005186 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005187 psa_des_set_key_parity( data, bytes );
5188#endif /* MBEDTLS_DES_C */
Ronald Crondd04d422020-11-28 15:14:42 +01005189
5190 status = psa_allocate_buffer_to_slot( slot, bytes );
5191 if( status != PSA_SUCCESS )
5192 return( status );
5193
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005194 psa_key_attributes_t attributes = {
5195 .core = slot->attr
5196 };
5197
5198 status = psa_import_key_into_slot( &attributes,
5199 data, bytes,
Ronald Crondd04d422020-11-28 15:14:42 +01005200 slot->key.data, slot->key.bytes,
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005201 &slot->key.bytes,
5202 &bits );
5203 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204
5205exit:
5206 mbedtls_free( data );
5207 return( status );
5208}
5209
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005210psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005212 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005213{
5214 psa_status_t status;
5215 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005216 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005217
Ronald Cron81709fc2020-11-14 12:10:32 +01005218 *key = MBEDTLS_SVC_KEY_ID_INIT;
5219
Gilles Peskine0f84d622019-09-12 19:03:13 +02005220 /* Reject any attempt to create a zero-length key so that we don't
5221 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5222 if( psa_get_key_bits( attributes ) == 0 )
5223 return( PSA_ERROR_INVALID_ARGUMENT );
5224
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005225 if( ! operation->can_output_key )
5226 return( PSA_ERROR_NOT_PERMITTED );
5227
Ronald Cron81709fc2020-11-14 12:10:32 +01005228 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5229 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5231 if( driver != NULL )
5232 {
5233 /* Deriving a key in a secure element is not implemented yet. */
5234 status = PSA_ERROR_NOT_SUPPORTED;
5235 }
5236#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005237 if( status == PSA_SUCCESS )
5238 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005239 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005240 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005242 }
5243 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005244 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005245 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005246 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005247
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005248 return( status );
5249}
5250
Gilles Peskineeab56e42018-07-12 17:12:33 +02005251
5252
5253/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005254/* Key derivation */
5255/****************************************************************/
5256
John Durkop07cc04a2020-11-16 22:08:34 -08005257#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005258static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005260 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261{
John Durkop07cc04a2020-11-16 22:08:34 -08005262 int is_kdf_alg_supported;
5263
Janos Follath5fe19732019-06-20 15:09:30 +01005264 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5265 * initialisers for this union leave some bytes unspecified.) */
5266 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5267
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005269#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005270 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5271 is_kdf_alg_supported = 1;
5272 else
5273#endif
5274#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5275 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5276 is_kdf_alg_supported = 1;
5277 else
5278#endif
5279#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5280 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5281 is_kdf_alg_supported = 1;
5282 else
5283#endif
5284 is_kdf_alg_supported = 0;
5285
5286 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005288 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005289 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005290 if( hash_size == 0 )
5291 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5293 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005294 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295 {
5296 return( PSA_ERROR_NOT_SUPPORTED );
5297 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005299 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 }
John Durkop07cc04a2020-11-16 22:08:34 -08005301
5302 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005303}
John Durkop07cc04a2020-11-16 22:08:34 -08005304#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005305
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005307 psa_algorithm_t alg )
5308{
5309 psa_status_t status;
5310
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005312 return( PSA_ERROR_BAD_STATE );
5313
Gilles Peskine6843c292019-01-18 16:44:49 +01005314 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5315 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005316#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005317 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318 {
5319 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005321 }
5322 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5323 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005325 }
John Durkop07cc04a2020-11-16 22:08:34 -08005326#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327 else
5328 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005329
5330 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005332 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005333}
5334
John Durkopd0321952020-10-29 21:37:36 -07005335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005336static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337 psa_algorithm_t hash_alg,
5338 psa_key_derivation_step_t step,
5339 const uint8_t *data,
5340 size_t data_length )
5341{
5342 psa_status_t status;
5343 switch( step )
5344 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005345 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005346 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005347 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005348 status = psa_hmac_setup_internal( &hkdf->hmac,
5349 data, data_length,
5350 hash_alg );
5351 if( status != PSA_SUCCESS )
5352 return( status );
5353 hkdf->state = HKDF_STATE_STARTED;
5354 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005355 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005356 /* If no salt was provided, use an empty salt. */
5357 if( hkdf->state == HKDF_STATE_INIT )
5358 {
5359 status = psa_hmac_setup_internal( &hkdf->hmac,
5360 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005361 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005362 if( status != PSA_SUCCESS )
5363 return( status );
5364 hkdf->state = HKDF_STATE_STARTED;
5365 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005366 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005368 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5369 data, data_length );
5370 if( status != PSA_SUCCESS )
5371 return( status );
5372 status = psa_hmac_finish_internal( &hkdf->hmac,
5373 hkdf->prk,
5374 sizeof( hkdf->prk ) );
5375 if( status != PSA_SUCCESS )
5376 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005377 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005378 hkdf->block_number = 0;
5379 hkdf->state = HKDF_STATE_KEYED;
5380 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005381 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005382 if( hkdf->state == HKDF_STATE_OUTPUT )
5383 return( PSA_ERROR_BAD_STATE );
5384 if( hkdf->info_set )
5385 return( PSA_ERROR_BAD_STATE );
5386 hkdf->info_length = data_length;
5387 if( data_length != 0 )
5388 {
5389 hkdf->info = mbedtls_calloc( 1, data_length );
5390 if( hkdf->info == NULL )
5391 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5392 memcpy( hkdf->info, data, data_length );
5393 }
5394 hkdf->info_set = 1;
5395 return( PSA_SUCCESS );
5396 default:
5397 return( PSA_ERROR_INVALID_ARGUMENT );
5398 }
5399}
John Durkop07cc04a2020-11-16 22:08:34 -08005400#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005401
John Durkop07cc04a2020-11-16 22:08:34 -08005402#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5403 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005404static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5405 const uint8_t *data,
5406 size_t data_length )
5407{
5408 if( prf->state != TLS12_PRF_STATE_INIT )
5409 return( PSA_ERROR_BAD_STATE );
5410
Janos Follathd6dce9f2019-07-04 09:11:38 +01005411 if( data_length != 0 )
5412 {
5413 prf->seed = mbedtls_calloc( 1, data_length );
5414 if( prf->seed == NULL )
5415 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005416
Janos Follathd6dce9f2019-07-04 09:11:38 +01005417 memcpy( prf->seed, data, data_length );
5418 prf->seed_length = data_length;
5419 }
Janos Follathf08e2652019-06-13 09:05:41 +01005420
5421 prf->state = TLS12_PRF_STATE_SEED_SET;
5422
5423 return( PSA_SUCCESS );
5424}
5425
Janos Follath81550542019-06-13 14:26:34 +01005426static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5427 psa_algorithm_t hash_alg,
5428 const uint8_t *data,
5429 size_t data_length )
5430{
5431 psa_status_t status;
5432 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5433 return( PSA_ERROR_BAD_STATE );
5434
5435 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438
5439 prf->state = TLS12_PRF_STATE_KEY_SET;
5440
5441 return( PSA_SUCCESS );
5442}
5443
Janos Follath63028dd2019-06-13 09:15:47 +01005444static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5445 const uint8_t *data,
5446 size_t data_length )
5447{
5448 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5449 return( PSA_ERROR_BAD_STATE );
5450
Janos Follathd6dce9f2019-07-04 09:11:38 +01005451 if( data_length != 0 )
5452 {
5453 prf->label = mbedtls_calloc( 1, data_length );
5454 if( prf->label == NULL )
5455 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005456
Janos Follathd6dce9f2019-07-04 09:11:38 +01005457 memcpy( prf->label, data, data_length );
5458 prf->label_length = data_length;
5459 }
Janos Follath63028dd2019-06-13 09:15:47 +01005460
5461 prf->state = TLS12_PRF_STATE_LABEL_SET;
5462
5463 return( PSA_SUCCESS );
5464}
5465
Janos Follathb03233e2019-06-11 15:30:30 +01005466static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5467 psa_algorithm_t hash_alg,
5468 psa_key_derivation_step_t step,
5469 const uint8_t *data,
5470 size_t data_length )
5471{
Janos Follathb03233e2019-06-11 15:30:30 +01005472 switch( step )
5473 {
Janos Follathf08e2652019-06-13 09:05:41 +01005474 case PSA_KEY_DERIVATION_INPUT_SEED:
5475 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005476 case PSA_KEY_DERIVATION_INPUT_SECRET:
5477 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005478 case PSA_KEY_DERIVATION_INPUT_LABEL:
5479 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005480 default:
5481 return( PSA_ERROR_INVALID_ARGUMENT );
5482 }
5483}
John Durkop07cc04a2020-11-16 22:08:34 -08005484#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5485 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5486
5487#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5488static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5489 psa_tls12_prf_key_derivation_t *prf,
5490 psa_algorithm_t hash_alg,
5491 const uint8_t *data,
5492 size_t data_length )
5493{
5494 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005495 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005496 uint8_t *cur = pms;
5497
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005498 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005499 return( PSA_ERROR_INVALID_ARGUMENT );
5500
5501 /* Quoting RFC 4279, Section 2:
5502 *
5503 * The premaster secret is formed as follows: if the PSK is N octets
5504 * long, concatenate a uint16 with the value N, N zero octets, a second
5505 * uint16 with the value N, and the PSK itself.
5506 */
5507
5508 *cur++ = ( data_length >> 8 ) & 0xff;
5509 *cur++ = ( data_length >> 0 ) & 0xff;
5510 memset( cur, 0, data_length );
5511 cur += data_length;
5512 *cur++ = pms[0];
5513 *cur++ = pms[1];
5514 memcpy( cur, data, data_length );
5515 cur += data_length;
5516
5517 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5518
5519 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5520 return( status );
5521}
Janos Follath6660f0e2019-06-17 08:44:03 +01005522
5523static psa_status_t psa_tls12_prf_psk_to_ms_input(
5524 psa_tls12_prf_key_derivation_t *prf,
5525 psa_algorithm_t hash_alg,
5526 psa_key_derivation_step_t step,
5527 const uint8_t *data,
5528 size_t data_length )
5529{
5530 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005531 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005532 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5533 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005534 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005535
5536 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5537}
John Durkop07cc04a2020-11-16 22:08:34 -08005538#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005539
Gilles Peskineb8965192019-09-24 16:21:10 +02005540/** Check whether the given key type is acceptable for the given
5541 * input step of a key derivation.
5542 *
5543 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5544 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5545 * Both secret and non-secret inputs can alternatively have the type
5546 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5547 * that the input was passed as a buffer rather than via a key object.
5548 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005549static int psa_key_derivation_check_input_type(
5550 psa_key_derivation_step_t step,
5551 psa_key_type_t key_type )
5552{
5553 switch( step )
5554 {
5555 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005556 if( key_type == PSA_KEY_TYPE_DERIVE )
5557 return( PSA_SUCCESS );
5558 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005559 return( PSA_SUCCESS );
5560 break;
5561 case PSA_KEY_DERIVATION_INPUT_LABEL:
5562 case PSA_KEY_DERIVATION_INPUT_SALT:
5563 case PSA_KEY_DERIVATION_INPUT_INFO:
5564 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005565 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5566 return( PSA_SUCCESS );
5567 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005568 return( PSA_SUCCESS );
5569 break;
5570 }
5571 return( PSA_ERROR_INVALID_ARGUMENT );
5572}
5573
Janos Follathb80a94e2019-06-12 15:54:46 +01005574static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005576 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005577 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005578 const uint8_t *data,
5579 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005580{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005581 psa_status_t status;
5582 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5583
5584 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005585 if( status != PSA_SUCCESS )
5586 goto exit;
5587
John Durkopd0321952020-10-29 21:37:36 -07005588#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005589 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005590 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005592 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005593 step, data, data_length );
5594 }
John Durkop07cc04a2020-11-16 22:08:34 -08005595 else
5596#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5597#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5598 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005599 {
Janos Follathb03233e2019-06-11 15:30:30 +01005600 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5601 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5602 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005603 }
John Durkop07cc04a2020-11-16 22:08:34 -08005604 else
5605#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5606#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5607 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005608 {
5609 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5610 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5611 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005612 }
5613 else
John Durkop07cc04a2020-11-16 22:08:34 -08005614#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005615 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005616 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617 return( PSA_ERROR_BAD_STATE );
5618 }
5619
Gilles Peskine224b0d62019-09-23 18:13:17 +02005620exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005621 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005622 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005623 return( status );
5624}
5625
Janos Follath51f4a0f2019-06-14 11:35:55 +01005626psa_status_t psa_key_derivation_input_bytes(
5627 psa_key_derivation_operation_t *operation,
5628 psa_key_derivation_step_t step,
5629 const uint8_t *data,
5630 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005631{
Gilles Peskineb8965192019-09-24 16:21:10 +02005632 return( psa_key_derivation_input_internal( operation, step,
5633 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005634 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005635}
5636
Janos Follath51f4a0f2019-06-14 11:35:55 +01005637psa_status_t psa_key_derivation_input_key(
5638 psa_key_derivation_operation_t *operation,
5639 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005640 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005641{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005643 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005644 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005645
Ronald Cron5c522922020-11-14 16:35:34 +01005646 status = psa_get_and_lock_transparent_key_slot_with_policy(
5647 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005648 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005649 {
5650 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005651 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005652 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005653
5654 /* Passing a key object as a SECRET input unlocks the permission
5655 * to output to a key object. */
5656 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5657 operation->can_output_key = 1;
5658
Ronald Cronf95a2b12020-10-22 15:24:49 +02005659 status = psa_key_derivation_input_internal( operation,
5660 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005661 slot->key.data,
5662 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005663
Ronald Cron5c522922020-11-14 16:35:34 +01005664 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005665
Ronald Cron5c522922020-11-14 16:35:34 +01005666 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005667}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005668
5669
5670
5671/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005672/* Key agreement */
5673/****************************************************************/
5674
John Durkop6ba40d12020-11-10 08:50:04 -08005675#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005676static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5677 size_t peer_key_length,
5678 const mbedtls_ecp_keypair *our_key,
5679 uint8_t *shared_secret,
5680 size_t shared_secret_size,
5681 size_t *shared_secret_length )
5682{
Steven Cooremand4867872020-08-05 16:31:39 +02005683 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005684 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005685 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005686 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005687 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005688 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005689
Ronald Cron90857082020-11-25 14:58:33 +01005690 status = mbedtls_psa_ecp_load_representation(
5691 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5692 peer_key,
5693 peer_key_length,
5694 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005695 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005696 goto exit;
5697
Jaeden Amero97271b32019-01-10 19:38:51 +00005698 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005699 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005700 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005701 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005702 status = mbedtls_to_psa_error(
5703 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5704 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005705 goto exit;
5706
Jaeden Amero97271b32019-01-10 19:38:51 +00005707 status = mbedtls_to_psa_error(
5708 mbedtls_ecdh_calc_secret( &ecdh,
5709 shared_secret_length,
5710 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005711 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005712 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005713 if( status != PSA_SUCCESS )
5714 goto exit;
5715 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5716 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005717
5718exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005719 if( status != PSA_SUCCESS )
5720 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005721 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005722 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005723 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005724
Jaeden Amero97271b32019-01-10 19:38:51 +00005725 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005726}
John Durkop6ba40d12020-11-10 08:50:04 -08005727#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005728
Gilles Peskine01d718c2018-09-18 12:01:02 +02005729#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5730
Gilles Peskine0216fe12019-04-11 21:23:21 +02005731static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5732 psa_key_slot_t *private_key,
5733 const uint8_t *peer_key,
5734 size_t peer_key_length,
5735 uint8_t *shared_secret,
5736 size_t shared_secret_size,
5737 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005738{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005739 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005740 {
John Durkop6ba40d12020-11-10 08:50:04 -08005741#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005742 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005743 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005744 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005745 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005746 psa_status_t status = mbedtls_psa_ecp_load_representation(
5747 private_key->attr.type,
5748 private_key->key.data,
5749 private_key->key.bytes,
5750 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005751 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005752 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005753 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005754 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005755 shared_secret, shared_secret_size,
5756 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005757 mbedtls_ecp_keypair_free( ecp );
5758 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005759 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005760#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005761 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005762 (void) private_key;
5763 (void) peer_key;
5764 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005765 (void) shared_secret;
5766 (void) shared_secret_size;
5767 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005768 return( PSA_ERROR_NOT_SUPPORTED );
5769 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005770}
5771
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005772/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005773 * to potentially free embedded data structures and wipe confidential data.
5774 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005775static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005776 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005777 psa_key_slot_t *private_key,
5778 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005779 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005780{
5781 psa_status_t status;
5782 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5783 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005784 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005785
5786 /* Step 1: run the secret agreement algorithm to generate the shared
5787 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005788 status = psa_key_agreement_raw_internal( ka_alg,
5789 private_key,
5790 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005791 shared_secret,
5792 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005793 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005794 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005795 goto exit;
5796
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005797 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005798 * the shared secret. A shared secret is permitted wherever a key
5799 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005800 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005801 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005802 shared_secret,
5803 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005804exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005805 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005806 return( status );
5807}
5808
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005810 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005811 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005812 const uint8_t *peer_key,
5813 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005814{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005816 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005817 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005819 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005820 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005821 status = psa_get_and_lock_transparent_key_slot_with_policy(
5822 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005823 if( status != PSA_SUCCESS )
5824 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005825 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005826 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005827 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005828 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005829 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005830 else
5831 {
5832 /* If a private key has been added as SECRET, we allow the derived
5833 * key material to be used as a key in PSA Crypto. */
5834 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5835 operation->can_output_key = 1;
5836 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005837
Ronald Cron5c522922020-11-14 16:35:34 +01005838 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005839
Ronald Cron5c522922020-11-14 16:35:34 +01005840 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005841}
5842
Gilles Peskinebe697d82019-05-16 18:00:41 +02005843psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005844 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005845 const uint8_t *peer_key,
5846 size_t peer_key_length,
5847 uint8_t *output,
5848 size_t output_size,
5849 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005850{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005852 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005853 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005854
5855 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5856 {
5857 status = PSA_ERROR_INVALID_ARGUMENT;
5858 goto exit;
5859 }
Ronald Cron5c522922020-11-14 16:35:34 +01005860 status = psa_get_and_lock_transparent_key_slot_with_policy(
5861 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005862 if( status != PSA_SUCCESS )
5863 goto exit;
5864
5865 status = psa_key_agreement_raw_internal( alg, slot,
5866 peer_key, peer_key_length,
5867 output, output_size,
5868 output_length );
5869
5870exit:
5871 if( status != PSA_SUCCESS )
5872 {
5873 /* If an error happens and is not handled properly, the output
5874 * may be used as a key to protect sensitive data. Arrange for such
5875 * a key to be random, which is likely to result in decryption or
5876 * verification errors. This is better than filling the buffer with
5877 * some constant data such as zeros, which would result in the data
5878 * being protected with a reproducible, easily knowable key.
5879 */
5880 psa_generate_random( output, output_size );
5881 *output_length = output_size;
5882 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005883
Ronald Cron5c522922020-11-14 16:35:34 +01005884 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005885
Ronald Cron5c522922020-11-14 16:35:34 +01005886 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005887}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005888
5889
Gilles Peskine30524eb2020-11-13 17:02:26 +01005890
Gilles Peskine86a440b2018-11-12 18:39:40 +01005891/****************************************************************/
5892/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005893/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894
Gilles Peskine30524eb2020-11-13 17:02:26 +01005895/** Initialize the PSA random generator.
5896 */
5897static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5898{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005899#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5900 memset( rng, 0, sizeof( *rng ) );
5901#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5902
Gilles Peskine30524eb2020-11-13 17:02:26 +01005903 /* Set default configuration if
5904 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5905 if( rng->entropy_init == NULL )
5906 rng->entropy_init = mbedtls_entropy_init;
5907 if( rng->entropy_free == NULL )
5908 rng->entropy_free = mbedtls_entropy_free;
5909
5910 rng->entropy_init( &rng->entropy );
5911#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5912 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5913 /* The PSA entropy injection feature depends on using NV seed as an entropy
5914 * source. Add NV seed as an entropy source for PSA entropy injection. */
5915 mbedtls_entropy_add_source( &rng->entropy,
5916 mbedtls_nv_seed_poll, NULL,
5917 MBEDTLS_ENTROPY_BLOCK_SIZE,
5918 MBEDTLS_ENTROPY_SOURCE_STRONG );
5919#endif
5920
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005921 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005922#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005923}
5924
5925/** Deinitialize the PSA random generator.
5926 */
5927static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5928{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005929#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5930 memset( rng, 0, sizeof( *rng ) );
5931#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005932 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005933 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005934#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005935}
5936
5937/** Seed the PSA random generator.
5938 */
5939static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5940{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005941#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5942 /* Do nothing: the external RNG seeds itself. */
5943 (void) rng;
5944 return( PSA_SUCCESS );
5945#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005946 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005947 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5948 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005949 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005950#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005951}
5952
Gilles Peskinee59236f2018-01-27 23:32:46 +01005953psa_status_t psa_generate_random( uint8_t *output,
5954 size_t output_size )
5955{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005956 GUARD_MODULE_INITIALIZED;
5957
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005958#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5959
5960 size_t output_length = 0;
5961 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5962 output, output_size,
5963 &output_length );
5964 if( status != PSA_SUCCESS )
5965 return( status );
5966 /* Breaking up a request into smaller chunks is currently not supported
5967 * for the extrernal RNG interface. */
5968 if( output_length != output_size )
5969 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5970 return( PSA_SUCCESS );
5971
5972#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5973
Gilles Peskine71ddab92021-01-04 21:01:07 +01005974 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005975 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005976 size_t request_size =
5977 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5978 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5979 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005980 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5981 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005982 if( ret != 0 )
5983 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005984 output_size -= request_size;
5985 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005986 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005987 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005988#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005989}
5990
Gilles Peskine8814fc42020-12-14 15:33:44 +01005991/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005992 *
5993 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5994 * `psa_generate_random(...)`. The state parameter is ignored since the
5995 * PSA API doesn't support passing an explicit state.
5996 *
5997 * In the non-external case, psa_generate_random() calls an
5998 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5999 * and semantics as mbedtls_psa_get_random(). As an optimization,
6000 * instead of doing this back-and-forth between the PSA API and the
6001 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6002 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006003 */
6004#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6005int mbedtls_psa_get_random( void *p_rng,
6006 unsigned char *output,
6007 size_t output_size )
6008{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006009 /* This function takes a pointer to the RNG state because that's what
6010 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6011 * its own state internally and doesn't let the caller access that state.
6012 * So we just ignore the state parameter, and in practice we'll pass
6013 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006014 (void) p_rng;
6015 psa_status_t status = psa_generate_random( output, output_size );
6016 if( status == PSA_SUCCESS )
6017 return( 0 );
6018 else
6019 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6020}
6021#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6022
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006023#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6024#include "mbedtls/entropy_poll.h"
6025
Gilles Peskine7228da22019-07-15 11:06:15 +02006026psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006027 size_t seed_size )
6028{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006029 if( global_data.initialized )
6030 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006031
6032 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6033 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6034 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6035 return( PSA_ERROR_INVALID_ARGUMENT );
6036
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006037 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006038}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006039#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006040
John Durkop07cc04a2020-11-16 22:08:34 -08006041#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006042static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6043 size_t domain_parameters_size,
6044 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006045{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006046 size_t i;
6047 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006048
Gilles Peskinee56e8782019-04-26 17:34:02 +02006049 if( domain_parameters_size == 0 )
6050 {
6051 *exponent = 65537;
6052 return( PSA_SUCCESS );
6053 }
6054
6055 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6056 * support values that fit in a 32-bit integer, which is larger than
6057 * int on just about every platform anyway. */
6058 if( domain_parameters_size > sizeof( acc ) )
6059 return( PSA_ERROR_NOT_SUPPORTED );
6060 for( i = 0; i < domain_parameters_size; i++ )
6061 acc = ( acc << 8 ) | domain_parameters[i];
6062 if( acc > INT_MAX )
6063 return( PSA_ERROR_NOT_SUPPORTED );
6064 *exponent = acc;
6065 return( PSA_SUCCESS );
6066}
John Durkop07cc04a2020-11-16 22:08:34 -08006067#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006068
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006069static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006070 psa_key_slot_t *slot, size_t bits,
6071 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072{
Gilles Peskine8e338702019-07-30 20:06:31 +02006073 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006074
Gilles Peskinee56e8782019-04-26 17:34:02 +02006075 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006076 return( PSA_ERROR_INVALID_ARGUMENT );
6077
Gilles Peskinee59236f2018-01-27 23:32:46 +01006078 if( key_type_is_raw_bytes( type ) )
6079 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006080 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006081
6082 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006083 if( status != PSA_SUCCESS )
6084 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006085
6086 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006087 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6088 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006089 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006090
Ronald Cronea0f8a62020-11-25 17:52:23 +01006091 status = psa_generate_random( slot->key.data,
6092 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006093 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006094 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006095
6096 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006097#if defined(MBEDTLS_DES_C)
6098 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006099 psa_des_set_key_parity( slot->key.data,
6100 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101#endif /* MBEDTLS_DES_C */
6102 }
6103 else
6104
John Durkop07cc04a2020-11-16 22:08:34 -08006105#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006106 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006107 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006108 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006110 int exponent;
6111 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006112 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6113 return( PSA_ERROR_NOT_SUPPORTED );
6114 /* Accept only byte-aligned keys, for the same reasons as
Ronald Cronb6420e32020-11-22 16:15:38 +01006115 * in mbedtls_psa_rsa_import_key(). */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006116 if( bits % 8 != 0 )
6117 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006118 status = psa_read_rsa_exponent( domain_parameters,
6119 domain_parameters_size,
6120 &exponent );
6121 if( status != PSA_SUCCESS )
6122 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006123 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6124 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006125 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006126 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127 (unsigned int) bits,
6128 exponent );
6129 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006130 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006131
6132 /* Make sure to always have an export representation available */
6133 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6134
Steven Cooreman75b74362020-07-28 14:30:13 +02006135 status = psa_allocate_buffer_to_slot( slot, bytes );
6136 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006137 {
6138 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006139 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006140 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006141
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006142 status = mbedtls_psa_rsa_export_key( type,
6143 &rsa,
6144 slot->key.data,
6145 bytes,
6146 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006147 mbedtls_rsa_free( &rsa );
6148 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006149 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006150 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151 }
6152 else
John Durkop07cc04a2020-11-16 22:08:34 -08006153#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006154
John Durkop9814fa22020-11-04 12:28:15 -08006155#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006156 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006157 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006158 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006159 mbedtls_ecp_group_id grp_id =
6160 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161 const mbedtls_ecp_curve_info *curve_info =
6162 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006163 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006165 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006166 return( PSA_ERROR_NOT_SUPPORTED );
6167 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6168 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006169 mbedtls_ecp_keypair_init( &ecp );
6170 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006171 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006172 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006173 if( ret != 0 )
6174 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006175 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006176 return( mbedtls_to_psa_error( ret ) );
6177 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006178
6179
6180 /* Make sure to always have an export representation available */
6181 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006182 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6183 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006184 {
6185 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006186 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006187 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006188
6189 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006190 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006191
6192 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006193 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006194 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006195 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006196 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006197 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198 }
6199 else
John Durkop9814fa22020-11-04 12:28:15 -08006200#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006201 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006202 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006203 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006204
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006205 return( PSA_SUCCESS );
6206}
Darryl Green0c6575a2018-11-07 16:05:30 +00006207
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006208psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006209 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006210{
6211 psa_status_t status;
6212 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006213 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006214
Ronald Cron81709fc2020-11-14 12:10:32 +01006215 *key = MBEDTLS_SVC_KEY_ID_INIT;
6216
Gilles Peskine0f84d622019-09-12 19:03:13 +02006217 /* Reject any attempt to create a zero-length key so that we don't
6218 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6219 if( psa_get_key_bits( attributes ) == 0 )
6220 return( PSA_ERROR_INVALID_ARGUMENT );
6221
Ronald Cron81709fc2020-11-14 12:10:32 +01006222 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6223 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006224 if( status != PSA_SUCCESS )
6225 goto exit;
6226
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006227 status = psa_driver_wrapper_generate_key( attributes,
6228 slot );
6229 if( status != PSA_ERROR_NOT_SUPPORTED ||
6230 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6231 goto exit;
6232
6233 status = psa_generate_key_internal(
6234 slot, attributes->core.bits,
6235 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006236
6237exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006238 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006239 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006240 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006241 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006242
Darryl Green0c6575a2018-11-07 16:05:30 +00006243 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006244}
6245
6246
Gilles Peskinee59236f2018-01-27 23:32:46 +01006247
6248/****************************************************************/
6249/* Module setup */
6250/****************************************************************/
6251
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006252#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006253psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6254 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6255 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6256{
6257 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6258 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006259 global_data.rng.entropy_init = entropy_init;
6260 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006261 return( PSA_SUCCESS );
6262}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006263#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006264
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265void mbedtls_psa_crypto_free( void )
6266{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006267 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006268 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6269 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006270 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006271 }
6272 /* Wipe all remaining data, including configuration.
6273 * In particular, this sets all state indicator to the value
6274 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006275 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006276#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006277 /* Unregister all secure element drivers, so that we restart from
6278 * a pristine state. */
6279 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006280#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006281}
6282
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006283#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6284/** Recover a transaction that was interrupted by a power failure.
6285 *
6286 * This function is called during initialization, before psa_crypto_init()
6287 * returns. If this function returns a failure status, the initialization
6288 * fails.
6289 */
6290static psa_status_t psa_crypto_recover_transaction(
6291 const psa_crypto_transaction_t *transaction )
6292{
6293 switch( transaction->unknown.type )
6294 {
6295 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6296 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006297 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006298 * is implemented.
6299 * https://github.com/ARMmbed/mbed-crypto/issues/218
6300 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006301 default:
6302 /* We found an unsupported transaction in the storage.
6303 * We don't know what state the storage is in. Give up. */
6304 return( PSA_ERROR_STORAGE_FAILURE );
6305 }
6306}
6307#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6308
Gilles Peskinee59236f2018-01-27 23:32:46 +01006309psa_status_t psa_crypto_init( void )
6310{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006311 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006312
Gilles Peskinec6b69072018-11-20 21:42:52 +01006313 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006314 if( global_data.initialized != 0 )
6315 return( PSA_SUCCESS );
6316
Gilles Peskine30524eb2020-11-13 17:02:26 +01006317 /* Initialize and seed the random generator. */
6318 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006319 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006320 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006321 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006322 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006323 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324
Gilles Peskine66fb1262018-12-10 16:29:04 +01006325 status = psa_initialize_key_slots( );
6326 if( status != PSA_SUCCESS )
6327 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006328
Gilles Peskined9348f22019-10-01 15:22:29 +02006329#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6330 status = psa_init_all_se_drivers( );
6331 if( status != PSA_SUCCESS )
6332 goto exit;
6333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6334
Gilles Peskine4b734222019-07-24 15:56:31 +02006335#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006336 status = psa_crypto_load_transaction( );
6337 if( status == PSA_SUCCESS )
6338 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006339 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6340 if( status != PSA_SUCCESS )
6341 goto exit;
6342 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006343 }
6344 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6345 {
6346 /* There's no transaction to complete. It's all good. */
6347 status = PSA_SUCCESS;
6348 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006349#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006350
Gilles Peskinec6b69072018-11-20 21:42:52 +01006351 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006352 global_data.initialized = 1;
6353
6354exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006355 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006356 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006357 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006358}
6359
6360#endif /* MBEDTLS_PSA_CRYPTO_C */