blob: 0d3ae4789fbb0de938d723d56a33d9948bbc96af [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020038#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020039#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010040#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010041/* Include internal declarations that are useful for implementing persistently
42 * stored keys. */
43#include "psa_crypto_storage.h"
44
Gilles Peskineb2b64d32020-12-14 16:43:58 +010045#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010046
Gilles Peskineb46bef22019-07-30 21:32:04 +020047#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include <stdlib.h>
49#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020051#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#define mbedtls_calloc calloc
53#define mbedtls_free free
54#endif
55
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/blowfish.h"
62#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020063#include "mbedtls/chacha20.h"
64#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/cipher.h"
66#include "mbedtls/ccm.h"
67#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
74#include "mbedtls/md2.h"
75#include "mbedtls/md4.h"
76#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
78#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010081#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000082#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
88#include "mbedtls/xtea.h"
89
Gilles Peskine996deb12018-08-01 15:45:45 +020090#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
91
Gilles Peskine9ef733f2018-02-07 21:05:37 +010092/* constant-time buffer comparison */
93static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
94{
95 size_t i;
96 unsigned char diff = 0;
97
98 for( i = 0; i < n; i++ )
99 diff |= a[i] ^ b[i];
100
101 return( diff );
102}
103
104
105
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100106/****************************************************************/
107/* Global data, support functions and library management */
108/****************************************************************/
109
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200110static int key_type_is_raw_bytes( psa_key_type_t type )
111{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200112 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200113}
114
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115/* Values for psa_global_data_t::rng_state */
116#define RNG_NOT_INITIALIZED 0
117#define RNG_INITIALIZED 1
118#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119
Gilles Peskine2d277862018-06-18 15:41:12 +0200120typedef struct
121{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100122 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100123 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125} psa_global_data_t;
126
127static psa_global_data_t global_data;
128
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100129#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
130mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
131 &global_data.rng.drbg;
132#endif
133
itayzafrir0adf0fc2018-09-06 16:24:41 +0300134#define GUARD_MODULE_INITIALIZED \
135 if( global_data.initialized == 0 ) \
136 return( PSA_ERROR_BAD_STATE );
137
Steven Cooreman01164162020-07-20 15:31:37 +0200138psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100139{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100140 /* Mbed TLS error codes can combine a high-level error code and a
141 * low-level error code. The low-level error usually reflects the
142 * root cause better, so dispatch on that preferably. */
143 int low_level_ret = - ( -ret & 0x007f );
144 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145 {
146 case 0:
147 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100148
149 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
150 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
151 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
152 return( PSA_ERROR_NOT_SUPPORTED );
153 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
154 return( PSA_ERROR_HARDWARE_FAILURE );
155
156 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
164 return( PSA_ERROR_INVALID_ARGUMENT );
165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
166 return( PSA_ERROR_INSUFFICIENT_MEMORY );
167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
168 return( PSA_ERROR_BUFFER_TOO_SMALL );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
173 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000181 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000182#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
183 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
184#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
186 return( PSA_ERROR_NOT_SUPPORTED );
187 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
188 return( PSA_ERROR_HARDWARE_FAILURE );
189
190 case MBEDTLS_ERR_CCM_BAD_INPUT:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192 case MBEDTLS_ERR_CCM_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
Gilles Peskine26869f22019-05-06 15:25:00 +0200197 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199
200 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
206 return( PSA_ERROR_NOT_SUPPORTED );
207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
208 return( PSA_ERROR_INVALID_ARGUMENT );
209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
210 return( PSA_ERROR_INSUFFICIENT_MEMORY );
211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
212 return( PSA_ERROR_INVALID_PADDING );
213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200214 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
216 return( PSA_ERROR_INVALID_SIGNATURE );
217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200218 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
226 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100227 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
228 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
Gilles Peskinee59236f2018-01-27 23:32:46 +0100243 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
244 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
245 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
246 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
248 case MBEDTLS_ERR_GCM_AUTH_FAILED:
249 return( PSA_ERROR_INVALID_SIGNATURE );
250 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200251 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
256 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100257 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
258 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
261 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
262 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
265 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
266#endif
267
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
269 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
272
273 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
274 return( PSA_ERROR_NOT_SUPPORTED );
275 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MD_ALLOC_FAILED:
278 return( PSA_ERROR_INSUFFICIENT_MEMORY );
279 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
280 return( PSA_ERROR_STORAGE_FAILURE );
281 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
285 return( PSA_ERROR_STORAGE_FAILURE );
286 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
291 return( PSA_ERROR_BUFFER_TOO_SMALL );
292 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
299 return( PSA_ERROR_INSUFFICIENT_MEMORY );
300
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_ALLOC_FAILED:
302 return( PSA_ERROR_INSUFFICIENT_MEMORY );
303 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
304 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
309 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
314 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
315 return( PSA_ERROR_NOT_PERMITTED );
316 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
317 return( PSA_ERROR_INVALID_ARGUMENT );
318 case MBEDTLS_ERR_PK_INVALID_ALG:
319 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
320 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
321 return( PSA_ERROR_NOT_SUPPORTED );
322 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
323 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
Gilles Peskineff2d2002019-05-06 15:26:23 +0200327 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
330 return( PSA_ERROR_NOT_SUPPORTED );
331
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_RSA_INVALID_PADDING:
338 return( PSA_ERROR_INVALID_PADDING );
339 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
341 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
342 return( PSA_ERROR_INVALID_ARGUMENT );
343 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
344 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200345 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
349 return( PSA_ERROR_BUFFER_TOO_SMALL );
350 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100351 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
353 return( PSA_ERROR_NOT_SUPPORTED );
354 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
355 return( PSA_ERROR_HARDWARE_FAILURE );
356
357 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
358 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
360 return( PSA_ERROR_HARDWARE_FAILURE );
361
362 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
363 return( PSA_ERROR_INVALID_ARGUMENT );
364 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
365 return( PSA_ERROR_HARDWARE_FAILURE );
366
itayzafrir5c753392018-05-08 11:18:38 +0300367 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300369 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
371 return( PSA_ERROR_BUFFER_TOO_SMALL );
372 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
373 return( PSA_ERROR_NOT_SUPPORTED );
374 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
375 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
376 return( PSA_ERROR_INVALID_SIGNATURE );
377 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100379 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
380 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300381 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
382 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100383
Janos Follatha13b9052019-11-22 12:48:59 +0000384 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
385 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300386
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 default:
David Saadab4ecc272019-02-14 13:48:10 +0200388 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100389 }
390}
391
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200392
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200393
394
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395/****************************************************************/
396/* Key management */
397/****************************************************************/
398
Gilles Peskine73167e12019-07-12 23:44:37 +0200399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
400static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
401{
Gilles Peskine8e338702019-07-30 20:06:31 +0200402 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200403}
404#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
405
John Durkop07cc04a2020-11-16 22:08:34 -0800406/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
407 * current test driver in key_management.c is using this function
408 * when accelerators are used for ECC key pair and public key.
409 * Once that dependency is resolved these guards can be removed.
410 */
John Durkop9814fa22020-11-04 12:28:15 -0800411#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800412 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
413 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100415mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100416 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200417{
418 switch( curve )
419 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100420 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100421 switch( byte_length )
422 {
423 case PSA_BITS_TO_BYTES( 192 ):
424 return( MBEDTLS_ECP_DP_SECP192R1 );
425 case PSA_BITS_TO_BYTES( 224 ):
426 return( MBEDTLS_ECP_DP_SECP224R1 );
427 case PSA_BITS_TO_BYTES( 256 ):
428 return( MBEDTLS_ECP_DP_SECP256R1 );
429 case PSA_BITS_TO_BYTES( 384 ):
430 return( MBEDTLS_ECP_DP_SECP384R1 );
431 case PSA_BITS_TO_BYTES( 521 ):
432 return( MBEDTLS_ECP_DP_SECP521R1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Paul Elliott8ff510a2020-06-02 17:19:28 +0100438 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 switch( byte_length )
440 {
441 case PSA_BITS_TO_BYTES( 256 ):
442 return( MBEDTLS_ECP_DP_BP256R1 );
443 case PSA_BITS_TO_BYTES( 384 ):
444 return( MBEDTLS_ECP_DP_BP384R1 );
445 case PSA_BITS_TO_BYTES( 512 ):
446 return( MBEDTLS_ECP_DP_BP512R1 );
447 default:
448 return( MBEDTLS_ECP_DP_NONE );
449 }
450 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100451
Paul Elliott8ff510a2020-06-02 17:19:28 +0100452 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100453 switch( byte_length )
454 {
455 case PSA_BITS_TO_BYTES( 255 ):
456 return( MBEDTLS_ECP_DP_CURVE25519 );
457 case PSA_BITS_TO_BYTES( 448 ):
458 return( MBEDTLS_ECP_DP_CURVE448 );
459 default:
460 return( MBEDTLS_ECP_DP_NONE );
461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
Paul Elliott8ff510a2020-06-02 17:19:28 +0100464 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 switch( byte_length )
466 {
467 case PSA_BITS_TO_BYTES( 192 ):
468 return( MBEDTLS_ECP_DP_SECP192K1 );
469 case PSA_BITS_TO_BYTES( 224 ):
470 return( MBEDTLS_ECP_DP_SECP224K1 );
471 case PSA_BITS_TO_BYTES( 256 ):
472 return( MBEDTLS_ECP_DP_SECP256K1 );
473 default:
474 return( MBEDTLS_ECP_DP_NONE );
475 }
476 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100477
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 default:
479 return( MBEDTLS_ECP_DP_NONE );
480 }
481}
John Durkop9814fa22020-11-04 12:28:15 -0800482#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800483 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
484 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200487static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
488 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489{
490 /* Check that the bit size is acceptable for the key type */
491 switch( type )
492 {
493 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200495 case PSA_KEY_TYPE_DERIVE:
496 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497#if defined(MBEDTLS_AES_C)
498 case PSA_KEY_TYPE_AES:
499 if( bits != 128 && bits != 192 && bits != 256 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503#if defined(MBEDTLS_CAMELLIA_C)
504 case PSA_KEY_TYPE_CAMELLIA:
505 if( bits != 128 && bits != 192 && bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
509#if defined(MBEDTLS_DES_C)
510 case PSA_KEY_TYPE_DES:
511 if( bits != 64 && bits != 128 && bits != 192 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_ARC4_C)
516 case PSA_KEY_TYPE_ARC4:
517 if( bits < 8 || bits > 2048 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200521#if defined(MBEDTLS_CHACHA20_C)
522 case PSA_KEY_TYPE_CHACHA20:
523 if( bits != 256 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 default:
528 return( PSA_ERROR_NOT_SUPPORTED );
529 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200530 if( bits % 8 != 0 )
531 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 return( PSA_SUCCESS );
534}
535
John Durkop6ba40d12020-11-10 08:50:04 -0800536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
537 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
538
Steven Cooremana01795d2020-07-24 22:48:15 +0200539/** Export an RSA key to export representation
540 *
541 * \param[in] type The type of key (public/private) to export
542 * \param[in] rsa The internal RSA representation from which to export
543 * \param[out] data The buffer to export to
544 * \param[in] data_size The length of the buffer to export to
545 * \param[out] data_length The amount of bytes written to \p data
546 */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100547static psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
548 mbedtls_rsa_context *rsa,
549 uint8_t *data,
550 size_t data_size,
551 size_t *data_length )
Steven Cooremana01795d2020-07-24 22:48:15 +0200552{
553#if defined(MBEDTLS_PK_WRITE_C)
554 int ret;
555 mbedtls_pk_context pk;
556 uint8_t *pos = data + data_size;
557
558 mbedtls_pk_init( &pk );
559 pk.pk_info = &mbedtls_rsa_info;
560 pk.pk_ctx = rsa;
561
562 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200563 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
564 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
566 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
567 else
568 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
569
570 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200571 {
572 /* Clean up in case pk_write failed halfway through. */
573 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200574 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200575 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200576
577 /* The mbedtls_pk_xxx functions write to the end of the buffer.
578 * Move the data to the beginning and erase remaining data
579 * at the original location. */
580 if( 2 * (size_t) ret <= data_size )
581 {
582 memcpy( data, data + data_size - ret, ret );
583 memset( data + data_size - ret, 0, ret );
584 }
585 else if( (size_t) ret < data_size )
586 {
587 memmove( data, data + data_size - ret, ret );
588 memset( data + ret, 0, data_size - ret );
589 }
590
591 *data_length = ret;
592 return( PSA_SUCCESS );
593#else
594 (void) type;
595 (void) rsa;
596 (void) data;
597 (void) data_size;
598 (void) data_length;
599 return( PSA_ERROR_NOT_SUPPORTED );
600#endif /* MBEDTLS_PK_WRITE_C */
601}
602
603/** Import an RSA key from import representation to a slot
604 *
605 * \param[in,out] slot The slot where to store the export representation to
606 * \param[in] data The buffer containing the import representation
607 * \param[in] data_length The amount of bytes in \p data
608 */
609static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
610 const uint8_t *data,
611 size_t data_length )
612{
613 psa_status_t status;
614 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200615 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200616
Steven Cooremana01795d2020-07-24 22:48:15 +0200617 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100618 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
619 data,
620 data_length,
621 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200622 if( status != PSA_SUCCESS )
623 goto exit;
624
625 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200626 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200627
Steven Cooreman75b74362020-07-28 14:30:13 +0200628 /* Re-export the data to PSA export format, such that we can store export
629 * representation in the key slot. Export representation in case of RSA is
630 * the smallest representation that's allowed as input, so a straight-up
631 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200632 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200633 if( output == NULL )
634 {
635 status = PSA_ERROR_INSUFFICIENT_MEMORY;
636 goto exit;
637 }
638
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100639 status = mbedtls_psa_rsa_export_key( slot->attr.type,
640 rsa,
641 output,
642 data_length,
643 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200644exit:
645 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200646 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200647 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200648
649 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000650 if( status != PSA_SUCCESS )
651 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200652 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000653 return( status );
654 }
655
Steven Cooremana01795d2020-07-24 22:48:15 +0200656 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100657 slot->key.data = output;
658 slot->key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000659
660 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200661}
John Durkop6ba40d12020-11-10 08:50:04 -0800662
663#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800664 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200665
John Durkop9814fa22020-11-04 12:28:15 -0800666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668/** Export an ECP key to export representation
669 *
670 * \param[in] type The type of key (public/private) to export
671 * \param[in] ecp The internal ECP representation from which to export
672 * \param[out] data The buffer to export to
673 * \param[in] data_size The length of the buffer to export to
674 * \param[out] data_length The amount of bytes written to \p data
675 */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100676static psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
677 mbedtls_ecp_keypair *ecp,
678 uint8_t *data,
679 size_t data_size,
680 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200681{
Steven Cooremanacda8342020-07-24 23:09:52 +0200682 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000683
Steven Cooremanacda8342020-07-24 23:09:52 +0200684 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200685 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200686 /* Check whether the public part is loaded */
687 if( mbedtls_ecp_is_zero( &ecp->Q ) )
688 {
689 /* Calculate the public key */
690 status = mbedtls_to_psa_error(
691 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100692 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200693 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200694 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200695 }
696
Steven Cooreman4fed4552020-08-03 14:46:03 +0200697 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200698 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
699 MBEDTLS_ECP_PF_UNCOMPRESSED,
700 data_length,
701 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200702 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200703 if( status != PSA_SUCCESS )
704 memset( data, 0, data_size );
705
Steven Cooreman29149862020-08-05 15:43:42 +0200706 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200708 else
709 {
Steven Cooreman29149862020-08-05 15:43:42 +0200710 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200711 return( PSA_ERROR_BUFFER_TOO_SMALL );
712
713 status = mbedtls_to_psa_error(
714 mbedtls_ecp_write_key( ecp,
715 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200716 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200717 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200718 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200719 else
720 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200721
722 return( status );
723 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200724}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200725
Steven Cooreman4fed4552020-08-03 14:46:03 +0200726/** Import an ECP key from import representation to a slot
727 *
728 * \param[in,out] slot The slot where to store the export representation to
729 * \param[in] data The buffer containing the import representation
730 * \param[in] data_length The amount of bytes in \p data
731 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200732static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
733 const uint8_t *data,
734 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735{
Steven Cooremanacda8342020-07-24 23:09:52 +0200736 psa_status_t status;
737 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200738 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100739
Steven Cooremanacda8342020-07-24 23:09:52 +0200740 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100741 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
742 data,
743 data_length,
744 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100745 if( status != PSA_SUCCESS )
746 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100747
Steven Cooremanacda8342020-07-24 23:09:52 +0200748 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200749 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200750 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200751 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200752
Steven Cooremanacda8342020-07-24 23:09:52 +0200753 /* Re-export the data to PSA export format. There is currently no support
754 * for other input formats then the export format, so this is a 1-1
755 * copy operation. */
756 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200757 if( output == NULL )
758 {
759 status = PSA_ERROR_INSUFFICIENT_MEMORY;
760 goto exit;
761 }
762
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100763 status = mbedtls_psa_ecp_export_key( slot->attr.type,
764 ecp,
765 output,
766 data_length,
767 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200769 /* Always free the PK object (will also free contained ECP context) */
770 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200771 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200772
773 /* Free the allocated buffer only on error. */
774 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100775 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200776 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200779
780 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100781 slot->key.data = output;
782 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200783
784 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100785}
John Durkop9814fa22020-11-04 12:28:15 -0800786#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
787 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100788
Gilles Peskineb46bef22019-07-30 21:32:04 +0200789/** Return the size of the key in the given slot, in bits.
790 *
791 * \param[in] slot A key slot.
792 *
793 * \return The key size in bits, read from the metadata in the slot.
794 */
795static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
796{
797 return( slot->attr.bits );
798}
799
Steven Cooreman75b74362020-07-28 14:30:13 +0200800/** Try to allocate a buffer to an empty key slot.
801 *
802 * \param[in,out] slot Key slot to attach buffer to.
803 * \param[in] buffer_length Requested size of the buffer.
804 *
805 * \retval #PSA_SUCCESS
806 * The buffer has been successfully allocated.
807 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
808 * Not enough memory was available for allocation.
809 * \retval #PSA_ERROR_ALREADY_EXISTS
810 * Trying to allocate a buffer to a non-empty key slot.
811 */
812static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
813 size_t buffer_length )
814{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100815 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200816 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200817
Ronald Cronea0f8a62020-11-25 17:52:23 +0100818 slot->key.data = mbedtls_calloc( 1, buffer_length );
819 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200821
Ronald Cronea0f8a62020-11-25 17:52:23 +0100822 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200823 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200824}
825
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200826psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
827 const uint8_t* data,
828 size_t data_length )
829{
830 psa_status_t status = psa_allocate_buffer_to_slot( slot,
831 data_length );
832 if( status != PSA_SUCCESS )
833 return( status );
834
Ronald Cronea0f8a62020-11-25 17:52:23 +0100835 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200836 return( PSA_SUCCESS );
837}
838
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200839/** Import key data into a slot.
840 *
841 * `slot->type` must have been set previously.
842 * This function assumes that the slot does not contain any key material yet.
843 * On failure, the slot content is unchanged.
844 *
845 * Persistent storage is not affected.
846 *
847 * \param[in,out] slot The key slot to import data into.
848 * Its `type` field must have previously been set to
849 * the desired key type.
850 * It must not contain any key material yet.
851 * \param[in] data Buffer containing the key material to parse and import.
852 * \param data_length Size of \p data in bytes.
853 *
854 * \retval #PSA_SUCCESS
855 * \retval #PSA_ERROR_INVALID_ARGUMENT
856 * \retval #PSA_ERROR_NOT_SUPPORTED
857 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
858 */
859static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
860 const uint8_t *data,
861 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100862{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200863 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +0200864 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200866 /* zero-length keys are never supported. */
867 if( data_length == 0 )
868 return( PSA_ERROR_NOT_SUPPORTED );
869
Gilles Peskine8e338702019-07-30 20:06:31 +0200870 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 {
Steven Cooreman04524762020-10-13 17:43:44 +0200872 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200873
Steven Cooreman75b74362020-07-28 14:30:13 +0200874 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
875 if( data_length > SIZE_MAX / 8 )
876 return( PSA_ERROR_NOT_SUPPORTED );
877
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200878 /* Enforce a size limit, and in particular ensure that the bit
879 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200880 if( bit_size > PSA_MAX_KEY_BITS )
881 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200882
883 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200884 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200885 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200886
887 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200888 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +0200889 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200890 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200891
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200892 /* Write the actual key size to the slot.
893 * psa_start_key_creation() wrote the size declared by the
894 * caller, which may be 0 (meaning unspecified) or wrong. */
895 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +0100896
897 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200899 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200900 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200901 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200902 psa_key_attributes_t attributes = {
903 .core = slot->attr
904 };
Ronald Cron83282872020-11-22 14:02:39 +0100905
906 status = psa_allocate_buffer_to_slot( slot, data_length );
907 if( status != PSA_SUCCESS )
908 return( status );
909
910 bit_size = slot->attr.bits;
911 status = psa_driver_wrapper_import_key( &attributes,
912 data, data_length,
913 slot->key.data,
914 slot->key.bytes,
915 &slot->key.bytes,
916 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200917 if( status == PSA_SUCCESS )
918 {
Ronald Cron83282872020-11-22 14:02:39 +0100919 if( slot->attr.bits == 0 )
920 slot->attr.bits = (psa_key_bits_t) bit_size;
921 else if( bit_size != slot->attr.bits )
922 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200923
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200924 return( PSA_SUCCESS );
925 }
Ronald Cron83282872020-11-22 14:02:39 +0100926 else
927 {
928 if( status != PSA_ERROR_NOT_SUPPORTED )
929 return( status );
930 }
931
932 mbedtls_platform_zeroize( slot->key.data, data_length );
933 mbedtls_free( slot->key.data );
934 slot->key.data = NULL;
935 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200936
937 /* Key format is not supported by any accelerator, try software fallback
938 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800939#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
940 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200941 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
942 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100943 return( psa_import_ecp_key( slot, data, data_length ) );
944 }
John Durkop9814fa22020-11-04 12:28:15 -0800945#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
946 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700947#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
948 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +0100949 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200950 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100951 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200952 }
John Durkop9814fa22020-11-04 12:28:15 -0800953#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
954 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +0100955
956 /* Fell through the fallback as well, so have nothing else to try. */
957 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 }
959 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100960 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200961 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100963 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100964}
965
Gilles Peskinef603c712019-01-19 13:40:11 +0100966/** Calculate the intersection of two algorithm usage policies.
967 *
968 * Return 0 (which allows no operation) on incompatibility.
969 */
970static psa_algorithm_t psa_key_policy_algorithm_intersection(
971 psa_algorithm_t alg1,
972 psa_algorithm_t alg2 )
973{
Gilles Peskine549ea862019-05-22 11:45:59 +0200974 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100975 if( alg1 == alg2 )
976 return( alg1 );
977 /* If the policies are from the same hash-and-sign family, check
978 * if one is a wildcard. If so the other has the specific algorithm. */
979 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
980 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
981 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
982 {
983 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
984 return( alg2 );
985 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
986 return( alg1 );
987 }
988 /* If the policies are incompatible, allow nothing. */
989 return( 0 );
990}
991
Gilles Peskined6f371b2019-05-10 19:33:38 +0200992static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
993 psa_algorithm_t requested_alg )
994{
Gilles Peskine549ea862019-05-22 11:45:59 +0200995 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200996 if( requested_alg == policy_alg )
997 return( 1 );
998 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200999 * and requested_alg is the same hash-and-sign family with any hash,
1000 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001001 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1002 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1003 {
1004 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1005 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1006 }
Steven Cooremance48e852020-10-05 16:02:45 +02001007 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001008 * a key derivation with that key agreement should also be allowed. This
1009 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001010 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1011 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1012 {
1013 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1014 policy_alg );
1015 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001016 /* If it isn't permitted, it's forbidden. */
1017 return( 0 );
1018}
1019
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020/** Test whether a policy permits an algorithm.
1021 *
1022 * The caller must test usage flags separately.
1023 */
1024static int psa_key_policy_permits( const psa_key_policy_t *policy,
1025 psa_algorithm_t alg )
1026{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001027 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1028 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001029}
1030
Gilles Peskinef603c712019-01-19 13:40:11 +01001031/** Restrict a key policy based on a constraint.
1032 *
1033 * \param[in,out] policy The policy to restrict.
1034 * \param[in] constraint The policy constraint to apply.
1035 *
1036 * \retval #PSA_SUCCESS
1037 * \c *policy contains the intersection of the original value of
1038 * \c *policy and \c *constraint.
1039 * \retval #PSA_ERROR_INVALID_ARGUMENT
1040 * \c *policy and \c *constraint are incompatible.
1041 * \c *policy is unchanged.
1042 */
1043static psa_status_t psa_restrict_key_policy(
1044 psa_key_policy_t *policy,
1045 const psa_key_policy_t *constraint )
1046{
1047 psa_algorithm_t intersection_alg =
1048 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001049 psa_algorithm_t intersection_alg2 =
1050 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001051 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1052 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001053 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1054 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 policy->usage &= constraint->usage;
1056 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001057 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001058 return( PSA_SUCCESS );
1059}
1060
Ronald Cron5c522922020-11-14 16:35:34 +01001061/** Get the description of a key given its identifier and policy constraints
1062 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063 *
Ronald Cron5c522922020-11-14 16:35:34 +01001064 * The key must have allow all the usage flags set in \p usage. If \p alg is
1065 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001066 *
Ronald Cron5c522922020-11-14 16:35:34 +01001067 * In case of a persistent key, the function loads the description of the key
1068 * into a key slot if not already done.
1069 *
1070 * On success, the returned key slot is locked. It is the responsibility of
1071 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001072 */
Ronald Cron5c522922020-11-14 16:35:34 +01001073static psa_status_t psa_get_and_lock_key_slot_with_policy(
1074 mbedtls_svc_key_id_t key,
1075 psa_key_slot_t **p_slot,
1076 psa_key_usage_t usage,
1077 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001078{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1080 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001081
Ronald Cron5c522922020-11-14 16:35:34 +01001082 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001083 if( status != PSA_SUCCESS )
1084 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001085 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001086
1087 /* Enforce that usage policy for the key slot contains all the flags
1088 * required by the usage parameter. There is one exception: public
1089 * keys can always be exported, so we treat public key objects as
1090 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001091 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001092 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001093
1094 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001095 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001097
1098 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001099 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001100 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001101
Darryl Green06fd18d2018-07-16 11:21:11 +01001102 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001103
1104error:
1105 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001106 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001107
1108 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001109}
Darryl Green940d72c2018-07-13 13:18:51 +01001110
Ronald Cron5c522922020-11-14 16:35:34 +01001111/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001112 *
1113 * A transparent key is a key for which the key material is directly
1114 * available, as opposed to a key in a secure element.
1115 *
Ronald Cron5c522922020-11-14 16:35:34 +01001116 * This is a temporary function to use instead of
1117 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1118 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001119 *
Ronald Cron5c522922020-11-14 16:35:34 +01001120 * On success, the returned key slot is locked. It is the responsibility of the
1121 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 */
1123#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001124static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1125 mbedtls_svc_key_id_t key,
1126 psa_key_slot_t **p_slot,
1127 psa_key_usage_t usage,
1128 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001129{
Ronald Cron5c522922020-11-14 16:35:34 +01001130 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1131 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132 if( status != PSA_SUCCESS )
1133 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001134
Gilles Peskineadad8132019-07-25 11:31:23 +02001135 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136 {
Ronald Cron5c522922020-11-14 16:35:34 +01001137 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001138 *p_slot = NULL;
1139 return( PSA_ERROR_NOT_SUPPORTED );
1140 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001141
Gilles Peskine28f8f302019-07-24 13:30:31 +02001142 return( PSA_SUCCESS );
1143}
1144#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1145/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001146#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1147 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1149
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001150/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001151static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001152{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001153 /* Data pointer will always be either a valid pointer or NULL in an
1154 * initialized slot, so we can just free it. */
1155 if( slot->key.data != NULL )
1156 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1157
1158 mbedtls_free( slot->key.data );
1159 slot->key.data = NULL;
1160 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001161
1162 return( PSA_SUCCESS );
1163}
1164
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001165/** Completely wipe a slot in memory, including its policy.
1166 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001167psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001168{
1169 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001170
1171 /*
1172 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001173 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001174 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1175 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001176 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001177 */
Ronald Cron5c522922020-11-14 16:35:34 +01001178 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001179 {
1180#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001181 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001182#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001183 status = PSA_ERROR_CORRUPTION_DETECTED;
1184 }
1185
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001186 /* Multipart operations may still be using the key. This is safe
1187 * because all multipart operation objects are independent from
1188 * the key slot: if they need to access the key after the setup
1189 * phase, they have a copy of the key. Note that this means that
1190 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001191 /* At this point, key material and other type-specific content has
1192 * been wiped. Clear remaining metadata. We can call memset and not
1193 * zeroize because the metadata is not particularly sensitive. */
1194 memset( slot, 0, sizeof( *slot ) );
1195 return( status );
1196}
1197
Ronald Croncf56a0a2020-08-04 09:51:30 +02001198psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 psa_status_t status; /* status of the last operation */
1202 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1204 psa_se_drv_table_entry_t *driver;
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206
Ronald Croncf56a0a2020-08-04 09:51:30 +02001207 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001208 return( PSA_SUCCESS );
1209
Ronald Cronf2911112020-10-29 17:51:10 +01001210 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001211 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001212 * key, this will load the key description from persistent memory if not
1213 * done yet. We cannot avoid this loading as without it we don't know if
1214 * the key is operated by an SE or not and this information is needed by
1215 * the current implementation.
1216 */
Ronald Cron5c522922020-11-14 16:35:34 +01001217 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001218 if( status != PSA_SUCCESS )
1219 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001220
Ronald Cronf2911112020-10-29 17:51:10 +01001221 /*
1222 * If the key slot containing the key description is under access by the
1223 * library (apart from the present access), the key cannot be destroyed
1224 * yet. For the time being, just return in error. Eventually (to be
1225 * implemented), the key should be destroyed when all accesses have
1226 * stopped.
1227 */
Ronald Cron5c522922020-11-14 16:35:34 +01001228 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001229 {
Ronald Cron5c522922020-11-14 16:35:34 +01001230 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001231 return( PSA_ERROR_GENERIC_ERROR );
1232 }
1233
Gilles Peskine354f7672019-07-12 23:46:38 +02001234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001235 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001236 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001237 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001238 /* For a key in a secure element, we need to do three things:
1239 * remove the key file in internal storage, destroy the
1240 * key inside the secure element, and update the driver's
1241 * persistent data. Start a transaction that will encompass these
1242 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001243 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001245 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001246 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001247 status = psa_crypto_save_transaction( );
1248 if( status != PSA_SUCCESS )
1249 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001250 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001251 /* We should still try to destroy the key in the secure
1252 * element and the key metadata in storage. This is especially
1253 * important if the error is that the storage is full.
1254 * But how to do it exactly without risking an inconsistent
1255 * state after a reset?
1256 * https://github.com/ARMmbed/mbed-crypto/issues/215
1257 */
1258 overall_status = status;
1259 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001260 }
1261
Ronald Cronea0f8a62020-11-25 17:52:23 +01001262 status = psa_destroy_se_key( driver,
1263 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001264 if( overall_status == PSA_SUCCESS )
1265 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001266 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1268
Darryl Greend49a4992018-06-18 17:27:26 +01001269#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001270 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001271 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001272 status = psa_destroy_persistent_key( slot->attr.id );
1273 if( overall_status == PSA_SUCCESS )
1274 overall_status = status;
1275
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001276 /* TODO: other slots may have a copy of the same key. We should
1277 * invalidate them.
1278 * https://github.com/ARMmbed/mbed-crypto/issues/214
1279 */
Darryl Greend49a4992018-06-18 17:27:26 +01001280 }
1281#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001282
Gilles Peskinefc762652019-07-22 19:30:34 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1284 if( driver != NULL )
1285 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001286 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287 if( overall_status == PSA_SUCCESS )
1288 overall_status = status;
1289 status = psa_crypto_stop_transaction( );
1290 if( overall_status == PSA_SUCCESS )
1291 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001292 }
1293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1294
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1296exit:
1297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001298 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1300 if( overall_status == PSA_SUCCESS )
1301 overall_status = status;
1302 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001303}
1304
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001305void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001306{
Gilles Peskineb699f072019-04-26 16:06:02 +02001307 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001308 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001309}
1310
Gilles Peskineb699f072019-04-26 16:06:02 +02001311psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1312 psa_key_type_t type,
1313 const uint8_t *data,
1314 size_t data_length )
1315{
1316 uint8_t *copy = NULL;
1317
1318 if( data_length != 0 )
1319 {
1320 copy = mbedtls_calloc( 1, data_length );
1321 if( copy == NULL )
1322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1323 memcpy( copy, data, data_length );
1324 }
1325 /* After this point, this function is guaranteed to succeed, so it
1326 * can start modifying `*attributes`. */
1327
1328 if( attributes->domain_parameters != NULL )
1329 {
1330 mbedtls_free( attributes->domain_parameters );
1331 attributes->domain_parameters = NULL;
1332 attributes->domain_parameters_size = 0;
1333 }
1334
1335 attributes->domain_parameters = copy;
1336 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001337 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001338 return( PSA_SUCCESS );
1339}
1340
1341psa_status_t psa_get_key_domain_parameters(
1342 const psa_key_attributes_t *attributes,
1343 uint8_t *data, size_t data_size, size_t *data_length )
1344{
1345 if( attributes->domain_parameters_size > data_size )
1346 return( PSA_ERROR_BUFFER_TOO_SMALL );
1347 *data_length = attributes->domain_parameters_size;
1348 if( attributes->domain_parameters_size != 0 )
1349 memcpy( data, attributes->domain_parameters,
1350 attributes->domain_parameters_size );
1351 return( PSA_SUCCESS );
1352}
1353
John Durkop07cc04a2020-11-16 22:08:34 -08001354#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001355 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001356static psa_status_t psa_get_rsa_public_exponent(
1357 const mbedtls_rsa_context *rsa,
1358 psa_key_attributes_t *attributes )
1359{
1360 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001362 uint8_t *buffer = NULL;
1363 size_t buflen;
1364 mbedtls_mpi_init( &mpi );
1365
1366 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1367 if( ret != 0 )
1368 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001369 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1370 {
1371 /* It's the default value, which is reported as an empty string,
1372 * so there's nothing to do. */
1373 goto exit;
1374 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001375
1376 buflen = mbedtls_mpi_size( &mpi );
1377 buffer = mbedtls_calloc( 1, buflen );
1378 if( buffer == NULL )
1379 {
1380 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1381 goto exit;
1382 }
1383 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1384 if( ret != 0 )
1385 goto exit;
1386 attributes->domain_parameters = buffer;
1387 attributes->domain_parameters_size = buflen;
1388
1389exit:
1390 mbedtls_mpi_free( &mpi );
1391 if( ret != 0 )
1392 mbedtls_free( buffer );
1393 return( mbedtls_to_psa_error( ret ) );
1394}
John Durkop07cc04a2020-11-16 22:08:34 -08001395#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001396 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001397
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001398/** Retrieve all the publicly-accessible attributes of a key.
1399 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001400psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001404 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001405 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001407 psa_reset_key_attributes( attributes );
1408
Ronald Cron5c522922020-11-14 16:35:34 +01001409 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001410 if( status != PSA_SUCCESS )
1411 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001412
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001413 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001414 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1415 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1416
1417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1418 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001419 psa_set_key_slot_number( attributes,
1420 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001422
Gilles Peskine8e338702019-07-30 20:06:31 +02001423 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001424 {
John Durkop07cc04a2020-11-16 22:08:34 -08001425#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001426 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001427 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001428 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001430 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001431 * is not yet implemented.
1432 * https://github.com/ARMmbed/mbed-crypto/issues/216
1433 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001435 break;
1436#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001437 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001438 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001439
Ronald Cron90857082020-11-25 14:58:33 +01001440 status = mbedtls_psa_rsa_load_representation(
1441 slot->attr.type,
1442 slot->key.data,
1443 slot->key.bytes,
1444 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001445 if( status != PSA_SUCCESS )
1446 break;
1447
Steven Cooremana2371e52020-07-28 14:30:39 +02001448 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001449 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001450 mbedtls_rsa_free( rsa );
1451 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001452 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001453 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001454#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001455 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001456 default:
1457 /* Nothing else to do. */
1458 break;
1459 }
1460
1461 if( status != PSA_SUCCESS )
1462 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001463
Ronald Cron5c522922020-11-14 16:35:34 +01001464 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001465
Ronald Cron5c522922020-11-14 16:35:34 +01001466 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467}
1468
Gilles Peskinec8000c02019-08-02 20:15:51 +02001469#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1470psa_status_t psa_get_key_slot_number(
1471 const psa_key_attributes_t *attributes,
1472 psa_key_slot_number_t *slot_number )
1473{
1474 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1475 {
1476 *slot_number = attributes->slot_number;
1477 return( PSA_SUCCESS );
1478 }
1479 else
1480 return( PSA_ERROR_INVALID_ARGUMENT );
1481}
1482#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1483
Steven Cooremana01795d2020-07-24 22:48:15 +02001484static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1485 uint8_t *data,
1486 size_t data_size,
1487 size_t *data_length )
1488{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001489 if( slot->key.bytes > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001490 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Cronea0f8a62020-11-25 17:52:23 +01001491 memcpy( data, slot->key.data, slot->key.bytes );
1492 memset( data + slot->key.bytes, 0,
1493 data_size - slot->key.bytes );
1494 *data_length = slot->key.bytes;
Steven Cooremana01795d2020-07-24 22:48:15 +02001495 return( PSA_SUCCESS );
1496}
1497
Gilles Peskinef603c712019-01-19 13:40:11 +01001498static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1499 uint8_t *data,
1500 size_t data_size,
1501 size_t *data_length,
1502 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503{
Gilles Peskine5d309672019-07-12 23:47:28 +02001504#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1505 const psa_drv_se_t *drv;
1506 psa_drv_se_context_t *drv_context;
1507#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1508
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001509 *data_length = 0;
1510
Gilles Peskine8e338702019-07-30 20:06:31 +02001511 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001512 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001513
Gilles Peskinef9168942019-09-12 19:20:29 +02001514 /* Reject a zero-length output buffer now, since this can never be a
1515 * valid key representation. This way we know that data must be a valid
1516 * pointer and we can do things like memset(data, ..., data_size). */
1517 if( data_size == 0 )
1518 return( PSA_ERROR_BUFFER_TOO_SMALL );
1519
Gilles Peskine5d309672019-07-12 23:47:28 +02001520#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001521 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001522 {
1523 psa_drv_se_export_key_t method;
1524 if( drv->key_management == NULL )
1525 return( PSA_ERROR_NOT_SUPPORTED );
1526 method = ( export_public_key ?
1527 drv->key_management->p_export_public :
1528 drv->key_management->p_export );
1529 if( method == NULL )
1530 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001531 return( method( drv_context,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001532 psa_key_slot_get_slot_number( slot ),
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001533 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001534 }
1535#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1536
Gilles Peskine8e338702019-07-30 20:06:31 +02001537 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001539 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001540 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001541 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1542 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001543 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001544 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001545 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001546 /* Exporting public -> public */
1547 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1548 }
1549 else if( !export_public_key )
1550 {
1551 /* Exporting private -> private */
1552 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1553 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001554
Steven Cooreman560c28a2020-07-24 23:20:24 +02001555 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001556 * so conversion is needed. Try the accelerators first. */
Ronald Cron84cc9942020-11-25 14:30:05 +01001557 psa_key_attributes_t attributes = {
1558 .core = slot->attr
1559 };
1560 psa_status_t status = psa_driver_wrapper_export_public_key(
1561 &attributes, slot->key.data, slot->key.bytes,
1562 data, data_size, data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +02001563
1564 if( status != PSA_ERROR_NOT_SUPPORTED ||
1565 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1566 return( status );
1567
Steven Cooreman560c28a2020-07-24 23:20:24 +02001568 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1569 {
John Durkop0e005192020-10-31 22:06:54 -07001570#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1571 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001572 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01001573 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
1574 slot->key.data,
1575 slot->key.bytes,
1576 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001577 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001578 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001579
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001580 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1581 rsa,
1582 data,
1583 data_size,
1584 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001585
Steven Cooremana2371e52020-07-28 14:30:39 +02001586 mbedtls_rsa_free( rsa );
1587 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001588
Steven Cooreman560c28a2020-07-24 23:20:24 +02001589 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001590#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001591 /* We don't know how to convert a private RSA key to public. */
1592 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001593#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1594 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001595 }
1596 else
Moran Pekera998bc62018-04-16 18:16:20 +03001597 {
John Durkop6ba40d12020-11-10 08:50:04 -08001598#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1599 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001600 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01001601 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
1602 slot->key.data,
1603 slot->key.bytes,
1604 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001605 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001606 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001607
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001608 status = mbedtls_psa_ecp_export_key(
1609 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1610 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) ),
1611 ecp,
1612 data,
1613 data_size,
1614 data_length );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001615
Steven Cooremana2371e52020-07-28 14:30:39 +02001616 mbedtls_ecp_keypair_free( ecp );
1617 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001618 return( status );
1619#else
1620 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001621 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001622#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1623 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001624 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001626 else
1627 {
1628 /* This shouldn't happen in the reference implementation, but
1629 it is valid for a special-purpose implementation to omit
1630 support for exporting certain key types. */
1631 return( PSA_ERROR_NOT_SUPPORTED );
1632 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633}
1634
Ronald Croncf56a0a2020-08-04 09:51:30 +02001635psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001636 uint8_t *data,
1637 size_t data_size,
1638 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001639{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001641 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001642 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001643
1644 /* Set the key to empty now, so that even when there are errors, we always
1645 * set data_length to a value between 0 and data_size. On error, setting
1646 * the key to empty is a good choice because an empty key representation is
1647 * unlikely to be accepted anywhere. */
1648 *data_length = 0;
1649
1650 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001651 * which don't require any flag, but
1652 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1653 */
1654 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1655 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001656 if( status != PSA_SUCCESS )
1657 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001658
1659 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001660 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001661
Ronald Cron5c522922020-11-14 16:35:34 +01001662 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001663}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001664
Ronald Croncf56a0a2020-08-04 09:51:30 +02001665psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001666 uint8_t *data,
1667 size_t data_size,
1668 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001669{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001671 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001672 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001673
1674 /* Set the key to empty now, so that even when there are errors, we always
1675 * set data_length to a value between 0 and data_size. On error, setting
1676 * the key to empty is a good choice because an empty key representation is
1677 * unlikely to be accepted anywhere. */
1678 *data_length = 0;
1679
1680 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001681 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001682 if( status != PSA_SUCCESS )
1683 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001684
1685 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001686 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001687
Ronald Cron5c522922020-11-14 16:35:34 +01001688 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001689}
1690
Gilles Peskine91e8c332019-08-02 19:19:39 +02001691#if defined(static_assert)
1692static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1693 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001694static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001695 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001696static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001697 "One or more key attribute flag is listed as both internal-only and external-only" );
1698#endif
1699
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001700/** Validate that a key policy is internally well-formed.
1701 *
1702 * This function only rejects invalid policies. It does not validate the
1703 * consistency of the policy with respect to other attributes of the key
1704 * such as the key type.
1705 */
1706static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001707{
1708 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001709 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 PSA_KEY_USAGE_ENCRYPT |
1711 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001712 PSA_KEY_USAGE_SIGN_HASH |
1713 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1715 return( PSA_ERROR_INVALID_ARGUMENT );
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 return( PSA_SUCCESS );
1718}
1719
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001720/** Validate the internal consistency of key attributes.
1721 *
1722 * This function only rejects invalid attribute values. If does not
1723 * validate the consistency of the attributes with any key data that may
1724 * be involved in the creation of the key.
1725 *
1726 * Call this function early in the key creation process.
1727 *
1728 * \param[in] attributes Key attributes for the new key.
1729 * \param[out] p_drv On any return, the driver for the key, if any.
1730 * NULL for a transparent key.
1731 *
1732 */
1733static psa_status_t psa_validate_key_attributes(
1734 const psa_key_attributes_t *attributes,
1735 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001736{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001737 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001738 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001739 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001740
Ronald Cron54b90082020-10-29 15:26:43 +01001741 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001742 if( status != PSA_SUCCESS )
1743 return( status );
1744
Ronald Crond2ed4812020-07-17 16:11:30 +02001745 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001746 if( status != PSA_SUCCESS )
1747 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748
Ronald Cron65f38a32020-10-23 17:11:13 +02001749 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1750 {
1751 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1752 return( PSA_ERROR_INVALID_ARGUMENT );
1753 }
1754 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001755 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001756 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001757 if( status != PSA_SUCCESS )
1758 return( status );
1759 }
1760
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001761 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001762 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763 return( status );
1764
1765 /* Refuse to create overly large keys.
1766 * Note that this doesn't trigger on import if the attributes don't
1767 * explicitly specify a size (so psa_get_key_bits returns 0), so
1768 * psa_import_key() needs its own checks. */
1769 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1770 return( PSA_ERROR_NOT_SUPPORTED );
1771
Gilles Peskine91e8c332019-08-02 19:19:39 +02001772 /* Reject invalid flags. These should not be reachable through the API. */
1773 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1774 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1775 return( PSA_ERROR_INVALID_ARGUMENT );
1776
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001777 return( PSA_SUCCESS );
1778}
1779
Gilles Peskine4747d192019-04-17 15:05:45 +02001780/** Prepare a key slot to receive key material.
1781 *
1782 * This function allocates a key slot and sets its metadata.
1783 *
1784 * If this function fails, call psa_fail_key_creation().
1785 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001786 * This function is intended to be used as follows:
1787 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001788 * it with the specified attributes, and in case of a volatile key assign it
1789 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001790 * -# Populate the slot with the key material.
1791 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1792 * In case of failure at any step, stop the sequence and call
1793 * psa_fail_key_creation().
1794 *
Ronald Cron5c522922020-11-14 16:35:34 +01001795 * On success, the key slot is locked. It is the responsibility of the caller
1796 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001797 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001798 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001799 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001800 * \param[out] p_slot On success, a pointer to the prepared slot.
1801 * \param[out] p_drv On any return, the driver for the key, if any.
1802 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001803 *
1804 * \retval #PSA_SUCCESS
1805 * The key slot is ready to receive key material.
1806 * \return If this function fails, the key slot is an invalid state.
1807 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 */
1809static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001810 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001812 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001813 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001814{
1815 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001816 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 psa_key_slot_t *slot;
1818
Gilles Peskinedf179142019-07-15 22:02:14 +02001819 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001820 *p_drv = NULL;
1821
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001822 status = psa_validate_key_attributes( attributes, p_drv );
1823 if( status != PSA_SUCCESS )
1824 return( status );
1825
Ronald Cronc4d1b512020-07-31 11:26:37 +02001826 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001827 if( status != PSA_SUCCESS )
1828 return( status );
1829 slot = *p_slot;
1830
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001831 /* We're storing the declared bit-size of the key. It's up to each
1832 * creation mechanism to verify that this information is correct.
1833 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001834 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001835 * is optional (import, copy). In case of a volatile key, assign it the
1836 * volatile key identifier associated to the slot returned to contain its
1837 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001838
1839 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001840 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1841 {
1842#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1843 slot->attr.id = volatile_key_id;
1844#else
1845 slot->attr.id.key_id = volatile_key_id;
1846#endif
1847 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001848
Gilles Peskine91e8c332019-08-02 19:19:39 +02001849 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001850 * external-only flags, query `attributes`. Thanks to the check
1851 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001852 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001853 * may have set. */
1854 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001855
Gilles Peskinecbaff462019-07-12 23:46:04 +02001856#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001857 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001858 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001859 * create the key file in internal storage, create the
1860 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001861 * persistent data. This is done by starting a transaction that will
1862 * encompass these three actions.
1863 * For registering a volatile key, we just need to find an appropriate
1864 * slot number inside the SE. Since the key is designated volatile, creating
1865 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001866 /* The first thing to do is to find a slot number for the new key.
1867 * We save the slot number in persistent storage as part of the
1868 * transaction data. It will be needed to recover if the power
1869 * fails during the key creation process, to clean up on the secure
1870 * element side after restarting. Obtaining a slot number from the
1871 * secure element driver updates its persistent state, but we do not yet
1872 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001873 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001874 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001875 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001876 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001877 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001878 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001879 if( status != PSA_SUCCESS )
1880 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001881
1882 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001883 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001884 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1885 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001886 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001887 psa_crypto_transaction.key.id = slot->attr.id;
1888 status = psa_crypto_save_transaction( );
1889 if( status != PSA_SUCCESS )
1890 {
1891 (void) psa_crypto_stop_transaction( );
1892 return( status );
1893 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001894 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001895
1896 status = psa_copy_key_material_into_slot(
1897 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001898 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001899
1900 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1901 {
1902 /* Key registration only makes sense with a secure element. */
1903 return( PSA_ERROR_INVALID_ARGUMENT );
1904 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001905#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1906
Ronald Cronc4d1b512020-07-31 11:26:37 +02001907 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001908}
Gilles Peskine4747d192019-04-17 15:05:45 +02001909
1910/** Finalize the creation of a key once its key material has been set.
1911 *
1912 * This entails writing the key to persistent storage.
1913 *
1914 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001915 * See the documentation of psa_start_key_creation() for the intended use
1916 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001917 *
Ronald Cron5c522922020-11-14 16:35:34 +01001918 * If the finalization succeeds, the function unlocks the key slot (it was
1919 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1920 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001921 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001922 * \param[in,out] slot Pointer to the slot with key material.
1923 * \param[in] driver The secure element driver for the key,
1924 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001925 * \param[out] key On success, identifier of the key. Note that the
1926 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001927 *
1928 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001929 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001930 * \return If this function fails, the key slot is an invalid state.
1931 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001933static psa_status_t psa_finish_key_creation(
1934 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001935 psa_se_drv_table_entry_t *driver,
1936 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001937{
1938 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001939 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001940 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001941
1942#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001943 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001944 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001945#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1946 if( driver != NULL )
1947 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001948 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001949 psa_key_slot_number_t slot_number =
1950 psa_key_slot_get_slot_number( slot ) ;
1951
Gilles Peskineb46bef22019-07-30 21:32:04 +02001952#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001953 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001954 sizeof( data.slot_number ),
1955 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001956#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001957 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001958 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001959 (uint8_t*) &data,
1960 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001961 }
1962 else
1963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1964 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001965 /* Key material is saved in export representation in the slot, so
1966 * just pass the slot buffer for storage. */
1967 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001968 slot->key.data,
1969 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001970 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001972#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1973
Gilles Peskinecbaff462019-07-12 23:46:04 +02001974#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001975 /* Finish the transaction for a key creation. This does not
1976 * happen when registering an existing key. Detect this case
1977 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001978 * creation of a persistent key in a secure element requires a transaction,
1979 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001980 if( driver != NULL &&
1981 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001982 {
1983 status = psa_save_se_persistent_data( driver );
1984 if( status != PSA_SUCCESS )
1985 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001986 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001987 return( status );
1988 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001989 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001990 }
1991#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1992
Ronald Cron50972942020-11-14 11:28:25 +01001993 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001994 {
1995 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001996 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001997 if( status != PSA_SUCCESS )
1998 *key = MBEDTLS_SVC_KEY_ID_INIT;
1999 }
Ronald Cron50972942020-11-14 11:28:25 +01002000
Gilles Peskine4747d192019-04-17 15:05:45 +02002001 return( status );
2002}
2003
2004/** Abort the creation of a key.
2005 *
2006 * You may call this function after calling psa_start_key_creation(),
2007 * or after psa_finish_key_creation() fails. In other circumstances, this
2008 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002009 * See the documentation of psa_start_key_creation() for the intended use
2010 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002011 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002012 * \param[in,out] slot Pointer to the slot with key material.
2013 * \param[in] driver The secure element driver for the key,
2014 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002015 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002016static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002017 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002018{
Gilles Peskine011e4282019-06-26 18:34:38 +02002019 (void) driver;
2020
Gilles Peskine4747d192019-04-17 15:05:45 +02002021 if( slot == NULL )
2022 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002023
2024#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002025 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002026 * element, and the failure happened later (when saving metadata
2027 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002028 * element.
2029 * https://github.com/ARMmbed/mbed-crypto/issues/217
2030 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002031
Gilles Peskined7729582019-08-05 15:55:54 +02002032 /* Abort the ongoing transaction if any (there may not be one if
2033 * the creation process failed before starting one, or if the
2034 * key creation is a registration of a key in a secure element).
2035 * Earlier functions must already have done what it takes to undo any
2036 * partial creation. All that's left is to update the transaction data
2037 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002038 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002039#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2040
Gilles Peskine4747d192019-04-17 15:05:45 +02002041 psa_wipe_key_slot( slot );
2042}
2043
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002044/** Validate optional attributes during key creation.
2045 *
2046 * Some key attributes are optional during key creation. If they are
2047 * specified in the attributes structure, check that they are consistent
2048 * with the data in the slot.
2049 *
2050 * This function should be called near the end of key creation, after
2051 * the slot in memory is fully populated but before saving persistent data.
2052 */
2053static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002054 const psa_key_slot_t *slot,
2055 const psa_key_attributes_t *attributes )
2056{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002057 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002059 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 return( PSA_ERROR_INVALID_ARGUMENT );
2061 }
2062
2063 if( attributes->domain_parameters_size != 0 )
2064 {
John Durkop0e005192020-10-31 22:06:54 -07002065#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2066 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002067 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002069 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002070 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002072
Ronald Cron90857082020-11-25 14:58:33 +01002073 psa_status_t status = mbedtls_psa_rsa_load_representation(
2074 slot->attr.type,
2075 slot->key.data,
2076 slot->key.bytes,
2077 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002078 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002079 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002080
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002081 mbedtls_mpi_init( &actual );
2082 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002083 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002084 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002085 mbedtls_rsa_free( rsa );
2086 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087 if( ret != 0 )
2088 goto rsa_exit;
2089 ret = mbedtls_mpi_read_binary( &required,
2090 attributes->domain_parameters,
2091 attributes->domain_parameters_size );
2092 if( ret != 0 )
2093 goto rsa_exit;
2094 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2095 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2096 rsa_exit:
2097 mbedtls_mpi_free( &actual );
2098 mbedtls_mpi_free( &required );
2099 if( ret != 0)
2100 return( mbedtls_to_psa_error( ret ) );
2101 }
2102 else
John Durkop9814fa22020-11-04 12:28:15 -08002103#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2104 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105 {
2106 return( PSA_ERROR_INVALID_ARGUMENT );
2107 }
2108 }
2109
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002110 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002112 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 return( PSA_ERROR_INVALID_ARGUMENT );
2114 }
2115
2116 return( PSA_SUCCESS );
2117}
2118
Gilles Peskine4747d192019-04-17 15:05:45 +02002119psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002120 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002121 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002122 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002123{
2124 psa_status_t status;
2125 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002126 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002127
Ronald Cron81709fc2020-11-14 12:10:32 +01002128 *key = MBEDTLS_SVC_KEY_ID_INIT;
2129
Gilles Peskine0f84d622019-09-12 19:03:13 +02002130 /* Reject zero-length symmetric keys (including raw data key objects).
2131 * This also rejects any key which might be encoded as an empty string,
2132 * which is never valid. */
2133 if( data_length == 0 )
2134 return( PSA_ERROR_INVALID_ARGUMENT );
2135
Gilles Peskinedf179142019-07-15 22:02:14 +02002136 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002137 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002138 if( status != PSA_SUCCESS )
2139 goto exit;
2140
Gilles Peskine5d309672019-07-12 23:47:28 +02002141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2142 if( driver != NULL )
2143 {
2144 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002145 /* The driver should set the number of key bits, however in
2146 * case it doesn't, we initialize bits to an invalid value. */
2147 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002148 if( drv->key_management == NULL ||
2149 drv->key_management->p_import == NULL )
2150 {
2151 status = PSA_ERROR_NOT_SUPPORTED;
2152 goto exit;
2153 }
2154 status = drv->key_management->p_import(
2155 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002156 psa_key_slot_get_slot_number( slot ),
2157 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002158 if( status != PSA_SUCCESS )
2159 goto exit;
2160 if( bits > PSA_MAX_KEY_BITS )
2161 {
2162 status = PSA_ERROR_NOT_SUPPORTED;
2163 goto exit;
2164 }
2165 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002166 }
2167 else
2168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002169 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2170 {
2171 /* Importing a key with external lifetime through the driver wrapper
2172 * interface is not yet supported. Return as if this was an invalid
2173 * lifetime. */
2174 status = PSA_ERROR_INVALID_ARGUMENT;
2175 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002176 }
2177 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002178 {
2179 status = psa_import_key_into_slot( slot, data, data_length );
2180 if( status != PSA_SUCCESS )
2181 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002182 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002183 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002184 if( status != PSA_SUCCESS )
2185 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002186
Ronald Cron81709fc2020-11-14 12:10:32 +01002187 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002188exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002189 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002190 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskine4747d192019-04-17 15:05:45 +02002192 return( status );
2193}
2194
Gilles Peskined7729582019-08-05 15:55:54 +02002195#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2196psa_status_t mbedtls_psa_register_se_key(
2197 const psa_key_attributes_t *attributes )
2198{
2199 psa_status_t status;
2200 psa_key_slot_t *slot = NULL;
2201 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002203
2204 /* Leaving attributes unspecified is not currently supported.
2205 * It could make sense to query the key type and size from the
2206 * secure element, but not all secure elements support this
2207 * and the driver HAL doesn't currently support it. */
2208 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2209 return( PSA_ERROR_NOT_SUPPORTED );
2210 if( psa_get_key_bits( attributes ) == 0 )
2211 return( PSA_ERROR_NOT_SUPPORTED );
2212
2213 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002214 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002215 if( status != PSA_SUCCESS )
2216 goto exit;
2217
Ronald Cron81709fc2020-11-14 12:10:32 +01002218 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002219
2220exit:
2221 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002222 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002223
Gilles Peskined7729582019-08-05 15:55:54 +02002224 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002225 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002226 return( status );
2227}
2228#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2229
Gilles Peskinef603c712019-01-19 13:40:11 +01002230static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002231 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002232{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002233 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002234 source->key.data,
2235 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002236 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002237 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002238
Steven Cooreman398aee52020-10-13 14:35:45 +02002239 target->attr.type = source->attr.type;
2240 target->attr.bits = source->attr.bits;
2241
2242 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002243}
2244
Ronald Croncf56a0a2020-08-04 09:51:30 +02002245psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002246 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002247 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002248{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002251 psa_key_slot_t *source_slot = NULL;
2252 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002253 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002254 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002255
Ronald Cron81709fc2020-11-14 12:10:32 +01002256 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2257
Ronald Cron5c522922020-11-14 16:35:34 +01002258 status = psa_get_and_lock_transparent_key_slot_with_policy(
2259 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002260 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002261 goto exit;
2262
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002263 status = psa_validate_optional_attributes( source_slot,
2264 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002265 if( status != PSA_SUCCESS )
2266 goto exit;
2267
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002268 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002269 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002270 if( status != PSA_SUCCESS )
2271 goto exit;
2272
Ronald Cron81709fc2020-11-14 12:10:32 +01002273 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2274 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002275 if( status != PSA_SUCCESS )
2276 goto exit;
2277
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002278#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2279 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002280 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002281 /* Copying to a secure element is not implemented yet. */
2282 status = PSA_ERROR_NOT_SUPPORTED;
2283 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002284 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002285#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002286
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002287 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002288 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002289 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002290
Ronald Cron81709fc2020-11-14 12:10:32 +01002291 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002292exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002293 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002294 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002295
Ronald Cron5c522922020-11-14 16:35:34 +01002296 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002297
Ronald Cron5c522922020-11-14 16:35:34 +01002298 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002299}
2300
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002301
2302
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002303/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002304/* Message digests */
2305/****************************************************************/
2306
John Durkop07cc04a2020-11-16 22:08:34 -08002307#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002308 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2309 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002310 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002311static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002312{
2313 switch( alg )
2314 {
John Durkopee4e6602020-11-27 08:48:46 -08002315#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002316 case PSA_ALG_MD2:
2317 return( &mbedtls_md2_info );
2318#endif
John Durkopee4e6602020-11-27 08:48:46 -08002319#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002320 case PSA_ALG_MD4:
2321 return( &mbedtls_md4_info );
2322#endif
John Durkopee4e6602020-11-27 08:48:46 -08002323#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002324 case PSA_ALG_MD5:
2325 return( &mbedtls_md5_info );
2326#endif
John Durkopee4e6602020-11-27 08:48:46 -08002327#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002328 case PSA_ALG_RIPEMD160:
2329 return( &mbedtls_ripemd160_info );
2330#endif
John Durkopee4e6602020-11-27 08:48:46 -08002331#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002332 case PSA_ALG_SHA_1:
2333 return( &mbedtls_sha1_info );
2334#endif
John Durkopee4e6602020-11-27 08:48:46 -08002335#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002336 case PSA_ALG_SHA_224:
2337 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002338#endif
2339#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002340 case PSA_ALG_SHA_256:
2341 return( &mbedtls_sha256_info );
2342#endif
John Durkopee4e6602020-11-27 08:48:46 -08002343#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002344 case PSA_ALG_SHA_384:
2345 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002346#endif
John Durkopee4e6602020-11-27 08:48:46 -08002347#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002348 case PSA_ALG_SHA_512:
2349 return( &mbedtls_sha512_info );
2350#endif
2351 default:
2352 return( NULL );
2353 }
2354}
John Durkop07cc04a2020-11-16 22:08:34 -08002355#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002356 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2357 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2358 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002359
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2361{
2362 switch( operation->alg )
2363 {
Gilles Peskine81736312018-06-26 15:04:31 +02002364 case 0:
2365 /* The object has (apparently) been initialized but it is not
2366 * in use. It's ok to call abort on such an object, and there's
2367 * nothing to do. */
2368 break;
John Durkopee4e6602020-11-27 08:48:46 -08002369#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 case PSA_ALG_MD2:
2371 mbedtls_md2_free( &operation->ctx.md2 );
2372 break;
2373#endif
John Durkopee4e6602020-11-27 08:48:46 -08002374#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 case PSA_ALG_MD4:
2376 mbedtls_md4_free( &operation->ctx.md4 );
2377 break;
2378#endif
John Durkopee4e6602020-11-27 08:48:46 -08002379#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380 case PSA_ALG_MD5:
2381 mbedtls_md5_free( &operation->ctx.md5 );
2382 break;
2383#endif
John Durkopee4e6602020-11-27 08:48:46 -08002384#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385 case PSA_ALG_RIPEMD160:
2386 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2387 break;
2388#endif
John Durkopee4e6602020-11-27 08:48:46 -08002389#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390 case PSA_ALG_SHA_1:
2391 mbedtls_sha1_free( &operation->ctx.sha1 );
2392 break;
2393#endif
John Durkop6ca23272020-12-03 06:01:32 -08002394#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002396 mbedtls_sha256_free( &operation->ctx.sha256 );
2397 break;
2398#endif
2399#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400 case PSA_ALG_SHA_256:
2401 mbedtls_sha256_free( &operation->ctx.sha256 );
2402 break;
2403#endif
John Durkop6ca23272020-12-03 06:01:32 -08002404#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002406 mbedtls_sha512_free( &operation->ctx.sha512 );
2407 break;
2408#endif
2409#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410 case PSA_ALG_SHA_512:
2411 mbedtls_sha512_free( &operation->ctx.sha512 );
2412 break;
2413#endif
2414 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002415 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002416 }
2417 operation->alg = 0;
2418 return( PSA_SUCCESS );
2419}
2420
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002421psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422 psa_algorithm_t alg )
2423{
Janos Follath24eed8d2019-11-22 13:21:35 +00002424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002425
2426 /* A context must be freshly initialized before it can be set up. */
2427 if( operation->alg != 0 )
2428 {
2429 return( PSA_ERROR_BAD_STATE );
2430 }
2431
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432 switch( alg )
2433 {
John Durkopee4e6602020-11-27 08:48:46 -08002434#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435 case PSA_ALG_MD2:
2436 mbedtls_md2_init( &operation->ctx.md2 );
2437 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2438 break;
2439#endif
John Durkopee4e6602020-11-27 08:48:46 -08002440#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002441 case PSA_ALG_MD4:
2442 mbedtls_md4_init( &operation->ctx.md4 );
2443 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2444 break;
2445#endif
John Durkopee4e6602020-11-27 08:48:46 -08002446#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002447 case PSA_ALG_MD5:
2448 mbedtls_md5_init( &operation->ctx.md5 );
2449 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2450 break;
2451#endif
John Durkopee4e6602020-11-27 08:48:46 -08002452#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453 case PSA_ALG_RIPEMD160:
2454 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2455 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2456 break;
2457#endif
John Durkopee4e6602020-11-27 08:48:46 -08002458#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459 case PSA_ALG_SHA_1:
2460 mbedtls_sha1_init( &operation->ctx.sha1 );
2461 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2462 break;
2463#endif
John Durkopee4e6602020-11-27 08:48:46 -08002464#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002465 case PSA_ALG_SHA_224:
2466 mbedtls_sha256_init( &operation->ctx.sha256 );
2467 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2468 break;
John Durkopee4e6602020-11-27 08:48:46 -08002469#endif
2470#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002471 case PSA_ALG_SHA_256:
2472 mbedtls_sha256_init( &operation->ctx.sha256 );
2473 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2474 break;
2475#endif
John Durkopee4e6602020-11-27 08:48:46 -08002476#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002477 case PSA_ALG_SHA_384:
2478 mbedtls_sha512_init( &operation->ctx.sha512 );
2479 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2480 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002481#endif
John Durkopee4e6602020-11-27 08:48:46 -08002482#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483 case PSA_ALG_SHA_512:
2484 mbedtls_sha512_init( &operation->ctx.sha512 );
2485 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2486 break;
2487#endif
2488 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002489 return( PSA_ALG_IS_HASH( alg ) ?
2490 PSA_ERROR_NOT_SUPPORTED :
2491 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492 }
2493 if( ret == 0 )
2494 operation->alg = alg;
2495 else
2496 psa_hash_abort( operation );
2497 return( mbedtls_to_psa_error( ret ) );
2498}
2499
2500psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2501 const uint8_t *input,
2502 size_t input_length )
2503{
Janos Follath24eed8d2019-11-22 13:21:35 +00002504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002505
2506 /* Don't require hash implementations to behave correctly on a
2507 * zero-length input, which may have an invalid pointer. */
2508 if( input_length == 0 )
2509 return( PSA_SUCCESS );
2510
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511 switch( operation->alg )
2512 {
John Durkopee4e6602020-11-27 08:48:46 -08002513#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514 case PSA_ALG_MD2:
2515 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2516 input, input_length );
2517 break;
2518#endif
John Durkopee4e6602020-11-27 08:48:46 -08002519#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520 case PSA_ALG_MD4:
2521 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2522 input, input_length );
2523 break;
2524#endif
John Durkopee4e6602020-11-27 08:48:46 -08002525#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002526 case PSA_ALG_MD5:
2527 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2528 input, input_length );
2529 break;
2530#endif
John Durkopee4e6602020-11-27 08:48:46 -08002531#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002532 case PSA_ALG_RIPEMD160:
2533 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2534 input, input_length );
2535 break;
2536#endif
John Durkopee4e6602020-11-27 08:48:46 -08002537#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002538 case PSA_ALG_SHA_1:
2539 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2540 input, input_length );
2541 break;
2542#endif
John Durkop6ca23272020-12-03 06:01:32 -08002543#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002545 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2546 input, input_length );
2547 break;
2548#endif
2549#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002550 case PSA_ALG_SHA_256:
2551 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2552 input, input_length );
2553 break;
2554#endif
John Durkop6ca23272020-12-03 06:01:32 -08002555#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002556 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002557 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2558 input, input_length );
2559 break;
2560#endif
2561#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562 case PSA_ALG_SHA_512:
2563 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2564 input, input_length );
2565 break;
2566#endif
2567 default:
John Durkopee4e6602020-11-27 08:48:46 -08002568 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002569 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002570 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002571
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572 if( ret != 0 )
2573 psa_hash_abort( operation );
2574 return( mbedtls_to_psa_error( ret ) );
2575}
2576
2577psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2578 uint8_t *hash,
2579 size_t hash_size,
2580 size_t *hash_length )
2581{
itayzafrir40835d42018-08-02 13:14:17 +03002582 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002584 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002585
2586 /* Fill the output buffer with something that isn't a valid hash
2587 * (barring an attack on the hash and deliberately-crafted input),
2588 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002589 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002590 /* If hash_size is 0 then hash may be NULL and then the
2591 * call to memset would have undefined behavior. */
2592 if( hash_size != 0 )
2593 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594
2595 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002596 {
2597 status = PSA_ERROR_BUFFER_TOO_SMALL;
2598 goto exit;
2599 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002600
2601 switch( operation->alg )
2602 {
John Durkopee4e6602020-11-27 08:48:46 -08002603#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002604 case PSA_ALG_MD2:
2605 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2606 break;
2607#endif
John Durkopee4e6602020-11-27 08:48:46 -08002608#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002609 case PSA_ALG_MD4:
2610 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2611 break;
2612#endif
John Durkopee4e6602020-11-27 08:48:46 -08002613#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002614 case PSA_ALG_MD5:
2615 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2616 break;
2617#endif
John Durkopee4e6602020-11-27 08:48:46 -08002618#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619 case PSA_ALG_RIPEMD160:
2620 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2621 break;
2622#endif
John Durkopee4e6602020-11-27 08:48:46 -08002623#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 case PSA_ALG_SHA_1:
2625 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2626 break;
2627#endif
John Durkop6ca23272020-12-03 06:01:32 -08002628#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002629 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002630 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2631 break;
2632#endif
2633#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002634 case PSA_ALG_SHA_256:
2635 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2636 break;
2637#endif
John Durkop6ca23272020-12-03 06:01:32 -08002638#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002639 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002640 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2641 break;
2642#endif
2643#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002644 case PSA_ALG_SHA_512:
2645 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2646 break;
2647#endif
2648 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002649 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002650 }
itayzafrir40835d42018-08-02 13:14:17 +03002651 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002652
itayzafrir40835d42018-08-02 13:14:17 +03002653exit:
2654 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002655 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002656 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002657 return( psa_hash_abort( operation ) );
2658 }
2659 else
2660 {
2661 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002662 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663 }
2664}
2665
Gilles Peskine2d277862018-06-18 15:41:12 +02002666psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2667 const uint8_t *hash,
2668 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669{
2670 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2671 size_t actual_hash_length;
2672 psa_status_t status = psa_hash_finish( operation,
2673 actual_hash, sizeof( actual_hash ),
2674 &actual_hash_length );
2675 if( status != PSA_SUCCESS )
2676 return( status );
2677 if( actual_hash_length != hash_length )
2678 return( PSA_ERROR_INVALID_SIGNATURE );
2679 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2680 return( PSA_ERROR_INVALID_SIGNATURE );
2681 return( PSA_SUCCESS );
2682}
2683
Gilles Peskine0a749c82019-11-28 19:33:58 +01002684psa_status_t psa_hash_compute( psa_algorithm_t alg,
2685 const uint8_t *input, size_t input_length,
2686 uint8_t *hash, size_t hash_size,
2687 size_t *hash_length )
2688{
2689 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2691
2692 *hash_length = hash_size;
2693 status = psa_hash_setup( &operation, alg );
2694 if( status != PSA_SUCCESS )
2695 goto exit;
2696 status = psa_hash_update( &operation, input, input_length );
2697 if( status != PSA_SUCCESS )
2698 goto exit;
2699 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2700 if( status != PSA_SUCCESS )
2701 goto exit;
2702
2703exit:
2704 if( status == PSA_SUCCESS )
2705 status = psa_hash_abort( &operation );
2706 else
2707 psa_hash_abort( &operation );
2708 return( status );
2709}
2710
2711psa_status_t psa_hash_compare( psa_algorithm_t alg,
2712 const uint8_t *input, size_t input_length,
2713 const uint8_t *hash, size_t hash_length )
2714{
2715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2717
2718 status = psa_hash_setup( &operation, alg );
2719 if( status != PSA_SUCCESS )
2720 goto exit;
2721 status = psa_hash_update( &operation, input, input_length );
2722 if( status != PSA_SUCCESS )
2723 goto exit;
2724 status = psa_hash_verify( &operation, hash, hash_length );
2725 if( status != PSA_SUCCESS )
2726 goto exit;
2727
2728exit:
2729 if( status == PSA_SUCCESS )
2730 status = psa_hash_abort( &operation );
2731 else
2732 psa_hash_abort( &operation );
2733 return( status );
2734}
2735
Gilles Peskineeb35d782019-01-22 17:56:16 +01002736psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2737 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002738{
2739 if( target_operation->alg != 0 )
2740 return( PSA_ERROR_BAD_STATE );
2741
2742 switch( source_operation->alg )
2743 {
2744 case 0:
2745 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002746#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002747 case PSA_ALG_MD2:
2748 mbedtls_md2_clone( &target_operation->ctx.md2,
2749 &source_operation->ctx.md2 );
2750 break;
2751#endif
John Durkopee4e6602020-11-27 08:48:46 -08002752#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002753 case PSA_ALG_MD4:
2754 mbedtls_md4_clone( &target_operation->ctx.md4,
2755 &source_operation->ctx.md4 );
2756 break;
2757#endif
John Durkopee4e6602020-11-27 08:48:46 -08002758#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002759 case PSA_ALG_MD5:
2760 mbedtls_md5_clone( &target_operation->ctx.md5,
2761 &source_operation->ctx.md5 );
2762 break;
2763#endif
John Durkopee4e6602020-11-27 08:48:46 -08002764#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002765 case PSA_ALG_RIPEMD160:
2766 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2767 &source_operation->ctx.ripemd160 );
2768 break;
2769#endif
John Durkopee4e6602020-11-27 08:48:46 -08002770#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002771 case PSA_ALG_SHA_1:
2772 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2773 &source_operation->ctx.sha1 );
2774 break;
2775#endif
John Durkop6ca23272020-12-03 06:01:32 -08002776#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002777 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002778 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2779 &source_operation->ctx.sha256 );
2780 break;
2781#endif
2782#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002783 case PSA_ALG_SHA_256:
2784 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2785 &source_operation->ctx.sha256 );
2786 break;
2787#endif
John Durkop6ca23272020-12-03 06:01:32 -08002788#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002789 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002790 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2791 &source_operation->ctx.sha512 );
2792 break;
2793#endif
2794#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002795 case PSA_ALG_SHA_512:
2796 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2797 &source_operation->ctx.sha512 );
2798 break;
2799#endif
2800 default:
2801 return( PSA_ERROR_NOT_SUPPORTED );
2802 }
2803
2804 target_operation->alg = source_operation->alg;
2805 return( PSA_SUCCESS );
2806}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807
2808
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002809/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810/* MAC */
2811/****************************************************************/
2812
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002813static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814 psa_algorithm_t alg,
2815 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002816 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002817 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002820 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002822 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002823 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002824
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2826 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002827 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002829 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830 mode = MBEDTLS_MODE_STREAM;
2831 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832 case PSA_ALG_CTR:
2833 mode = MBEDTLS_MODE_CTR;
2834 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002835 case PSA_ALG_CFB:
2836 mode = MBEDTLS_MODE_CFB;
2837 break;
2838 case PSA_ALG_OFB:
2839 mode = MBEDTLS_MODE_OFB;
2840 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002841 case PSA_ALG_ECB_NO_PADDING:
2842 mode = MBEDTLS_MODE_ECB;
2843 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002844 case PSA_ALG_CBC_NO_PADDING:
2845 mode = MBEDTLS_MODE_CBC;
2846 break;
2847 case PSA_ALG_CBC_PKCS7:
2848 mode = MBEDTLS_MODE_CBC;
2849 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002850 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 mode = MBEDTLS_MODE_CCM;
2852 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002853 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854 mode = MBEDTLS_MODE_GCM;
2855 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002856 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2857 mode = MBEDTLS_MODE_CHACHAPOLY;
2858 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859 default:
2860 return( NULL );
2861 }
2862 }
2863 else if( alg == PSA_ALG_CMAC )
2864 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865 else
2866 return( NULL );
2867
2868 switch( key_type )
2869 {
2870 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002871 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 break;
2873 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002874 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2875 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002877 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002879 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002880 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2881 * but two-key Triple-DES is functionally three-key Triple-DES
2882 * with K1=K3, so that's how we present it to mbedtls. */
2883 if( key_bits == 128 )
2884 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885 break;
2886 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002887 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888 break;
2889 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002890 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002892 case PSA_KEY_TYPE_CHACHA20:
2893 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2894 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002895 default:
2896 return( NULL );
2897 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002898 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002899 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900
Jaeden Amero23bbb752018-06-26 14:16:54 +01002901 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2902 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903}
2904
John Durkop6ba40d12020-11-10 08:50:04 -08002905#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002906static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002907{
Gilles Peskine2d277862018-06-18 15:41:12 +02002908 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002909 {
2910 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002911 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002912 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002913 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002914 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002915 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002916 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002917 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002918 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002919 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002920 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002921 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002922 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002923 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002924 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002925 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002926 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002927 return( 128 );
2928 default:
2929 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002930 }
2931}
John Durkop07cc04a2020-11-16 22:08:34 -08002932#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002933
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002934/* Initialize the MAC operation structure. Once this function has been
2935 * called, psa_mac_abort can run and will do the right thing. */
2936static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2937 psa_algorithm_t alg )
2938{
2939 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2940
2941 operation->alg = alg;
2942 operation->key_set = 0;
2943 operation->iv_set = 0;
2944 operation->iv_required = 0;
2945 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002946 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002947
2948#if defined(MBEDTLS_CMAC_C)
2949 if( alg == PSA_ALG_CMAC )
2950 {
2951 operation->iv_required = 0;
2952 mbedtls_cipher_init( &operation->ctx.cmac );
2953 status = PSA_SUCCESS;
2954 }
2955 else
2956#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002957#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002958 if( PSA_ALG_IS_HMAC( operation->alg ) )
2959 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002960 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2961 operation->ctx.hmac.hash_ctx.alg = 0;
2962 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002963 }
2964 else
John Durkopd0321952020-10-29 21:37:36 -07002965#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002966 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002967 if( ! PSA_ALG_IS_MAC( alg ) )
2968 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002969 }
2970
2971 if( status != PSA_SUCCESS )
2972 memset( operation, 0, sizeof( *operation ) );
2973 return( status );
2974}
2975
John Durkop6ba40d12020-11-10 08:50:04 -08002976#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002977static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2978{
Gilles Peskine3f108122018-12-07 18:14:53 +01002979 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002980 return( psa_hash_abort( &hmac->hash_ctx ) );
2981}
John Durkop6ba40d12020-11-10 08:50:04 -08002982#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002983
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2985{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002986 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002987 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002988 /* The object has (apparently) been initialized but it is not
2989 * in use. It's ok to call abort on such an object, and there's
2990 * nothing to do. */
2991 return( PSA_SUCCESS );
2992 }
2993 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002994#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002995 if( operation->alg == PSA_ALG_CMAC )
2996 {
2997 mbedtls_cipher_free( &operation->ctx.cmac );
2998 }
2999 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003000#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003001#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003002 if( PSA_ALG_IS_HMAC( operation->alg ) )
3003 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003004 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003005 }
3006 else
John Durkopd0321952020-10-29 21:37:36 -07003007#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003008 {
3009 /* Sanity check (shouldn't happen: operation->alg should
3010 * always have been initialized to a valid value). */
3011 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012 }
Moran Peker41deec42018-04-04 15:43:05 +03003013
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 operation->alg = 0;
3015 operation->key_set = 0;
3016 operation->iv_set = 0;
3017 operation->iv_required = 0;
3018 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003019 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003020
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022
3023bad_state:
3024 /* If abort is called on an uninitialized object, we can't trust
3025 * anything. Wipe the object in case it contains confidential data.
3026 * This may result in a memory leak if a pointer gets overwritten,
3027 * but it's too late to do anything about this. */
3028 memset( operation, 0, sizeof( *operation ) );
3029 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030}
3031
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003032#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003033static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003034 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003035 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003036 const mbedtls_cipher_info_t *cipher_info )
3037{
Janos Follath24eed8d2019-11-22 13:21:35 +00003038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003039
3040 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003041
3042 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3043 if( ret != 0 )
3044 return( ret );
3045
3046 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003047 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003048 key_bits );
3049 return( ret );
3050}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003051#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003052
John Durkop6ba40d12020-11-10 08:50:04 -08003053#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003054static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3055 const uint8_t *key,
3056 size_t key_length,
3057 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003058{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003059 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003060 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003061 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003062 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003063 psa_status_t status;
3064
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003065 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3066 * overflow below. This should never trigger if the hash algorithm
3067 * is implemented correctly. */
3068 /* The size checks against the ipad and opad buffers cannot be written
3069 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3070 * because that triggers -Wlogical-op on GCC 7.3. */
3071 if( block_size > sizeof( ipad ) )
3072 return( PSA_ERROR_NOT_SUPPORTED );
3073 if( block_size > sizeof( hmac->opad ) )
3074 return( PSA_ERROR_NOT_SUPPORTED );
3075 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003076 return( PSA_ERROR_NOT_SUPPORTED );
3077
Gilles Peskined223b522018-06-11 18:12:58 +02003078 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003079 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003080 status = psa_hash_compute( hash_alg, key, key_length,
3081 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003082 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003083 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003084 }
Gilles Peskine96889972018-07-12 17:07:03 +02003085 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3086 * but it is permitted. It is common when HMAC is used in HKDF, for
3087 * example. Don't call `memcpy` in the 0-length because `key` could be
3088 * an invalid pointer which would make the behavior undefined. */
3089 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003090 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003091
Gilles Peskined223b522018-06-11 18:12:58 +02003092 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3093 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003094 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003095 ipad[i] ^= 0x36;
3096 memset( ipad + key_length, 0x36, block_size - key_length );
3097
3098 /* Copy the key material from ipad to opad, flipping the requisite bits,
3099 * and filling the rest of opad with the requisite constant. */
3100 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003101 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3102 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003103
Gilles Peskine01126fa2018-07-12 17:04:55 +02003104 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003105 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003106 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003107
Gilles Peskine01126fa2018-07-12 17:04:55 +02003108 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003109
3110cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003111 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003112
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003113 return( status );
3114}
John Durkop6ba40d12020-11-10 08:50:04 -08003115#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003116
Gilles Peskine89167cb2018-07-08 20:12:23 +02003117static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003118 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003119 psa_algorithm_t alg,
3120 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003123 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003124 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003126 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003127 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003128 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003129 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003131 /* A context must be freshly initialized before it can be set up. */
3132 if( operation->alg != 0 )
3133 {
3134 return( PSA_ERROR_BAD_STATE );
3135 }
3136
Gilles Peskined911eb72018-08-14 15:18:45 +02003137 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138 if( status != PSA_SUCCESS )
3139 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003140 if( is_sign )
3141 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142
Ronald Cron5c522922020-11-14 16:35:34 +01003143 status = psa_get_and_lock_transparent_key_slot_with_policy(
3144 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003145 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003146 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003147 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003148
Gilles Peskine8c9def32018-02-08 10:02:12 +01003149#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003150 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003151 {
3152 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003153 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003154 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003156 if( cipher_info == NULL )
3157 {
3158 status = PSA_ERROR_NOT_SUPPORTED;
3159 goto exit;
3160 }
3161 operation->mac_size = cipher_info->block_size;
3162 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3163 status = mbedtls_to_psa_error( ret );
3164 }
3165 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003167#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003168 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003170 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003171 if( hash_alg == 0 )
3172 {
3173 status = PSA_ERROR_NOT_SUPPORTED;
3174 goto exit;
3175 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003176
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003177 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003178 /* Sanity check. This shouldn't fail on a valid configuration. */
3179 if( operation->mac_size == 0 ||
3180 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3181 {
3182 status = PSA_ERROR_NOT_SUPPORTED;
3183 goto exit;
3184 }
3185
Gilles Peskine8e338702019-07-30 20:06:31 +02003186 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003187 {
3188 status = PSA_ERROR_INVALID_ARGUMENT;
3189 goto exit;
3190 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003191
Gilles Peskine01126fa2018-07-12 17:04:55 +02003192 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003193 slot->key.data,
3194 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003195 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003196 }
3197 else
John Durkopd0321952020-10-29 21:37:36 -07003198#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003199 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003200 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003201 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003202 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003203
Gilles Peskined911eb72018-08-14 15:18:45 +02003204 if( truncated == 0 )
3205 {
3206 /* The "normal" case: untruncated algorithm. Nothing to do. */
3207 }
3208 else if( truncated < 4 )
3209 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003210 /* A very short MAC is too short for security since it can be
3211 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3212 * so we make this our minimum, even though 32 bits is still
3213 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003214 status = PSA_ERROR_NOT_SUPPORTED;
3215 }
3216 else if( truncated > operation->mac_size )
3217 {
3218 /* It's impossible to "truncate" to a larger length. */
3219 status = PSA_ERROR_INVALID_ARGUMENT;
3220 }
3221 else
3222 operation->mac_size = truncated;
3223
Gilles Peskinefbfac682018-07-08 20:51:54 +02003224exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003225 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003226 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003227 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003228 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003229 else
3230 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003231 operation->key_set = 1;
3232 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003233
Ronald Cron5c522922020-11-14 16:35:34 +01003234 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235
Ronald Cron5c522922020-11-14 16:35:34 +01003236 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003237}
3238
Gilles Peskine89167cb2018-07-08 20:12:23 +02003239psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003240 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003241 psa_algorithm_t alg )
3242{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003243 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003244}
3245
3246psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003247 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003248 psa_algorithm_t alg )
3249{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003250 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003251}
3252
Gilles Peskine8c9def32018-02-08 10:02:12 +01003253psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3254 const uint8_t *input,
3255 size_t input_length )
3256{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003257 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003259 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003261 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003262 operation->has_input = 1;
3263
Gilles Peskine8c9def32018-02-08 10:02:12 +01003264#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003265 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003266 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003267 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3268 input, input_length );
3269 status = mbedtls_to_psa_error( ret );
3270 }
3271 else
3272#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003273#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003274 if( PSA_ALG_IS_HMAC( operation->alg ) )
3275 {
3276 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3277 input_length );
3278 }
3279 else
John Durkopd0321952020-10-29 21:37:36 -07003280#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003281 {
3282 /* This shouldn't happen if `operation` was initialized by
3283 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003284 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003285 }
3286
Gilles Peskinefbfac682018-07-08 20:51:54 +02003287 if( status != PSA_SUCCESS )
3288 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003289 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003290}
3291
John Durkop6ba40d12020-11-10 08:50:04 -08003292#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003293static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3294 uint8_t *mac,
3295 size_t mac_size )
3296{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003297 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003298 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3299 size_t hash_size = 0;
3300 size_t block_size = psa_get_hash_block_size( hash_alg );
3301 psa_status_t status;
3302
Gilles Peskine01126fa2018-07-12 17:04:55 +02003303 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3304 if( status != PSA_SUCCESS )
3305 return( status );
3306 /* From here on, tmp needs to be wiped. */
3307
3308 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3309 if( status != PSA_SUCCESS )
3310 goto exit;
3311
3312 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3313 if( status != PSA_SUCCESS )
3314 goto exit;
3315
3316 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3317 if( status != PSA_SUCCESS )
3318 goto exit;
3319
Gilles Peskined911eb72018-08-14 15:18:45 +02003320 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3321 if( status != PSA_SUCCESS )
3322 goto exit;
3323
3324 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003325
3326exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003327 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003328 return( status );
3329}
John Durkop6ba40d12020-11-10 08:50:04 -08003330#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003331
mohammad16036df908f2018-04-02 08:34:15 -07003332static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003333 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003334 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003335{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003336 if( ! operation->key_set )
3337 return( PSA_ERROR_BAD_STATE );
3338 if( operation->iv_required && ! operation->iv_set )
3339 return( PSA_ERROR_BAD_STATE );
3340
Gilles Peskine8c9def32018-02-08 10:02:12 +01003341 if( mac_size < operation->mac_size )
3342 return( PSA_ERROR_BUFFER_TOO_SMALL );
3343
Gilles Peskine8c9def32018-02-08 10:02:12 +01003344#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003345 if( operation->alg == PSA_ALG_CMAC )
3346 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003347 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003348 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3349 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003350 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003351 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003352 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003353 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003354 else
3355#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003357 if( PSA_ALG_IS_HMAC( operation->alg ) )
3358 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003359 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003360 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003361 }
3362 else
John Durkopd0321952020-10-29 21:37:36 -07003363#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003364 {
3365 /* This shouldn't happen if `operation` was initialized by
3366 * a setup function. */
3367 return( PSA_ERROR_BAD_STATE );
3368 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003369}
3370
Gilles Peskineacd4be32018-07-08 19:56:25 +02003371psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3372 uint8_t *mac,
3373 size_t mac_size,
3374 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003375{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003376 psa_status_t status;
3377
Jaeden Amero252ef282019-02-15 14:05:35 +00003378 if( operation->alg == 0 )
3379 {
3380 return( PSA_ERROR_BAD_STATE );
3381 }
3382
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003383 /* Fill the output buffer with something that isn't a valid mac
3384 * (barring an attack on the mac and deliberately-crafted input),
3385 * in case the caller doesn't check the return status properly. */
3386 *mac_length = mac_size;
3387 /* If mac_size is 0 then mac may be NULL and then the
3388 * call to memset would have undefined behavior. */
3389 if( mac_size != 0 )
3390 memset( mac, '!', mac_size );
3391
Gilles Peskine89167cb2018-07-08 20:12:23 +02003392 if( ! operation->is_sign )
3393 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003394 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003395 }
mohammad16036df908f2018-04-02 08:34:15 -07003396
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003397 status = psa_mac_finish_internal( operation, mac, mac_size );
3398
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003399 if( status == PSA_SUCCESS )
3400 {
3401 status = psa_mac_abort( operation );
3402 if( status == PSA_SUCCESS )
3403 *mac_length = operation->mac_size;
3404 else
3405 memset( mac, '!', mac_size );
3406 }
3407 else
3408 psa_mac_abort( operation );
3409 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003410}
3411
Gilles Peskineacd4be32018-07-08 19:56:25 +02003412psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3413 const uint8_t *mac,
3414 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415{
Gilles Peskine828ed142018-06-18 23:25:51 +02003416 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003417 psa_status_t status;
3418
Jaeden Amero252ef282019-02-15 14:05:35 +00003419 if( operation->alg == 0 )
3420 {
3421 return( PSA_ERROR_BAD_STATE );
3422 }
3423
Gilles Peskine89167cb2018-07-08 20:12:23 +02003424 if( operation->is_sign )
3425 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003426 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003427 }
3428 if( operation->mac_size != mac_length )
3429 {
3430 status = PSA_ERROR_INVALID_SIGNATURE;
3431 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003432 }
mohammad16036df908f2018-04-02 08:34:15 -07003433
3434 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003435 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003436 if( status != PSA_SUCCESS )
3437 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003438
3439 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3440 status = PSA_ERROR_INVALID_SIGNATURE;
3441
3442cleanup:
3443 if( status == PSA_SUCCESS )
3444 status = psa_mac_abort( operation );
3445 else
3446 psa_mac_abort( operation );
3447
Gilles Peskine3f108122018-12-07 18:14:53 +01003448 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003449
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003450 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003451}
3452
3453
Gilles Peskine20035e32018-02-03 22:44:14 +01003454
Gilles Peskine20035e32018-02-03 22:44:14 +01003455/****************************************************************/
3456/* Asymmetric cryptography */
3457/****************************************************************/
3458
John Durkop6ba40d12020-11-10 08:50:04 -08003459#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3460 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003461/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003462 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003463static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3464 size_t hash_length,
3465 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003466{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003467 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003468 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003469 *md_alg = mbedtls_md_get_type( md_info );
3470
3471 /* The Mbed TLS RSA module uses an unsigned int for hash length
3472 * parameters. Validate that it fits so that we don't risk an
3473 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003474#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003475 if( hash_length > UINT_MAX )
3476 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003477#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003478
John Durkop0e005192020-10-31 22:06:54 -07003479#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003480 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3481 * must be correct. */
3482 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3483 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003484 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003485 if( md_info == NULL )
3486 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003487 if( mbedtls_md_get_size( md_info ) != hash_length )
3488 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003489 }
John Durkop0e005192020-10-31 22:06:54 -07003490#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003491
John Durkop0e005192020-10-31 22:06:54 -07003492#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003493 /* PSS requires a hash internally. */
3494 if( PSA_ALG_IS_RSA_PSS( alg ) )
3495 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003496 if( md_info == NULL )
3497 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003498 }
John Durkop0e005192020-10-31 22:06:54 -07003499#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003500
Gilles Peskine61b91d42018-06-08 16:09:36 +02003501 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003502}
3503
Gilles Peskine2b450e32018-06-27 15:42:46 +02003504static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3505 psa_algorithm_t alg,
3506 const uint8_t *hash,
3507 size_t hash_length,
3508 uint8_t *signature,
3509 size_t signature_size,
3510 size_t *signature_length )
3511{
3512 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003514 mbedtls_md_type_t md_alg;
3515
3516 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3517 if( status != PSA_SUCCESS )
3518 return( status );
3519
Gilles Peskine630a18a2018-06-29 17:49:35 +02003520 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003521 return( PSA_ERROR_BUFFER_TOO_SMALL );
3522
John Durkop0e005192020-10-31 22:06:54 -07003523#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003524 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3525 {
3526 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3527 MBEDTLS_MD_NONE );
3528 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003529 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003530 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003531 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003532 md_alg,
3533 (unsigned int) hash_length,
3534 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003535 signature );
3536 }
3537 else
John Durkop0e005192020-10-31 22:06:54 -07003538#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3539#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003540 if( PSA_ALG_IS_RSA_PSS( alg ) )
3541 {
3542 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3543 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003544 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003545 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003546 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003547 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003548 (unsigned int) hash_length,
3549 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003550 signature );
3551 }
3552 else
John Durkop0e005192020-10-31 22:06:54 -07003553#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003554 {
3555 return( PSA_ERROR_INVALID_ARGUMENT );
3556 }
3557
3558 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003559 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003560 return( mbedtls_to_psa_error( ret ) );
3561}
3562
3563static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3564 psa_algorithm_t alg,
3565 const uint8_t *hash,
3566 size_t hash_length,
3567 const uint8_t *signature,
3568 size_t signature_length )
3569{
3570 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003572 mbedtls_md_type_t md_alg;
3573
3574 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3575 if( status != PSA_SUCCESS )
3576 return( status );
3577
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003578 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3579 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003580
John Durkop0e005192020-10-31 22:06:54 -07003581#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003582 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3583 {
3584 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3585 MBEDTLS_MD_NONE );
3586 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003587 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003588 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003589 MBEDTLS_RSA_PUBLIC,
3590 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003591 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003592 hash,
3593 signature );
3594 }
3595 else
John Durkop0e005192020-10-31 22:06:54 -07003596#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3597#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003598 if( PSA_ALG_IS_RSA_PSS( alg ) )
3599 {
3600 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3601 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003602 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003603 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003604 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003605 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003606 (unsigned int) hash_length,
3607 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003608 signature );
3609 }
3610 else
John Durkop0e005192020-10-31 22:06:54 -07003611#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003612 {
3613 return( PSA_ERROR_INVALID_ARGUMENT );
3614 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003615
3616 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3617 * the rest of the signature is invalid". This has little use in
3618 * practice and PSA doesn't report this distinction. */
3619 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3620 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003621 return( mbedtls_to_psa_error( ret ) );
3622}
John Durkop6ba40d12020-11-10 08:50:04 -08003623#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3624 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003625
John Durkop6ba40d12020-11-10 08:50:04 -08003626#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3627 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003628/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3629 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3630 * (even though these functions don't modify it). */
3631static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3632 psa_algorithm_t alg,
3633 const uint8_t *hash,
3634 size_t hash_length,
3635 uint8_t *signature,
3636 size_t signature_size,
3637 size_t *signature_length )
3638{
Janos Follath24eed8d2019-11-22 13:21:35 +00003639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003640 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003641 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003642 mbedtls_mpi_init( &r );
3643 mbedtls_mpi_init( &s );
3644
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003645 if( signature_size < 2 * curve_bytes )
3646 {
3647 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3648 goto cleanup;
3649 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003650
John Durkop0ea39e02020-10-13 19:58:20 -07003651#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003652 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3653 {
3654 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3655 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3656 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003657 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3658 &ecp->d, hash,
3659 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003660 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003661 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003662 }
3663 else
John Durkop0ea39e02020-10-13 19:58:20 -07003664#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003665 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003666 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003667 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3668 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003669 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003670 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003671 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003672
3673 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3674 signature,
3675 curve_bytes ) );
3676 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3677 signature + curve_bytes,
3678 curve_bytes ) );
3679
3680cleanup:
3681 mbedtls_mpi_free( &r );
3682 mbedtls_mpi_free( &s );
3683 if( ret == 0 )
3684 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003685 return( mbedtls_to_psa_error( ret ) );
3686}
3687
3688static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3689 const uint8_t *hash,
3690 size_t hash_length,
3691 const uint8_t *signature,
3692 size_t signature_length )
3693{
Janos Follath24eed8d2019-11-22 13:21:35 +00003694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003695 mbedtls_mpi r, s;
3696 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3697 mbedtls_mpi_init( &r );
3698 mbedtls_mpi_init( &s );
3699
3700 if( signature_length != 2 * curve_bytes )
3701 return( PSA_ERROR_INVALID_SIGNATURE );
3702
3703 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3704 signature,
3705 curve_bytes ) );
3706 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3707 signature + curve_bytes,
3708 curve_bytes ) );
3709
Steven Cooremanacda8342020-07-24 23:09:52 +02003710 /* Check whether the public part is loaded. If not, load it. */
3711 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3712 {
3713 MBEDTLS_MPI_CHK(
3714 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003715 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003716 }
3717
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003718 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3719 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003720
3721cleanup:
3722 mbedtls_mpi_free( &r );
3723 mbedtls_mpi_free( &s );
3724 return( mbedtls_to_psa_error( ret ) );
3725}
John Durkop6ba40d12020-11-10 08:50:04 -08003726#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3727 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003728
Ronald Croncf56a0a2020-08-04 09:51:30 +02003729psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003730 psa_algorithm_t alg,
3731 const uint8_t *hash,
3732 size_t hash_length,
3733 uint8_t *signature,
3734 size_t signature_size,
3735 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003736{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003738 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003739 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003740
3741 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003742 /* Immediately reject a zero-length signature buffer. This guarantees
3743 * that signature must be a valid pointer. (On the other hand, the hash
3744 * buffer can in principle be empty since it doesn't actually have
3745 * to be a hash.) */
3746 if( signature_size == 0 )
3747 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003748
Ronald Cron5c522922020-11-14 16:35:34 +01003749 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3750 PSA_KEY_USAGE_SIGN_HASH,
3751 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003752 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003753 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003754 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003755 {
3756 status = PSA_ERROR_INVALID_ARGUMENT;
3757 goto exit;
3758 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003759
Steven Cooremancd84cb42020-07-16 20:28:36 +02003760 /* Try any of the available accelerators first */
3761 status = psa_driver_wrapper_sign_hash( slot,
3762 alg,
3763 hash,
3764 hash_length,
3765 signature,
3766 signature_size,
3767 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003768 if( status != PSA_ERROR_NOT_SUPPORTED ||
3769 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003770 goto exit;
3771
Steven Cooreman7a250572020-07-17 16:43:05 +02003772 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003773#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3774 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003775 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003776 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003777 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003778
Ronald Cron90857082020-11-25 14:58:33 +01003779 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3780 slot->key.data,
3781 slot->key.bytes,
3782 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003783 if( status != PSA_SUCCESS )
3784 goto exit;
3785
Steven Cooremana2371e52020-07-28 14:30:39 +02003786 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003787 alg,
3788 hash, hash_length,
3789 signature, signature_size,
3790 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003791
Steven Cooremana2371e52020-07-28 14:30:39 +02003792 mbedtls_rsa_free( rsa );
3793 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003794 }
3795 else
John Durkop6ba40d12020-11-10 08:50:04 -08003796#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3797 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003798 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003799 {
John Durkop9814fa22020-11-04 12:28:15 -08003800#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3801 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003802 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003803#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003804 PSA_ALG_IS_ECDSA( alg )
3805#else
3806 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3807#endif
3808 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003809 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003810 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003811 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3812 slot->key.data,
3813 slot->key.bytes,
3814 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003815 if( status != PSA_SUCCESS )
3816 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003817 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003818 alg,
3819 hash, hash_length,
3820 signature, signature_size,
3821 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003822 mbedtls_ecp_keypair_free( ecp );
3823 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003824 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003825 else
John Durkop9814fa22020-11-04 12:28:15 -08003826#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3827 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003828 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003829 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003830 }
itayzafrir5c753392018-05-08 11:18:38 +03003831 }
3832 else
itayzafrir5c753392018-05-08 11:18:38 +03003833 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003834 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003835 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003836
3837exit:
3838 /* Fill the unused part of the output buffer (the whole buffer on error,
3839 * the trailing part on success) with something that isn't a valid mac
3840 * (barring an attack on the mac and deliberately-crafted input),
3841 * in case the caller doesn't check the return status properly. */
3842 if( status == PSA_SUCCESS )
3843 memset( signature + *signature_length, '!',
3844 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003845 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003846 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003847 /* If signature_size is 0 then we have nothing to do. We must not call
3848 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003849
Ronald Cron5c522922020-11-14 16:35:34 +01003850 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003851
Ronald Cron5c522922020-11-14 16:35:34 +01003852 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003853}
3854
Ronald Croncf56a0a2020-08-04 09:51:30 +02003855psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003856 psa_algorithm_t alg,
3857 const uint8_t *hash,
3858 size_t hash_length,
3859 const uint8_t *signature,
3860 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003861{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003863 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003864 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003865
Ronald Cron5c522922020-11-14 16:35:34 +01003866 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3867 PSA_KEY_USAGE_VERIFY_HASH,
3868 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003869 if( status != PSA_SUCCESS )
3870 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003871
Steven Cooreman55ae2172020-07-17 19:46:15 +02003872 /* Try any of the available accelerators first */
3873 status = psa_driver_wrapper_verify_hash( slot,
3874 alg,
3875 hash,
3876 hash_length,
3877 signature,
3878 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003879 if( status != PSA_ERROR_NOT_SUPPORTED ||
3880 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003881 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003882
John Durkop6ba40d12020-11-10 08:50:04 -08003883#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3884 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003885 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003887 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003888
Ronald Cron90857082020-11-25 14:58:33 +01003889 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3890 slot->key.data,
3891 slot->key.bytes,
3892 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003893 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003894 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003895
Steven Cooremana2371e52020-07-28 14:30:39 +02003896 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003897 alg,
3898 hash, hash_length,
3899 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003900 mbedtls_rsa_free( rsa );
3901 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003902 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903 }
3904 else
John Durkop6ba40d12020-11-10 08:50:04 -08003905#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3906 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003907 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003908 {
John Durkop9814fa22020-11-04 12:28:15 -08003909#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3910 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003911 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003912 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003913 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003914 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3915 slot->key.data,
3916 slot->key.bytes,
3917 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003918 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003919 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003920 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003921 hash, hash_length,
3922 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003923 mbedtls_ecp_keypair_free( ecp );
3924 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003925 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003926 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003927 else
John Durkop9814fa22020-11-04 12:28:15 -08003928#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3929 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003930 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003931 status = PSA_ERROR_INVALID_ARGUMENT;
3932 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003933 }
itayzafrir5c753392018-05-08 11:18:38 +03003934 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003935 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003936 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003937 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003938 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003939
3940exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003941 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003942
Ronald Cron5c522922020-11-14 16:35:34 +01003943 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003944}
3945
John Durkop0e005192020-10-31 22:06:54 -07003946#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003947static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3948 mbedtls_rsa_context *rsa )
3949{
3950 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3951 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3952 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3953 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3954}
John Durkop0e005192020-10-31 22:06:54 -07003955#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003956
Ronald Croncf56a0a2020-08-04 09:51:30 +02003957psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003958 psa_algorithm_t alg,
3959 const uint8_t *input,
3960 size_t input_length,
3961 const uint8_t *salt,
3962 size_t salt_length,
3963 uint8_t *output,
3964 size_t output_size,
3965 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003968 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003969 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003970
Darryl Green5cc689a2018-07-24 15:34:10 +01003971 (void) input;
3972 (void) input_length;
3973 (void) salt;
3974 (void) output;
3975 (void) output_size;
3976
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003977 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003979 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3980 return( PSA_ERROR_INVALID_ARGUMENT );
3981
Ronald Cron5c522922020-11-14 16:35:34 +01003982 status = psa_get_and_lock_transparent_key_slot_with_policy(
3983 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003984 if( status != PSA_SUCCESS )
3985 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003986 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3987 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003988 {
3989 status = PSA_ERROR_INVALID_ARGUMENT;
3990 goto exit;
3991 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992
John Durkop6ba40d12020-11-10 08:50:04 -08003993#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3994 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003995 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003997 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003998 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3999 slot->key.data,
4000 slot->key.bytes,
4001 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004002 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004003 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004004
4005 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004006 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004007 status = PSA_ERROR_BUFFER_TOO_SMALL;
4008 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004009 }
John Durkop0e005192020-10-31 22:06:54 -07004010#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004011 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004013 status = mbedtls_to_psa_error(
4014 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004015 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004016 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004017 MBEDTLS_RSA_PUBLIC,
4018 input_length,
4019 input,
4020 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021 }
4022 else
John Durkop0e005192020-10-31 22:06:54 -07004023#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4024#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004025 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004027 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004028 status = mbedtls_to_psa_error(
4029 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004030 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004031 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004032 MBEDTLS_RSA_PUBLIC,
4033 salt, salt_length,
4034 input_length,
4035 input,
4036 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004037 }
4038 else
John Durkop0e005192020-10-31 22:06:54 -07004039#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004041 status = PSA_ERROR_INVALID_ARGUMENT;
4042 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004044rsa_exit:
4045 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004046 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004047
Steven Cooremana2371e52020-07-28 14:30:39 +02004048 mbedtls_rsa_free( rsa );
4049 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004051 else
John Durkop9814fa22020-11-04 12:28:15 -08004052#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004053 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004055 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004057
4058exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004059 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004060
Ronald Cron5c522922020-11-14 16:35:34 +01004061 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004062}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063
Ronald Croncf56a0a2020-08-04 09:51:30 +02004064psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004065 psa_algorithm_t alg,
4066 const uint8_t *input,
4067 size_t input_length,
4068 const uint8_t *salt,
4069 size_t salt_length,
4070 uint8_t *output,
4071 size_t output_size,
4072 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004073{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004076 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004077
Darryl Green5cc689a2018-07-24 15:34:10 +01004078 (void) input;
4079 (void) input_length;
4080 (void) salt;
4081 (void) output;
4082 (void) output_size;
4083
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004084 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004086 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4087 return( PSA_ERROR_INVALID_ARGUMENT );
4088
Ronald Cron5c522922020-11-14 16:35:34 +01004089 status = psa_get_and_lock_transparent_key_slot_with_policy(
4090 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004091 if( status != PSA_SUCCESS )
4092 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004093 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004094 {
4095 status = PSA_ERROR_INVALID_ARGUMENT;
4096 goto exit;
4097 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098
John Durkop6ba40d12020-11-10 08:50:04 -08004099#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4100 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004101 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004102 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004103 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004104 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4105 slot->key.data,
4106 slot->key.bytes,
4107 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004108 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004109 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110
Steven Cooremana2371e52020-07-28 14:30:39 +02004111 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004112 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004113 status = PSA_ERROR_INVALID_ARGUMENT;
4114 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004115 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116
John Durkop0e005192020-10-31 22:06:54 -07004117#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004118 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004119 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004120 status = mbedtls_to_psa_error(
4121 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004122 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004123 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004124 MBEDTLS_RSA_PRIVATE,
4125 output_length,
4126 input,
4127 output,
4128 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004129 }
4130 else
John Durkop0e005192020-10-31 22:06:54 -07004131#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4132#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004133 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004134 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004135 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004136 status = mbedtls_to_psa_error(
4137 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004138 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004139 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004140 MBEDTLS_RSA_PRIVATE,
4141 salt, salt_length,
4142 output_length,
4143 input,
4144 output,
4145 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146 }
4147 else
John Durkop0e005192020-10-31 22:06:54 -07004148#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004149 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004150 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004152
Steven Cooreman4fed4552020-08-03 14:46:03 +02004153rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004154 mbedtls_rsa_free( rsa );
4155 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157 else
John Durkop6ba40d12020-11-10 08:50:04 -08004158#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4159 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004161 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004162 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004163
4164exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004165 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004166
Ronald Cron5c522922020-11-14 16:35:34 +01004167 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004168}
Gilles Peskine20035e32018-02-03 22:44:14 +01004169
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004170
4171
mohammad1603503973b2018-03-12 15:59:30 +02004172/****************************************************************/
4173/* Symmetric cryptography */
4174/****************************************************************/
4175
Gilles Peskinee553c652018-06-04 16:22:46 +02004176static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004177 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004178 psa_algorithm_t alg,
4179 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004180{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004182 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004183 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004184 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004185 size_t key_bits;
4186 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004187 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4188 PSA_KEY_USAGE_ENCRYPT :
4189 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004190
Steven Cooremand3feccd2020-09-01 15:56:14 +02004191 /* A context must be freshly initialized before it can be set up. */
4192 if( operation->alg != 0 )
4193 return( PSA_ERROR_BAD_STATE );
4194
Steven Cooremana07b9972020-09-10 14:54:14 +02004195 /* The requested algorithm must be one that can be processed by cipher. */
4196 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004197 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004198
Steven Cooremanef8575e2020-09-11 11:44:50 +02004199 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004200 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004201 if( status != PSA_SUCCESS )
4202 goto exit;
4203
4204 /* Initialize the operation struct members, except for alg. The alg member
4205 * is used to indicate to psa_cipher_abort that there are resources to free,
4206 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004207 operation->key_set = 0;
4208 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004209 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004210 operation->iv_size = 0;
4211 operation->block_size = 0;
4212 if( alg == PSA_ALG_ECB_NO_PADDING )
4213 operation->iv_required = 0;
4214 else
4215 operation->iv_required = 1;
4216
Steven Cooremana07b9972020-09-10 14:54:14 +02004217 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004218 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004219 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004220 slot,
4221 alg );
4222 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004223 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004224 slot,
4225 alg );
4226
Steven Cooremanef8575e2020-09-11 11:44:50 +02004227 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004228 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004229 /* Once the driver context is initialised, it needs to be freed using
4230 * psa_cipher_abort. Indicate this through setting alg. */
4231 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004232 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004233
Steven Cooremand3feccd2020-09-01 15:56:14 +02004234 if( status != PSA_ERROR_NOT_SUPPORTED ||
4235 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4236 goto exit;
4237
Steven Cooremana07b9972020-09-10 14:54:14 +02004238 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004239 * available for the given algorithm & key. */
4240 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004241
Steven Cooremana07b9972020-09-10 14:54:14 +02004242 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004243 * psa_cipher_abort. Indicate there is something to be freed through setting
4244 * alg, and indicate the operation is being done using mbedtls crypto through
4245 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004246 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004247 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004248
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004249 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004250 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004251 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004252 {
4253 status = PSA_ERROR_NOT_SUPPORTED;
4254 goto exit;
4255 }
mohammad1603503973b2018-03-12 15:59:30 +02004256
mohammad1603503973b2018-03-12 15:59:30 +02004257 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004258 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004259 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004260
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004261#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004262 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004263 {
4264 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004265 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004266 memcpy( keys, slot->key.data, 16 );
4267 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004268 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4269 keys,
4270 192, cipher_operation );
4271 }
4272 else
4273#endif
4274 {
4275 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004276 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004277 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004278 }
Moran Peker41deec42018-04-04 15:43:05 +03004279 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004280 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004281
mohammad16038481e742018-03-18 13:57:31 +02004282#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004283 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004284 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004285 case PSA_ALG_CBC_NO_PADDING:
4286 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4287 MBEDTLS_PADDING_NONE );
4288 break;
4289 case PSA_ALG_CBC_PKCS7:
4290 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4291 MBEDTLS_PADDING_PKCS7 );
4292 break;
4293 default:
4294 /* The algorithm doesn't involve padding. */
4295 ret = 0;
4296 break;
4297 }
4298 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004299 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004300#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4301
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004302 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004303 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004304 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4305 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004306 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004307 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004308 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004309#if defined(MBEDTLS_CHACHA20_C)
4310 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004311 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004312 operation->iv_size = 12;
4313#endif
mohammad1603503973b2018-03-12 15:59:30 +02004314
Steven Cooreman7df02922020-09-09 15:28:49 +02004315 status = PSA_SUCCESS;
4316
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004317exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004318 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004319 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004320 if( status == PSA_SUCCESS )
4321 {
4322 /* Update operation flags for both driver and software implementations */
4323 operation->key_set = 1;
4324 }
4325 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004326 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004327
Ronald Cron5c522922020-11-14 16:35:34 +01004328 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004329
Ronald Cron5c522922020-11-14 16:35:34 +01004330 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004331}
4332
Gilles Peskinefe119512018-07-08 21:39:34 +02004333psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004334 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004335 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004336{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004337 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004338}
4339
Gilles Peskinefe119512018-07-08 21:39:34 +02004340psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004341 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004342 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004343{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004344 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004345}
4346
Gilles Peskinefe119512018-07-08 21:39:34 +02004347psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004348 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004349 size_t iv_size,
4350 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004351{
itayzafrir534bd7c2018-08-02 13:56:32 +03004352 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004354 if( operation->iv_set || ! operation->iv_required )
4355 {
4356 return( PSA_ERROR_BAD_STATE );
4357 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004358
Steven Cooremanef8575e2020-09-11 11:44:50 +02004359 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004360 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004361 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004362 iv,
4363 iv_size,
4364 iv_length );
4365 goto exit;
4366 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004367
Moran Peker41deec42018-04-04 15:43:05 +03004368 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004369 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004370 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004371 goto exit;
4372 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004373 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004374 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004375 if( ret != 0 )
4376 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004377 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004378 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004379 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004380
mohammad16038481e742018-03-18 13:57:31 +02004381 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004382 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004383
Moran Peker395db872018-05-31 14:07:14 +03004384exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004385 if( status == PSA_SUCCESS )
4386 operation->iv_set = 1;
4387 else
Moran Peker395db872018-05-31 14:07:14 +03004388 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004389 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004390}
4391
Gilles Peskinefe119512018-07-08 21:39:34 +02004392psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004393 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004394 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004395{
itayzafrir534bd7c2018-08-02 13:56:32 +03004396 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004398 if( operation->iv_set || ! operation->iv_required )
4399 {
4400 return( PSA_ERROR_BAD_STATE );
4401 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004402
Steven Cooremanef8575e2020-09-11 11:44:50 +02004403 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004404 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004405 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004406 iv,
4407 iv_length );
4408 goto exit;
4409 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004410
Moran Pekera28258c2018-05-29 16:25:04 +03004411 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004412 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004413 status = PSA_ERROR_INVALID_ARGUMENT;
4414 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004415 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004416 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4417 status = mbedtls_to_psa_error( ret );
4418exit:
4419 if( status == PSA_SUCCESS )
4420 operation->iv_set = 1;
4421 else
mohammad1603503973b2018-03-12 15:59:30 +02004422 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004423 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004424}
4425
Steven Cooreman177deba2020-09-07 17:14:14 +02004426/* Process input for which the algorithm is set to ECB mode. This requires
4427 * manual processing, since the PSA API is defined as being able to process
4428 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4429 * underlying mbedtls_cipher_update only takes full blocks. */
4430static psa_status_t psa_cipher_update_ecb_internal(
4431 mbedtls_cipher_context_t *ctx,
4432 const uint8_t *input,
4433 size_t input_length,
4434 uint8_t *output,
4435 size_t output_size,
4436 size_t *output_length )
4437{
4438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4439 size_t block_size = ctx->cipher_info->block_size;
4440 size_t internal_output_length = 0;
4441 *output_length = 0;
4442
4443 if( input_length == 0 )
4444 {
4445 status = PSA_SUCCESS;
4446 goto exit;
4447 }
4448
4449 if( ctx->unprocessed_len > 0 )
4450 {
4451 /* Fill up to block size, and run the block if there's a full one. */
4452 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4453
4454 if( input_length < bytes_to_copy )
4455 bytes_to_copy = input_length;
4456
4457 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4458 input, bytes_to_copy );
4459 input_length -= bytes_to_copy;
4460 input += bytes_to_copy;
4461 ctx->unprocessed_len += bytes_to_copy;
4462
4463 if( ctx->unprocessed_len == block_size )
4464 {
4465 status = mbedtls_to_psa_error(
4466 mbedtls_cipher_update( ctx,
4467 ctx->unprocessed_data,
4468 block_size,
4469 output, &internal_output_length ) );
4470
4471 if( status != PSA_SUCCESS )
4472 goto exit;
4473
4474 output += internal_output_length;
4475 output_size -= internal_output_length;
4476 *output_length += internal_output_length;
4477 ctx->unprocessed_len = 0;
4478 }
4479 }
4480
4481 while( input_length >= block_size )
4482 {
4483 /* Run all full blocks we have, one by one */
4484 status = mbedtls_to_psa_error(
4485 mbedtls_cipher_update( ctx, input,
4486 block_size,
4487 output, &internal_output_length ) );
4488
4489 if( status != PSA_SUCCESS )
4490 goto exit;
4491
4492 input_length -= block_size;
4493 input += block_size;
4494
4495 output += internal_output_length;
4496 output_size -= internal_output_length;
4497 *output_length += internal_output_length;
4498 }
4499
4500 if( input_length > 0 )
4501 {
4502 /* Save unprocessed bytes for later processing */
4503 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4504 input, input_length );
4505 ctx->unprocessed_len += input_length;
4506 }
4507
4508 status = PSA_SUCCESS;
4509
4510exit:
4511 return( status );
4512}
4513
Gilles Peskinee553c652018-06-04 16:22:46 +02004514psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4515 const uint8_t *input,
4516 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004517 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004518 size_t output_size,
4519 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004520{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004522 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004523 if( operation->alg == 0 )
4524 {
4525 return( PSA_ERROR_BAD_STATE );
4526 }
4527 if( operation->iv_required && ! operation->iv_set )
4528 {
4529 return( PSA_ERROR_BAD_STATE );
4530 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004531
Steven Cooremanef8575e2020-09-11 11:44:50 +02004532 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004533 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004534 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004535 input,
4536 input_length,
4537 output,
4538 output_size,
4539 output_length );
4540 goto exit;
4541 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004542
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004543 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004544 {
4545 /* Take the unprocessed partial block left over from previous
4546 * update calls, if any, plus the input to this call. Remove
4547 * the last partial block, if any. You get the data that will be
4548 * output in this call. */
4549 expected_output_size =
4550 ( operation->ctx.cipher.unprocessed_len + input_length )
4551 / operation->block_size * operation->block_size;
4552 }
4553 else
4554 {
4555 expected_output_size = input_length;
4556 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004557
Gilles Peskine89d789c2018-06-04 17:17:16 +02004558 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004559 {
4560 status = PSA_ERROR_BUFFER_TOO_SMALL;
4561 goto exit;
4562 }
mohammad160382759612018-03-12 18:16:40 +02004563
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004564 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4565 {
4566 /* mbedtls_cipher_update has an API inconsistency: it will only
4567 * process a single block at a time in ECB mode. Abstract away that
4568 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004569 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4570 input,
4571 input_length,
4572 output,
4573 output_size,
4574 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004575 }
4576 else
4577 {
4578 status = mbedtls_to_psa_error(
4579 mbedtls_cipher_update( &operation->ctx.cipher, input,
4580 input_length, output, output_length ) );
4581 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004582exit:
4583 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004584 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004585 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004586}
4587
Gilles Peskinee553c652018-06-04 16:22:46 +02004588psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4589 uint8_t *output,
4590 size_t output_size,
4591 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004592{
David Saadab4ecc272019-02-14 13:48:10 +02004593 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004594 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004595 if( operation->alg == 0 )
4596 {
4597 return( PSA_ERROR_BAD_STATE );
4598 }
4599 if( operation->iv_required && ! operation->iv_set )
4600 {
4601 return( PSA_ERROR_BAD_STATE );
4602 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004603
Steven Cooremanef8575e2020-09-11 11:44:50 +02004604 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004605 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004606 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004607 output,
4608 output_size,
4609 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004610 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004611 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004612
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004613 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004614 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004615 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004616 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004617 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004618 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004619 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004620 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004621 }
4622
Steven Cooremanef8575e2020-09-11 11:44:50 +02004623 status = mbedtls_to_psa_error(
4624 mbedtls_cipher_finish( &operation->ctx.cipher,
4625 temp_output_buffer,
4626 output_length ) );
4627 if( status != PSA_SUCCESS )
4628 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004629
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004630 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004631 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004632 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004633 memcpy( output, temp_output_buffer, *output_length );
4634 else
Janos Follath315b51c2018-07-09 16:04:51 +01004635 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004636
Steven Cooremanef8575e2020-09-11 11:44:50 +02004637exit:
4638 if( operation->mbedtls_in_use == 1 )
4639 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004640
Steven Cooremanef8575e2020-09-11 11:44:50 +02004641 if( status == PSA_SUCCESS )
4642 return( psa_cipher_abort( operation ) );
4643 else
4644 {
4645 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004646 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004647
Steven Cooremanef8575e2020-09-11 11:44:50 +02004648 return( status );
4649 }
mohammad1603503973b2018-03-12 15:59:30 +02004650}
4651
Gilles Peskinee553c652018-06-04 16:22:46 +02004652psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4653{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004654 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004655 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004656 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004657 * in use. It's ok to call abort on such an object, and there's
4658 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004659 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004660 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004661
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004662 /* Sanity check (shouldn't happen: operation->alg should
4663 * always have been initialized to a valid value). */
4664 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4665 return( PSA_ERROR_BAD_STATE );
4666
Steven Cooremanef8575e2020-09-11 11:44:50 +02004667 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004668 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004669 else
4670 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004671
Moran Peker41deec42018-04-04 15:43:05 +03004672 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004673 operation->key_set = 0;
4674 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004675 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004676 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004677 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004678 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004679
Moran Peker395db872018-05-31 14:07:14 +03004680 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004681}
4682
Gilles Peskinea0655c32018-04-30 17:06:50 +02004683
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004684
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004685
mohammad16035955c982018-04-26 00:53:03 +03004686/****************************************************************/
4687/* AEAD */
4688/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004689
Gilles Peskineedf9a652018-08-17 18:11:56 +02004690typedef struct
4691{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004692 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004693 const mbedtls_cipher_info_t *cipher_info;
4694 union
4695 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004696 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004697#if defined(MBEDTLS_CCM_C)
4698 mbedtls_ccm_context ccm;
4699#endif /* MBEDTLS_CCM_C */
4700#if defined(MBEDTLS_GCM_C)
4701 mbedtls_gcm_context gcm;
4702#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004703#if defined(MBEDTLS_CHACHAPOLY_C)
4704 mbedtls_chachapoly_context chachapoly;
4705#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004706 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004707 psa_algorithm_t core_alg;
4708 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004709 uint8_t tag_length;
4710} aead_operation_t;
4711
Ronald Cronf95a2b12020-10-22 15:24:49 +02004712#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4713
Gilles Peskine30a9e412019-01-14 18:36:12 +01004714static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004715{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004716 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004717 {
4718#if defined(MBEDTLS_CCM_C)
4719 case PSA_ALG_CCM:
4720 mbedtls_ccm_free( &operation->ctx.ccm );
4721 break;
4722#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004723#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004724 case PSA_ALG_GCM:
4725 mbedtls_gcm_free( &operation->ctx.gcm );
4726 break;
4727#endif /* MBEDTLS_GCM_C */
4728 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004729
Ronald Cron5c522922020-11-14 16:35:34 +01004730 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731}
4732
4733static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004734 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004735 psa_key_usage_t usage,
4736 psa_algorithm_t alg )
4737{
4738 psa_status_t status;
4739 size_t key_bits;
4740 mbedtls_cipher_id_t cipher_id;
4741
Ronald Cron5c522922020-11-14 16:35:34 +01004742 status = psa_get_and_lock_transparent_key_slot_with_policy(
4743 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004744 if( status != PSA_SUCCESS )
4745 return( status );
4746
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004747 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004748
4749 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004750 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004751 &cipher_id );
4752 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004753 {
4754 status = PSA_ERROR_NOT_SUPPORTED;
4755 goto cleanup;
4756 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004757
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004758 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004759 {
4760#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004761 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4762 operation->core_alg = PSA_ALG_CCM;
4763 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004764 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4765 * The call to mbedtls_ccm_encrypt_and_tag or
4766 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004767 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004768 {
4769 status = PSA_ERROR_INVALID_ARGUMENT;
4770 goto cleanup;
4771 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004772 mbedtls_ccm_init( &operation->ctx.ccm );
4773 status = mbedtls_to_psa_error(
4774 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004775 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004776 (unsigned int) key_bits ) );
4777 if( status != 0 )
4778 goto cleanup;
4779 break;
4780#endif /* MBEDTLS_CCM_C */
4781
4782#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004783 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4784 operation->core_alg = PSA_ALG_GCM;
4785 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004786 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4787 * The call to mbedtls_gcm_crypt_and_tag or
4788 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004789 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004790 {
4791 status = PSA_ERROR_INVALID_ARGUMENT;
4792 goto cleanup;
4793 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004794 mbedtls_gcm_init( &operation->ctx.gcm );
4795 status = mbedtls_to_psa_error(
4796 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004797 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004798 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004799 if( status != 0 )
4800 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004801 break;
4802#endif /* MBEDTLS_GCM_C */
4803
Gilles Peskine26869f22019-05-06 15:25:00 +02004804#if defined(MBEDTLS_CHACHAPOLY_C)
4805 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4806 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4807 operation->full_tag_length = 16;
4808 /* We only support the default tag length. */
4809 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004810 {
4811 status = PSA_ERROR_NOT_SUPPORTED;
4812 goto cleanup;
4813 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004814 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4815 status = mbedtls_to_psa_error(
4816 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004817 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004818 if( status != 0 )
4819 goto cleanup;
4820 break;
4821#endif /* MBEDTLS_CHACHAPOLY_C */
4822
Gilles Peskineedf9a652018-08-17 18:11:56 +02004823 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004824 status = PSA_ERROR_NOT_SUPPORTED;
4825 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004826 }
4827
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004828 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4829 {
4830 status = PSA_ERROR_INVALID_ARGUMENT;
4831 goto cleanup;
4832 }
4833 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004834
Gilles Peskineedf9a652018-08-17 18:11:56 +02004835 return( PSA_SUCCESS );
4836
4837cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004838 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004839 return( status );
4840}
4841
Ronald Croncf56a0a2020-08-04 09:51:30 +02004842psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004843 psa_algorithm_t alg,
4844 const uint8_t *nonce,
4845 size_t nonce_length,
4846 const uint8_t *additional_data,
4847 size_t additional_data_length,
4848 const uint8_t *plaintext,
4849 size_t plaintext_length,
4850 uint8_t *ciphertext,
4851 size_t ciphertext_size,
4852 size_t *ciphertext_length )
4853{
mohammad16035955c982018-04-26 00:53:03 +03004854 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004855 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004856 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004857
mohammad1603f08a5502018-06-03 15:05:47 +03004858 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004859
Ronald Croncf56a0a2020-08-04 09:51:30 +02004860 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004861 if( status != PSA_SUCCESS )
4862 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004863
Gilles Peskineedf9a652018-08-17 18:11:56 +02004864 /* For all currently supported modes, the tag is at the end of the
4865 * ciphertext. */
4866 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4867 {
4868 status = PSA_ERROR_BUFFER_TOO_SMALL;
4869 goto exit;
4870 }
4871 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004872
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004873#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004874 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004875 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876 status = mbedtls_to_psa_error(
4877 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4878 MBEDTLS_GCM_ENCRYPT,
4879 plaintext_length,
4880 nonce, nonce_length,
4881 additional_data, additional_data_length,
4882 plaintext, ciphertext,
4883 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004884 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004885 else
4886#endif /* MBEDTLS_GCM_C */
4887#if defined(MBEDTLS_CCM_C)
4888 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004889 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004890 status = mbedtls_to_psa_error(
4891 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4892 plaintext_length,
4893 nonce, nonce_length,
4894 additional_data,
4895 additional_data_length,
4896 plaintext, ciphertext,
4897 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004898 }
mohammad16035c8845f2018-05-09 05:40:09 -07004899 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004900#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004901#if defined(MBEDTLS_CHACHAPOLY_C)
4902 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4903 {
4904 if( nonce_length != 12 || operation.tag_length != 16 )
4905 {
4906 status = PSA_ERROR_NOT_SUPPORTED;
4907 goto exit;
4908 }
4909 status = mbedtls_to_psa_error(
4910 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4911 plaintext_length,
4912 nonce,
4913 additional_data,
4914 additional_data_length,
4915 plaintext,
4916 ciphertext,
4917 tag ) );
4918 }
4919 else
4920#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004921 {
mohammad1603554faad2018-06-03 15:07:38 +03004922 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004923 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004924
Gilles Peskineedf9a652018-08-17 18:11:56 +02004925 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4926 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004927
Gilles Peskineedf9a652018-08-17 18:11:56 +02004928exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004929 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004930 if( status == PSA_SUCCESS )
4931 *ciphertext_length = plaintext_length + operation.tag_length;
4932 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004933}
4934
Gilles Peskineee652a32018-06-01 19:23:52 +02004935/* Locate the tag in a ciphertext buffer containing the encrypted data
4936 * followed by the tag. Return the length of the part preceding the tag in
4937 * *plaintext_length. This is the size of the plaintext in modes where
4938 * the encrypted data has the same size as the plaintext, such as
4939 * CCM and GCM. */
4940static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4941 const uint8_t *ciphertext,
4942 size_t ciphertext_length,
4943 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004944 const uint8_t **p_tag )
4945{
4946 size_t payload_length;
4947 if( tag_length > ciphertext_length )
4948 return( PSA_ERROR_INVALID_ARGUMENT );
4949 payload_length = ciphertext_length - tag_length;
4950 if( payload_length > plaintext_size )
4951 return( PSA_ERROR_BUFFER_TOO_SMALL );
4952 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004953 return( PSA_SUCCESS );
4954}
4955
Ronald Croncf56a0a2020-08-04 09:51:30 +02004956psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004957 psa_algorithm_t alg,
4958 const uint8_t *nonce,
4959 size_t nonce_length,
4960 const uint8_t *additional_data,
4961 size_t additional_data_length,
4962 const uint8_t *ciphertext,
4963 size_t ciphertext_length,
4964 uint8_t *plaintext,
4965 size_t plaintext_size,
4966 size_t *plaintext_length )
4967{
mohammad16035955c982018-04-26 00:53:03 +03004968 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004969 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004970 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004971
Gilles Peskineee652a32018-06-01 19:23:52 +02004972 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004973
Ronald Croncf56a0a2020-08-04 09:51:30 +02004974 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004975 if( status != PSA_SUCCESS )
4976 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004977
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004978 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4979 ciphertext, ciphertext_length,
4980 plaintext_size, &tag );
4981 if( status != PSA_SUCCESS )
4982 goto exit;
4983
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004984#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004985 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004986 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004987 status = mbedtls_to_psa_error(
4988 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4989 ciphertext_length - operation.tag_length,
4990 nonce, nonce_length,
4991 additional_data,
4992 additional_data_length,
4993 tag, operation.tag_length,
4994 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004995 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004996 else
4997#endif /* MBEDTLS_GCM_C */
4998#if defined(MBEDTLS_CCM_C)
4999 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005000 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005001 status = mbedtls_to_psa_error(
5002 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5003 ciphertext_length - operation.tag_length,
5004 nonce, nonce_length,
5005 additional_data,
5006 additional_data_length,
5007 ciphertext, plaintext,
5008 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005009 }
mohammad160339574652018-06-01 04:39:53 -07005010 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005011#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005012#if defined(MBEDTLS_CHACHAPOLY_C)
5013 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5014 {
5015 if( nonce_length != 12 || operation.tag_length != 16 )
5016 {
5017 status = PSA_ERROR_NOT_SUPPORTED;
5018 goto exit;
5019 }
5020 status = mbedtls_to_psa_error(
5021 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5022 ciphertext_length - operation.tag_length,
5023 nonce,
5024 additional_data,
5025 additional_data_length,
5026 tag,
5027 ciphertext,
5028 plaintext ) );
5029 }
5030 else
5031#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005032 {
mohammad1603554faad2018-06-03 15:07:38 +03005033 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005034 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005035
Gilles Peskineedf9a652018-08-17 18:11:56 +02005036 if( status != PSA_SUCCESS && plaintext_size != 0 )
5037 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005038
Gilles Peskineedf9a652018-08-17 18:11:56 +02005039exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005040 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005041 if( status == PSA_SUCCESS )
5042 *plaintext_length = ciphertext_length - operation.tag_length;
5043 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005044}
5045
Gilles Peskinea0655c32018-04-30 17:06:50 +02005046
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005047
Gilles Peskine20035e32018-02-03 22:44:14 +01005048/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005049/* Generators */
5050/****************************************************************/
5051
John Durkop07cc04a2020-11-16 22:08:34 -08005052#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5053 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5054 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5055#define AT_LEAST_ONE_BUILTIN_KDF
5056#endif
5057
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005058#define HKDF_STATE_INIT 0 /* no input yet */
5059#define HKDF_STATE_STARTED 1 /* got salt */
5060#define HKDF_STATE_KEYED 2 /* got key */
5061#define HKDF_STATE_OUTPUT 3 /* output started */
5062
Gilles Peskinecbe66502019-05-16 16:59:18 +02005063static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005065{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5067 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005068 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005070}
5071
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005073{
5074 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005076 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005077 {
5078 /* The object has (apparently) been initialized but it is not
5079 * in use. It's ok to call abort on such an object, and there's
5080 * nothing to do. */
5081 }
5082 else
John Durkopd0321952020-10-29 21:37:36 -07005083#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005084 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005085 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 mbedtls_free( operation->ctx.hkdf.info );
5087 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005088 }
John Durkop07cc04a2020-11-16 22:08:34 -08005089 else
5090#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5091#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5092 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5093 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005095 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005096 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005097 if( operation->ctx.tls12_prf.seed != NULL )
5098 {
5099 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5100 operation->ctx.tls12_prf.seed_length );
5101 mbedtls_free( operation->ctx.tls12_prf.seed );
5102 }
5103
5104 if( operation->ctx.tls12_prf.label != NULL )
5105 {
5106 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5107 operation->ctx.tls12_prf.label_length );
5108 mbedtls_free( operation->ctx.tls12_prf.label );
5109 }
5110
5111 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5112
5113 /* We leave the fields Ai and output_block to be erased safely by the
5114 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005115 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005116 else
John Durkop07cc04a2020-11-16 22:08:34 -08005117#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5118 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005119 {
5120 status = PSA_ERROR_BAD_STATE;
5121 }
Janos Follath083036a2019-06-11 10:22:26 +01005122 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005123 return( status );
5124}
5125
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005127 size_t *capacity)
5128{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005130 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005132 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005133 }
5134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136 return( PSA_SUCCESS );
5137}
5138
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005140 size_t capacity )
5141{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005143 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005147 return( PSA_SUCCESS );
5148}
5149
John Durkopd0321952020-10-29 21:37:36 -07005150#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005152 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005153static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005154 psa_algorithm_t hash_alg,
5155 uint8_t *output,
5156 size_t output_length )
5157{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005158 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005159 psa_status_t status;
5160
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005161 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5162 return( PSA_ERROR_BAD_STATE );
5163 hkdf->state = HKDF_STATE_OUTPUT;
5164
Gilles Peskinebef7f142018-07-12 17:22:21 +02005165 while( output_length != 0 )
5166 {
5167 /* Copy what remains of the current block */
5168 uint8_t n = hash_length - hkdf->offset_in_block;
5169 if( n > output_length )
5170 n = (uint8_t) output_length;
5171 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5172 output += n;
5173 output_length -= n;
5174 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005175 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005176 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005177 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005178 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005180 * object was corrupted or if this function is called directly
5181 * inside the library. */
5182 if( hkdf->block_number == 0xff )
5183 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005184
5185 /* We need a new block */
5186 ++hkdf->block_number;
5187 hkdf->offset_in_block = 0;
5188 status = psa_hmac_setup_internal( &hkdf->hmac,
5189 hkdf->prk, hash_length,
5190 hash_alg );
5191 if( status != PSA_SUCCESS )
5192 return( status );
5193 if( hkdf->block_number != 1 )
5194 {
5195 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5196 hkdf->output_block,
5197 hash_length );
5198 if( status != PSA_SUCCESS )
5199 return( status );
5200 }
5201 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5202 hkdf->info,
5203 hkdf->info_length );
5204 if( status != PSA_SUCCESS )
5205 return( status );
5206 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5207 &hkdf->block_number, 1 );
5208 if( status != PSA_SUCCESS )
5209 return( status );
5210 status = psa_hmac_finish_internal( &hkdf->hmac,
5211 hkdf->output_block,
5212 sizeof( hkdf->output_block ) );
5213 if( status != PSA_SUCCESS )
5214 return( status );
5215 }
5216
5217 return( PSA_SUCCESS );
5218}
John Durkop07cc04a2020-11-16 22:08:34 -08005219#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005220
John Durkop07cc04a2020-11-16 22:08:34 -08005221#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5222 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005223static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5224 psa_tls12_prf_key_derivation_t *tls12_prf,
5225 psa_algorithm_t alg )
5226{
5227 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005228 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005229 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5230 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005231
Janos Follath7742fee2019-06-17 12:58:10 +01005232 /* We can't be wanting more output after block 0xff, otherwise
5233 * the capacity check in psa_key_derivation_output_bytes() would have
5234 * prevented this call. It could happen only if the operation
5235 * object was corrupted or if this function is called directly
5236 * inside the library. */
5237 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005238 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005239
5240 /* We need a new block */
5241 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005242 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005243
5244 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5245 *
5246 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5247 *
5248 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5249 * HMAC_hash(secret, A(2) + seed) +
5250 * HMAC_hash(secret, A(3) + seed) + ...
5251 *
5252 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005253 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005254 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005255 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005256 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005257 * is currently extracted as `output_block` and where i is
5258 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005259 */
5260
Janos Follathea29bfb2019-06-19 12:21:20 +01005261 /* Save the hash context before using it, to preserve the hash state with
5262 * only the inner padding in it. We need this, because inner padding depends
5263 * on the key (secret in the RFC's terminology). */
5264 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5265 if( status != PSA_SUCCESS )
5266 goto cleanup;
5267
5268 /* Calculate A(i) where i = tls12_prf->block_number. */
5269 if( tls12_prf->block_number == 1 )
5270 {
5271 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5272 * the variable seed and in this instance means it in the context of the
5273 * P_hash function, where seed = label + seed.) */
5274 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5275 tls12_prf->label, tls12_prf->label_length );
5276 if( status != PSA_SUCCESS )
5277 goto cleanup;
5278 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5279 tls12_prf->seed, tls12_prf->seed_length );
5280 if( status != PSA_SUCCESS )
5281 goto cleanup;
5282 }
5283 else
5284 {
5285 /* A(i) = HMAC_hash(secret, A(i-1)) */
5286 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5287 tls12_prf->Ai, hash_length );
5288 if( status != PSA_SUCCESS )
5289 goto cleanup;
5290 }
5291
5292 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5293 tls12_prf->Ai, hash_length );
5294 if( status != PSA_SUCCESS )
5295 goto cleanup;
5296 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5297 if( status != PSA_SUCCESS )
5298 goto cleanup;
5299
5300 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5301 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5302 tls12_prf->Ai, hash_length );
5303 if( status != PSA_SUCCESS )
5304 goto cleanup;
5305 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5306 tls12_prf->label, tls12_prf->label_length );
5307 if( status != PSA_SUCCESS )
5308 goto cleanup;
5309 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5310 tls12_prf->seed, tls12_prf->seed_length );
5311 if( status != PSA_SUCCESS )
5312 goto cleanup;
5313 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5314 tls12_prf->output_block, hash_length );
5315 if( status != PSA_SUCCESS )
5316 goto cleanup;
5317 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5318 if( status != PSA_SUCCESS )
5319 goto cleanup;
5320
Janos Follath7742fee2019-06-17 12:58:10 +01005321
5322cleanup:
5323
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 cleanup_status = psa_hash_abort( &backup );
5325 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5326 status = cleanup_status;
5327
5328 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005329}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005330
Janos Follath844eb0e2019-06-19 12:10:49 +01005331static psa_status_t psa_key_derivation_tls12_prf_read(
5332 psa_tls12_prf_key_derivation_t *tls12_prf,
5333 psa_algorithm_t alg,
5334 uint8_t *output,
5335 size_t output_length )
5336{
5337 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005338 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005339 psa_status_t status;
5340 uint8_t offset, length;
5341
5342 while( output_length != 0 )
5343 {
5344 /* Check if we have fully processed the current block. */
5345 if( tls12_prf->left_in_block == 0 )
5346 {
5347 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5348 alg );
5349 if( status != PSA_SUCCESS )
5350 return( status );
5351
5352 continue;
5353 }
5354
5355 if( tls12_prf->left_in_block > output_length )
5356 length = (uint8_t) output_length;
5357 else
5358 length = tls12_prf->left_in_block;
5359
5360 offset = hash_length - tls12_prf->left_in_block;
5361 memcpy( output, tls12_prf->output_block + offset, length );
5362 output += length;
5363 output_length -= length;
5364 tls12_prf->left_in_block -= length;
5365 }
5366
5367 return( PSA_SUCCESS );
5368}
John Durkop07cc04a2020-11-16 22:08:34 -08005369#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5370 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005371
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005372psa_status_t psa_key_derivation_output_bytes(
5373 psa_key_derivation_operation_t *operation,
5374 uint8_t *output,
5375 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005376{
5377 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005379
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005380 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005381 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005383 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005384 }
5385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005387 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005388 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005389 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005391 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005392 goto exit;
5393 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005394 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005395 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005396 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005397 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005398 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5399 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005400 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005401 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005402 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005403 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005405
John Durkopd0321952020-10-29 21:37:36 -07005406#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005408 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005409 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005411 output, output_length );
5412 }
Janos Follathadbec812019-06-14 11:05:39 +01005413 else
John Durkop07cc04a2020-11-16 22:08:34 -08005414#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5415#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5416 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005417 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005418 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005419 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005421 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005422 output_length );
5423 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005424 else
John Durkop07cc04a2020-11-16 22:08:34 -08005425#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5426 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427 {
5428 return( PSA_ERROR_BAD_STATE );
5429 }
5430
5431exit:
5432 if( status != PSA_SUCCESS )
5433 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005434 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 * This allows us to differentiate between exhausted operations and
5436 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5437 * operations. */
5438 psa_algorithm_t alg = operation->alg;
5439 psa_key_derivation_abort( operation );
5440 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 memset( output, '!', output_length );
5442 }
5443 return( status );
5444}
5445
Gilles Peskine08542d82018-07-19 17:05:42 +02005446#if defined(MBEDTLS_DES_C)
5447static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5448{
5449 if( data_size >= 8 )
5450 mbedtls_des_key_set_parity( data );
5451 if( data_size >= 16 )
5452 mbedtls_des_key_set_parity( data + 8 );
5453 if( data_size >= 24 )
5454 mbedtls_des_key_set_parity( data + 16 );
5455}
5456#endif /* MBEDTLS_DES_C */
5457
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005458static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005459 psa_key_slot_t *slot,
5460 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005462{
5463 uint8_t *data = NULL;
5464 size_t bytes = PSA_BITS_TO_BYTES( bits );
5465 psa_status_t status;
5466
Gilles Peskine8e338702019-07-30 20:06:31 +02005467 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005468 return( PSA_ERROR_INVALID_ARGUMENT );
5469 if( bits % 8 != 0 )
5470 return( PSA_ERROR_INVALID_ARGUMENT );
5471 data = mbedtls_calloc( 1, bytes );
5472 if( data == NULL )
5473 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5474
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005476 if( status != PSA_SUCCESS )
5477 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005478#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005479 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005480 psa_des_set_key_parity( data, bytes );
5481#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005482 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005483
5484exit:
5485 mbedtls_free( data );
5486 return( status );
5487}
5488
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005489psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005490 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005491 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005492{
5493 psa_status_t status;
5494 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005495 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005496
Ronald Cron81709fc2020-11-14 12:10:32 +01005497 *key = MBEDTLS_SVC_KEY_ID_INIT;
5498
Gilles Peskine0f84d622019-09-12 19:03:13 +02005499 /* Reject any attempt to create a zero-length key so that we don't
5500 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5501 if( psa_get_key_bits( attributes ) == 0 )
5502 return( PSA_ERROR_INVALID_ARGUMENT );
5503
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005504 if( ! operation->can_output_key )
5505 return( PSA_ERROR_NOT_PERMITTED );
5506
Ronald Cron81709fc2020-11-14 12:10:32 +01005507 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5508 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005509#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5510 if( driver != NULL )
5511 {
5512 /* Deriving a key in a secure element is not implemented yet. */
5513 status = PSA_ERROR_NOT_SUPPORTED;
5514 }
5515#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005516 if( status == PSA_SUCCESS )
5517 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005518 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005519 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005520 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005521 }
5522 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005523 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005524 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005525 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005526
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005527 return( status );
5528}
5529
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530
5531
5532/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005533/* Key derivation */
5534/****************************************************************/
5535
John Durkop07cc04a2020-11-16 22:08:34 -08005536#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005537static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005538 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005539 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005540{
John Durkop07cc04a2020-11-16 22:08:34 -08005541 int is_kdf_alg_supported;
5542
Janos Follath5fe19732019-06-20 15:09:30 +01005543 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5544 * initialisers for this union leave some bytes unspecified.) */
5545 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5546
Gilles Peskine969c5d62019-01-16 15:53:06 +01005547 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005548#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005549 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5550 is_kdf_alg_supported = 1;
5551 else
5552#endif
5553#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5554 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5555 is_kdf_alg_supported = 1;
5556 else
5557#endif
5558#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5559 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5560 is_kdf_alg_supported = 1;
5561 else
5562#endif
5563 is_kdf_alg_supported = 0;
5564
5565 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005566 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005567 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005568 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005569 if( hash_size == 0 )
5570 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005571 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5572 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005573 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 {
5575 return( PSA_ERROR_NOT_SUPPORTED );
5576 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005578 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005579 }
John Durkop07cc04a2020-11-16 22:08:34 -08005580
5581 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005582}
John Durkop07cc04a2020-11-16 22:08:34 -08005583#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005584
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005586 psa_algorithm_t alg )
5587{
5588 psa_status_t status;
5589
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005590 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005591 return( PSA_ERROR_BAD_STATE );
5592
Gilles Peskine6843c292019-01-18 16:44:49 +01005593 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5594 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005595#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005596 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005597 {
5598 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005599 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005600 }
5601 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5602 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005603 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005604 }
John Durkop07cc04a2020-11-16 22:08:34 -08005605#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005606 else
5607 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005608
5609 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005610 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005611 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005612}
5613
John Durkopd0321952020-10-29 21:37:36 -07005614#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005615static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005616 psa_algorithm_t hash_alg,
5617 psa_key_derivation_step_t step,
5618 const uint8_t *data,
5619 size_t data_length )
5620{
5621 psa_status_t status;
5622 switch( step )
5623 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005624 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005625 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005626 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005627 status = psa_hmac_setup_internal( &hkdf->hmac,
5628 data, data_length,
5629 hash_alg );
5630 if( status != PSA_SUCCESS )
5631 return( status );
5632 hkdf->state = HKDF_STATE_STARTED;
5633 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005634 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005635 /* If no salt was provided, use an empty salt. */
5636 if( hkdf->state == HKDF_STATE_INIT )
5637 {
5638 status = psa_hmac_setup_internal( &hkdf->hmac,
5639 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005640 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005641 if( status != PSA_SUCCESS )
5642 return( status );
5643 hkdf->state = HKDF_STATE_STARTED;
5644 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005645 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005646 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005647 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5648 data, data_length );
5649 if( status != PSA_SUCCESS )
5650 return( status );
5651 status = psa_hmac_finish_internal( &hkdf->hmac,
5652 hkdf->prk,
5653 sizeof( hkdf->prk ) );
5654 if( status != PSA_SUCCESS )
5655 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005656 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005657 hkdf->block_number = 0;
5658 hkdf->state = HKDF_STATE_KEYED;
5659 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005660 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005661 if( hkdf->state == HKDF_STATE_OUTPUT )
5662 return( PSA_ERROR_BAD_STATE );
5663 if( hkdf->info_set )
5664 return( PSA_ERROR_BAD_STATE );
5665 hkdf->info_length = data_length;
5666 if( data_length != 0 )
5667 {
5668 hkdf->info = mbedtls_calloc( 1, data_length );
5669 if( hkdf->info == NULL )
5670 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5671 memcpy( hkdf->info, data, data_length );
5672 }
5673 hkdf->info_set = 1;
5674 return( PSA_SUCCESS );
5675 default:
5676 return( PSA_ERROR_INVALID_ARGUMENT );
5677 }
5678}
John Durkop07cc04a2020-11-16 22:08:34 -08005679#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005680
John Durkop07cc04a2020-11-16 22:08:34 -08005681#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5682 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005683static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5684 const uint8_t *data,
5685 size_t data_length )
5686{
5687 if( prf->state != TLS12_PRF_STATE_INIT )
5688 return( PSA_ERROR_BAD_STATE );
5689
Janos Follathd6dce9f2019-07-04 09:11:38 +01005690 if( data_length != 0 )
5691 {
5692 prf->seed = mbedtls_calloc( 1, data_length );
5693 if( prf->seed == NULL )
5694 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005695
Janos Follathd6dce9f2019-07-04 09:11:38 +01005696 memcpy( prf->seed, data, data_length );
5697 prf->seed_length = data_length;
5698 }
Janos Follathf08e2652019-06-13 09:05:41 +01005699
5700 prf->state = TLS12_PRF_STATE_SEED_SET;
5701
5702 return( PSA_SUCCESS );
5703}
5704
Janos Follath81550542019-06-13 14:26:34 +01005705static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5706 psa_algorithm_t hash_alg,
5707 const uint8_t *data,
5708 size_t data_length )
5709{
5710 psa_status_t status;
5711 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5712 return( PSA_ERROR_BAD_STATE );
5713
5714 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5715 if( status != PSA_SUCCESS )
5716 return( status );
5717
5718 prf->state = TLS12_PRF_STATE_KEY_SET;
5719
5720 return( PSA_SUCCESS );
5721}
5722
Janos Follath63028dd2019-06-13 09:15:47 +01005723static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5724 const uint8_t *data,
5725 size_t data_length )
5726{
5727 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5728 return( PSA_ERROR_BAD_STATE );
5729
Janos Follathd6dce9f2019-07-04 09:11:38 +01005730 if( data_length != 0 )
5731 {
5732 prf->label = mbedtls_calloc( 1, data_length );
5733 if( prf->label == NULL )
5734 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005735
Janos Follathd6dce9f2019-07-04 09:11:38 +01005736 memcpy( prf->label, data, data_length );
5737 prf->label_length = data_length;
5738 }
Janos Follath63028dd2019-06-13 09:15:47 +01005739
5740 prf->state = TLS12_PRF_STATE_LABEL_SET;
5741
5742 return( PSA_SUCCESS );
5743}
5744
Janos Follathb03233e2019-06-11 15:30:30 +01005745static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5746 psa_algorithm_t hash_alg,
5747 psa_key_derivation_step_t step,
5748 const uint8_t *data,
5749 size_t data_length )
5750{
Janos Follathb03233e2019-06-11 15:30:30 +01005751 switch( step )
5752 {
Janos Follathf08e2652019-06-13 09:05:41 +01005753 case PSA_KEY_DERIVATION_INPUT_SEED:
5754 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005755 case PSA_KEY_DERIVATION_INPUT_SECRET:
5756 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005757 case PSA_KEY_DERIVATION_INPUT_LABEL:
5758 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005759 default:
5760 return( PSA_ERROR_INVALID_ARGUMENT );
5761 }
5762}
John Durkop07cc04a2020-11-16 22:08:34 -08005763#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5764 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5765
5766#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5767static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5768 psa_tls12_prf_key_derivation_t *prf,
5769 psa_algorithm_t hash_alg,
5770 const uint8_t *data,
5771 size_t data_length )
5772{
5773 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005774 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005775 uint8_t *cur = pms;
5776
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005777 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005778 return( PSA_ERROR_INVALID_ARGUMENT );
5779
5780 /* Quoting RFC 4279, Section 2:
5781 *
5782 * The premaster secret is formed as follows: if the PSK is N octets
5783 * long, concatenate a uint16 with the value N, N zero octets, a second
5784 * uint16 with the value N, and the PSK itself.
5785 */
5786
5787 *cur++ = ( data_length >> 8 ) & 0xff;
5788 *cur++ = ( data_length >> 0 ) & 0xff;
5789 memset( cur, 0, data_length );
5790 cur += data_length;
5791 *cur++ = pms[0];
5792 *cur++ = pms[1];
5793 memcpy( cur, data, data_length );
5794 cur += data_length;
5795
5796 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5797
5798 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5799 return( status );
5800}
Janos Follath6660f0e2019-06-17 08:44:03 +01005801
5802static psa_status_t psa_tls12_prf_psk_to_ms_input(
5803 psa_tls12_prf_key_derivation_t *prf,
5804 psa_algorithm_t hash_alg,
5805 psa_key_derivation_step_t step,
5806 const uint8_t *data,
5807 size_t data_length )
5808{
5809 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005810 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005811 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5812 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005813 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005814
5815 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5816}
John Durkop07cc04a2020-11-16 22:08:34 -08005817#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005818
Gilles Peskineb8965192019-09-24 16:21:10 +02005819/** Check whether the given key type is acceptable for the given
5820 * input step of a key derivation.
5821 *
5822 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5823 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5824 * Both secret and non-secret inputs can alternatively have the type
5825 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5826 * that the input was passed as a buffer rather than via a key object.
5827 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005828static int psa_key_derivation_check_input_type(
5829 psa_key_derivation_step_t step,
5830 psa_key_type_t key_type )
5831{
5832 switch( step )
5833 {
5834 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005835 if( key_type == PSA_KEY_TYPE_DERIVE )
5836 return( PSA_SUCCESS );
5837 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005838 return( PSA_SUCCESS );
5839 break;
5840 case PSA_KEY_DERIVATION_INPUT_LABEL:
5841 case PSA_KEY_DERIVATION_INPUT_SALT:
5842 case PSA_KEY_DERIVATION_INPUT_INFO:
5843 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005844 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5845 return( PSA_SUCCESS );
5846 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005847 return( PSA_SUCCESS );
5848 break;
5849 }
5850 return( PSA_ERROR_INVALID_ARGUMENT );
5851}
5852
Janos Follathb80a94e2019-06-12 15:54:46 +01005853static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005854 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005855 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005856 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005857 const uint8_t *data,
5858 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005859{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005860 psa_status_t status;
5861 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5862
5863 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005864 if( status != PSA_SUCCESS )
5865 goto exit;
5866
John Durkopd0321952020-10-29 21:37:36 -07005867#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005868 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005869 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005871 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005872 step, data, data_length );
5873 }
John Durkop07cc04a2020-11-16 22:08:34 -08005874 else
5875#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5876#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5877 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005878 {
Janos Follathb03233e2019-06-11 15:30:30 +01005879 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5880 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5881 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005882 }
John Durkop07cc04a2020-11-16 22:08:34 -08005883 else
5884#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5885#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5886 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005887 {
5888 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5889 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5890 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005891 }
5892 else
John Durkop07cc04a2020-11-16 22:08:34 -08005893#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005894 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005895 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005896 return( PSA_ERROR_BAD_STATE );
5897 }
5898
Gilles Peskine224b0d62019-09-23 18:13:17 +02005899exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005900 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005901 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005902 return( status );
5903}
5904
Janos Follath51f4a0f2019-06-14 11:35:55 +01005905psa_status_t psa_key_derivation_input_bytes(
5906 psa_key_derivation_operation_t *operation,
5907 psa_key_derivation_step_t step,
5908 const uint8_t *data,
5909 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005910{
Gilles Peskineb8965192019-09-24 16:21:10 +02005911 return( psa_key_derivation_input_internal( operation, step,
5912 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005913 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005914}
5915
Janos Follath51f4a0f2019-06-14 11:35:55 +01005916psa_status_t psa_key_derivation_input_key(
5917 psa_key_derivation_operation_t *operation,
5918 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005919 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005920{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005921 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005922 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005923 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005924
Ronald Cron5c522922020-11-14 16:35:34 +01005925 status = psa_get_and_lock_transparent_key_slot_with_policy(
5926 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005927 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005928 {
5929 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005930 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005931 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005932
5933 /* Passing a key object as a SECRET input unlocks the permission
5934 * to output to a key object. */
5935 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5936 operation->can_output_key = 1;
5937
Ronald Cronf95a2b12020-10-22 15:24:49 +02005938 status = psa_key_derivation_input_internal( operation,
5939 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005940 slot->key.data,
5941 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005942
Ronald Cron5c522922020-11-14 16:35:34 +01005943 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005944
Ronald Cron5c522922020-11-14 16:35:34 +01005945 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005946}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005947
5948
5949
5950/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005951/* Key agreement */
5952/****************************************************************/
5953
John Durkop6ba40d12020-11-10 08:50:04 -08005954#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005955static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5956 size_t peer_key_length,
5957 const mbedtls_ecp_keypair *our_key,
5958 uint8_t *shared_secret,
5959 size_t shared_secret_size,
5960 size_t *shared_secret_length )
5961{
Steven Cooremand4867872020-08-05 16:31:39 +02005962 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005963 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005964 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005965 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005966 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005967 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005968
Ronald Cron90857082020-11-25 14:58:33 +01005969 status = mbedtls_psa_ecp_load_representation(
5970 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5971 peer_key,
5972 peer_key_length,
5973 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005974 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005975 goto exit;
5976
Jaeden Amero97271b32019-01-10 19:38:51 +00005977 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005978 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005979 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005980 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005981 status = mbedtls_to_psa_error(
5982 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5983 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005984 goto exit;
5985
Jaeden Amero97271b32019-01-10 19:38:51 +00005986 status = mbedtls_to_psa_error(
5987 mbedtls_ecdh_calc_secret( &ecdh,
5988 shared_secret_length,
5989 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005990 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005991 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005992 if( status != PSA_SUCCESS )
5993 goto exit;
5994 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5995 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005996
5997exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005998 if( status != PSA_SUCCESS )
5999 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006000 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006001 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006002 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006003
Jaeden Amero97271b32019-01-10 19:38:51 +00006004 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006005}
John Durkop6ba40d12020-11-10 08:50:04 -08006006#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006007
Gilles Peskine01d718c2018-09-18 12:01:02 +02006008#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6009
Gilles Peskine0216fe12019-04-11 21:23:21 +02006010static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6011 psa_key_slot_t *private_key,
6012 const uint8_t *peer_key,
6013 size_t peer_key_length,
6014 uint8_t *shared_secret,
6015 size_t shared_secret_size,
6016 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006017{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006018 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006019 {
John Durkop6ba40d12020-11-10 08:50:04 -08006020#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006021 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006022 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006023 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006024 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01006025 psa_status_t status = mbedtls_psa_ecp_load_representation(
6026 private_key->attr.type,
6027 private_key->key.data,
6028 private_key->key.bytes,
6029 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006030 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006031 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006032 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006033 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006034 shared_secret, shared_secret_size,
6035 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006036 mbedtls_ecp_keypair_free( ecp );
6037 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006038 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006039#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006040 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006041 (void) private_key;
6042 (void) peer_key;
6043 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006044 (void) shared_secret;
6045 (void) shared_secret_size;
6046 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006047 return( PSA_ERROR_NOT_SUPPORTED );
6048 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006049}
6050
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006051/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006052 * to potentially free embedded data structures and wipe confidential data.
6053 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006054static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006055 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006056 psa_key_slot_t *private_key,
6057 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006058 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006059{
6060 psa_status_t status;
6061 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6062 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006063 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006064
6065 /* Step 1: run the secret agreement algorithm to generate the shared
6066 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006067 status = psa_key_agreement_raw_internal( ka_alg,
6068 private_key,
6069 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006070 shared_secret,
6071 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006072 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006073 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006074 goto exit;
6075
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006076 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006077 * the shared secret. A shared secret is permitted wherever a key
6078 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006079 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006080 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006081 shared_secret,
6082 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006083exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006084 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006085 return( status );
6086}
6087
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006088psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006089 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006090 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006091 const uint8_t *peer_key,
6092 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006093{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006095 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006096 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006097
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006098 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006099 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006100 status = psa_get_and_lock_transparent_key_slot_with_policy(
6101 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006102 if( status != PSA_SUCCESS )
6103 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006104 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006105 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006106 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006107 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006108 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006109 else
6110 {
6111 /* If a private key has been added as SECRET, we allow the derived
6112 * key material to be used as a key in PSA Crypto. */
6113 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6114 operation->can_output_key = 1;
6115 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006116
Ronald Cron5c522922020-11-14 16:35:34 +01006117 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006118
Ronald Cron5c522922020-11-14 16:35:34 +01006119 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006120}
6121
Gilles Peskinebe697d82019-05-16 18:00:41 +02006122psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006123 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006124 const uint8_t *peer_key,
6125 size_t peer_key_length,
6126 uint8_t *output,
6127 size_t output_size,
6128 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006129{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006131 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006132 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006133
6134 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6135 {
6136 status = PSA_ERROR_INVALID_ARGUMENT;
6137 goto exit;
6138 }
Ronald Cron5c522922020-11-14 16:35:34 +01006139 status = psa_get_and_lock_transparent_key_slot_with_policy(
6140 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006141 if( status != PSA_SUCCESS )
6142 goto exit;
6143
6144 status = psa_key_agreement_raw_internal( alg, slot,
6145 peer_key, peer_key_length,
6146 output, output_size,
6147 output_length );
6148
6149exit:
6150 if( status != PSA_SUCCESS )
6151 {
6152 /* If an error happens and is not handled properly, the output
6153 * may be used as a key to protect sensitive data. Arrange for such
6154 * a key to be random, which is likely to result in decryption or
6155 * verification errors. This is better than filling the buffer with
6156 * some constant data such as zeros, which would result in the data
6157 * being protected with a reproducible, easily knowable key.
6158 */
6159 psa_generate_random( output, output_size );
6160 *output_length = output_size;
6161 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006162
Ronald Cron5c522922020-11-14 16:35:34 +01006163 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006164
Ronald Cron5c522922020-11-14 16:35:34 +01006165 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006166}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006167
6168
Gilles Peskine30524eb2020-11-13 17:02:26 +01006169
Gilles Peskine86a440b2018-11-12 18:39:40 +01006170/****************************************************************/
6171/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006172/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006173
Gilles Peskine30524eb2020-11-13 17:02:26 +01006174/** Initialize the PSA random generator.
6175 */
6176static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6177{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006178#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6179 memset( rng, 0, sizeof( *rng ) );
6180#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6181
Gilles Peskine30524eb2020-11-13 17:02:26 +01006182 /* Set default configuration if
6183 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6184 if( rng->entropy_init == NULL )
6185 rng->entropy_init = mbedtls_entropy_init;
6186 if( rng->entropy_free == NULL )
6187 rng->entropy_free = mbedtls_entropy_free;
6188
6189 rng->entropy_init( &rng->entropy );
6190#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6191 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6192 /* The PSA entropy injection feature depends on using NV seed as an entropy
6193 * source. Add NV seed as an entropy source for PSA entropy injection. */
6194 mbedtls_entropy_add_source( &rng->entropy,
6195 mbedtls_nv_seed_poll, NULL,
6196 MBEDTLS_ENTROPY_BLOCK_SIZE,
6197 MBEDTLS_ENTROPY_SOURCE_STRONG );
6198#endif
6199
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006200 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006201#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006202}
6203
6204/** Deinitialize the PSA random generator.
6205 */
6206static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6207{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006208#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6209 memset( rng, 0, sizeof( *rng ) );
6210#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006211 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006212 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006213#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006214}
6215
6216/** Seed the PSA random generator.
6217 */
6218static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6219{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006220#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6221 /* Do nothing: the external RNG seeds itself. */
6222 (void) rng;
6223 return( PSA_SUCCESS );
6224#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006225 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006226 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6227 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006228 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006229#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006230}
6231
Gilles Peskinee59236f2018-01-27 23:32:46 +01006232psa_status_t psa_generate_random( uint8_t *output,
6233 size_t output_size )
6234{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006235 GUARD_MODULE_INITIALIZED;
6236
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006237#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6238
6239 size_t output_length = 0;
6240 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6241 output, output_size,
6242 &output_length );
6243 if( status != PSA_SUCCESS )
6244 return( status );
6245 /* Breaking up a request into smaller chunks is currently not supported
6246 * for the extrernal RNG interface. */
6247 if( output_length != output_size )
6248 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6249 return( PSA_SUCCESS );
6250
6251#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6252
Gilles Peskine71ddab92021-01-04 21:01:07 +01006253 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006254 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006255 size_t request_size =
6256 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6257 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6258 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006259 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6260 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006261 if( ret != 0 )
6262 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006263 output_size -= request_size;
6264 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006265 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006266 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006267#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268}
6269
Gilles Peskine8814fc42020-12-14 15:33:44 +01006270/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006271 *
6272 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6273 * `psa_generate_random(...)`. The state parameter is ignored since the
6274 * PSA API doesn't support passing an explicit state.
6275 *
6276 * In the non-external case, psa_generate_random() calls an
6277 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6278 * and semantics as mbedtls_psa_get_random(). As an optimization,
6279 * instead of doing this back-and-forth between the PSA API and the
6280 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6281 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006282 */
6283#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6284int mbedtls_psa_get_random( void *p_rng,
6285 unsigned char *output,
6286 size_t output_size )
6287{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006288 /* This function takes a pointer to the RNG state because that's what
6289 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6290 * its own state internally and doesn't let the caller access that state.
6291 * So we just ignore the state parameter, and in practice we'll pass
6292 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006293 (void) p_rng;
6294 psa_status_t status = psa_generate_random( output, output_size );
6295 if( status == PSA_SUCCESS )
6296 return( 0 );
6297 else
6298 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6299}
6300#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6301
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006302#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6303#include "mbedtls/entropy_poll.h"
6304
Gilles Peskine7228da22019-07-15 11:06:15 +02006305psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006306 size_t seed_size )
6307{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006308 if( global_data.initialized )
6309 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006310
6311 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6312 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6313 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6314 return( PSA_ERROR_INVALID_ARGUMENT );
6315
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006316 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006317}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006318#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006319
John Durkop07cc04a2020-11-16 22:08:34 -08006320#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006321static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6322 size_t domain_parameters_size,
6323 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006325 size_t i;
6326 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006327
Gilles Peskinee56e8782019-04-26 17:34:02 +02006328 if( domain_parameters_size == 0 )
6329 {
6330 *exponent = 65537;
6331 return( PSA_SUCCESS );
6332 }
6333
6334 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6335 * support values that fit in a 32-bit integer, which is larger than
6336 * int on just about every platform anyway. */
6337 if( domain_parameters_size > sizeof( acc ) )
6338 return( PSA_ERROR_NOT_SUPPORTED );
6339 for( i = 0; i < domain_parameters_size; i++ )
6340 acc = ( acc << 8 ) | domain_parameters[i];
6341 if( acc > INT_MAX )
6342 return( PSA_ERROR_NOT_SUPPORTED );
6343 *exponent = acc;
6344 return( PSA_SUCCESS );
6345}
John Durkop07cc04a2020-11-16 22:08:34 -08006346#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006347
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006348static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006349 psa_key_slot_t *slot, size_t bits,
6350 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006351{
Gilles Peskine8e338702019-07-30 20:06:31 +02006352 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006353
Gilles Peskinee56e8782019-04-26 17:34:02 +02006354 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006355 return( PSA_ERROR_INVALID_ARGUMENT );
6356
Gilles Peskinee59236f2018-01-27 23:32:46 +01006357 if( key_type_is_raw_bytes( type ) )
6358 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006359 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006360
6361 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006362 if( status != PSA_SUCCESS )
6363 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006364
6365 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006366 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6367 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006368 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006369
Ronald Cronea0f8a62020-11-25 17:52:23 +01006370 status = psa_generate_random( slot->key.data,
6371 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006372 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006374
6375 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376#if defined(MBEDTLS_DES_C)
6377 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006378 psa_des_set_key_parity( slot->key.data,
6379 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006380#endif /* MBEDTLS_DES_C */
6381 }
6382 else
6383
John Durkop07cc04a2020-11-16 22:08:34 -08006384#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006385 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006386 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006387 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006389 int exponent;
6390 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006391 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6392 return( PSA_ERROR_NOT_SUPPORTED );
6393 /* Accept only byte-aligned keys, for the same reasons as
6394 * in psa_import_rsa_key(). */
6395 if( bits % 8 != 0 )
6396 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006397 status = psa_read_rsa_exponent( domain_parameters,
6398 domain_parameters_size,
6399 &exponent );
6400 if( status != PSA_SUCCESS )
6401 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006402 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6403 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006404 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006405 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006406 (unsigned int) bits,
6407 exponent );
6408 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006410
6411 /* Make sure to always have an export representation available */
6412 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6413
Steven Cooreman75b74362020-07-28 14:30:13 +02006414 status = psa_allocate_buffer_to_slot( slot, bytes );
6415 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006416 {
6417 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006418 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006420
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006421 status = mbedtls_psa_rsa_export_key( type,
6422 &rsa,
6423 slot->key.data,
6424 bytes,
6425 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006426 mbedtls_rsa_free( &rsa );
6427 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006428 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006429 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430 }
6431 else
John Durkop07cc04a2020-11-16 22:08:34 -08006432#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006433
John Durkop9814fa22020-11-04 12:28:15 -08006434#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006435 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006436 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006437 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006438 mbedtls_ecp_group_id grp_id =
6439 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 const mbedtls_ecp_curve_info *curve_info =
6441 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006442 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006444 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006445 return( PSA_ERROR_NOT_SUPPORTED );
6446 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6447 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006448 mbedtls_ecp_keypair_init( &ecp );
6449 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006450 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006451 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006452 if( ret != 0 )
6453 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006454 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006455 return( mbedtls_to_psa_error( ret ) );
6456 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006457
6458
6459 /* Make sure to always have an export representation available */
6460 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006461 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6462 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006463 {
6464 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006465 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006466 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006467
6468 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006469 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006470
6471 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006472 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006473 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006474 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006475 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006476 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006477 }
6478 else
John Durkop9814fa22020-11-04 12:28:15 -08006479#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006480 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006481 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006482 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006483
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006484 return( PSA_SUCCESS );
6485}
Darryl Green0c6575a2018-11-07 16:05:30 +00006486
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006487psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006488 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006489{
6490 psa_status_t status;
6491 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006492 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006493
Ronald Cron81709fc2020-11-14 12:10:32 +01006494 *key = MBEDTLS_SVC_KEY_ID_INIT;
6495
Gilles Peskine0f84d622019-09-12 19:03:13 +02006496 /* Reject any attempt to create a zero-length key so that we don't
6497 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6498 if( psa_get_key_bits( attributes ) == 0 )
6499 return( PSA_ERROR_INVALID_ARGUMENT );
6500
Ronald Cron81709fc2020-11-14 12:10:32 +01006501 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6502 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006503 if( status != PSA_SUCCESS )
6504 goto exit;
6505
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006506 status = psa_driver_wrapper_generate_key( attributes,
6507 slot );
6508 if( status != PSA_ERROR_NOT_SUPPORTED ||
6509 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6510 goto exit;
6511
6512 status = psa_generate_key_internal(
6513 slot, attributes->core.bits,
6514 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006515
6516exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006517 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006518 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006519 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006520 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006521
Darryl Green0c6575a2018-11-07 16:05:30 +00006522 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006523}
6524
6525
Gilles Peskinee59236f2018-01-27 23:32:46 +01006526
6527/****************************************************************/
6528/* Module setup */
6529/****************************************************************/
6530
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006531#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006532psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6533 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6534 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6535{
6536 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6537 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006538 global_data.rng.entropy_init = entropy_init;
6539 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006540 return( PSA_SUCCESS );
6541}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006542#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006543
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544void mbedtls_psa_crypto_free( void )
6545{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006546 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006547 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6548 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006549 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006550 }
6551 /* Wipe all remaining data, including configuration.
6552 * In particular, this sets all state indicator to the value
6553 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006554 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006555#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006556 /* Unregister all secure element drivers, so that we restart from
6557 * a pristine state. */
6558 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006559#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560}
6561
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006562#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6563/** Recover a transaction that was interrupted by a power failure.
6564 *
6565 * This function is called during initialization, before psa_crypto_init()
6566 * returns. If this function returns a failure status, the initialization
6567 * fails.
6568 */
6569static psa_status_t psa_crypto_recover_transaction(
6570 const psa_crypto_transaction_t *transaction )
6571{
6572 switch( transaction->unknown.type )
6573 {
6574 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6575 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006576 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006577 * is implemented.
6578 * https://github.com/ARMmbed/mbed-crypto/issues/218
6579 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006580 default:
6581 /* We found an unsupported transaction in the storage.
6582 * We don't know what state the storage is in. Give up. */
6583 return( PSA_ERROR_STORAGE_FAILURE );
6584 }
6585}
6586#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6587
Gilles Peskinee59236f2018-01-27 23:32:46 +01006588psa_status_t psa_crypto_init( void )
6589{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006590 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006591
Gilles Peskinec6b69072018-11-20 21:42:52 +01006592 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006593 if( global_data.initialized != 0 )
6594 return( PSA_SUCCESS );
6595
Gilles Peskine30524eb2020-11-13 17:02:26 +01006596 /* Initialize and seed the random generator. */
6597 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006598 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006599 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006600 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006601 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006602 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006603
Gilles Peskine66fb1262018-12-10 16:29:04 +01006604 status = psa_initialize_key_slots( );
6605 if( status != PSA_SUCCESS )
6606 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006607
Gilles Peskined9348f22019-10-01 15:22:29 +02006608#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6609 status = psa_init_all_se_drivers( );
6610 if( status != PSA_SUCCESS )
6611 goto exit;
6612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6613
Gilles Peskine4b734222019-07-24 15:56:31 +02006614#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006615 status = psa_crypto_load_transaction( );
6616 if( status == PSA_SUCCESS )
6617 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006618 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6619 if( status != PSA_SUCCESS )
6620 goto exit;
6621 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006622 }
6623 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6624 {
6625 /* There's no transaction to complete. It's all good. */
6626 status = PSA_SUCCESS;
6627 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006628#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006629
Gilles Peskinec6b69072018-11-20 21:42:52 +01006630 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006631 global_data.initialized = 1;
6632
6633exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006634 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006635 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006636 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006637}
6638
6639#endif /* MBEDTLS_PSA_CRYPTO_C */