blob: c041e8eed7f32369435c0553c992c18ad41f64ef [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"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine90edc992020-11-13 15:39:19 +010043#include "psa_crypto_random.h"
44
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020055#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000056#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020060#include "mbedtls/chacha20.h"
61#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/cipher.h"
63#include "mbedtls/ccm.h"
64#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010065#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine996deb12018-08-01 15:45:45 +020088#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
89
Gilles Peskine9ef733f2018-02-07 21:05:37 +010090/* constant-time buffer comparison */
91static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
92{
93 size_t i;
94 unsigned char diff = 0;
95
96 for( i = 0; i < n; i++ )
97 diff |= a[i] ^ b[i];
98
99 return( diff );
100}
101
102
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/****************************************************************/
105/* Global data, support functions and library management */
106/****************************************************************/
107
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108static int key_type_is_raw_bytes( psa_key_type_t type )
109{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200110 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111}
112
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113/* Values for psa_global_data_t::rng_state */
114#define RNG_NOT_INITIALIZED 0
115#define RNG_INITIALIZED 1
116#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskine5e769522018-11-20 21:59:56 +0100120 void (* entropy_init )( mbedtls_entropy_context *ctx );
121 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122 mbedtls_entropy_context entropy;
123 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
itayzafrir0adf0fc2018-09-06 16:24:41 +0300130#define GUARD_MODULE_INITIALIZED \
131 if( global_data.initialized == 0 ) \
132 return( PSA_ERROR_BAD_STATE );
133
Steven Cooreman01164162020-07-20 15:31:37 +0200134psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135{
Gilles Peskinea5905292018-02-07 20:59:33 +0100136 /* If there's both a high-level code and low-level code, dispatch on
137 * the high-level code. */
138 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100139 {
140 case 0:
141 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100142
143 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
144 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
145 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
146 return( PSA_ERROR_NOT_SUPPORTED );
147 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
150 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
151 return( PSA_ERROR_HARDWARE_FAILURE );
152
Gilles Peskine9a944802018-06-21 09:35:35 +0200153 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
154 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
155 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
156 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
157 case MBEDTLS_ERR_ASN1_INVALID_DATA:
158 return( PSA_ERROR_INVALID_ARGUMENT );
159 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
160 return( PSA_ERROR_INSUFFICIENT_MEMORY );
161 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
162 return( PSA_ERROR_BUFFER_TOO_SMALL );
163
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000165 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
167 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
168#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
170 return( PSA_ERROR_NOT_SUPPORTED );
171 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000175 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000176#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
177 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
178#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
180 return( PSA_ERROR_NOT_SUPPORTED );
181 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
182 return( PSA_ERROR_HARDWARE_FAILURE );
183
184 case MBEDTLS_ERR_CCM_BAD_INPUT:
185 return( PSA_ERROR_INVALID_ARGUMENT );
186 case MBEDTLS_ERR_CCM_AUTH_FAILED:
187 return( PSA_ERROR_INVALID_SIGNATURE );
188 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
Gilles Peskine26869f22019-05-06 15:25:00 +0200191 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
192 return( PSA_ERROR_INVALID_ARGUMENT );
193
194 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
195 return( PSA_ERROR_BAD_STATE );
196 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
197 return( PSA_ERROR_INVALID_SIGNATURE );
198
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
200 return( PSA_ERROR_NOT_SUPPORTED );
201 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
202 return( PSA_ERROR_INVALID_ARGUMENT );
203 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_MEMORY );
205 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
206 return( PSA_ERROR_INVALID_PADDING );
207 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200208 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
210 return( PSA_ERROR_INVALID_SIGNATURE );
211 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200212 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
217 return( PSA_ERROR_HARDWARE_FAILURE );
218
219 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
222 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
226
227 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
Gilles Peskinee59236f2018-01-27 23:32:46 +0100232 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
233 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
234 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
237 case MBEDTLS_ERR_GCM_AUTH_FAILED:
238 return( PSA_ERROR_INVALID_SIGNATURE );
239 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200240 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
245 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
246 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
252 return( PSA_ERROR_INVALID_ARGUMENT );
253 case MBEDTLS_ERR_MD_ALLOC_FAILED:
254 return( PSA_ERROR_INSUFFICIENT_MEMORY );
255 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
258 return( PSA_ERROR_HARDWARE_FAILURE );
259
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
261 return( PSA_ERROR_STORAGE_FAILURE );
262 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
267 return( PSA_ERROR_BUFFER_TOO_SMALL );
268 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
273 return( PSA_ERROR_INVALID_ARGUMENT );
274 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_ALLOC_FAILED:
278 return( PSA_ERROR_INSUFFICIENT_MEMORY );
279 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
280 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100284 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
285 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
288 return( PSA_ERROR_NOT_SUPPORTED );
289 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
290 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
291 return( PSA_ERROR_NOT_PERMITTED );
292 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_PK_INVALID_ALG:
295 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
296 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
297 return( PSA_ERROR_NOT_SUPPORTED );
298 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
299 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302
Gilles Peskineff2d2002019-05-06 15:26:23 +0200303 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
306 return( PSA_ERROR_NOT_SUPPORTED );
307
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
309 return( PSA_ERROR_HARDWARE_FAILURE );
310
311 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_INVALID_PADDING:
314 return( PSA_ERROR_INVALID_PADDING );
315 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
316 return( PSA_ERROR_HARDWARE_FAILURE );
317 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
320 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200321 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
323 return( PSA_ERROR_INVALID_SIGNATURE );
324 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
325 return( PSA_ERROR_BUFFER_TOO_SMALL );
326 case MBEDTLS_ERR_RSA_RNG_FAILED:
327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
328 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
329 return( PSA_ERROR_NOT_SUPPORTED );
330 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
334 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
335 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
338 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
339 return( PSA_ERROR_INVALID_ARGUMENT );
340 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342
itayzafrir5c753392018-05-08 11:18:38 +0300343 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300345 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300346 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
349 return( PSA_ERROR_NOT_SUPPORTED );
350 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
351 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
352 return( PSA_ERROR_INVALID_SIGNATURE );
353 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
354 return( PSA_ERROR_INSUFFICIENT_MEMORY );
355 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000357 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
358 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300359
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 default:
David Saadab4ecc272019-02-14 13:48:10 +0200361 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100362 }
363}
364
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200365
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200366
367
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368/****************************************************************/
369/* Key management */
370/****************************************************************/
371
Gilles Peskine73167e12019-07-12 23:44:37 +0200372#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
373static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
374{
Gilles Peskine8e338702019-07-30 20:06:31 +0200375 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200376}
377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
378
John Durkop07cc04a2020-11-16 22:08:34 -0800379/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
380 * current test driver in key_management.c is using this function
381 * when accelerators are used for ECC key pair and public key.
382 * Once that dependency is resolved these guards can be removed.
383 */
John Durkop9814fa22020-11-04 12:28:15 -0800384#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800385 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
386 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
387 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100388mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100389 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200390{
391 switch( curve )
392 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100393 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100394 switch( byte_length )
395 {
396 case PSA_BITS_TO_BYTES( 192 ):
397 return( MBEDTLS_ECP_DP_SECP192R1 );
398 case PSA_BITS_TO_BYTES( 224 ):
399 return( MBEDTLS_ECP_DP_SECP224R1 );
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_SECP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_SECP384R1 );
404 case PSA_BITS_TO_BYTES( 521 ):
405 return( MBEDTLS_ECP_DP_SECP521R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 256 ):
415 return( MBEDTLS_ECP_DP_BP256R1 );
416 case PSA_BITS_TO_BYTES( 384 ):
417 return( MBEDTLS_ECP_DP_BP384R1 );
418 case PSA_BITS_TO_BYTES( 512 ):
419 return( MBEDTLS_ECP_DP_BP512R1 );
420 default:
421 return( MBEDTLS_ECP_DP_NONE );
422 }
423 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100424
Paul Elliott8ff510a2020-06-02 17:19:28 +0100425 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 switch( byte_length )
427 {
428 case PSA_BITS_TO_BYTES( 255 ):
429 return( MBEDTLS_ECP_DP_CURVE25519 );
430 case PSA_BITS_TO_BYTES( 448 ):
431 return( MBEDTLS_ECP_DP_CURVE448 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Paul Elliott8ff510a2020-06-02 17:19:28 +0100437 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100438 switch( byte_length )
439 {
440 case PSA_BITS_TO_BYTES( 192 ):
441 return( MBEDTLS_ECP_DP_SECP192K1 );
442 case PSA_BITS_TO_BYTES( 224 ):
443 return( MBEDTLS_ECP_DP_SECP224K1 );
444 case PSA_BITS_TO_BYTES( 256 ):
445 return( MBEDTLS_ECP_DP_SECP256K1 );
446 default:
447 return( MBEDTLS_ECP_DP_NONE );
448 }
449 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100450
Gilles Peskine12313cd2018-06-20 00:20:32 +0200451 default:
452 return( MBEDTLS_ECP_DP_NONE );
453 }
454}
John Durkop9814fa22020-11-04 12:28:15 -0800455#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800456 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
457 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
458 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200459
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200460static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
461 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462{
463 /* Check that the bit size is acceptable for the key type */
464 switch( type )
465 {
466 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200468 case PSA_KEY_TYPE_DERIVE:
469 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470#if defined(MBEDTLS_AES_C)
471 case PSA_KEY_TYPE_AES:
472 if( bits != 128 && bits != 192 && bits != 256 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476#if defined(MBEDTLS_CAMELLIA_C)
477 case PSA_KEY_TYPE_CAMELLIA:
478 if( bits != 128 && bits != 192 && bits != 256 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
480 break;
481#endif
482#if defined(MBEDTLS_DES_C)
483 case PSA_KEY_TYPE_DES:
484 if( bits != 64 && bits != 128 && bits != 192 )
485 return( PSA_ERROR_INVALID_ARGUMENT );
486 break;
487#endif
488#if defined(MBEDTLS_ARC4_C)
489 case PSA_KEY_TYPE_ARC4:
490 if( bits < 8 || bits > 2048 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
492 break;
493#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200494#if defined(MBEDTLS_CHACHA20_C)
495 case PSA_KEY_TYPE_CHACHA20:
496 if( bits != 256 )
497 return( PSA_ERROR_INVALID_ARGUMENT );
498 break;
499#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500 default:
501 return( PSA_ERROR_NOT_SUPPORTED );
502 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200503 if( bits % 8 != 0 )
504 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 return( PSA_SUCCESS );
507}
508
John Durkop0e005192020-10-31 22:06:54 -0700509#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
510 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
511 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800512 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
513 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
514 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200515
Gilles Peskine86a440b2018-11-12 18:39:40 +0100516/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
517 * that are not a multiple of 8) well. For example, there is only
518 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
519 * way to return the exact bit size of a key.
520 * To keep things simple, reject non-byte-aligned key sizes. */
521static psa_status_t psa_check_rsa_key_byte_aligned(
522 const mbedtls_rsa_context *rsa )
523{
524 mbedtls_mpi n;
525 psa_status_t status;
526 mbedtls_mpi_init( &n );
527 status = mbedtls_to_psa_error(
528 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
529 if( status == PSA_SUCCESS )
530 {
531 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
532 status = PSA_ERROR_NOT_SUPPORTED;
533 }
534 mbedtls_mpi_free( &n );
535 return( status );
536}
537
Steven Cooreman7f391872020-07-30 14:57:44 +0200538/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200540 * \param[in] type The type of key contained in \p data.
541 * \param[in] data The buffer from which to load the representation.
542 * \param[in] data_length The size in bytes of \p data.
543 * \param[out] p_rsa Returns a pointer to an RSA context on success.
544 * The caller is responsible for freeing both the
545 * contents of the context and the context itself
546 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200547 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200548static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
549 const uint8_t *data,
550 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200551 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200554 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200556 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000557
558 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200559 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200561 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200562 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200564 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000565 if( status != PSA_SUCCESS )
566 goto exit;
567
568 /* We have something that the pkparse module recognizes. If it is a
569 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200570 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200571 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000572 status = PSA_ERROR_INVALID_ARGUMENT;
573 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200574 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
577 * supports non-byte-aligned key sizes, but not well. For example,
578 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200579 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
581 {
582 status = PSA_ERROR_NOT_SUPPORTED;
583 goto exit;
584 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200585 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200586 if( status != PSA_SUCCESS )
587 goto exit;
588
Steven Cooremana2371e52020-07-28 14:30:39 +0200589 /* Copy out the pointer to the RSA context, and reset the PK context
590 * such that pk_free doesn't free the RSA context we just grabbed. */
591 *p_rsa = mbedtls_pk_rsa( ctx );
592 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000593
594exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200595 mbedtls_pk_free( &ctx );
596 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200597}
598
John Durkop6ba40d12020-11-10 08:50:04 -0800599#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
600 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
601 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
602 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
603 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
604 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
605
606#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
607 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
608
Steven Cooremana01795d2020-07-24 22:48:15 +0200609/** Export an RSA key to export representation
610 *
611 * \param[in] type The type of key (public/private) to export
612 * \param[in] rsa The internal RSA representation from which to export
613 * \param[out] data The buffer to export to
614 * \param[in] data_size The length of the buffer to export to
615 * \param[out] data_length The amount of bytes written to \p data
616 */
617static psa_status_t psa_export_rsa_key( psa_key_type_t type,
618 mbedtls_rsa_context *rsa,
619 uint8_t *data,
620 size_t data_size,
621 size_t *data_length )
622{
623#if defined(MBEDTLS_PK_WRITE_C)
624 int ret;
625 mbedtls_pk_context pk;
626 uint8_t *pos = data + data_size;
627
628 mbedtls_pk_init( &pk );
629 pk.pk_info = &mbedtls_rsa_info;
630 pk.pk_ctx = rsa;
631
632 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200633 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
634 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200635 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
636 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
637 else
638 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
639
640 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200641 {
642 /* Clean up in case pk_write failed halfway through. */
643 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200644 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200645 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200646
647 /* The mbedtls_pk_xxx functions write to the end of the buffer.
648 * Move the data to the beginning and erase remaining data
649 * at the original location. */
650 if( 2 * (size_t) ret <= data_size )
651 {
652 memcpy( data, data + data_size - ret, ret );
653 memset( data + data_size - ret, 0, ret );
654 }
655 else if( (size_t) ret < data_size )
656 {
657 memmove( data, data + data_size - ret, ret );
658 memset( data + ret, 0, data_size - ret );
659 }
660
661 *data_length = ret;
662 return( PSA_SUCCESS );
663#else
664 (void) type;
665 (void) rsa;
666 (void) data;
667 (void) data_size;
668 (void) data_length;
669 return( PSA_ERROR_NOT_SUPPORTED );
670#endif /* MBEDTLS_PK_WRITE_C */
671}
672
673/** Import an RSA key from import representation to a slot
674 *
675 * \param[in,out] slot The slot where to store the export representation to
676 * \param[in] data The buffer containing the import representation
677 * \param[in] data_length The amount of bytes in \p data
678 */
679static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
680 const uint8_t *data,
681 size_t data_length )
682{
683 psa_status_t status;
684 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200685 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200686
Steven Cooremana01795d2020-07-24 22:48:15 +0200687 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200688 status = psa_load_rsa_representation( slot->attr.type,
689 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200690 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200691 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200692 if( status != PSA_SUCCESS )
693 goto exit;
694
695 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200697
Steven Cooreman75b74362020-07-28 14:30:13 +0200698 /* Re-export the data to PSA export format, such that we can store export
699 * representation in the key slot. Export representation in case of RSA is
700 * the smallest representation that's allowed as input, so a straight-up
701 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200703 if( output == NULL )
704 {
705 status = PSA_ERROR_INSUFFICIENT_MEMORY;
706 goto exit;
707 }
708
Steven Cooremana01795d2020-07-24 22:48:15 +0200709 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200710 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200711 output,
712 data_length,
713 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200714exit:
715 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200717 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200718
719 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000720 if( status != PSA_SUCCESS )
721 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200722 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000723 return( status );
724 }
725
Steven Cooremana01795d2020-07-24 22:48:15 +0200726 /* On success, store the allocated export-formatted key. */
727 slot->data.key.data = output;
728 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000729
730 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200731}
John Durkop6ba40d12020-11-10 08:50:04 -0800732
733#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800734 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200735
John Durkop9814fa22020-11-04 12:28:15 -0800736#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800737 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
738 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
739 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
740 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200741/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200742 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200743 * \param[in] type The type of key contained in \p data.
744 * \param[in] data The buffer from which to load the representation.
745 * \param[in] data_length The size in bytes of \p data.
746 * \param[out] p_ecp Returns a pointer to an ECP context on success.
747 * The caller is responsible for freeing both the
748 * contents of the context and the context itself
749 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200750 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200751static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
752 const uint8_t *data,
753 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200754 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100755{
756 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200757 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200758 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200759 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100760
Steven Cooreman4fed4552020-08-03 14:46:03 +0200761 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
762 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100763 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200764 /* A Weierstrass public key is represented as:
765 * - The byte 0x04;
766 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
767 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200768 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200769 */
770 if( ( data_length & 1 ) == 0 )
771 return( PSA_ERROR_INVALID_ARGUMENT );
772 curve_size = data_length / 2;
773
774 /* Montgomery public keys are represented in compressed format, meaning
775 * their curve_size is equal to the amount of input. */
776
777 /* Private keys are represented in uncompressed private random integer
778 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100779 }
780
Steven Cooreman6d839f02020-07-30 11:36:45 +0200781 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200782 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200783 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200784 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200785 mbedtls_ecp_keypair_init( ecp );
786
Gilles Peskine4cd32772019-12-02 20:49:42 +0100787 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200788 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
789 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100790 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200791 {
792 status = PSA_ERROR_INVALID_ARGUMENT;
793 goto exit;
794 }
795
Steven Cooremanacda8342020-07-24 23:09:52 +0200796 status = mbedtls_to_psa_error(
797 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
798 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200799 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200800
Steven Cooreman6d839f02020-07-30 11:36:45 +0200801 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200802 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200803 {
804 /* Load the public value. */
805 status = mbedtls_to_psa_error(
806 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200807 data,
808 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200809 if( status != PSA_SUCCESS )
810 goto exit;
811
812 /* Check that the point is on the curve. */
813 status = mbedtls_to_psa_error(
814 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
815 if( status != PSA_SUCCESS )
816 goto exit;
817 }
818 else
819 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200820 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200821 status = mbedtls_to_psa_error(
822 mbedtls_ecp_read_key( ecp->grp.id,
823 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200824 data,
825 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200826 if( status != PSA_SUCCESS )
827 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200828 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200829
830 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200831exit:
832 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200833 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200835 mbedtls_free( ecp );
836 }
837
Steven Cooreman29149862020-08-05 15:43:42 +0200838 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100839}
John Durkop6ba40d12020-11-10 08:50:04 -0800840#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
841 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
842 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
843 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
844 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000845
John Durkop6ba40d12020-11-10 08:50:04 -0800846#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
847 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200848/** Export an ECP key to export representation
849 *
850 * \param[in] type The type of key (public/private) to export
851 * \param[in] ecp The internal ECP representation from which to export
852 * \param[out] data The buffer to export to
853 * \param[in] data_size The length of the buffer to export to
854 * \param[out] data_length The amount of bytes written to \p data
855 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200856static psa_status_t psa_export_ecp_key( psa_key_type_t type,
857 mbedtls_ecp_keypair *ecp,
858 uint8_t *data,
859 size_t data_size,
860 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200861{
Steven Cooremanacda8342020-07-24 23:09:52 +0200862 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000863
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200865 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200866 /* Check whether the public part is loaded */
867 if( mbedtls_ecp_is_zero( &ecp->Q ) )
868 {
869 /* Calculate the public key */
870 status = mbedtls_to_psa_error(
871 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
872 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
873 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200874 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200875 }
876
Steven Cooreman4fed4552020-08-03 14:46:03 +0200877 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200878 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
879 MBEDTLS_ECP_PF_UNCOMPRESSED,
880 data_length,
881 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200882 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200883 if( status != PSA_SUCCESS )
884 memset( data, 0, data_size );
885
Steven Cooreman29149862020-08-05 15:43:42 +0200886 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200887 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200888 else
889 {
Steven Cooreman29149862020-08-05 15:43:42 +0200890 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200891 return( PSA_ERROR_BUFFER_TOO_SMALL );
892
893 status = mbedtls_to_psa_error(
894 mbedtls_ecp_write_key( ecp,
895 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200896 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200898 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200899 else
900 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200901
902 return( status );
903 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200904}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200905
Steven Cooreman4fed4552020-08-03 14:46:03 +0200906/** Import an ECP key from import representation to a slot
907 *
908 * \param[in,out] slot The slot where to store the export representation to
909 * \param[in] data The buffer containing the import representation
910 * \param[in] data_length The amount of bytes in \p data
911 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200912static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
913 const uint8_t *data,
914 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915{
Steven Cooremanacda8342020-07-24 23:09:52 +0200916 psa_status_t status;
917 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200918 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100919
Steven Cooremanacda8342020-07-24 23:09:52 +0200920 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200921 status = psa_load_ecp_representation( slot->attr.type,
922 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200923 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200924 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 if( status != PSA_SUCCESS )
926 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100927
Steven Cooremanacda8342020-07-24 23:09:52 +0200928 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200929 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200930 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200931 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200932
Steven Cooremanacda8342020-07-24 23:09:52 +0200933 /* Re-export the data to PSA export format. There is currently no support
934 * for other input formats then the export format, so this is a 1-1
935 * copy operation. */
936 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200937 if( output == NULL )
938 {
939 status = PSA_ERROR_INSUFFICIENT_MEMORY;
940 goto exit;
941 }
942
943 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200944 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200945 output,
946 data_length,
947 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100948exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200949 /* Always free the PK object (will also free contained ECP context) */
950 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200951 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200952
953 /* Free the allocated buffer only on error. */
954 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100955 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200956 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200957 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100958 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200959
960 /* On success, store the allocated export-formatted key. */
961 slot->data.key.data = output;
962 slot->data.key.bytes = data_length;
963
964 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100965}
John Durkop9814fa22020-11-04 12:28:15 -0800966#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
967 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100968
Gilles Peskineb46bef22019-07-30 21:32:04 +0200969/** Return the size of the key in the given slot, in bits.
970 *
971 * \param[in] slot A key slot.
972 *
973 * \return The key size in bits, read from the metadata in the slot.
974 */
975static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
976{
977 return( slot->attr.bits );
978}
979
Steven Cooreman75b74362020-07-28 14:30:13 +0200980/** Try to allocate a buffer to an empty key slot.
981 *
982 * \param[in,out] slot Key slot to attach buffer to.
983 * \param[in] buffer_length Requested size of the buffer.
984 *
985 * \retval #PSA_SUCCESS
986 * The buffer has been successfully allocated.
987 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
988 * Not enough memory was available for allocation.
989 * \retval #PSA_ERROR_ALREADY_EXISTS
990 * Trying to allocate a buffer to a non-empty key slot.
991 */
992static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
993 size_t buffer_length )
994{
995 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200996 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200997
998 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
999 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001000 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001001
1002 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001003 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001004}
1005
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001006psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1007 const uint8_t* data,
1008 size_t data_length )
1009{
1010 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1011 data_length );
1012 if( status != PSA_SUCCESS )
1013 return( status );
1014
1015 memcpy( slot->data.key.data, data, data_length );
1016 return( PSA_SUCCESS );
1017}
1018
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001019/** Import key data into a slot.
1020 *
1021 * `slot->type` must have been set previously.
1022 * This function assumes that the slot does not contain any key material yet.
1023 * On failure, the slot content is unchanged.
1024 *
1025 * Persistent storage is not affected.
1026 *
1027 * \param[in,out] slot The key slot to import data into.
1028 * Its `type` field must have previously been set to
1029 * the desired key type.
1030 * It must not contain any key material yet.
1031 * \param[in] data Buffer containing the key material to parse and import.
1032 * \param data_length Size of \p data in bytes.
1033 *
1034 * \retval #PSA_SUCCESS
1035 * \retval #PSA_ERROR_INVALID_ARGUMENT
1036 * \retval #PSA_ERROR_NOT_SUPPORTED
1037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1038 */
1039static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1040 const uint8_t *data,
1041 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001043 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001044 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001046 /* zero-length keys are never supported. */
1047 if( data_length == 0 )
1048 return( PSA_ERROR_NOT_SUPPORTED );
1049
Gilles Peskine8e338702019-07-30 20:06:31 +02001050 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051 {
Steven Cooreman04524762020-10-13 17:43:44 +02001052 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001053
Steven Cooreman75b74362020-07-28 14:30:13 +02001054 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1055 if( data_length > SIZE_MAX / 8 )
1056 return( PSA_ERROR_NOT_SUPPORTED );
1057
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001058 /* Enforce a size limit, and in particular ensure that the bit
1059 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001060 if( bit_size > PSA_MAX_KEY_BITS )
1061 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001062
1063 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001064 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001065 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001066
1067 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001068 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001069 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001070 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001071
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001072 /* Write the actual key size to the slot.
1073 * psa_start_key_creation() wrote the size declared by the
1074 * caller, which may be 0 (meaning unspecified) or wrong. */
1075 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001076
1077 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001079 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001080 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001081 /* Try validation through accelerators first. */
1082 bit_size = slot->attr.bits;
1083 psa_key_attributes_t attributes = {
1084 .core = slot->attr
1085 };
1086 status = psa_driver_wrapper_validate_key( &attributes,
1087 data,
1088 data_length,
1089 &bit_size );
1090 if( status == PSA_SUCCESS )
1091 {
1092 /* Key has been validated successfully by an accelerator.
1093 * Copy key material into slot. */
1094 status = psa_copy_key_material_into_slot( slot, data, data_length );
1095 if( status != PSA_SUCCESS )
1096 return( status );
1097
1098 slot->attr.bits = (psa_key_bits_t) bit_size;
1099 return( PSA_SUCCESS );
1100 }
1101 else if( status != PSA_ERROR_NOT_SUPPORTED )
1102 return( status );
1103
1104 /* Key format is not supported by any accelerator, try software fallback
1105 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001106#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1107 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001108 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1109 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001110 return( psa_import_ecp_key( slot, data, data_length ) );
1111 }
John Durkop9814fa22020-11-04 12:28:15 -08001112#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1113 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001114#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1115 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001116 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001117 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001118 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001119 }
John Durkop9814fa22020-11-04 12:28:15 -08001120#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1121 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001122
1123 /* Fell through the fallback as well, so have nothing else to try. */
1124 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001125 }
1126 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001127 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001128 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001129 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001130 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001131}
1132
Gilles Peskinef603c712019-01-19 13:40:11 +01001133/** Calculate the intersection of two algorithm usage policies.
1134 *
1135 * Return 0 (which allows no operation) on incompatibility.
1136 */
1137static psa_algorithm_t psa_key_policy_algorithm_intersection(
1138 psa_algorithm_t alg1,
1139 psa_algorithm_t alg2 )
1140{
Gilles Peskine549ea862019-05-22 11:45:59 +02001141 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001142 if( alg1 == alg2 )
1143 return( alg1 );
1144 /* If the policies are from the same hash-and-sign family, check
1145 * if one is a wildcard. If so the other has the specific algorithm. */
1146 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1147 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1148 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1149 {
1150 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1151 return( alg2 );
1152 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1153 return( alg1 );
1154 }
1155 /* If the policies are incompatible, allow nothing. */
1156 return( 0 );
1157}
1158
Gilles Peskined6f371b2019-05-10 19:33:38 +02001159static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1160 psa_algorithm_t requested_alg )
1161{
Gilles Peskine549ea862019-05-22 11:45:59 +02001162 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001163 if( requested_alg == policy_alg )
1164 return( 1 );
1165 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001166 * and requested_alg is the same hash-and-sign family with any hash,
1167 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001168 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1169 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1170 {
1171 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1172 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1173 }
Steven Cooremance48e852020-10-05 16:02:45 +02001174 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001175 * a key derivation with that key agreement should also be allowed. This
1176 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001177 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1178 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1179 {
1180 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1181 policy_alg );
1182 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001183 /* If it isn't permitted, it's forbidden. */
1184 return( 0 );
1185}
1186
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001187/** Test whether a policy permits an algorithm.
1188 *
1189 * The caller must test usage flags separately.
1190 */
1191static int psa_key_policy_permits( const psa_key_policy_t *policy,
1192 psa_algorithm_t alg )
1193{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001194 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1195 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001196}
1197
Gilles Peskinef603c712019-01-19 13:40:11 +01001198/** Restrict a key policy based on a constraint.
1199 *
1200 * \param[in,out] policy The policy to restrict.
1201 * \param[in] constraint The policy constraint to apply.
1202 *
1203 * \retval #PSA_SUCCESS
1204 * \c *policy contains the intersection of the original value of
1205 * \c *policy and \c *constraint.
1206 * \retval #PSA_ERROR_INVALID_ARGUMENT
1207 * \c *policy and \c *constraint are incompatible.
1208 * \c *policy is unchanged.
1209 */
1210static psa_status_t psa_restrict_key_policy(
1211 psa_key_policy_t *policy,
1212 const psa_key_policy_t *constraint )
1213{
1214 psa_algorithm_t intersection_alg =
1215 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001216 psa_algorithm_t intersection_alg2 =
1217 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001218 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1219 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001220 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1221 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001222 policy->usage &= constraint->usage;
1223 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001224 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001225 return( PSA_SUCCESS );
1226}
1227
Ronald Cron5c522922020-11-14 16:35:34 +01001228/** Get the description of a key given its identifier and policy constraints
1229 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230 *
Ronald Cron5c522922020-11-14 16:35:34 +01001231 * The key must have allow all the usage flags set in \p usage. If \p alg is
1232 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001233 *
Ronald Cron5c522922020-11-14 16:35:34 +01001234 * In case of a persistent key, the function loads the description of the key
1235 * into a key slot if not already done.
1236 *
1237 * On success, the returned key slot is locked. It is the responsibility of
1238 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001239 */
Ronald Cron5c522922020-11-14 16:35:34 +01001240static psa_status_t psa_get_and_lock_key_slot_with_policy(
1241 mbedtls_svc_key_id_t key,
1242 psa_key_slot_t **p_slot,
1243 psa_key_usage_t usage,
1244 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001245{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1247 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001248
Ronald Cron5c522922020-11-14 16:35:34 +01001249 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001250 if( status != PSA_SUCCESS )
1251 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001252 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001253
1254 /* Enforce that usage policy for the key slot contains all the flags
1255 * required by the usage parameter. There is one exception: public
1256 * keys can always be exported, so we treat public key objects as
1257 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001258 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001259 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001260
1261 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001262 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001263 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001264
1265 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001266 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001267 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001268
Darryl Green06fd18d2018-07-16 11:21:11 +01001269 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001270
1271error:
1272 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001273 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001274
1275 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001276}
Darryl Green940d72c2018-07-13 13:18:51 +01001277
Ronald Cron5c522922020-11-14 16:35:34 +01001278/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001279 *
1280 * A transparent key is a key for which the key material is directly
1281 * available, as opposed to a key in a secure element.
1282 *
Ronald Cron5c522922020-11-14 16:35:34 +01001283 * This is a temporary function to use instead of
1284 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1285 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001286 *
Ronald Cron5c522922020-11-14 16:35:34 +01001287 * On success, the returned key slot is locked. It is the responsibility of the
1288 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001289 */
1290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001291static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1292 mbedtls_svc_key_id_t key,
1293 psa_key_slot_t **p_slot,
1294 psa_key_usage_t usage,
1295 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001296{
Ronald Cron5c522922020-11-14 16:35:34 +01001297 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1298 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001299 if( status != PSA_SUCCESS )
1300 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001301
Gilles Peskineadad8132019-07-25 11:31:23 +02001302 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001303 {
Ronald Cron5c522922020-11-14 16:35:34 +01001304 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001305 *p_slot = NULL;
1306 return( PSA_ERROR_NOT_SUPPORTED );
1307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Gilles Peskine28f8f302019-07-24 13:30:31 +02001309 return( PSA_SUCCESS );
1310}
1311#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1312/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001313#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1314 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001315#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1316
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001317/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001318static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001319{
Gilles Peskine73167e12019-07-12 23:44:37 +02001320#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1321 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001322 {
1323 /* No key material to clean. */
1324 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001325 else
1326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001327 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001328 /* Data pointer will always be either a valid pointer or NULL in an
1329 * initialized slot, so we can just free it. */
1330 if( slot->data.key.data != NULL )
1331 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001332 mbedtls_free( slot->data.key.data );
1333 slot->data.key.data = NULL;
1334 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001335 }
Darryl Green40225ba2018-11-15 14:48:15 +00001336
1337 return( PSA_SUCCESS );
1338}
1339
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001340/** Completely wipe a slot in memory, including its policy.
1341 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001342psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001343{
1344 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001345
1346 /*
1347 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001348 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001349 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1350 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001351 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001352 */
Ronald Cron5c522922020-11-14 16:35:34 +01001353 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001354 {
1355#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001356 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001357#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001358 status = PSA_ERROR_CORRUPTION_DETECTED;
1359 }
1360
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001361 /* Multipart operations may still be using the key. This is safe
1362 * because all multipart operation objects are independent from
1363 * the key slot: if they need to access the key after the setup
1364 * phase, they have a copy of the key. Note that this means that
1365 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001366 /* At this point, key material and other type-specific content has
1367 * been wiped. Clear remaining metadata. We can call memset and not
1368 * zeroize because the metadata is not particularly sensitive. */
1369 memset( slot, 0, sizeof( *slot ) );
1370 return( status );
1371}
1372
Ronald Croncf56a0a2020-08-04 09:51:30 +02001373psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001374{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001375 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001376 psa_status_t status; /* status of the last operation */
1377 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001378#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1379 psa_se_drv_table_entry_t *driver;
1380#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381
Ronald Croncf56a0a2020-08-04 09:51:30 +02001382 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001383 return( PSA_SUCCESS );
1384
Ronald Cronf2911112020-10-29 17:51:10 +01001385 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001386 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001387 * key, this will load the key description from persistent memory if not
1388 * done yet. We cannot avoid this loading as without it we don't know if
1389 * the key is operated by an SE or not and this information is needed by
1390 * the current implementation.
1391 */
Ronald Cron5c522922020-11-14 16:35:34 +01001392 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001393 if( status != PSA_SUCCESS )
1394 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001395
Ronald Cronf2911112020-10-29 17:51:10 +01001396 /*
1397 * If the key slot containing the key description is under access by the
1398 * library (apart from the present access), the key cannot be destroyed
1399 * yet. For the time being, just return in error. Eventually (to be
1400 * implemented), the key should be destroyed when all accesses have
1401 * stopped.
1402 */
Ronald Cron5c522922020-11-14 16:35:34 +01001403 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001404 {
Ronald Cron5c522922020-11-14 16:35:34 +01001405 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001406 return( PSA_ERROR_GENERIC_ERROR );
1407 }
1408
Gilles Peskine354f7672019-07-12 23:46:38 +02001409#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001410 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001411 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001412 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001413 /* For a key in a secure element, we need to do three things:
1414 * remove the key file in internal storage, destroy the
1415 * key inside the secure element, and update the driver's
1416 * persistent data. Start a transaction that will encompass these
1417 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001418 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001419 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001420 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001421 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001422 status = psa_crypto_save_transaction( );
1423 if( status != PSA_SUCCESS )
1424 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001425 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001426 /* We should still try to destroy the key in the secure
1427 * element and the key metadata in storage. This is especially
1428 * important if the error is that the storage is full.
1429 * But how to do it exactly without risking an inconsistent
1430 * state after a reset?
1431 * https://github.com/ARMmbed/mbed-crypto/issues/215
1432 */
1433 overall_status = status;
1434 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001435 }
1436
Gilles Peskine354f7672019-07-12 23:46:38 +02001437 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001438 if( overall_status == PSA_SUCCESS )
1439 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001440 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1442
Darryl Greend49a4992018-06-18 17:27:26 +01001443#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001444 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001445 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001446 status = psa_destroy_persistent_key( slot->attr.id );
1447 if( overall_status == PSA_SUCCESS )
1448 overall_status = status;
1449
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001450 /* TODO: other slots may have a copy of the same key. We should
1451 * invalidate them.
1452 * https://github.com/ARMmbed/mbed-crypto/issues/214
1453 */
Darryl Greend49a4992018-06-18 17:27:26 +01001454 }
1455#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001456
Gilles Peskinefc762652019-07-22 19:30:34 +02001457#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1458 if( driver != NULL )
1459 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001460 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001461 if( overall_status == PSA_SUCCESS )
1462 overall_status = status;
1463 status = psa_crypto_stop_transaction( );
1464 if( overall_status == PSA_SUCCESS )
1465 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001466 }
1467#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1468
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001469#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1470exit:
1471#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001472 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001473 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1474 if( overall_status == PSA_SUCCESS )
1475 overall_status = status;
1476 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477}
1478
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001479void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001480{
Gilles Peskineb699f072019-04-26 16:06:02 +02001481 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001482 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001483}
1484
Gilles Peskineb699f072019-04-26 16:06:02 +02001485psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1486 psa_key_type_t type,
1487 const uint8_t *data,
1488 size_t data_length )
1489{
1490 uint8_t *copy = NULL;
1491
1492 if( data_length != 0 )
1493 {
1494 copy = mbedtls_calloc( 1, data_length );
1495 if( copy == NULL )
1496 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1497 memcpy( copy, data, data_length );
1498 }
1499 /* After this point, this function is guaranteed to succeed, so it
1500 * can start modifying `*attributes`. */
1501
1502 if( attributes->domain_parameters != NULL )
1503 {
1504 mbedtls_free( attributes->domain_parameters );
1505 attributes->domain_parameters = NULL;
1506 attributes->domain_parameters_size = 0;
1507 }
1508
1509 attributes->domain_parameters = copy;
1510 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001511 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001512 return( PSA_SUCCESS );
1513}
1514
1515psa_status_t psa_get_key_domain_parameters(
1516 const psa_key_attributes_t *attributes,
1517 uint8_t *data, size_t data_size, size_t *data_length )
1518{
1519 if( attributes->domain_parameters_size > data_size )
1520 return( PSA_ERROR_BUFFER_TOO_SMALL );
1521 *data_length = attributes->domain_parameters_size;
1522 if( attributes->domain_parameters_size != 0 )
1523 memcpy( data, attributes->domain_parameters,
1524 attributes->domain_parameters_size );
1525 return( PSA_SUCCESS );
1526}
1527
John Durkop07cc04a2020-11-16 22:08:34 -08001528#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001529 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001530static psa_status_t psa_get_rsa_public_exponent(
1531 const mbedtls_rsa_context *rsa,
1532 psa_key_attributes_t *attributes )
1533{
1534 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001536 uint8_t *buffer = NULL;
1537 size_t buflen;
1538 mbedtls_mpi_init( &mpi );
1539
1540 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1541 if( ret != 0 )
1542 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001543 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1544 {
1545 /* It's the default value, which is reported as an empty string,
1546 * so there's nothing to do. */
1547 goto exit;
1548 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001549
1550 buflen = mbedtls_mpi_size( &mpi );
1551 buffer = mbedtls_calloc( 1, buflen );
1552 if( buffer == NULL )
1553 {
1554 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1555 goto exit;
1556 }
1557 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1558 if( ret != 0 )
1559 goto exit;
1560 attributes->domain_parameters = buffer;
1561 attributes->domain_parameters_size = buflen;
1562
1563exit:
1564 mbedtls_mpi_free( &mpi );
1565 if( ret != 0 )
1566 mbedtls_free( buffer );
1567 return( mbedtls_to_psa_error( ret ) );
1568}
John Durkop07cc04a2020-11-16 22:08:34 -08001569#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001570 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001571
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001572/** Retrieve all the publicly-accessible attributes of a key.
1573 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001574psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001575 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001576{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001578 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001579 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001581 psa_reset_key_attributes( attributes );
1582
Ronald Cron5c522922020-11-14 16:35:34 +01001583 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001584 if( status != PSA_SUCCESS )
1585 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001586
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001587 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001588 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1589 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1590
1591#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1592 if( psa_key_slot_is_external( slot ) )
1593 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1594#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001595
Gilles Peskine8e338702019-07-30 20:06:31 +02001596 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001597 {
John Durkop07cc04a2020-11-16 22:08:34 -08001598#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001599 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001600 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001601 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001602#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001603 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001604 * is not yet implemented.
1605 * https://github.com/ARMmbed/mbed-crypto/issues/216
1606 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001607 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001608 break;
1609#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001610 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001611 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001612
Steven Cooreman4fed4552020-08-03 14:46:03 +02001613 status = psa_load_rsa_representation( slot->attr.type,
1614 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001615 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001616 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001617 if( status != PSA_SUCCESS )
1618 break;
1619
Steven Cooremana2371e52020-07-28 14:30:39 +02001620 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001621 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001622 mbedtls_rsa_free( rsa );
1623 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001624 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001625 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001626#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001627 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001628 default:
1629 /* Nothing else to do. */
1630 break;
1631 }
1632
1633 if( status != PSA_SUCCESS )
1634 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001635
Ronald Cron5c522922020-11-14 16:35:34 +01001636 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001637
Ronald Cron5c522922020-11-14 16:35:34 +01001638 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639}
1640
Gilles Peskinec8000c02019-08-02 20:15:51 +02001641#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1642psa_status_t psa_get_key_slot_number(
1643 const psa_key_attributes_t *attributes,
1644 psa_key_slot_number_t *slot_number )
1645{
1646 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1647 {
1648 *slot_number = attributes->slot_number;
1649 return( PSA_SUCCESS );
1650 }
1651 else
1652 return( PSA_ERROR_INVALID_ARGUMENT );
1653}
1654#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1655
Steven Cooremana01795d2020-07-24 22:48:15 +02001656static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1657 uint8_t *data,
1658 size_t data_size,
1659 size_t *data_length )
1660{
1661 if( slot->data.key.bytes > data_size )
1662 return( PSA_ERROR_BUFFER_TOO_SMALL );
1663 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1664 memset( data + slot->data.key.bytes, 0,
1665 data_size - slot->data.key.bytes );
1666 *data_length = slot->data.key.bytes;
1667 return( PSA_SUCCESS );
1668}
1669
Gilles Peskinef603c712019-01-19 13:40:11 +01001670static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1671 uint8_t *data,
1672 size_t data_size,
1673 size_t *data_length,
1674 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001675{
Gilles Peskine5d309672019-07-12 23:47:28 +02001676#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1677 const psa_drv_se_t *drv;
1678 psa_drv_se_context_t *drv_context;
1679#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1680
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001681 *data_length = 0;
1682
Gilles Peskine8e338702019-07-30 20:06:31 +02001683 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001684 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001685
Gilles Peskinef9168942019-09-12 19:20:29 +02001686 /* Reject a zero-length output buffer now, since this can never be a
1687 * valid key representation. This way we know that data must be a valid
1688 * pointer and we can do things like memset(data, ..., data_size). */
1689 if( data_size == 0 )
1690 return( PSA_ERROR_BUFFER_TOO_SMALL );
1691
Gilles Peskine5d309672019-07-12 23:47:28 +02001692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001693 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001694 {
1695 psa_drv_se_export_key_t method;
1696 if( drv->key_management == NULL )
1697 return( PSA_ERROR_NOT_SUPPORTED );
1698 method = ( export_public_key ?
1699 drv->key_management->p_export_public :
1700 drv->key_management->p_export );
1701 if( method == NULL )
1702 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001703 return( method( drv_context,
1704 slot->data.se.slot_number,
1705 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001706 }
1707#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1708
Gilles Peskine8e338702019-07-30 20:06:31 +02001709 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001710 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001711 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001712 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001713 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1714 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001715 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001716 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001717 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001718 /* Exporting public -> public */
1719 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1720 }
1721 else if( !export_public_key )
1722 {
1723 /* Exporting private -> private */
1724 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1725 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001726
Steven Cooreman560c28a2020-07-24 23:20:24 +02001727 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001728 * so conversion is needed. Try the accelerators first. */
1729 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1730 data,
1731 data_size,
1732 data_length );
1733
1734 if( status != PSA_ERROR_NOT_SUPPORTED ||
1735 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1736 return( status );
1737
Steven Cooreman560c28a2020-07-24 23:20:24 +02001738 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1739 {
John Durkop0e005192020-10-31 22:06:54 -07001740#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1741 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001742 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001743 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001744 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001745 slot->data.key.data,
1746 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001747 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001748 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001749 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001750
Steven Cooreman560c28a2020-07-24 23:20:24 +02001751 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001752 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001753 data,
1754 data_size,
1755 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001756
Steven Cooremana2371e52020-07-28 14:30:39 +02001757 mbedtls_rsa_free( rsa );
1758 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001759
Steven Cooreman560c28a2020-07-24 23:20:24 +02001760 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001761#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001762 /* We don't know how to convert a private RSA key to public. */
1763 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001764#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1765 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001766 }
1767 else
Moran Pekera998bc62018-04-16 18:16:20 +03001768 {
John Durkop6ba40d12020-11-10 08:50:04 -08001769#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1770 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001771 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001772 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001773 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001774 slot->data.key.data,
1775 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001776 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001777 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001778 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001779
1780 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1781 PSA_KEY_TYPE_ECC_GET_FAMILY(
1782 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001783 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001784 data,
1785 data_size,
1786 data_length );
1787
Steven Cooremana2371e52020-07-28 14:30:39 +02001788 mbedtls_ecp_keypair_free( ecp );
1789 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001790 return( status );
1791#else
1792 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001793 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001794#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1795 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001796 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001797 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001798 else
1799 {
1800 /* This shouldn't happen in the reference implementation, but
1801 it is valid for a special-purpose implementation to omit
1802 support for exporting certain key types. */
1803 return( PSA_ERROR_NOT_SUPPORTED );
1804 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001805}
1806
Ronald Croncf56a0a2020-08-04 09:51:30 +02001807psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001808 uint8_t *data,
1809 size_t data_size,
1810 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001811{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001813 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001814 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001815
1816 /* Set the key to empty now, so that even when there are errors, we always
1817 * set data_length to a value between 0 and data_size. On error, setting
1818 * the key to empty is a good choice because an empty key representation is
1819 * unlikely to be accepted anywhere. */
1820 *data_length = 0;
1821
1822 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001823 * which don't require any flag, but
1824 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1825 */
1826 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1827 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001828 if( status != PSA_SUCCESS )
1829 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001830
1831 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001832 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001833
Ronald Cron5c522922020-11-14 16:35:34 +01001834 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001835}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001836
Ronald Croncf56a0a2020-08-04 09:51:30 +02001837psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001838 uint8_t *data,
1839 size_t data_size,
1840 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001841{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001843 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001844 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001845
1846 /* Set the key to empty now, so that even when there are errors, we always
1847 * set data_length to a value between 0 and data_size. On error, setting
1848 * the key to empty is a good choice because an empty key representation is
1849 * unlikely to be accepted anywhere. */
1850 *data_length = 0;
1851
1852 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001853 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001854 if( status != PSA_SUCCESS )
1855 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001856
1857 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001858 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001859
Ronald Cron5c522922020-11-14 16:35:34 +01001860 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001861}
1862
Gilles Peskine91e8c332019-08-02 19:19:39 +02001863#if defined(static_assert)
1864static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1865 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001866static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001867 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001868static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001869 "One or more key attribute flag is listed as both internal-only and external-only" );
1870#endif
1871
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001872/** Validate that a key policy is internally well-formed.
1873 *
1874 * This function only rejects invalid policies. It does not validate the
1875 * consistency of the policy with respect to other attributes of the key
1876 * such as the key type.
1877 */
1878static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001879{
1880 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001881 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 PSA_KEY_USAGE_ENCRYPT |
1883 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001884 PSA_KEY_USAGE_SIGN_HASH |
1885 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001886 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1887 return( PSA_ERROR_INVALID_ARGUMENT );
1888
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 return( PSA_SUCCESS );
1890}
1891
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001892/** Validate the internal consistency of key attributes.
1893 *
1894 * This function only rejects invalid attribute values. If does not
1895 * validate the consistency of the attributes with any key data that may
1896 * be involved in the creation of the key.
1897 *
1898 * Call this function early in the key creation process.
1899 *
1900 * \param[in] attributes Key attributes for the new key.
1901 * \param[out] p_drv On any return, the driver for the key, if any.
1902 * NULL for a transparent key.
1903 *
1904 */
1905static psa_status_t psa_validate_key_attributes(
1906 const psa_key_attributes_t *attributes,
1907 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001908{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001909 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001910 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001911 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001912
Ronald Cron54b90082020-10-29 15:26:43 +01001913 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001914 if( status != PSA_SUCCESS )
1915 return( status );
1916
Ronald Crond2ed4812020-07-17 16:11:30 +02001917 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001918 if( status != PSA_SUCCESS )
1919 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001920
Ronald Cron65f38a32020-10-23 17:11:13 +02001921 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1922 {
1923 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1924 return( PSA_ERROR_INVALID_ARGUMENT );
1925 }
1926 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001927 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001928 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001929 if( status != PSA_SUCCESS )
1930 return( status );
1931 }
1932
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001933 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001934 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001935 return( status );
1936
1937 /* Refuse to create overly large keys.
1938 * Note that this doesn't trigger on import if the attributes don't
1939 * explicitly specify a size (so psa_get_key_bits returns 0), so
1940 * psa_import_key() needs its own checks. */
1941 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1942 return( PSA_ERROR_NOT_SUPPORTED );
1943
Gilles Peskine91e8c332019-08-02 19:19:39 +02001944 /* Reject invalid flags. These should not be reachable through the API. */
1945 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1946 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1947 return( PSA_ERROR_INVALID_ARGUMENT );
1948
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001949 return( PSA_SUCCESS );
1950}
1951
Gilles Peskine4747d192019-04-17 15:05:45 +02001952/** Prepare a key slot to receive key material.
1953 *
1954 * This function allocates a key slot and sets its metadata.
1955 *
1956 * If this function fails, call psa_fail_key_creation().
1957 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001958 * This function is intended to be used as follows:
1959 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001960 * it with the specified attributes, and in case of a volatile key assign it
1961 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001962 * -# Populate the slot with the key material.
1963 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1964 * In case of failure at any step, stop the sequence and call
1965 * psa_fail_key_creation().
1966 *
Ronald Cron5c522922020-11-14 16:35:34 +01001967 * On success, the key slot is locked. It is the responsibility of the caller
1968 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001969 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001970 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001971 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001972 * \param[out] p_slot On success, a pointer to the prepared slot.
1973 * \param[out] p_drv On any return, the driver for the key, if any.
1974 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001975 *
1976 * \retval #PSA_SUCCESS
1977 * The key slot is ready to receive key material.
1978 * \return If this function fails, the key slot is an invalid state.
1979 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001980 */
1981static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001982 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001983 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001984 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001985 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001986{
1987 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001988 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001989 psa_key_slot_t *slot;
1990
Gilles Peskinedf179142019-07-15 22:02:14 +02001991 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 *p_drv = NULL;
1993
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001994 status = psa_validate_key_attributes( attributes, p_drv );
1995 if( status != PSA_SUCCESS )
1996 return( status );
1997
Ronald Cronc4d1b512020-07-31 11:26:37 +02001998 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001999 if( status != PSA_SUCCESS )
2000 return( status );
2001 slot = *p_slot;
2002
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002003 /* We're storing the declared bit-size of the key. It's up to each
2004 * creation mechanism to verify that this information is correct.
2005 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002006 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002007 * is optional (import, copy). In case of a volatile key, assign it the
2008 * volatile key identifier associated to the slot returned to contain its
2009 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002010
2011 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002012 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2013 {
2014#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2015 slot->attr.id = volatile_key_id;
2016#else
2017 slot->attr.id.key_id = volatile_key_id;
2018#endif
2019 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002020
Gilles Peskine91e8c332019-08-02 19:19:39 +02002021 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002022 * external-only flags, query `attributes`. Thanks to the check
2023 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002024 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002025 * may have set. */
2026 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002027
Gilles Peskinecbaff462019-07-12 23:46:04 +02002028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002029 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002030 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002031 * create the key file in internal storage, create the
2032 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002033 * persistent data. This is done by starting a transaction that will
2034 * encompass these three actions.
2035 * For registering a volatile key, we just need to find an appropriate
2036 * slot number inside the SE. Since the key is designated volatile, creating
2037 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002038 /* The first thing to do is to find a slot number for the new key.
2039 * We save the slot number in persistent storage as part of the
2040 * transaction data. It will be needed to recover if the power
2041 * fails during the key creation process, to clean up on the secure
2042 * element side after restarting. Obtaining a slot number from the
2043 * secure element driver updates its persistent state, but we do not yet
2044 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002045 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002046 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002047 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002048 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002049 &slot->data.se.slot_number );
2050 if( status != PSA_SUCCESS )
2051 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002052
2053 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002054 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002055 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2056 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2057 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2058 psa_crypto_transaction.key.id = slot->attr.id;
2059 status = psa_crypto_save_transaction( );
2060 if( status != PSA_SUCCESS )
2061 {
2062 (void) psa_crypto_stop_transaction( );
2063 return( status );
2064 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002065 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002066 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002067
2068 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2069 {
2070 /* Key registration only makes sense with a secure element. */
2071 return( PSA_ERROR_INVALID_ARGUMENT );
2072 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002073#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2074
Ronald Cronc4d1b512020-07-31 11:26:37 +02002075 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002076}
Gilles Peskine4747d192019-04-17 15:05:45 +02002077
2078/** Finalize the creation of a key once its key material has been set.
2079 *
2080 * This entails writing the key to persistent storage.
2081 *
2082 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002083 * See the documentation of psa_start_key_creation() for the intended use
2084 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002085 *
Ronald Cron5c522922020-11-14 16:35:34 +01002086 * If the finalization succeeds, the function unlocks the key slot (it was
2087 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2088 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002089 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002090 * \param[in,out] slot Pointer to the slot with key material.
2091 * \param[in] driver The secure element driver for the key,
2092 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002093 * \param[out] key On success, identifier of the key. Note that the
2094 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002095 *
2096 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002097 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002098 * \return If this function fails, the key slot is an invalid state.
2099 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002100 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002101static psa_status_t psa_finish_key_creation(
2102 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002103 psa_se_drv_table_entry_t *driver,
2104 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002105{
2106 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002107 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002108 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002109
2110#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002111 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002112 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002113#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2114 if( driver != NULL )
2115 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002116 psa_se_key_data_storage_t data;
2117#if defined(static_assert)
2118 static_assert( sizeof( slot->data.se.slot_number ) ==
2119 sizeof( data.slot_number ),
2120 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002121#endif
2122 memcpy( &data.slot_number, &slot->data.se.slot_number,
2123 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002124 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002125 (uint8_t*) &data,
2126 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002127 }
2128 else
2129#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2130 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002131 /* Key material is saved in export representation in the slot, so
2132 * just pass the slot buffer for storage. */
2133 status = psa_save_persistent_key( &slot->attr,
2134 slot->data.key.data,
2135 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002136 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002137 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002138#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2139
Gilles Peskinecbaff462019-07-12 23:46:04 +02002140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002141 /* Finish the transaction for a key creation. This does not
2142 * happen when registering an existing key. Detect this case
2143 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002144 * creation of a persistent key in a secure element requires a transaction,
2145 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002146 if( driver != NULL &&
2147 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002148 {
2149 status = psa_save_se_persistent_data( driver );
2150 if( status != PSA_SUCCESS )
2151 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002152 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002153 return( status );
2154 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002155 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002156 }
2157#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2158
Ronald Cron50972942020-11-14 11:28:25 +01002159 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002160 {
2161 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002162 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002163 if( status != PSA_SUCCESS )
2164 *key = MBEDTLS_SVC_KEY_ID_INIT;
2165 }
Ronald Cron50972942020-11-14 11:28:25 +01002166
Gilles Peskine4747d192019-04-17 15:05:45 +02002167 return( status );
2168}
2169
2170/** Abort the creation of a key.
2171 *
2172 * You may call this function after calling psa_start_key_creation(),
2173 * or after psa_finish_key_creation() fails. In other circumstances, this
2174 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002175 * See the documentation of psa_start_key_creation() for the intended use
2176 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002177 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002178 * \param[in,out] slot Pointer to the slot with key material.
2179 * \param[in] driver The secure element driver for the key,
2180 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002181 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002182static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002183 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002184{
Gilles Peskine011e4282019-06-26 18:34:38 +02002185 (void) driver;
2186
Gilles Peskine4747d192019-04-17 15:05:45 +02002187 if( slot == NULL )
2188 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002189
2190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002191 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002192 * element, and the failure happened later (when saving metadata
2193 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002194 * element.
2195 * https://github.com/ARMmbed/mbed-crypto/issues/217
2196 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002197
Gilles Peskined7729582019-08-05 15:55:54 +02002198 /* Abort the ongoing transaction if any (there may not be one if
2199 * the creation process failed before starting one, or if the
2200 * key creation is a registration of a key in a secure element).
2201 * Earlier functions must already have done what it takes to undo any
2202 * partial creation. All that's left is to update the transaction data
2203 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002204 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2206
Gilles Peskine4747d192019-04-17 15:05:45 +02002207 psa_wipe_key_slot( slot );
2208}
2209
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002210/** Validate optional attributes during key creation.
2211 *
2212 * Some key attributes are optional during key creation. If they are
2213 * specified in the attributes structure, check that they are consistent
2214 * with the data in the slot.
2215 *
2216 * This function should be called near the end of key creation, after
2217 * the slot in memory is fully populated but before saving persistent data.
2218 */
2219static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002220 const psa_key_slot_t *slot,
2221 const psa_key_attributes_t *attributes )
2222{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002223 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002224 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002225 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002226 return( PSA_ERROR_INVALID_ARGUMENT );
2227 }
2228
2229 if( attributes->domain_parameters_size != 0 )
2230 {
John Durkop0e005192020-10-31 22:06:54 -07002231#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2232 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002233 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002234 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002235 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002236 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002238
Steven Cooreman7f391872020-07-30 14:57:44 +02002239 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002240 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002241 slot->data.key.data,
2242 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002243 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002244 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002245 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002246
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002247 mbedtls_mpi_init( &actual );
2248 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002249 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002250 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002251 mbedtls_rsa_free( rsa );
2252 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002253 if( ret != 0 )
2254 goto rsa_exit;
2255 ret = mbedtls_mpi_read_binary( &required,
2256 attributes->domain_parameters,
2257 attributes->domain_parameters_size );
2258 if( ret != 0 )
2259 goto rsa_exit;
2260 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2261 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2262 rsa_exit:
2263 mbedtls_mpi_free( &actual );
2264 mbedtls_mpi_free( &required );
2265 if( ret != 0)
2266 return( mbedtls_to_psa_error( ret ) );
2267 }
2268 else
John Durkop9814fa22020-11-04 12:28:15 -08002269#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2270 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002271 {
2272 return( PSA_ERROR_INVALID_ARGUMENT );
2273 }
2274 }
2275
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002276 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002277 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002278 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002279 return( PSA_ERROR_INVALID_ARGUMENT );
2280 }
2281
2282 return( PSA_SUCCESS );
2283}
2284
Gilles Peskine4747d192019-04-17 15:05:45 +02002285psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002286 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002287 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002288 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002289{
2290 psa_status_t status;
2291 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002292 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002293
Ronald Cron81709fc2020-11-14 12:10:32 +01002294 *key = MBEDTLS_SVC_KEY_ID_INIT;
2295
Gilles Peskine0f84d622019-09-12 19:03:13 +02002296 /* Reject zero-length symmetric keys (including raw data key objects).
2297 * This also rejects any key which might be encoded as an empty string,
2298 * which is never valid. */
2299 if( data_length == 0 )
2300 return( PSA_ERROR_INVALID_ARGUMENT );
2301
Gilles Peskinedf179142019-07-15 22:02:14 +02002302 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002303 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002304 if( status != PSA_SUCCESS )
2305 goto exit;
2306
Gilles Peskine5d309672019-07-12 23:47:28 +02002307#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2308 if( driver != NULL )
2309 {
2310 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002311 /* The driver should set the number of key bits, however in
2312 * case it doesn't, we initialize bits to an invalid value. */
2313 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002314 if( drv->key_management == NULL ||
2315 drv->key_management->p_import == NULL )
2316 {
2317 status = PSA_ERROR_NOT_SUPPORTED;
2318 goto exit;
2319 }
2320 status = drv->key_management->p_import(
2321 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002322 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002323 &bits );
2324 if( status != PSA_SUCCESS )
2325 goto exit;
2326 if( bits > PSA_MAX_KEY_BITS )
2327 {
2328 status = PSA_ERROR_NOT_SUPPORTED;
2329 goto exit;
2330 }
2331 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002332 }
2333 else
2334#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2335 {
2336 status = psa_import_key_into_slot( slot, data, data_length );
2337 if( status != PSA_SUCCESS )
2338 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002339 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002340 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002341 if( status != PSA_SUCCESS )
2342 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002343
Ronald Cron81709fc2020-11-14 12:10:32 +01002344 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002345exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002346 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002347 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002348
Gilles Peskine4747d192019-04-17 15:05:45 +02002349 return( status );
2350}
2351
Gilles Peskined7729582019-08-05 15:55:54 +02002352#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2353psa_status_t mbedtls_psa_register_se_key(
2354 const psa_key_attributes_t *attributes )
2355{
2356 psa_status_t status;
2357 psa_key_slot_t *slot = NULL;
2358 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002360
2361 /* Leaving attributes unspecified is not currently supported.
2362 * It could make sense to query the key type and size from the
2363 * secure element, but not all secure elements support this
2364 * and the driver HAL doesn't currently support it. */
2365 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2366 return( PSA_ERROR_NOT_SUPPORTED );
2367 if( psa_get_key_bits( attributes ) == 0 )
2368 return( PSA_ERROR_NOT_SUPPORTED );
2369
2370 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002371 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002372 if( status != PSA_SUCCESS )
2373 goto exit;
2374
Ronald Cron81709fc2020-11-14 12:10:32 +01002375 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002376
2377exit:
2378 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002379 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002380
Gilles Peskined7729582019-08-05 15:55:54 +02002381 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002382 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002383 return( status );
2384}
2385#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2386
Gilles Peskinef603c712019-01-19 13:40:11 +01002387static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002388 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002389{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002390 psa_status_t status = psa_copy_key_material_into_slot( target,
2391 source->data.key.data,
2392 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002393 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002394 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002395
Steven Cooreman398aee52020-10-13 14:35:45 +02002396 target->attr.type = source->attr.type;
2397 target->attr.bits = source->attr.bits;
2398
2399 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002400}
2401
Ronald Croncf56a0a2020-08-04 09:51:30 +02002402psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002403 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002404 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002405{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002407 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002408 psa_key_slot_t *source_slot = NULL;
2409 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002410 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002411 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002412
Ronald Cron81709fc2020-11-14 12:10:32 +01002413 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2414
Ronald Cron5c522922020-11-14 16:35:34 +01002415 status = psa_get_and_lock_transparent_key_slot_with_policy(
2416 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002417 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002418 goto exit;
2419
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002420 status = psa_validate_optional_attributes( source_slot,
2421 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002422 if( status != PSA_SUCCESS )
2423 goto exit;
2424
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002425 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002426 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002427 if( status != PSA_SUCCESS )
2428 goto exit;
2429
Ronald Cron81709fc2020-11-14 12:10:32 +01002430 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2431 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002432 if( status != PSA_SUCCESS )
2433 goto exit;
2434
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002435#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2436 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002437 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002438 /* Copying to a secure element is not implemented yet. */
2439 status = PSA_ERROR_NOT_SUPPORTED;
2440 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002441 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002442#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002443
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002444 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002445 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002446 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002447
Ronald Cron81709fc2020-11-14 12:10:32 +01002448 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002449exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002450 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002451 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002452
Ronald Cron5c522922020-11-14 16:35:34 +01002453 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002454
Ronald Cron5c522922020-11-14 16:35:34 +01002455 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002456}
2457
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002458
2459
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002460/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002461/* Message digests */
2462/****************************************************************/
2463
John Durkop07cc04a2020-11-16 22:08:34 -08002464#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002465 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2466 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002467 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002468static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002469{
2470 switch( alg )
2471 {
2472#if defined(MBEDTLS_MD2_C)
2473 case PSA_ALG_MD2:
2474 return( &mbedtls_md2_info );
2475#endif
2476#if defined(MBEDTLS_MD4_C)
2477 case PSA_ALG_MD4:
2478 return( &mbedtls_md4_info );
2479#endif
2480#if defined(MBEDTLS_MD5_C)
2481 case PSA_ALG_MD5:
2482 return( &mbedtls_md5_info );
2483#endif
2484#if defined(MBEDTLS_RIPEMD160_C)
2485 case PSA_ALG_RIPEMD160:
2486 return( &mbedtls_ripemd160_info );
2487#endif
2488#if defined(MBEDTLS_SHA1_C)
2489 case PSA_ALG_SHA_1:
2490 return( &mbedtls_sha1_info );
2491#endif
2492#if defined(MBEDTLS_SHA256_C)
2493 case PSA_ALG_SHA_224:
2494 return( &mbedtls_sha224_info );
2495 case PSA_ALG_SHA_256:
2496 return( &mbedtls_sha256_info );
2497#endif
2498#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002499#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002500 case PSA_ALG_SHA_384:
2501 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002502#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 case PSA_ALG_SHA_512:
2504 return( &mbedtls_sha512_info );
2505#endif
2506 default:
2507 return( NULL );
2508 }
2509}
John Durkop07cc04a2020-11-16 22:08:34 -08002510#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002511 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2512 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2513 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002514
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002515psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2516{
2517 switch( operation->alg )
2518 {
Gilles Peskine81736312018-06-26 15:04:31 +02002519 case 0:
2520 /* The object has (apparently) been initialized but it is not
2521 * in use. It's ok to call abort on such an object, and there's
2522 * nothing to do. */
2523 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002524#if defined(MBEDTLS_MD2_C)
2525 case PSA_ALG_MD2:
2526 mbedtls_md2_free( &operation->ctx.md2 );
2527 break;
2528#endif
2529#if defined(MBEDTLS_MD4_C)
2530 case PSA_ALG_MD4:
2531 mbedtls_md4_free( &operation->ctx.md4 );
2532 break;
2533#endif
2534#if defined(MBEDTLS_MD5_C)
2535 case PSA_ALG_MD5:
2536 mbedtls_md5_free( &operation->ctx.md5 );
2537 break;
2538#endif
2539#if defined(MBEDTLS_RIPEMD160_C)
2540 case PSA_ALG_RIPEMD160:
2541 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2542 break;
2543#endif
2544#if defined(MBEDTLS_SHA1_C)
2545 case PSA_ALG_SHA_1:
2546 mbedtls_sha1_free( &operation->ctx.sha1 );
2547 break;
2548#endif
2549#if defined(MBEDTLS_SHA256_C)
2550 case PSA_ALG_SHA_224:
2551 case PSA_ALG_SHA_256:
2552 mbedtls_sha256_free( &operation->ctx.sha256 );
2553 break;
2554#endif
2555#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002556#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002557 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002558#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002559 case PSA_ALG_SHA_512:
2560 mbedtls_sha512_free( &operation->ctx.sha512 );
2561 break;
2562#endif
2563 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002564 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002565 }
2566 operation->alg = 0;
2567 return( PSA_SUCCESS );
2568}
2569
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002570psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 psa_algorithm_t alg )
2572{
Janos Follath24eed8d2019-11-22 13:21:35 +00002573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002574
2575 /* A context must be freshly initialized before it can be set up. */
2576 if( operation->alg != 0 )
2577 {
2578 return( PSA_ERROR_BAD_STATE );
2579 }
2580
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581 switch( alg )
2582 {
2583#if defined(MBEDTLS_MD2_C)
2584 case PSA_ALG_MD2:
2585 mbedtls_md2_init( &operation->ctx.md2 );
2586 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2587 break;
2588#endif
2589#if defined(MBEDTLS_MD4_C)
2590 case PSA_ALG_MD4:
2591 mbedtls_md4_init( &operation->ctx.md4 );
2592 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2593 break;
2594#endif
2595#if defined(MBEDTLS_MD5_C)
2596 case PSA_ALG_MD5:
2597 mbedtls_md5_init( &operation->ctx.md5 );
2598 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2599 break;
2600#endif
2601#if defined(MBEDTLS_RIPEMD160_C)
2602 case PSA_ALG_RIPEMD160:
2603 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2604 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2605 break;
2606#endif
2607#if defined(MBEDTLS_SHA1_C)
2608 case PSA_ALG_SHA_1:
2609 mbedtls_sha1_init( &operation->ctx.sha1 );
2610 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2611 break;
2612#endif
2613#if defined(MBEDTLS_SHA256_C)
2614 case PSA_ALG_SHA_224:
2615 mbedtls_sha256_init( &operation->ctx.sha256 );
2616 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2617 break;
2618 case PSA_ALG_SHA_256:
2619 mbedtls_sha256_init( &operation->ctx.sha256 );
2620 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2621 break;
2622#endif
2623#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002624#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002625 case PSA_ALG_SHA_384:
2626 mbedtls_sha512_init( &operation->ctx.sha512 );
2627 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2628 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002629#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002630 case PSA_ALG_SHA_512:
2631 mbedtls_sha512_init( &operation->ctx.sha512 );
2632 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2633 break;
2634#endif
2635 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002636 return( PSA_ALG_IS_HASH( alg ) ?
2637 PSA_ERROR_NOT_SUPPORTED :
2638 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002639 }
2640 if( ret == 0 )
2641 operation->alg = alg;
2642 else
2643 psa_hash_abort( operation );
2644 return( mbedtls_to_psa_error( ret ) );
2645}
2646
2647psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2648 const uint8_t *input,
2649 size_t input_length )
2650{
Janos Follath24eed8d2019-11-22 13:21:35 +00002651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002652
2653 /* Don't require hash implementations to behave correctly on a
2654 * zero-length input, which may have an invalid pointer. */
2655 if( input_length == 0 )
2656 return( PSA_SUCCESS );
2657
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002658 switch( operation->alg )
2659 {
2660#if defined(MBEDTLS_MD2_C)
2661 case PSA_ALG_MD2:
2662 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2663 input, input_length );
2664 break;
2665#endif
2666#if defined(MBEDTLS_MD4_C)
2667 case PSA_ALG_MD4:
2668 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2669 input, input_length );
2670 break;
2671#endif
2672#if defined(MBEDTLS_MD5_C)
2673 case PSA_ALG_MD5:
2674 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2675 input, input_length );
2676 break;
2677#endif
2678#if defined(MBEDTLS_RIPEMD160_C)
2679 case PSA_ALG_RIPEMD160:
2680 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2681 input, input_length );
2682 break;
2683#endif
2684#if defined(MBEDTLS_SHA1_C)
2685 case PSA_ALG_SHA_1:
2686 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2687 input, input_length );
2688 break;
2689#endif
2690#if defined(MBEDTLS_SHA256_C)
2691 case PSA_ALG_SHA_224:
2692 case PSA_ALG_SHA_256:
2693 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2694 input, input_length );
2695 break;
2696#endif
2697#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002698#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002699 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002700#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002701 case PSA_ALG_SHA_512:
2702 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2703 input, input_length );
2704 break;
2705#endif
2706 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002707 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002708 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002709
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002710 if( ret != 0 )
2711 psa_hash_abort( operation );
2712 return( mbedtls_to_psa_error( ret ) );
2713}
2714
2715psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2716 uint8_t *hash,
2717 size_t hash_size,
2718 size_t *hash_length )
2719{
itayzafrir40835d42018-08-02 13:14:17 +03002720 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002722 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723
2724 /* Fill the output buffer with something that isn't a valid hash
2725 * (barring an attack on the hash and deliberately-crafted input),
2726 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002727 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002728 /* If hash_size is 0 then hash may be NULL and then the
2729 * call to memset would have undefined behavior. */
2730 if( hash_size != 0 )
2731 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002732
2733 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002734 {
2735 status = PSA_ERROR_BUFFER_TOO_SMALL;
2736 goto exit;
2737 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002738
2739 switch( operation->alg )
2740 {
2741#if defined(MBEDTLS_MD2_C)
2742 case PSA_ALG_MD2:
2743 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2744 break;
2745#endif
2746#if defined(MBEDTLS_MD4_C)
2747 case PSA_ALG_MD4:
2748 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2749 break;
2750#endif
2751#if defined(MBEDTLS_MD5_C)
2752 case PSA_ALG_MD5:
2753 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2754 break;
2755#endif
2756#if defined(MBEDTLS_RIPEMD160_C)
2757 case PSA_ALG_RIPEMD160:
2758 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2759 break;
2760#endif
2761#if defined(MBEDTLS_SHA1_C)
2762 case PSA_ALG_SHA_1:
2763 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2764 break;
2765#endif
2766#if defined(MBEDTLS_SHA256_C)
2767 case PSA_ALG_SHA_224:
2768 case PSA_ALG_SHA_256:
2769 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2770 break;
2771#endif
2772#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002773#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002774 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002775#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002776 case PSA_ALG_SHA_512:
2777 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2778 break;
2779#endif
2780 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002781 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002782 }
itayzafrir40835d42018-08-02 13:14:17 +03002783 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002784
itayzafrir40835d42018-08-02 13:14:17 +03002785exit:
2786 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002788 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789 return( psa_hash_abort( operation ) );
2790 }
2791 else
2792 {
2793 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002794 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002795 }
2796}
2797
Gilles Peskine2d277862018-06-18 15:41:12 +02002798psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2799 const uint8_t *hash,
2800 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002801{
2802 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2803 size_t actual_hash_length;
2804 psa_status_t status = psa_hash_finish( operation,
2805 actual_hash, sizeof( actual_hash ),
2806 &actual_hash_length );
2807 if( status != PSA_SUCCESS )
2808 return( status );
2809 if( actual_hash_length != hash_length )
2810 return( PSA_ERROR_INVALID_SIGNATURE );
2811 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2812 return( PSA_ERROR_INVALID_SIGNATURE );
2813 return( PSA_SUCCESS );
2814}
2815
Gilles Peskine0a749c82019-11-28 19:33:58 +01002816psa_status_t psa_hash_compute( psa_algorithm_t alg,
2817 const uint8_t *input, size_t input_length,
2818 uint8_t *hash, size_t hash_size,
2819 size_t *hash_length )
2820{
2821 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2823
2824 *hash_length = hash_size;
2825 status = psa_hash_setup( &operation, alg );
2826 if( status != PSA_SUCCESS )
2827 goto exit;
2828 status = psa_hash_update( &operation, input, input_length );
2829 if( status != PSA_SUCCESS )
2830 goto exit;
2831 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2832 if( status != PSA_SUCCESS )
2833 goto exit;
2834
2835exit:
2836 if( status == PSA_SUCCESS )
2837 status = psa_hash_abort( &operation );
2838 else
2839 psa_hash_abort( &operation );
2840 return( status );
2841}
2842
2843psa_status_t psa_hash_compare( psa_algorithm_t alg,
2844 const uint8_t *input, size_t input_length,
2845 const uint8_t *hash, size_t hash_length )
2846{
2847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2849
2850 status = psa_hash_setup( &operation, alg );
2851 if( status != PSA_SUCCESS )
2852 goto exit;
2853 status = psa_hash_update( &operation, input, input_length );
2854 if( status != PSA_SUCCESS )
2855 goto exit;
2856 status = psa_hash_verify( &operation, hash, hash_length );
2857 if( status != PSA_SUCCESS )
2858 goto exit;
2859
2860exit:
2861 if( status == PSA_SUCCESS )
2862 status = psa_hash_abort( &operation );
2863 else
2864 psa_hash_abort( &operation );
2865 return( status );
2866}
2867
Gilles Peskineeb35d782019-01-22 17:56:16 +01002868psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2869 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002870{
2871 if( target_operation->alg != 0 )
2872 return( PSA_ERROR_BAD_STATE );
2873
2874 switch( source_operation->alg )
2875 {
2876 case 0:
2877 return( PSA_ERROR_BAD_STATE );
2878#if defined(MBEDTLS_MD2_C)
2879 case PSA_ALG_MD2:
2880 mbedtls_md2_clone( &target_operation->ctx.md2,
2881 &source_operation->ctx.md2 );
2882 break;
2883#endif
2884#if defined(MBEDTLS_MD4_C)
2885 case PSA_ALG_MD4:
2886 mbedtls_md4_clone( &target_operation->ctx.md4,
2887 &source_operation->ctx.md4 );
2888 break;
2889#endif
2890#if defined(MBEDTLS_MD5_C)
2891 case PSA_ALG_MD5:
2892 mbedtls_md5_clone( &target_operation->ctx.md5,
2893 &source_operation->ctx.md5 );
2894 break;
2895#endif
2896#if defined(MBEDTLS_RIPEMD160_C)
2897 case PSA_ALG_RIPEMD160:
2898 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2899 &source_operation->ctx.ripemd160 );
2900 break;
2901#endif
2902#if defined(MBEDTLS_SHA1_C)
2903 case PSA_ALG_SHA_1:
2904 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2905 &source_operation->ctx.sha1 );
2906 break;
2907#endif
2908#if defined(MBEDTLS_SHA256_C)
2909 case PSA_ALG_SHA_224:
2910 case PSA_ALG_SHA_256:
2911 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2912 &source_operation->ctx.sha256 );
2913 break;
2914#endif
2915#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002916#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002917 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002918#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002919 case PSA_ALG_SHA_512:
2920 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2921 &source_operation->ctx.sha512 );
2922 break;
2923#endif
2924 default:
2925 return( PSA_ERROR_NOT_SUPPORTED );
2926 }
2927
2928 target_operation->alg = source_operation->alg;
2929 return( PSA_SUCCESS );
2930}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002931
2932
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002933/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934/* MAC */
2935/****************************************************************/
2936
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002937static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 psa_algorithm_t alg,
2939 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002940 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002941 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002944 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002946 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002947 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002948
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2950 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002951 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002952 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002953 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002954 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955 mode = MBEDTLS_MODE_STREAM;
2956 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 case PSA_ALG_CTR:
2958 mode = MBEDTLS_MODE_CTR;
2959 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002960 case PSA_ALG_CFB:
2961 mode = MBEDTLS_MODE_CFB;
2962 break;
2963 case PSA_ALG_OFB:
2964 mode = MBEDTLS_MODE_OFB;
2965 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002966 case PSA_ALG_ECB_NO_PADDING:
2967 mode = MBEDTLS_MODE_ECB;
2968 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002969 case PSA_ALG_CBC_NO_PADDING:
2970 mode = MBEDTLS_MODE_CBC;
2971 break;
2972 case PSA_ALG_CBC_PKCS7:
2973 mode = MBEDTLS_MODE_CBC;
2974 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002975 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976 mode = MBEDTLS_MODE_CCM;
2977 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002978 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002979 mode = MBEDTLS_MODE_GCM;
2980 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002981 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2982 mode = MBEDTLS_MODE_CHACHAPOLY;
2983 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984 default:
2985 return( NULL );
2986 }
2987 }
2988 else if( alg == PSA_ALG_CMAC )
2989 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002990 else
2991 return( NULL );
2992
2993 switch( key_type )
2994 {
2995 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002996 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997 break;
2998 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002999 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3000 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003001 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003002 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003004 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003005 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3006 * but two-key Triple-DES is functionally three-key Triple-DES
3007 * with K1=K3, so that's how we present it to mbedtls. */
3008 if( key_bits == 128 )
3009 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 break;
3011 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003012 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013 break;
3014 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003015 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003017 case PSA_KEY_TYPE_CHACHA20:
3018 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3019 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 default:
3021 return( NULL );
3022 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003023 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003024 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003025
Jaeden Amero23bbb752018-06-26 14:16:54 +01003026 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3027 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028}
3029
John Durkop6ba40d12020-11-10 08:50:04 -08003030#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003031static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003032{
Gilles Peskine2d277862018-06-18 15:41:12 +02003033 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003034 {
3035 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003036 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003037 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003038 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003039 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003040 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003041 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003042 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003043 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003044 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003045 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003046 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003047 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003048 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003049 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003050 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003051 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003052 return( 128 );
3053 default:
3054 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003055 }
3056}
John Durkop07cc04a2020-11-16 22:08:34 -08003057#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003058
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003059/* Initialize the MAC operation structure. Once this function has been
3060 * called, psa_mac_abort can run and will do the right thing. */
3061static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3062 psa_algorithm_t alg )
3063{
3064 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3065
3066 operation->alg = alg;
3067 operation->key_set = 0;
3068 operation->iv_set = 0;
3069 operation->iv_required = 0;
3070 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003071 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003072
3073#if defined(MBEDTLS_CMAC_C)
3074 if( alg == PSA_ALG_CMAC )
3075 {
3076 operation->iv_required = 0;
3077 mbedtls_cipher_init( &operation->ctx.cmac );
3078 status = PSA_SUCCESS;
3079 }
3080 else
3081#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003082#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003083 if( PSA_ALG_IS_HMAC( operation->alg ) )
3084 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003085 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3086 operation->ctx.hmac.hash_ctx.alg = 0;
3087 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003088 }
3089 else
John Durkopd0321952020-10-29 21:37:36 -07003090#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003091 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003092 if( ! PSA_ALG_IS_MAC( alg ) )
3093 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003094 }
3095
3096 if( status != PSA_SUCCESS )
3097 memset( operation, 0, sizeof( *operation ) );
3098 return( status );
3099}
3100
John Durkop6ba40d12020-11-10 08:50:04 -08003101#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003102static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3103{
Gilles Peskine3f108122018-12-07 18:14:53 +01003104 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003105 return( psa_hash_abort( &hmac->hash_ctx ) );
3106}
John Durkop6ba40d12020-11-10 08:50:04 -08003107#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003108
Gilles Peskine8c9def32018-02-08 10:02:12 +01003109psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3110{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003111 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003112 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003113 /* The object has (apparently) been initialized but it is not
3114 * in use. It's ok to call abort on such an object, and there's
3115 * nothing to do. */
3116 return( PSA_SUCCESS );
3117 }
3118 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003120 if( operation->alg == PSA_ALG_CMAC )
3121 {
3122 mbedtls_cipher_free( &operation->ctx.cmac );
3123 }
3124 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003126#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003127 if( PSA_ALG_IS_HMAC( operation->alg ) )
3128 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003129 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003130 }
3131 else
John Durkopd0321952020-10-29 21:37:36 -07003132#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003133 {
3134 /* Sanity check (shouldn't happen: operation->alg should
3135 * always have been initialized to a valid value). */
3136 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003137 }
Moran Peker41deec42018-04-04 15:43:05 +03003138
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139 operation->alg = 0;
3140 operation->key_set = 0;
3141 operation->iv_set = 0;
3142 operation->iv_required = 0;
3143 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003144 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003145
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003147
3148bad_state:
3149 /* If abort is called on an uninitialized object, we can't trust
3150 * anything. Wipe the object in case it contains confidential data.
3151 * This may result in a memory leak if a pointer gets overwritten,
3152 * but it's too late to do anything about this. */
3153 memset( operation, 0, sizeof( *operation ) );
3154 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003155}
3156
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003157#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003158static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003159 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003160 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003161 const mbedtls_cipher_info_t *cipher_info )
3162{
Janos Follath24eed8d2019-11-22 13:21:35 +00003163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003164
3165 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003166
3167 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3168 if( ret != 0 )
3169 return( ret );
3170
3171 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003172 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003173 key_bits );
3174 return( ret );
3175}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003176#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003177
John Durkop6ba40d12020-11-10 08:50:04 -08003178#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003179static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3180 const uint8_t *key,
3181 size_t key_length,
3182 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003183{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003184 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003185 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003186 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003187 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003188 psa_status_t status;
3189
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003190 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3191 * overflow below. This should never trigger if the hash algorithm
3192 * is implemented correctly. */
3193 /* The size checks against the ipad and opad buffers cannot be written
3194 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3195 * because that triggers -Wlogical-op on GCC 7.3. */
3196 if( block_size > sizeof( ipad ) )
3197 return( PSA_ERROR_NOT_SUPPORTED );
3198 if( block_size > sizeof( hmac->opad ) )
3199 return( PSA_ERROR_NOT_SUPPORTED );
3200 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003201 return( PSA_ERROR_NOT_SUPPORTED );
3202
Gilles Peskined223b522018-06-11 18:12:58 +02003203 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003204 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003205 status = psa_hash_compute( hash_alg, key, key_length,
3206 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003207 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003208 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003209 }
Gilles Peskine96889972018-07-12 17:07:03 +02003210 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3211 * but it is permitted. It is common when HMAC is used in HKDF, for
3212 * example. Don't call `memcpy` in the 0-length because `key` could be
3213 * an invalid pointer which would make the behavior undefined. */
3214 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003215 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003216
Gilles Peskined223b522018-06-11 18:12:58 +02003217 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3218 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003219 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003220 ipad[i] ^= 0x36;
3221 memset( ipad + key_length, 0x36, block_size - key_length );
3222
3223 /* Copy the key material from ipad to opad, flipping the requisite bits,
3224 * and filling the rest of opad with the requisite constant. */
3225 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003226 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3227 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003228
Gilles Peskine01126fa2018-07-12 17:04:55 +02003229 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003230 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003231 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003232
Gilles Peskine01126fa2018-07-12 17:04:55 +02003233 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003234
3235cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003236 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003237
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003238 return( status );
3239}
John Durkop6ba40d12020-11-10 08:50:04 -08003240#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241
Gilles Peskine89167cb2018-07-08 20:12:23 +02003242static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003243 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003244 psa_algorithm_t alg,
3245 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003246{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003248 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003249 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003250 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003251 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003252 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003253 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003254 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003255
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003256 /* A context must be freshly initialized before it can be set up. */
3257 if( operation->alg != 0 )
3258 {
3259 return( PSA_ERROR_BAD_STATE );
3260 }
3261
Gilles Peskined911eb72018-08-14 15:18:45 +02003262 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003263 if( status != PSA_SUCCESS )
3264 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003265 if( is_sign )
3266 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003267
Ronald Cron5c522922020-11-14 16:35:34 +01003268 status = psa_get_and_lock_transparent_key_slot_with_policy(
3269 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003270 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003271 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003272 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003273
Gilles Peskine8c9def32018-02-08 10:02:12 +01003274#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003275 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003276 {
3277 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003278 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003279 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003281 if( cipher_info == NULL )
3282 {
3283 status = PSA_ERROR_NOT_SUPPORTED;
3284 goto exit;
3285 }
3286 operation->mac_size = cipher_info->block_size;
3287 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3288 status = mbedtls_to_psa_error( ret );
3289 }
3290 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003291#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003292#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003293 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003294 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003295 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003296 if( hash_alg == 0 )
3297 {
3298 status = PSA_ERROR_NOT_SUPPORTED;
3299 goto exit;
3300 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003301
3302 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3303 /* Sanity check. This shouldn't fail on a valid configuration. */
3304 if( operation->mac_size == 0 ||
3305 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3306 {
3307 status = PSA_ERROR_NOT_SUPPORTED;
3308 goto exit;
3309 }
3310
Gilles Peskine8e338702019-07-30 20:06:31 +02003311 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003312 {
3313 status = PSA_ERROR_INVALID_ARGUMENT;
3314 goto exit;
3315 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003316
Gilles Peskine01126fa2018-07-12 17:04:55 +02003317 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003318 slot->data.key.data,
3319 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003320 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003321 }
3322 else
John Durkopd0321952020-10-29 21:37:36 -07003323#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003324 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003325 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003326 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003327 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003328
Gilles Peskined911eb72018-08-14 15:18:45 +02003329 if( truncated == 0 )
3330 {
3331 /* The "normal" case: untruncated algorithm. Nothing to do. */
3332 }
3333 else if( truncated < 4 )
3334 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003335 /* A very short MAC is too short for security since it can be
3336 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3337 * so we make this our minimum, even though 32 bits is still
3338 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003339 status = PSA_ERROR_NOT_SUPPORTED;
3340 }
3341 else if( truncated > operation->mac_size )
3342 {
3343 /* It's impossible to "truncate" to a larger length. */
3344 status = PSA_ERROR_INVALID_ARGUMENT;
3345 }
3346 else
3347 operation->mac_size = truncated;
3348
Gilles Peskinefbfac682018-07-08 20:51:54 +02003349exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003350 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003351 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003352 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003353 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003354 else
3355 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003356 operation->key_set = 1;
3357 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003358
Ronald Cron5c522922020-11-14 16:35:34 +01003359 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003360
Ronald Cron5c522922020-11-14 16:35:34 +01003361 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003362}
3363
Gilles Peskine89167cb2018-07-08 20:12:23 +02003364psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003365 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003366 psa_algorithm_t alg )
3367{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003368 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003369}
3370
3371psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003372 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003373 psa_algorithm_t alg )
3374{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003375 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003376}
3377
Gilles Peskine8c9def32018-02-08 10:02:12 +01003378psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3379 const uint8_t *input,
3380 size_t input_length )
3381{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003382 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003384 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003385 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003386 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003387 operation->has_input = 1;
3388
Gilles Peskine8c9def32018-02-08 10:02:12 +01003389#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003390 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003391 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003392 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3393 input, input_length );
3394 status = mbedtls_to_psa_error( ret );
3395 }
3396 else
3397#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003398#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003399 if( PSA_ALG_IS_HMAC( operation->alg ) )
3400 {
3401 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3402 input_length );
3403 }
3404 else
John Durkopd0321952020-10-29 21:37:36 -07003405#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003406 {
3407 /* This shouldn't happen if `operation` was initialized by
3408 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003409 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003410 }
3411
Gilles Peskinefbfac682018-07-08 20:51:54 +02003412 if( status != PSA_SUCCESS )
3413 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003414 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415}
3416
John Durkop6ba40d12020-11-10 08:50:04 -08003417#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003418static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3419 uint8_t *mac,
3420 size_t mac_size )
3421{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003422 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003423 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3424 size_t hash_size = 0;
3425 size_t block_size = psa_get_hash_block_size( hash_alg );
3426 psa_status_t status;
3427
Gilles Peskine01126fa2018-07-12 17:04:55 +02003428 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3429 if( status != PSA_SUCCESS )
3430 return( status );
3431 /* From here on, tmp needs to be wiped. */
3432
3433 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3434 if( status != PSA_SUCCESS )
3435 goto exit;
3436
3437 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3438 if( status != PSA_SUCCESS )
3439 goto exit;
3440
3441 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3442 if( status != PSA_SUCCESS )
3443 goto exit;
3444
Gilles Peskined911eb72018-08-14 15:18:45 +02003445 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3446 if( status != PSA_SUCCESS )
3447 goto exit;
3448
3449 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003450
3451exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003452 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003453 return( status );
3454}
John Durkop6ba40d12020-11-10 08:50:04 -08003455#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003456
mohammad16036df908f2018-04-02 08:34:15 -07003457static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003458 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003459 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003460{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003461 if( ! operation->key_set )
3462 return( PSA_ERROR_BAD_STATE );
3463 if( operation->iv_required && ! operation->iv_set )
3464 return( PSA_ERROR_BAD_STATE );
3465
Gilles Peskine8c9def32018-02-08 10:02:12 +01003466 if( mac_size < operation->mac_size )
3467 return( PSA_ERROR_BUFFER_TOO_SMALL );
3468
Gilles Peskine8c9def32018-02-08 10:02:12 +01003469#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003470 if( operation->alg == PSA_ALG_CMAC )
3471 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003472 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3473 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3474 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003475 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003476 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003477 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003478 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003479 else
3480#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003481#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003482 if( PSA_ALG_IS_HMAC( operation->alg ) )
3483 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003484 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003485 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003486 }
3487 else
John Durkopd0321952020-10-29 21:37:36 -07003488#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003489 {
3490 /* This shouldn't happen if `operation` was initialized by
3491 * a setup function. */
3492 return( PSA_ERROR_BAD_STATE );
3493 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003494}
3495
Gilles Peskineacd4be32018-07-08 19:56:25 +02003496psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3497 uint8_t *mac,
3498 size_t mac_size,
3499 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003500{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003501 psa_status_t status;
3502
Jaeden Amero252ef282019-02-15 14:05:35 +00003503 if( operation->alg == 0 )
3504 {
3505 return( PSA_ERROR_BAD_STATE );
3506 }
3507
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003508 /* Fill the output buffer with something that isn't a valid mac
3509 * (barring an attack on the mac and deliberately-crafted input),
3510 * in case the caller doesn't check the return status properly. */
3511 *mac_length = mac_size;
3512 /* If mac_size is 0 then mac may be NULL and then the
3513 * call to memset would have undefined behavior. */
3514 if( mac_size != 0 )
3515 memset( mac, '!', mac_size );
3516
Gilles Peskine89167cb2018-07-08 20:12:23 +02003517 if( ! operation->is_sign )
3518 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003519 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003520 }
mohammad16036df908f2018-04-02 08:34:15 -07003521
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003522 status = psa_mac_finish_internal( operation, mac, mac_size );
3523
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003524 if( status == PSA_SUCCESS )
3525 {
3526 status = psa_mac_abort( operation );
3527 if( status == PSA_SUCCESS )
3528 *mac_length = operation->mac_size;
3529 else
3530 memset( mac, '!', mac_size );
3531 }
3532 else
3533 psa_mac_abort( operation );
3534 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003535}
3536
Gilles Peskineacd4be32018-07-08 19:56:25 +02003537psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3538 const uint8_t *mac,
3539 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003540{
Gilles Peskine828ed142018-06-18 23:25:51 +02003541 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003542 psa_status_t status;
3543
Jaeden Amero252ef282019-02-15 14:05:35 +00003544 if( operation->alg == 0 )
3545 {
3546 return( PSA_ERROR_BAD_STATE );
3547 }
3548
Gilles Peskine89167cb2018-07-08 20:12:23 +02003549 if( operation->is_sign )
3550 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003551 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003552 }
3553 if( operation->mac_size != mac_length )
3554 {
3555 status = PSA_ERROR_INVALID_SIGNATURE;
3556 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003557 }
mohammad16036df908f2018-04-02 08:34:15 -07003558
3559 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003560 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003561 if( status != PSA_SUCCESS )
3562 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003563
3564 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3565 status = PSA_ERROR_INVALID_SIGNATURE;
3566
3567cleanup:
3568 if( status == PSA_SUCCESS )
3569 status = psa_mac_abort( operation );
3570 else
3571 psa_mac_abort( operation );
3572
Gilles Peskine3f108122018-12-07 18:14:53 +01003573 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003574
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003575 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003576}
3577
3578
Gilles Peskine20035e32018-02-03 22:44:14 +01003579
Gilles Peskine20035e32018-02-03 22:44:14 +01003580/****************************************************************/
3581/* Asymmetric cryptography */
3582/****************************************************************/
3583
John Durkop6ba40d12020-11-10 08:50:04 -08003584#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3585 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003586/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003587 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003588static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3589 size_t hash_length,
3590 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003591{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003592 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003593 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003594 *md_alg = mbedtls_md_get_type( md_info );
3595
3596 /* The Mbed TLS RSA module uses an unsigned int for hash length
3597 * parameters. Validate that it fits so that we don't risk an
3598 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003599#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003600 if( hash_length > UINT_MAX )
3601 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003602#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003603
John Durkop0e005192020-10-31 22:06:54 -07003604#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003605 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3606 * must be correct. */
3607 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3608 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003609 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003610 if( md_info == NULL )
3611 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003612 if( mbedtls_md_get_size( md_info ) != hash_length )
3613 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003614 }
John Durkop0e005192020-10-31 22:06:54 -07003615#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003616
John Durkop0e005192020-10-31 22:06:54 -07003617#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003618 /* PSS requires a hash internally. */
3619 if( PSA_ALG_IS_RSA_PSS( alg ) )
3620 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003621 if( md_info == NULL )
3622 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003623 }
John Durkop0e005192020-10-31 22:06:54 -07003624#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003625
Gilles Peskine61b91d42018-06-08 16:09:36 +02003626 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003627}
3628
Gilles Peskine2b450e32018-06-27 15:42:46 +02003629static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3630 psa_algorithm_t alg,
3631 const uint8_t *hash,
3632 size_t hash_length,
3633 uint8_t *signature,
3634 size_t signature_size,
3635 size_t *signature_length )
3636{
3637 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003639 mbedtls_md_type_t md_alg;
3640
3641 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3642 if( status != PSA_SUCCESS )
3643 return( status );
3644
Gilles Peskine630a18a2018-06-29 17:49:35 +02003645 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003646 return( PSA_ERROR_BUFFER_TOO_SMALL );
3647
John Durkop0e005192020-10-31 22:06:54 -07003648#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003649 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3650 {
3651 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3652 MBEDTLS_MD_NONE );
3653 ret = mbedtls_rsa_pkcs1_sign( rsa,
3654 mbedtls_ctr_drbg_random,
3655 &global_data.ctr_drbg,
3656 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003657 md_alg,
3658 (unsigned int) hash_length,
3659 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003660 signature );
3661 }
3662 else
John Durkop0e005192020-10-31 22:06:54 -07003663#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3664#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003665 if( PSA_ALG_IS_RSA_PSS( alg ) )
3666 {
3667 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3668 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3669 mbedtls_ctr_drbg_random,
3670 &global_data.ctr_drbg,
3671 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003672 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003673 (unsigned int) hash_length,
3674 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003675 signature );
3676 }
3677 else
John Durkop0e005192020-10-31 22:06:54 -07003678#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003679 {
3680 return( PSA_ERROR_INVALID_ARGUMENT );
3681 }
3682
3683 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003684 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003685 return( mbedtls_to_psa_error( ret ) );
3686}
3687
3688static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3689 psa_algorithm_t alg,
3690 const uint8_t *hash,
3691 size_t hash_length,
3692 const uint8_t *signature,
3693 size_t signature_length )
3694{
3695 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003697 mbedtls_md_type_t md_alg;
3698
3699 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3700 if( status != PSA_SUCCESS )
3701 return( status );
3702
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003703 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3704 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003705
John Durkop0e005192020-10-31 22:06:54 -07003706#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003707 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3708 {
3709 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3710 MBEDTLS_MD_NONE );
3711 ret = mbedtls_rsa_pkcs1_verify( rsa,
3712 mbedtls_ctr_drbg_random,
3713 &global_data.ctr_drbg,
3714 MBEDTLS_RSA_PUBLIC,
3715 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003716 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003717 hash,
3718 signature );
3719 }
3720 else
John Durkop0e005192020-10-31 22:06:54 -07003721#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3722#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003723 if( PSA_ALG_IS_RSA_PSS( alg ) )
3724 {
3725 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3726 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3727 mbedtls_ctr_drbg_random,
3728 &global_data.ctr_drbg,
3729 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003730 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003731 (unsigned int) hash_length,
3732 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003733 signature );
3734 }
3735 else
John Durkop0e005192020-10-31 22:06:54 -07003736#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003737 {
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003740
3741 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3742 * the rest of the signature is invalid". This has little use in
3743 * practice and PSA doesn't report this distinction. */
3744 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3745 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003746 return( mbedtls_to_psa_error( ret ) );
3747}
John Durkop6ba40d12020-11-10 08:50:04 -08003748#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3749 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003750
John Durkop6ba40d12020-11-10 08:50:04 -08003751#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3752 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003753/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3754 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3755 * (even though these functions don't modify it). */
3756static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3757 psa_algorithm_t alg,
3758 const uint8_t *hash,
3759 size_t hash_length,
3760 uint8_t *signature,
3761 size_t signature_size,
3762 size_t *signature_length )
3763{
Janos Follath24eed8d2019-11-22 13:21:35 +00003764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003765 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003766 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003767 mbedtls_mpi_init( &r );
3768 mbedtls_mpi_init( &s );
3769
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003770 if( signature_size < 2 * curve_bytes )
3771 {
3772 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3773 goto cleanup;
3774 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003775
John Durkop0ea39e02020-10-13 19:58:20 -07003776#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003777 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3778 {
3779 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3780 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3781 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003782 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3783 &ecp->d, hash,
3784 hash_length, md_alg,
3785 mbedtls_ctr_drbg_random,
3786 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003787 }
3788 else
John Durkop0ea39e02020-10-13 19:58:20 -07003789#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003790 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003791 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003792 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3793 hash, hash_length,
3794 mbedtls_ctr_drbg_random,
3795 &global_data.ctr_drbg ) );
3796 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003797
3798 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3799 signature,
3800 curve_bytes ) );
3801 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3802 signature + curve_bytes,
3803 curve_bytes ) );
3804
3805cleanup:
3806 mbedtls_mpi_free( &r );
3807 mbedtls_mpi_free( &s );
3808 if( ret == 0 )
3809 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003810 return( mbedtls_to_psa_error( ret ) );
3811}
3812
3813static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3814 const uint8_t *hash,
3815 size_t hash_length,
3816 const uint8_t *signature,
3817 size_t signature_length )
3818{
Janos Follath24eed8d2019-11-22 13:21:35 +00003819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003820 mbedtls_mpi r, s;
3821 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3822 mbedtls_mpi_init( &r );
3823 mbedtls_mpi_init( &s );
3824
3825 if( signature_length != 2 * curve_bytes )
3826 return( PSA_ERROR_INVALID_SIGNATURE );
3827
3828 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3829 signature,
3830 curve_bytes ) );
3831 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3832 signature + curve_bytes,
3833 curve_bytes ) );
3834
Steven Cooremanacda8342020-07-24 23:09:52 +02003835 /* Check whether the public part is loaded. If not, load it. */
3836 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3837 {
3838 MBEDTLS_MPI_CHK(
3839 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3840 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3841 }
3842
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003843 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3844 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003845
3846cleanup:
3847 mbedtls_mpi_free( &r );
3848 mbedtls_mpi_free( &s );
3849 return( mbedtls_to_psa_error( ret ) );
3850}
John Durkop6ba40d12020-11-10 08:50:04 -08003851#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3852 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003853
Ronald Croncf56a0a2020-08-04 09:51:30 +02003854psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003855 psa_algorithm_t alg,
3856 const uint8_t *hash,
3857 size_t hash_length,
3858 uint8_t *signature,
3859 size_t signature_size,
3860 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003861{
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 Peskinea26ff6a2018-06-28 12:21:19 +02003865
3866 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003867 /* Immediately reject a zero-length signature buffer. This guarantees
3868 * that signature must be a valid pointer. (On the other hand, the hash
3869 * buffer can in principle be empty since it doesn't actually have
3870 * to be a hash.) */
3871 if( signature_size == 0 )
3872 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003873
Ronald Cron5c522922020-11-14 16:35:34 +01003874 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3875 PSA_KEY_USAGE_SIGN_HASH,
3876 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003877 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003878 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003879 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003880 {
3881 status = PSA_ERROR_INVALID_ARGUMENT;
3882 goto exit;
3883 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003884
Steven Cooremancd84cb42020-07-16 20:28:36 +02003885 /* Try any of the available accelerators first */
3886 status = psa_driver_wrapper_sign_hash( slot,
3887 alg,
3888 hash,
3889 hash_length,
3890 signature,
3891 signature_size,
3892 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003893 if( status != PSA_ERROR_NOT_SUPPORTED ||
3894 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003895 goto exit;
3896
Steven Cooreman7a250572020-07-17 16:43:05 +02003897 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003898#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3899 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003900 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003901 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003902 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003903
Steven Cooreman4fed4552020-08-03 14:46:03 +02003904 status = psa_load_rsa_representation( slot->attr.type,
3905 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003906 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 &rsa );
3908 if( status != PSA_SUCCESS )
3909 goto exit;
3910
Steven Cooremana2371e52020-07-28 14:30:39 +02003911 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003912 alg,
3913 hash, hash_length,
3914 signature, signature_size,
3915 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003916
Steven Cooremana2371e52020-07-28 14:30:39 +02003917 mbedtls_rsa_free( rsa );
3918 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003919 }
3920 else
John Durkop6ba40d12020-11-10 08:50:04 -08003921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3922 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003923 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003924 {
John Durkop9814fa22020-11-04 12:28:15 -08003925#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3926 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003927 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003928#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003929 PSA_ALG_IS_ECDSA( alg )
3930#else
3931 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3932#endif
3933 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003934 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003935 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003936 status = psa_load_ecp_representation( slot->attr.type,
3937 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003938 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003939 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003940 if( status != PSA_SUCCESS )
3941 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003942 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003943 alg,
3944 hash, hash_length,
3945 signature, signature_size,
3946 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003947 mbedtls_ecp_keypair_free( ecp );
3948 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003949 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003950 else
John Durkop9814fa22020-11-04 12:28:15 -08003951#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3952 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003953 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003954 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003955 }
itayzafrir5c753392018-05-08 11:18:38 +03003956 }
3957 else
itayzafrir5c753392018-05-08 11:18:38 +03003958 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003959 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003960 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003961
3962exit:
3963 /* Fill the unused part of the output buffer (the whole buffer on error,
3964 * the trailing part on success) with something that isn't a valid mac
3965 * (barring an attack on the mac and deliberately-crafted input),
3966 * in case the caller doesn't check the return status properly. */
3967 if( status == PSA_SUCCESS )
3968 memset( signature + *signature_length, '!',
3969 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003970 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003971 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003972 /* If signature_size is 0 then we have nothing to do. We must not call
3973 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003974
Ronald Cron5c522922020-11-14 16:35:34 +01003975 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003976
Ronald Cron5c522922020-11-14 16:35:34 +01003977 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003978}
3979
Ronald Croncf56a0a2020-08-04 09:51:30 +02003980psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003981 psa_algorithm_t alg,
3982 const uint8_t *hash,
3983 size_t hash_length,
3984 const uint8_t *signature,
3985 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003986{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003988 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003989 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003990
Ronald Cron5c522922020-11-14 16:35:34 +01003991 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3992 PSA_KEY_USAGE_VERIFY_HASH,
3993 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003994 if( status != PSA_SUCCESS )
3995 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003996
Steven Cooreman55ae2172020-07-17 19:46:15 +02003997 /* Try any of the available accelerators first */
3998 status = psa_driver_wrapper_verify_hash( slot,
3999 alg,
4000 hash,
4001 hash_length,
4002 signature,
4003 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004004 if( status != PSA_ERROR_NOT_SUPPORTED ||
4005 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004006 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004007
John Durkop6ba40d12020-11-10 08:50:04 -08004008#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4009 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004010 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004012 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004013
Steven Cooreman4fed4552020-08-03 14:46:03 +02004014 status = psa_load_rsa_representation( slot->attr.type,
4015 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004016 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004017 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004018 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004019 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004020
Steven Cooremana2371e52020-07-28 14:30:39 +02004021 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004022 alg,
4023 hash, hash_length,
4024 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004025 mbedtls_rsa_free( rsa );
4026 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004027 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028 }
4029 else
John Durkop6ba40d12020-11-10 08:50:04 -08004030#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4031 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004032 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004033 {
John Durkop9814fa22020-11-04 12:28:15 -08004034#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4035 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004036 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004037 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004038 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004039 status = psa_load_ecp_representation( slot->attr.type,
4040 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004041 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004042 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004043 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004044 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004045 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004046 hash, hash_length,
4047 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004048 mbedtls_ecp_keypair_free( ecp );
4049 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004050 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004051 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004052 else
John Durkop9814fa22020-11-04 12:28:15 -08004053#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4054 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004055 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004056 status = PSA_ERROR_INVALID_ARGUMENT;
4057 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004058 }
itayzafrir5c753392018-05-08 11:18:38 +03004059 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004060 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004061 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004062 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004063 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004064
4065exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004066 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004067
Ronald Cron5c522922020-11-14 16:35:34 +01004068 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004069}
4070
John Durkop0e005192020-10-31 22:06:54 -07004071#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004072static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4073 mbedtls_rsa_context *rsa )
4074{
4075 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4076 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4077 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4078 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4079}
John Durkop0e005192020-10-31 22:06:54 -07004080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004081
Ronald Croncf56a0a2020-08-04 09:51:30 +02004082psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004083 psa_algorithm_t alg,
4084 const uint8_t *input,
4085 size_t input_length,
4086 const uint8_t *salt,
4087 size_t salt_length,
4088 uint8_t *output,
4089 size_t output_size,
4090 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004094 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004095
Darryl Green5cc689a2018-07-24 15:34:10 +01004096 (void) input;
4097 (void) input_length;
4098 (void) salt;
4099 (void) output;
4100 (void) output_size;
4101
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004102 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004104 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4105 return( PSA_ERROR_INVALID_ARGUMENT );
4106
Ronald Cron5c522922020-11-14 16:35:34 +01004107 status = psa_get_and_lock_transparent_key_slot_with_policy(
4108 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004109 if( status != PSA_SUCCESS )
4110 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004111 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4112 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004113 {
4114 status = PSA_ERROR_INVALID_ARGUMENT;
4115 goto exit;
4116 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117
John Durkop6ba40d12020-11-10 08:50:04 -08004118#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4119 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004120 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004122 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004123 status = psa_load_rsa_representation( slot->attr.type,
4124 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004125 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004126 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004127 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004128 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004129
4130 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004131 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004132 status = PSA_ERROR_BUFFER_TOO_SMALL;
4133 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004134 }
John Durkop0e005192020-10-31 22:06:54 -07004135#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004136 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004137 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004138 status = mbedtls_to_psa_error(
4139 mbedtls_rsa_pkcs1_encrypt( rsa,
4140 mbedtls_ctr_drbg_random,
4141 &global_data.ctr_drbg,
4142 MBEDTLS_RSA_PUBLIC,
4143 input_length,
4144 input,
4145 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146 }
4147 else
John Durkop0e005192020-10-31 22:06:54 -07004148#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4149#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004150 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004152 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004153 status = mbedtls_to_psa_error(
4154 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4155 mbedtls_ctr_drbg_random,
4156 &global_data.ctr_drbg,
4157 MBEDTLS_RSA_PUBLIC,
4158 salt, salt_length,
4159 input_length,
4160 input,
4161 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004162 }
4163 else
John Durkop0e005192020-10-31 22:06:54 -07004164#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004165 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004166 status = PSA_ERROR_INVALID_ARGUMENT;
4167 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004168 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004169rsa_exit:
4170 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004171 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004172
Steven Cooremana2371e52020-07-28 14:30:39 +02004173 mbedtls_rsa_free( rsa );
4174 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176 else
John Durkop9814fa22020-11-04 12:28:15 -08004177#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004178 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004180 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004182
4183exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004184 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004185
Ronald Cron5c522922020-11-14 16:35:34 +01004186 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004187}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188
Ronald Croncf56a0a2020-08-04 09:51:30 +02004189psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004190 psa_algorithm_t alg,
4191 const uint8_t *input,
4192 size_t input_length,
4193 const uint8_t *salt,
4194 size_t salt_length,
4195 uint8_t *output,
4196 size_t output_size,
4197 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004200 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004201 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004202
Darryl Green5cc689a2018-07-24 15:34:10 +01004203 (void) input;
4204 (void) input_length;
4205 (void) salt;
4206 (void) output;
4207 (void) output_size;
4208
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004209 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004210
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004211 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4212 return( PSA_ERROR_INVALID_ARGUMENT );
4213
Ronald Cron5c522922020-11-14 16:35:34 +01004214 status = psa_get_and_lock_transparent_key_slot_with_policy(
4215 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004216 if( status != PSA_SUCCESS )
4217 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004218 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004219 {
4220 status = PSA_ERROR_INVALID_ARGUMENT;
4221 goto exit;
4222 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223
John Durkop6ba40d12020-11-10 08:50:04 -08004224#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4225 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004226 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004227 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004228 mbedtls_rsa_context *rsa = NULL;
4229 status = psa_load_rsa_representation( slot->attr.type,
4230 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004231 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004232 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004233 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004234 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004235
Steven Cooremana2371e52020-07-28 14:30:39 +02004236 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004237 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004238 status = PSA_ERROR_INVALID_ARGUMENT;
4239 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004240 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241
John Durkop0e005192020-10-31 22:06:54 -07004242#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004243 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004245 status = mbedtls_to_psa_error(
4246 mbedtls_rsa_pkcs1_decrypt( rsa,
4247 mbedtls_ctr_drbg_random,
4248 &global_data.ctr_drbg,
4249 MBEDTLS_RSA_PRIVATE,
4250 output_length,
4251 input,
4252 output,
4253 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004254 }
4255 else
John Durkop0e005192020-10-31 22:06:54 -07004256#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4257#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004258 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004260 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004261 status = mbedtls_to_psa_error(
4262 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4263 mbedtls_ctr_drbg_random,
4264 &global_data.ctr_drbg,
4265 MBEDTLS_RSA_PRIVATE,
4266 salt, salt_length,
4267 output_length,
4268 input,
4269 output,
4270 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004271 }
4272 else
John Durkop0e005192020-10-31 22:06:54 -07004273#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004274 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004275 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004277
Steven Cooreman4fed4552020-08-03 14:46:03 +02004278rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004279 mbedtls_rsa_free( rsa );
4280 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004282 else
John Durkop6ba40d12020-11-10 08:50:04 -08004283#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4284 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004285 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004286 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004287 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004288
4289exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004290 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004291
Ronald Cron5c522922020-11-14 16:35:34 +01004292 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004293}
Gilles Peskine20035e32018-02-03 22:44:14 +01004294
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004295
4296
mohammad1603503973b2018-03-12 15:59:30 +02004297/****************************************************************/
4298/* Symmetric cryptography */
4299/****************************************************************/
4300
Gilles Peskinee553c652018-06-04 16:22:46 +02004301static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004302 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004303 psa_algorithm_t alg,
4304 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004305{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004307 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004308 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004309 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004310 size_t key_bits;
4311 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004312 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4313 PSA_KEY_USAGE_ENCRYPT :
4314 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004315
Steven Cooremand3feccd2020-09-01 15:56:14 +02004316 /* A context must be freshly initialized before it can be set up. */
4317 if( operation->alg != 0 )
4318 return( PSA_ERROR_BAD_STATE );
4319
Steven Cooremana07b9972020-09-10 14:54:14 +02004320 /* The requested algorithm must be one that can be processed by cipher. */
4321 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004322 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004323
Steven Cooremanef8575e2020-09-11 11:44:50 +02004324 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004325 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004326 if( status != PSA_SUCCESS )
4327 goto exit;
4328
4329 /* Initialize the operation struct members, except for alg. The alg member
4330 * is used to indicate to psa_cipher_abort that there are resources to free,
4331 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004332 operation->key_set = 0;
4333 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004334 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004335 operation->iv_size = 0;
4336 operation->block_size = 0;
4337 if( alg == PSA_ALG_ECB_NO_PADDING )
4338 operation->iv_required = 0;
4339 else
4340 operation->iv_required = 1;
4341
Steven Cooremana07b9972020-09-10 14:54:14 +02004342 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004343 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004344 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004345 slot,
4346 alg );
4347 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004348 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004349 slot,
4350 alg );
4351
Steven Cooremanef8575e2020-09-11 11:44:50 +02004352 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004353 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004354 /* Once the driver context is initialised, it needs to be freed using
4355 * psa_cipher_abort. Indicate this through setting alg. */
4356 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004357 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004358
Steven Cooremand3feccd2020-09-01 15:56:14 +02004359 if( status != PSA_ERROR_NOT_SUPPORTED ||
4360 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4361 goto exit;
4362
Steven Cooremana07b9972020-09-10 14:54:14 +02004363 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004364 * available for the given algorithm & key. */
4365 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004366
Steven Cooremana07b9972020-09-10 14:54:14 +02004367 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004368 * psa_cipher_abort. Indicate there is something to be freed through setting
4369 * alg, and indicate the operation is being done using mbedtls crypto through
4370 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004371 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004372 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004373
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004374 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004375 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004376 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004377 {
4378 status = PSA_ERROR_NOT_SUPPORTED;
4379 goto exit;
4380 }
mohammad1603503973b2018-03-12 15:59:30 +02004381
mohammad1603503973b2018-03-12 15:59:30 +02004382 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004383 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004384 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004385
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004386#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004387 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004388 {
4389 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004390 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004391 memcpy( keys, slot->data.key.data, 16 );
4392 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004393 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4394 keys,
4395 192, cipher_operation );
4396 }
4397 else
4398#endif
4399 {
4400 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004401 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004402 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004403 }
Moran Peker41deec42018-04-04 15:43:05 +03004404 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004405 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004406
mohammad16038481e742018-03-18 13:57:31 +02004407#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004408 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004409 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004410 case PSA_ALG_CBC_NO_PADDING:
4411 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4412 MBEDTLS_PADDING_NONE );
4413 break;
4414 case PSA_ALG_CBC_PKCS7:
4415 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4416 MBEDTLS_PADDING_PKCS7 );
4417 break;
4418 default:
4419 /* The algorithm doesn't involve padding. */
4420 ret = 0;
4421 break;
4422 }
4423 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004424 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004425#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4426
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004427 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004428 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004429 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4430 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004431 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004432 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004433 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004434#if defined(MBEDTLS_CHACHA20_C)
4435 else
4436 if( alg == PSA_ALG_CHACHA20 )
4437 operation->iv_size = 12;
4438#endif
mohammad1603503973b2018-03-12 15:59:30 +02004439
Steven Cooreman7df02922020-09-09 15:28:49 +02004440 status = PSA_SUCCESS;
4441
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004442exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004443 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004444 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004445 if( status == PSA_SUCCESS )
4446 {
4447 /* Update operation flags for both driver and software implementations */
4448 operation->key_set = 1;
4449 }
4450 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004451 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004452
Ronald Cron5c522922020-11-14 16:35:34 +01004453 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004454
Ronald Cron5c522922020-11-14 16:35:34 +01004455 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004456}
4457
Gilles Peskinefe119512018-07-08 21:39:34 +02004458psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004459 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004460 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004461{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004462 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004463}
4464
Gilles Peskinefe119512018-07-08 21:39:34 +02004465psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004466 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004467 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004468{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004469 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004470}
4471
Gilles Peskinefe119512018-07-08 21:39:34 +02004472psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004473 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004474 size_t iv_size,
4475 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004476{
itayzafrir534bd7c2018-08-02 13:56:32 +03004477 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004479 if( operation->iv_set || ! operation->iv_required )
4480 {
4481 return( PSA_ERROR_BAD_STATE );
4482 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004483
Steven Cooremanef8575e2020-09-11 11:44:50 +02004484 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004485 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004486 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004487 iv,
4488 iv_size,
4489 iv_length );
4490 goto exit;
4491 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004492
Moran Peker41deec42018-04-04 15:43:05 +03004493 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004494 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004495 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004496 goto exit;
4497 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004498 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4499 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004500 if( ret != 0 )
4501 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004502 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004503 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004504 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004505
mohammad16038481e742018-03-18 13:57:31 +02004506 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004507 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004508
Moran Peker395db872018-05-31 14:07:14 +03004509exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004510 if( status == PSA_SUCCESS )
4511 operation->iv_set = 1;
4512 else
Moran Peker395db872018-05-31 14:07:14 +03004513 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004514 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004515}
4516
Gilles Peskinefe119512018-07-08 21:39:34 +02004517psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004518 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004519 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004520{
itayzafrir534bd7c2018-08-02 13:56:32 +03004521 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004523 if( operation->iv_set || ! operation->iv_required )
4524 {
4525 return( PSA_ERROR_BAD_STATE );
4526 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004527
Steven Cooremanef8575e2020-09-11 11:44:50 +02004528 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004529 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004530 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004531 iv,
4532 iv_length );
4533 goto exit;
4534 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004535
Moran Pekera28258c2018-05-29 16:25:04 +03004536 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004537 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004538 status = PSA_ERROR_INVALID_ARGUMENT;
4539 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004540 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004541 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4542 status = mbedtls_to_psa_error( ret );
4543exit:
4544 if( status == PSA_SUCCESS )
4545 operation->iv_set = 1;
4546 else
mohammad1603503973b2018-03-12 15:59:30 +02004547 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004548 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004549}
4550
Steven Cooreman177deba2020-09-07 17:14:14 +02004551/* Process input for which the algorithm is set to ECB mode. This requires
4552 * manual processing, since the PSA API is defined as being able to process
4553 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4554 * underlying mbedtls_cipher_update only takes full blocks. */
4555static psa_status_t psa_cipher_update_ecb_internal(
4556 mbedtls_cipher_context_t *ctx,
4557 const uint8_t *input,
4558 size_t input_length,
4559 uint8_t *output,
4560 size_t output_size,
4561 size_t *output_length )
4562{
4563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4564 size_t block_size = ctx->cipher_info->block_size;
4565 size_t internal_output_length = 0;
4566 *output_length = 0;
4567
4568 if( input_length == 0 )
4569 {
4570 status = PSA_SUCCESS;
4571 goto exit;
4572 }
4573
4574 if( ctx->unprocessed_len > 0 )
4575 {
4576 /* Fill up to block size, and run the block if there's a full one. */
4577 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4578
4579 if( input_length < bytes_to_copy )
4580 bytes_to_copy = input_length;
4581
4582 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4583 input, bytes_to_copy );
4584 input_length -= bytes_to_copy;
4585 input += bytes_to_copy;
4586 ctx->unprocessed_len += bytes_to_copy;
4587
4588 if( ctx->unprocessed_len == block_size )
4589 {
4590 status = mbedtls_to_psa_error(
4591 mbedtls_cipher_update( ctx,
4592 ctx->unprocessed_data,
4593 block_size,
4594 output, &internal_output_length ) );
4595
4596 if( status != PSA_SUCCESS )
4597 goto exit;
4598
4599 output += internal_output_length;
4600 output_size -= internal_output_length;
4601 *output_length += internal_output_length;
4602 ctx->unprocessed_len = 0;
4603 }
4604 }
4605
4606 while( input_length >= block_size )
4607 {
4608 /* Run all full blocks we have, one by one */
4609 status = mbedtls_to_psa_error(
4610 mbedtls_cipher_update( ctx, input,
4611 block_size,
4612 output, &internal_output_length ) );
4613
4614 if( status != PSA_SUCCESS )
4615 goto exit;
4616
4617 input_length -= block_size;
4618 input += block_size;
4619
4620 output += internal_output_length;
4621 output_size -= internal_output_length;
4622 *output_length += internal_output_length;
4623 }
4624
4625 if( input_length > 0 )
4626 {
4627 /* Save unprocessed bytes for later processing */
4628 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4629 input, input_length );
4630 ctx->unprocessed_len += input_length;
4631 }
4632
4633 status = PSA_SUCCESS;
4634
4635exit:
4636 return( status );
4637}
4638
Gilles Peskinee553c652018-06-04 16:22:46 +02004639psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4640 const uint8_t *input,
4641 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004642 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004643 size_t output_size,
4644 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004645{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004647 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004648 if( operation->alg == 0 )
4649 {
4650 return( PSA_ERROR_BAD_STATE );
4651 }
4652 if( operation->iv_required && ! operation->iv_set )
4653 {
4654 return( PSA_ERROR_BAD_STATE );
4655 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004656
Steven Cooremanef8575e2020-09-11 11:44:50 +02004657 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004658 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004659 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004660 input,
4661 input_length,
4662 output,
4663 output_size,
4664 output_length );
4665 goto exit;
4666 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004667
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004668 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004669 {
4670 /* Take the unprocessed partial block left over from previous
4671 * update calls, if any, plus the input to this call. Remove
4672 * the last partial block, if any. You get the data that will be
4673 * output in this call. */
4674 expected_output_size =
4675 ( operation->ctx.cipher.unprocessed_len + input_length )
4676 / operation->block_size * operation->block_size;
4677 }
4678 else
4679 {
4680 expected_output_size = input_length;
4681 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004682
Gilles Peskine89d789c2018-06-04 17:17:16 +02004683 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004684 {
4685 status = PSA_ERROR_BUFFER_TOO_SMALL;
4686 goto exit;
4687 }
mohammad160382759612018-03-12 18:16:40 +02004688
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004689 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4690 {
4691 /* mbedtls_cipher_update has an API inconsistency: it will only
4692 * process a single block at a time in ECB mode. Abstract away that
4693 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004694 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4695 input,
4696 input_length,
4697 output,
4698 output_size,
4699 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004700 }
4701 else
4702 {
4703 status = mbedtls_to_psa_error(
4704 mbedtls_cipher_update( &operation->ctx.cipher, input,
4705 input_length, output, output_length ) );
4706 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004707exit:
4708 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004709 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004710 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004711}
4712
Gilles Peskinee553c652018-06-04 16:22:46 +02004713psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4714 uint8_t *output,
4715 size_t output_size,
4716 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004717{
David Saadab4ecc272019-02-14 13:48:10 +02004718 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004719 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004720 if( operation->alg == 0 )
4721 {
4722 return( PSA_ERROR_BAD_STATE );
4723 }
4724 if( operation->iv_required && ! operation->iv_set )
4725 {
4726 return( PSA_ERROR_BAD_STATE );
4727 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004728
Steven Cooremanef8575e2020-09-11 11:44:50 +02004729 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004730 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004731 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004732 output,
4733 output_size,
4734 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004735 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004736 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004737
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004738 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004739 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004740 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004741 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004742 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004743 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004744 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004745 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004746 }
4747
Steven Cooremanef8575e2020-09-11 11:44:50 +02004748 status = mbedtls_to_psa_error(
4749 mbedtls_cipher_finish( &operation->ctx.cipher,
4750 temp_output_buffer,
4751 output_length ) );
4752 if( status != PSA_SUCCESS )
4753 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004754
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004755 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004756 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004757 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004758 memcpy( output, temp_output_buffer, *output_length );
4759 else
Janos Follath315b51c2018-07-09 16:04:51 +01004760 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004761
Steven Cooremanef8575e2020-09-11 11:44:50 +02004762exit:
4763 if( operation->mbedtls_in_use == 1 )
4764 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004765
Steven Cooremanef8575e2020-09-11 11:44:50 +02004766 if( status == PSA_SUCCESS )
4767 return( psa_cipher_abort( operation ) );
4768 else
4769 {
4770 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004771 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004772
Steven Cooremanef8575e2020-09-11 11:44:50 +02004773 return( status );
4774 }
mohammad1603503973b2018-03-12 15:59:30 +02004775}
4776
Gilles Peskinee553c652018-06-04 16:22:46 +02004777psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4778{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004779 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004780 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004781 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004782 * in use. It's ok to call abort on such an object, and there's
4783 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004784 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004785 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004786
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004787 /* Sanity check (shouldn't happen: operation->alg should
4788 * always have been initialized to a valid value). */
4789 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4790 return( PSA_ERROR_BAD_STATE );
4791
Steven Cooremanef8575e2020-09-11 11:44:50 +02004792 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004793 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004794 else
4795 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004796
Moran Peker41deec42018-04-04 15:43:05 +03004797 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004798 operation->key_set = 0;
4799 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004800 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004801 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004802 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004803 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004804
Moran Peker395db872018-05-31 14:07:14 +03004805 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004806}
4807
Gilles Peskinea0655c32018-04-30 17:06:50 +02004808
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004809
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004810
mohammad16035955c982018-04-26 00:53:03 +03004811/****************************************************************/
4812/* AEAD */
4813/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004814
Gilles Peskineedf9a652018-08-17 18:11:56 +02004815typedef struct
4816{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004817 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004818 const mbedtls_cipher_info_t *cipher_info;
4819 union
4820 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004821 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004822#if defined(MBEDTLS_CCM_C)
4823 mbedtls_ccm_context ccm;
4824#endif /* MBEDTLS_CCM_C */
4825#if defined(MBEDTLS_GCM_C)
4826 mbedtls_gcm_context gcm;
4827#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004828#if defined(MBEDTLS_CHACHAPOLY_C)
4829 mbedtls_chachapoly_context chachapoly;
4830#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004831 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004832 psa_algorithm_t core_alg;
4833 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004834 uint8_t tag_length;
4835} aead_operation_t;
4836
Ronald Cronf95a2b12020-10-22 15:24:49 +02004837#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4838
Gilles Peskine30a9e412019-01-14 18:36:12 +01004839static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004840{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004841 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004842 {
4843#if defined(MBEDTLS_CCM_C)
4844 case PSA_ALG_CCM:
4845 mbedtls_ccm_free( &operation->ctx.ccm );
4846 break;
4847#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004848#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004849 case PSA_ALG_GCM:
4850 mbedtls_gcm_free( &operation->ctx.gcm );
4851 break;
4852#endif /* MBEDTLS_GCM_C */
4853 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004854
Ronald Cron5c522922020-11-14 16:35:34 +01004855 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004856}
4857
4858static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004859 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004860 psa_key_usage_t usage,
4861 psa_algorithm_t alg )
4862{
4863 psa_status_t status;
4864 size_t key_bits;
4865 mbedtls_cipher_id_t cipher_id;
4866
Ronald Cron5c522922020-11-14 16:35:34 +01004867 status = psa_get_and_lock_transparent_key_slot_with_policy(
4868 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004869 if( status != PSA_SUCCESS )
4870 return( status );
4871
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004872 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004873
4874 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004875 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876 &cipher_id );
4877 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004878 {
4879 status = PSA_ERROR_NOT_SUPPORTED;
4880 goto cleanup;
4881 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004882
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004883 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884 {
4885#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004886 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4887 operation->core_alg = PSA_ALG_CCM;
4888 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004889 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4890 * The call to mbedtls_ccm_encrypt_and_tag or
4891 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004892 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004893 {
4894 status = PSA_ERROR_INVALID_ARGUMENT;
4895 goto cleanup;
4896 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897 mbedtls_ccm_init( &operation->ctx.ccm );
4898 status = mbedtls_to_psa_error(
4899 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004900 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004901 (unsigned int) key_bits ) );
4902 if( status != 0 )
4903 goto cleanup;
4904 break;
4905#endif /* MBEDTLS_CCM_C */
4906
4907#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004908 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4909 operation->core_alg = PSA_ALG_GCM;
4910 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004911 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4912 * The call to mbedtls_gcm_crypt_and_tag or
4913 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004914 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004915 {
4916 status = PSA_ERROR_INVALID_ARGUMENT;
4917 goto cleanup;
4918 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004919 mbedtls_gcm_init( &operation->ctx.gcm );
4920 status = mbedtls_to_psa_error(
4921 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004922 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004923 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004924 if( status != 0 )
4925 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004926 break;
4927#endif /* MBEDTLS_GCM_C */
4928
Gilles Peskine26869f22019-05-06 15:25:00 +02004929#if defined(MBEDTLS_CHACHAPOLY_C)
4930 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4931 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4932 operation->full_tag_length = 16;
4933 /* We only support the default tag length. */
4934 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004935 {
4936 status = PSA_ERROR_NOT_SUPPORTED;
4937 goto cleanup;
4938 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004939 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4940 status = mbedtls_to_psa_error(
4941 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004942 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004943 if( status != 0 )
4944 goto cleanup;
4945 break;
4946#endif /* MBEDTLS_CHACHAPOLY_C */
4947
Gilles Peskineedf9a652018-08-17 18:11:56 +02004948 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004949 status = PSA_ERROR_NOT_SUPPORTED;
4950 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004951 }
4952
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004953 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4954 {
4955 status = PSA_ERROR_INVALID_ARGUMENT;
4956 goto cleanup;
4957 }
4958 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004959
Gilles Peskineedf9a652018-08-17 18:11:56 +02004960 return( PSA_SUCCESS );
4961
4962cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004963 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004964 return( status );
4965}
4966
Ronald Croncf56a0a2020-08-04 09:51:30 +02004967psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004968 psa_algorithm_t alg,
4969 const uint8_t *nonce,
4970 size_t nonce_length,
4971 const uint8_t *additional_data,
4972 size_t additional_data_length,
4973 const uint8_t *plaintext,
4974 size_t plaintext_length,
4975 uint8_t *ciphertext,
4976 size_t ciphertext_size,
4977 size_t *ciphertext_length )
4978{
mohammad16035955c982018-04-26 00:53:03 +03004979 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004980 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004981 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004982
mohammad1603f08a5502018-06-03 15:05:47 +03004983 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004984
Ronald Croncf56a0a2020-08-04 09:51:30 +02004985 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004986 if( status != PSA_SUCCESS )
4987 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004988
Gilles Peskineedf9a652018-08-17 18:11:56 +02004989 /* For all currently supported modes, the tag is at the end of the
4990 * ciphertext. */
4991 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4992 {
4993 status = PSA_ERROR_BUFFER_TOO_SMALL;
4994 goto exit;
4995 }
4996 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004997
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004998#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004999 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005000 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005001 status = mbedtls_to_psa_error(
5002 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5003 MBEDTLS_GCM_ENCRYPT,
5004 plaintext_length,
5005 nonce, nonce_length,
5006 additional_data, additional_data_length,
5007 plaintext, ciphertext,
5008 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005009 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005010 else
5011#endif /* MBEDTLS_GCM_C */
5012#if defined(MBEDTLS_CCM_C)
5013 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005014 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005015 status = mbedtls_to_psa_error(
5016 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5017 plaintext_length,
5018 nonce, nonce_length,
5019 additional_data,
5020 additional_data_length,
5021 plaintext, ciphertext,
5022 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005023 }
mohammad16035c8845f2018-05-09 05:40:09 -07005024 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005025#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005026#if defined(MBEDTLS_CHACHAPOLY_C)
5027 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5028 {
5029 if( nonce_length != 12 || operation.tag_length != 16 )
5030 {
5031 status = PSA_ERROR_NOT_SUPPORTED;
5032 goto exit;
5033 }
5034 status = mbedtls_to_psa_error(
5035 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5036 plaintext_length,
5037 nonce,
5038 additional_data,
5039 additional_data_length,
5040 plaintext,
5041 ciphertext,
5042 tag ) );
5043 }
5044 else
5045#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005046 {
mohammad1603554faad2018-06-03 15:07:38 +03005047 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005048 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005049
Gilles Peskineedf9a652018-08-17 18:11:56 +02005050 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5051 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005052
Gilles Peskineedf9a652018-08-17 18:11:56 +02005053exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005054 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005055 if( status == PSA_SUCCESS )
5056 *ciphertext_length = plaintext_length + operation.tag_length;
5057 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005058}
5059
Gilles Peskineee652a32018-06-01 19:23:52 +02005060/* Locate the tag in a ciphertext buffer containing the encrypted data
5061 * followed by the tag. Return the length of the part preceding the tag in
5062 * *plaintext_length. This is the size of the plaintext in modes where
5063 * the encrypted data has the same size as the plaintext, such as
5064 * CCM and GCM. */
5065static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5066 const uint8_t *ciphertext,
5067 size_t ciphertext_length,
5068 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005069 const uint8_t **p_tag )
5070{
5071 size_t payload_length;
5072 if( tag_length > ciphertext_length )
5073 return( PSA_ERROR_INVALID_ARGUMENT );
5074 payload_length = ciphertext_length - tag_length;
5075 if( payload_length > plaintext_size )
5076 return( PSA_ERROR_BUFFER_TOO_SMALL );
5077 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005078 return( PSA_SUCCESS );
5079}
5080
Ronald Croncf56a0a2020-08-04 09:51:30 +02005081psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005082 psa_algorithm_t alg,
5083 const uint8_t *nonce,
5084 size_t nonce_length,
5085 const uint8_t *additional_data,
5086 size_t additional_data_length,
5087 const uint8_t *ciphertext,
5088 size_t ciphertext_length,
5089 uint8_t *plaintext,
5090 size_t plaintext_size,
5091 size_t *plaintext_length )
5092{
mohammad16035955c982018-04-26 00:53:03 +03005093 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005094 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005095 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005096
Gilles Peskineee652a32018-06-01 19:23:52 +02005097 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005098
Ronald Croncf56a0a2020-08-04 09:51:30 +02005099 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005100 if( status != PSA_SUCCESS )
5101 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005102
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005103 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5104 ciphertext, ciphertext_length,
5105 plaintext_size, &tag );
5106 if( status != PSA_SUCCESS )
5107 goto exit;
5108
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005109#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005110 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005111 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005112 status = mbedtls_to_psa_error(
5113 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5114 ciphertext_length - operation.tag_length,
5115 nonce, nonce_length,
5116 additional_data,
5117 additional_data_length,
5118 tag, operation.tag_length,
5119 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005120 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005121 else
5122#endif /* MBEDTLS_GCM_C */
5123#if defined(MBEDTLS_CCM_C)
5124 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005125 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005126 status = mbedtls_to_psa_error(
5127 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5128 ciphertext_length - operation.tag_length,
5129 nonce, nonce_length,
5130 additional_data,
5131 additional_data_length,
5132 ciphertext, plaintext,
5133 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005134 }
mohammad160339574652018-06-01 04:39:53 -07005135 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005136#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005137#if defined(MBEDTLS_CHACHAPOLY_C)
5138 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5139 {
5140 if( nonce_length != 12 || operation.tag_length != 16 )
5141 {
5142 status = PSA_ERROR_NOT_SUPPORTED;
5143 goto exit;
5144 }
5145 status = mbedtls_to_psa_error(
5146 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5147 ciphertext_length - operation.tag_length,
5148 nonce,
5149 additional_data,
5150 additional_data_length,
5151 tag,
5152 ciphertext,
5153 plaintext ) );
5154 }
5155 else
5156#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005157 {
mohammad1603554faad2018-06-03 15:07:38 +03005158 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005159 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005160
Gilles Peskineedf9a652018-08-17 18:11:56 +02005161 if( status != PSA_SUCCESS && plaintext_size != 0 )
5162 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005163
Gilles Peskineedf9a652018-08-17 18:11:56 +02005164exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005165 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005166 if( status == PSA_SUCCESS )
5167 *plaintext_length = ciphertext_length - operation.tag_length;
5168 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005169}
5170
Gilles Peskinea0655c32018-04-30 17:06:50 +02005171
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005172
Gilles Peskine20035e32018-02-03 22:44:14 +01005173/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005174/* Generators */
5175/****************************************************************/
5176
John Durkop07cc04a2020-11-16 22:08:34 -08005177#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5178 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5179 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5180#define AT_LEAST_ONE_BUILTIN_KDF
5181#endif
5182
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183#define HKDF_STATE_INIT 0 /* no input yet */
5184#define HKDF_STATE_STARTED 1 /* got salt */
5185#define HKDF_STATE_KEYED 2 /* got key */
5186#define HKDF_STATE_OUTPUT 3 /* output started */
5187
Gilles Peskinecbe66502019-05-16 16:59:18 +02005188static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005189 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005190{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5192 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005193 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005195}
5196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005198{
5199 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005201 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005202 {
5203 /* The object has (apparently) been initialized but it is not
5204 * in use. It's ok to call abort on such an object, and there's
5205 * nothing to do. */
5206 }
5207 else
John Durkopd0321952020-10-29 21:37:36 -07005208#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005209 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005210 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 mbedtls_free( operation->ctx.hkdf.info );
5212 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005213 }
John Durkop07cc04a2020-11-16 22:08:34 -08005214 else
5215#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5216#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5217 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5218 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005221 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005222 if( operation->ctx.tls12_prf.seed != NULL )
5223 {
5224 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5225 operation->ctx.tls12_prf.seed_length );
5226 mbedtls_free( operation->ctx.tls12_prf.seed );
5227 }
5228
5229 if( operation->ctx.tls12_prf.label != NULL )
5230 {
5231 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5232 operation->ctx.tls12_prf.label_length );
5233 mbedtls_free( operation->ctx.tls12_prf.label );
5234 }
5235
5236 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5237
5238 /* We leave the fields Ai and output_block to be erased safely by the
5239 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005240 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005241 else
John Durkop07cc04a2020-11-16 22:08:34 -08005242#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5243 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005244 {
5245 status = PSA_ERROR_BAD_STATE;
5246 }
Janos Follath083036a2019-06-11 10:22:26 +01005247 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005248 return( status );
5249}
5250
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005252 size_t *capacity)
5253{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005255 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005257 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005258 }
5259
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005261 return( PSA_SUCCESS );
5262}
5263
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005265 size_t capacity )
5266{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005268 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005270 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272 return( PSA_SUCCESS );
5273}
5274
John Durkopd0321952020-10-29 21:37:36 -07005275#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005277 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005278static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005279 psa_algorithm_t hash_alg,
5280 uint8_t *output,
5281 size_t output_length )
5282{
5283 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5284 psa_status_t status;
5285
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5287 return( PSA_ERROR_BAD_STATE );
5288 hkdf->state = HKDF_STATE_OUTPUT;
5289
Gilles Peskinebef7f142018-07-12 17:22:21 +02005290 while( output_length != 0 )
5291 {
5292 /* Copy what remains of the current block */
5293 uint8_t n = hash_length - hkdf->offset_in_block;
5294 if( n > output_length )
5295 n = (uint8_t) output_length;
5296 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5297 output += n;
5298 output_length -= n;
5299 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005300 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005301 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005302 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005303 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005304 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005305 * object was corrupted or if this function is called directly
5306 * inside the library. */
5307 if( hkdf->block_number == 0xff )
5308 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309
5310 /* We need a new block */
5311 ++hkdf->block_number;
5312 hkdf->offset_in_block = 0;
5313 status = psa_hmac_setup_internal( &hkdf->hmac,
5314 hkdf->prk, hash_length,
5315 hash_alg );
5316 if( status != PSA_SUCCESS )
5317 return( status );
5318 if( hkdf->block_number != 1 )
5319 {
5320 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5321 hkdf->output_block,
5322 hash_length );
5323 if( status != PSA_SUCCESS )
5324 return( status );
5325 }
5326 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5327 hkdf->info,
5328 hkdf->info_length );
5329 if( status != PSA_SUCCESS )
5330 return( status );
5331 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5332 &hkdf->block_number, 1 );
5333 if( status != PSA_SUCCESS )
5334 return( status );
5335 status = psa_hmac_finish_internal( &hkdf->hmac,
5336 hkdf->output_block,
5337 sizeof( hkdf->output_block ) );
5338 if( status != PSA_SUCCESS )
5339 return( status );
5340 }
5341
5342 return( PSA_SUCCESS );
5343}
John Durkop07cc04a2020-11-16 22:08:34 -08005344#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005345
John Durkop07cc04a2020-11-16 22:08:34 -08005346#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5347 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005348static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5349 psa_tls12_prf_key_derivation_t *tls12_prf,
5350 psa_algorithm_t alg )
5351{
5352 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5353 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005354 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5355 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005356
Janos Follath7742fee2019-06-17 12:58:10 +01005357 /* We can't be wanting more output after block 0xff, otherwise
5358 * the capacity check in psa_key_derivation_output_bytes() would have
5359 * prevented this call. It could happen only if the operation
5360 * object was corrupted or if this function is called directly
5361 * inside the library. */
5362 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005363 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005364
5365 /* We need a new block */
5366 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005367 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005368
5369 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5370 *
5371 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5372 *
5373 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5374 * HMAC_hash(secret, A(2) + seed) +
5375 * HMAC_hash(secret, A(3) + seed) + ...
5376 *
5377 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005378 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005379 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005380 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005381 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005382 * is currently extracted as `output_block` and where i is
5383 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005384 */
5385
Janos Follathea29bfb2019-06-19 12:21:20 +01005386 /* Save the hash context before using it, to preserve the hash state with
5387 * only the inner padding in it. We need this, because inner padding depends
5388 * on the key (secret in the RFC's terminology). */
5389 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5390 if( status != PSA_SUCCESS )
5391 goto cleanup;
5392
5393 /* Calculate A(i) where i = tls12_prf->block_number. */
5394 if( tls12_prf->block_number == 1 )
5395 {
5396 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5397 * the variable seed and in this instance means it in the context of the
5398 * P_hash function, where seed = label + seed.) */
5399 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5400 tls12_prf->label, tls12_prf->label_length );
5401 if( status != PSA_SUCCESS )
5402 goto cleanup;
5403 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5404 tls12_prf->seed, tls12_prf->seed_length );
5405 if( status != PSA_SUCCESS )
5406 goto cleanup;
5407 }
5408 else
5409 {
5410 /* A(i) = HMAC_hash(secret, A(i-1)) */
5411 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5412 tls12_prf->Ai, hash_length );
5413 if( status != PSA_SUCCESS )
5414 goto cleanup;
5415 }
5416
5417 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5418 tls12_prf->Ai, hash_length );
5419 if( status != PSA_SUCCESS )
5420 goto cleanup;
5421 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5422 if( status != PSA_SUCCESS )
5423 goto cleanup;
5424
5425 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5426 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5427 tls12_prf->Ai, hash_length );
5428 if( status != PSA_SUCCESS )
5429 goto cleanup;
5430 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5431 tls12_prf->label, tls12_prf->label_length );
5432 if( status != PSA_SUCCESS )
5433 goto cleanup;
5434 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5435 tls12_prf->seed, tls12_prf->seed_length );
5436 if( status != PSA_SUCCESS )
5437 goto cleanup;
5438 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5439 tls12_prf->output_block, hash_length );
5440 if( status != PSA_SUCCESS )
5441 goto cleanup;
5442 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5443 if( status != PSA_SUCCESS )
5444 goto cleanup;
5445
Janos Follath7742fee2019-06-17 12:58:10 +01005446
5447cleanup:
5448
Janos Follathea29bfb2019-06-19 12:21:20 +01005449 cleanup_status = psa_hash_abort( &backup );
5450 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5451 status = cleanup_status;
5452
5453 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005454}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005455
Janos Follath844eb0e2019-06-19 12:10:49 +01005456static psa_status_t psa_key_derivation_tls12_prf_read(
5457 psa_tls12_prf_key_derivation_t *tls12_prf,
5458 psa_algorithm_t alg,
5459 uint8_t *output,
5460 size_t output_length )
5461{
5462 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5463 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5464 psa_status_t status;
5465 uint8_t offset, length;
5466
5467 while( output_length != 0 )
5468 {
5469 /* Check if we have fully processed the current block. */
5470 if( tls12_prf->left_in_block == 0 )
5471 {
5472 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5473 alg );
5474 if( status != PSA_SUCCESS )
5475 return( status );
5476
5477 continue;
5478 }
5479
5480 if( tls12_prf->left_in_block > output_length )
5481 length = (uint8_t) output_length;
5482 else
5483 length = tls12_prf->left_in_block;
5484
5485 offset = hash_length - tls12_prf->left_in_block;
5486 memcpy( output, tls12_prf->output_block + offset, length );
5487 output += length;
5488 output_length -= length;
5489 tls12_prf->left_in_block -= length;
5490 }
5491
5492 return( PSA_SUCCESS );
5493}
John Durkop07cc04a2020-11-16 22:08:34 -08005494#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5495 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005496
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005497psa_status_t psa_key_derivation_output_bytes(
5498 psa_key_derivation_operation_t *operation,
5499 uint8_t *output,
5500 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005501{
5502 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005504
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005505 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005506 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005508 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005509 }
5510
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005511 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005512 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005513 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005514 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005516 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 goto exit;
5518 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005520 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005522 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005523 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5524 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005526 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005527 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005528 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530
John Durkopd0321952020-10-29 21:37:36 -07005531#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005532 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005533 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005534 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005536 output, output_length );
5537 }
Janos Follathadbec812019-06-14 11:05:39 +01005538 else
John Durkop07cc04a2020-11-16 22:08:34 -08005539#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5540#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5541 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005542 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005543 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005544 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005546 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005547 output_length );
5548 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005549 else
John Durkop07cc04a2020-11-16 22:08:34 -08005550#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5551 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005552 {
5553 return( PSA_ERROR_BAD_STATE );
5554 }
5555
5556exit:
5557 if( status != PSA_SUCCESS )
5558 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005559 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 * This allows us to differentiate between exhausted operations and
5561 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5562 * operations. */
5563 psa_algorithm_t alg = operation->alg;
5564 psa_key_derivation_abort( operation );
5565 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005566 memset( output, '!', output_length );
5567 }
5568 return( status );
5569}
5570
Gilles Peskine08542d82018-07-19 17:05:42 +02005571#if defined(MBEDTLS_DES_C)
5572static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5573{
5574 if( data_size >= 8 )
5575 mbedtls_des_key_set_parity( data );
5576 if( data_size >= 16 )
5577 mbedtls_des_key_set_parity( data + 8 );
5578 if( data_size >= 24 )
5579 mbedtls_des_key_set_parity( data + 16 );
5580}
5581#endif /* MBEDTLS_DES_C */
5582
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005583static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005584 psa_key_slot_t *slot,
5585 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005586 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005587{
5588 uint8_t *data = NULL;
5589 size_t bytes = PSA_BITS_TO_BYTES( bits );
5590 psa_status_t status;
5591
Gilles Peskine8e338702019-07-30 20:06:31 +02005592 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593 return( PSA_ERROR_INVALID_ARGUMENT );
5594 if( bits % 8 != 0 )
5595 return( PSA_ERROR_INVALID_ARGUMENT );
5596 data = mbedtls_calloc( 1, bytes );
5597 if( data == NULL )
5598 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5599
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005600 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005601 if( status != PSA_SUCCESS )
5602 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005603#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005604 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005605 psa_des_set_key_parity( data, bytes );
5606#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005607 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005608
5609exit:
5610 mbedtls_free( data );
5611 return( status );
5612}
5613
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005614psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005615 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005616 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005617{
5618 psa_status_t status;
5619 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005620 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005621
Ronald Cron81709fc2020-11-14 12:10:32 +01005622 *key = MBEDTLS_SVC_KEY_ID_INIT;
5623
Gilles Peskine0f84d622019-09-12 19:03:13 +02005624 /* Reject any attempt to create a zero-length key so that we don't
5625 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5626 if( psa_get_key_bits( attributes ) == 0 )
5627 return( PSA_ERROR_INVALID_ARGUMENT );
5628
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005629 if( ! operation->can_output_key )
5630 return( PSA_ERROR_NOT_PERMITTED );
5631
Ronald Cron81709fc2020-11-14 12:10:32 +01005632 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5633 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005634#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5635 if( driver != NULL )
5636 {
5637 /* Deriving a key in a secure element is not implemented yet. */
5638 status = PSA_ERROR_NOT_SUPPORTED;
5639 }
5640#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005641 if( status == PSA_SUCCESS )
5642 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005643 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005644 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005645 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005646 }
5647 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005648 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005649 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005650 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005651
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005652 return( status );
5653}
5654
Gilles Peskineeab56e42018-07-12 17:12:33 +02005655
5656
5657/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005658/* Key derivation */
5659/****************************************************************/
5660
John Durkop07cc04a2020-11-16 22:08:34 -08005661#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005662static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005663 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005664 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665{
John Durkop07cc04a2020-11-16 22:08:34 -08005666 int is_kdf_alg_supported;
5667
Janos Follath5fe19732019-06-20 15:09:30 +01005668 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5669 * initialisers for this union leave some bytes unspecified.) */
5670 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5671
Gilles Peskine969c5d62019-01-16 15:53:06 +01005672 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005673#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005674 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5675 is_kdf_alg_supported = 1;
5676 else
5677#endif
5678#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5679 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5680 is_kdf_alg_supported = 1;
5681 else
5682#endif
5683#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5684 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5685 is_kdf_alg_supported = 1;
5686 else
5687#endif
5688 is_kdf_alg_supported = 0;
5689
5690 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005691 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005692 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005693 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5694 if( hash_size == 0 )
5695 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005696 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5697 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005698 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005699 {
5700 return( PSA_ERROR_NOT_SUPPORTED );
5701 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005702 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005703 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005704 }
John Durkop07cc04a2020-11-16 22:08:34 -08005705
5706 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005707}
John Durkop07cc04a2020-11-16 22:08:34 -08005708#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005711 psa_algorithm_t alg )
5712{
5713 psa_status_t status;
5714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005715 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005716 return( PSA_ERROR_BAD_STATE );
5717
Gilles Peskine6843c292019-01-18 16:44:49 +01005718 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5719 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005720#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005721 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722 {
5723 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725 }
5726 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5727 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729 }
John Durkop07cc04a2020-11-16 22:08:34 -08005730#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005731 else
5732 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005733
5734 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005736 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005737}
5738
John Durkopd0321952020-10-29 21:37:36 -07005739#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005740static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005741 psa_algorithm_t hash_alg,
5742 psa_key_derivation_step_t step,
5743 const uint8_t *data,
5744 size_t data_length )
5745{
5746 psa_status_t status;
5747 switch( step )
5748 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005749 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005750 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005751 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005752 status = psa_hmac_setup_internal( &hkdf->hmac,
5753 data, data_length,
5754 hash_alg );
5755 if( status != PSA_SUCCESS )
5756 return( status );
5757 hkdf->state = HKDF_STATE_STARTED;
5758 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005759 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005760 /* If no salt was provided, use an empty salt. */
5761 if( hkdf->state == HKDF_STATE_INIT )
5762 {
5763 status = psa_hmac_setup_internal( &hkdf->hmac,
5764 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005765 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005766 if( status != PSA_SUCCESS )
5767 return( status );
5768 hkdf->state = HKDF_STATE_STARTED;
5769 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005770 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005772 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5773 data, data_length );
5774 if( status != PSA_SUCCESS )
5775 return( status );
5776 status = psa_hmac_finish_internal( &hkdf->hmac,
5777 hkdf->prk,
5778 sizeof( hkdf->prk ) );
5779 if( status != PSA_SUCCESS )
5780 return( status );
5781 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5782 hkdf->block_number = 0;
5783 hkdf->state = HKDF_STATE_KEYED;
5784 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005785 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005786 if( hkdf->state == HKDF_STATE_OUTPUT )
5787 return( PSA_ERROR_BAD_STATE );
5788 if( hkdf->info_set )
5789 return( PSA_ERROR_BAD_STATE );
5790 hkdf->info_length = data_length;
5791 if( data_length != 0 )
5792 {
5793 hkdf->info = mbedtls_calloc( 1, data_length );
5794 if( hkdf->info == NULL )
5795 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5796 memcpy( hkdf->info, data, data_length );
5797 }
5798 hkdf->info_set = 1;
5799 return( PSA_SUCCESS );
5800 default:
5801 return( PSA_ERROR_INVALID_ARGUMENT );
5802 }
5803}
John Durkop07cc04a2020-11-16 22:08:34 -08005804#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005805
John Durkop07cc04a2020-11-16 22:08:34 -08005806#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5807 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005808static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5809 const uint8_t *data,
5810 size_t data_length )
5811{
5812 if( prf->state != TLS12_PRF_STATE_INIT )
5813 return( PSA_ERROR_BAD_STATE );
5814
Janos Follathd6dce9f2019-07-04 09:11:38 +01005815 if( data_length != 0 )
5816 {
5817 prf->seed = mbedtls_calloc( 1, data_length );
5818 if( prf->seed == NULL )
5819 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005820
Janos Follathd6dce9f2019-07-04 09:11:38 +01005821 memcpy( prf->seed, data, data_length );
5822 prf->seed_length = data_length;
5823 }
Janos Follathf08e2652019-06-13 09:05:41 +01005824
5825 prf->state = TLS12_PRF_STATE_SEED_SET;
5826
5827 return( PSA_SUCCESS );
5828}
5829
Janos Follath81550542019-06-13 14:26:34 +01005830static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5831 psa_algorithm_t hash_alg,
5832 const uint8_t *data,
5833 size_t data_length )
5834{
5835 psa_status_t status;
5836 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5837 return( PSA_ERROR_BAD_STATE );
5838
5839 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5840 if( status != PSA_SUCCESS )
5841 return( status );
5842
5843 prf->state = TLS12_PRF_STATE_KEY_SET;
5844
5845 return( PSA_SUCCESS );
5846}
5847
Janos Follath63028dd2019-06-13 09:15:47 +01005848static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5849 const uint8_t *data,
5850 size_t data_length )
5851{
5852 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5853 return( PSA_ERROR_BAD_STATE );
5854
Janos Follathd6dce9f2019-07-04 09:11:38 +01005855 if( data_length != 0 )
5856 {
5857 prf->label = mbedtls_calloc( 1, data_length );
5858 if( prf->label == NULL )
5859 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005860
Janos Follathd6dce9f2019-07-04 09:11:38 +01005861 memcpy( prf->label, data, data_length );
5862 prf->label_length = data_length;
5863 }
Janos Follath63028dd2019-06-13 09:15:47 +01005864
5865 prf->state = TLS12_PRF_STATE_LABEL_SET;
5866
5867 return( PSA_SUCCESS );
5868}
5869
Janos Follathb03233e2019-06-11 15:30:30 +01005870static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5871 psa_algorithm_t hash_alg,
5872 psa_key_derivation_step_t step,
5873 const uint8_t *data,
5874 size_t data_length )
5875{
Janos Follathb03233e2019-06-11 15:30:30 +01005876 switch( step )
5877 {
Janos Follathf08e2652019-06-13 09:05:41 +01005878 case PSA_KEY_DERIVATION_INPUT_SEED:
5879 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005880 case PSA_KEY_DERIVATION_INPUT_SECRET:
5881 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005882 case PSA_KEY_DERIVATION_INPUT_LABEL:
5883 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005884 default:
5885 return( PSA_ERROR_INVALID_ARGUMENT );
5886 }
5887}
John Durkop07cc04a2020-11-16 22:08:34 -08005888#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5889 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5890
5891#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5892static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5893 psa_tls12_prf_key_derivation_t *prf,
5894 psa_algorithm_t hash_alg,
5895 const uint8_t *data,
5896 size_t data_length )
5897{
5898 psa_status_t status;
5899 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5900 uint8_t *cur = pms;
5901
5902 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5903 return( PSA_ERROR_INVALID_ARGUMENT );
5904
5905 /* Quoting RFC 4279, Section 2:
5906 *
5907 * The premaster secret is formed as follows: if the PSK is N octets
5908 * long, concatenate a uint16 with the value N, N zero octets, a second
5909 * uint16 with the value N, and the PSK itself.
5910 */
5911
5912 *cur++ = ( data_length >> 8 ) & 0xff;
5913 *cur++ = ( data_length >> 0 ) & 0xff;
5914 memset( cur, 0, data_length );
5915 cur += data_length;
5916 *cur++ = pms[0];
5917 *cur++ = pms[1];
5918 memcpy( cur, data, data_length );
5919 cur += data_length;
5920
5921 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5922
5923 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5924 return( status );
5925}
Janos Follath6660f0e2019-06-17 08:44:03 +01005926
5927static psa_status_t psa_tls12_prf_psk_to_ms_input(
5928 psa_tls12_prf_key_derivation_t *prf,
5929 psa_algorithm_t hash_alg,
5930 psa_key_derivation_step_t step,
5931 const uint8_t *data,
5932 size_t data_length )
5933{
5934 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005935 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005936 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5937 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005938 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005939
5940 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5941}
John Durkop07cc04a2020-11-16 22:08:34 -08005942#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005943
Gilles Peskineb8965192019-09-24 16:21:10 +02005944/** Check whether the given key type is acceptable for the given
5945 * input step of a key derivation.
5946 *
5947 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5948 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5949 * Both secret and non-secret inputs can alternatively have the type
5950 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5951 * that the input was passed as a buffer rather than via a key object.
5952 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005953static int psa_key_derivation_check_input_type(
5954 psa_key_derivation_step_t step,
5955 psa_key_type_t key_type )
5956{
5957 switch( step )
5958 {
5959 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005960 if( key_type == PSA_KEY_TYPE_DERIVE )
5961 return( PSA_SUCCESS );
5962 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005963 return( PSA_SUCCESS );
5964 break;
5965 case PSA_KEY_DERIVATION_INPUT_LABEL:
5966 case PSA_KEY_DERIVATION_INPUT_SALT:
5967 case PSA_KEY_DERIVATION_INPUT_INFO:
5968 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005969 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5970 return( PSA_SUCCESS );
5971 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005972 return( PSA_SUCCESS );
5973 break;
5974 }
5975 return( PSA_ERROR_INVALID_ARGUMENT );
5976}
5977
Janos Follathb80a94e2019-06-12 15:54:46 +01005978static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005979 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005980 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005981 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005982 const uint8_t *data,
5983 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005984{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005985 psa_status_t status;
5986 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5987
5988 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005989 if( status != PSA_SUCCESS )
5990 goto exit;
5991
John Durkopd0321952020-10-29 21:37:36 -07005992#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005993 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005994 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005995 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005996 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005997 step, data, data_length );
5998 }
John Durkop07cc04a2020-11-16 22:08:34 -08005999 else
6000#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6001#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6002 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006003 {
Janos Follathb03233e2019-06-11 15:30:30 +01006004 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6005 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6006 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006007 }
John Durkop07cc04a2020-11-16 22:08:34 -08006008 else
6009#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6010#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6011 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006012 {
6013 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6014 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6015 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006016 }
6017 else
John Durkop07cc04a2020-11-16 22:08:34 -08006018#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006019 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006020 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021 return( PSA_ERROR_BAD_STATE );
6022 }
6023
Gilles Peskine224b0d62019-09-23 18:13:17 +02006024exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006025 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006026 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006027 return( status );
6028}
6029
Janos Follath51f4a0f2019-06-14 11:35:55 +01006030psa_status_t psa_key_derivation_input_bytes(
6031 psa_key_derivation_operation_t *operation,
6032 psa_key_derivation_step_t step,
6033 const uint8_t *data,
6034 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006035{
Gilles Peskineb8965192019-09-24 16:21:10 +02006036 return( psa_key_derivation_input_internal( operation, step,
6037 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006038 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006039}
6040
Janos Follath51f4a0f2019-06-14 11:35:55 +01006041psa_status_t psa_key_derivation_input_key(
6042 psa_key_derivation_operation_t *operation,
6043 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006044 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006045{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006047 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006048 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006049
Ronald Cron5c522922020-11-14 16:35:34 +01006050 status = psa_get_and_lock_transparent_key_slot_with_policy(
6051 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006052 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006053 {
6054 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006055 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006056 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006057
6058 /* Passing a key object as a SECRET input unlocks the permission
6059 * to output to a key object. */
6060 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6061 operation->can_output_key = 1;
6062
Ronald Cronf95a2b12020-10-22 15:24:49 +02006063 status = psa_key_derivation_input_internal( operation,
6064 step, slot->attr.type,
6065 slot->data.key.data,
6066 slot->data.key.bytes );
6067
Ronald Cron5c522922020-11-14 16:35:34 +01006068 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006069
Ronald Cron5c522922020-11-14 16:35:34 +01006070 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006071}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006072
6073
6074
6075/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006076/* Key agreement */
6077/****************************************************************/
6078
John Durkop6ba40d12020-11-10 08:50:04 -08006079#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006080static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6081 size_t peer_key_length,
6082 const mbedtls_ecp_keypair *our_key,
6083 uint8_t *shared_secret,
6084 size_t shared_secret_size,
6085 size_t *shared_secret_length )
6086{
Steven Cooremand4867872020-08-05 16:31:39 +02006087 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006088 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006089 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006090 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006091 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006092 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006093
Steven Cooreman4fed4552020-08-03 14:46:03 +02006094 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6095 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006096 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006097 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006098 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006099 goto exit;
6100
Jaeden Amero97271b32019-01-10 19:38:51 +00006101 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006102 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006103 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006104 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006105 status = mbedtls_to_psa_error(
6106 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6107 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006108 goto exit;
6109
Jaeden Amero97271b32019-01-10 19:38:51 +00006110 status = mbedtls_to_psa_error(
6111 mbedtls_ecdh_calc_secret( &ecdh,
6112 shared_secret_length,
6113 shared_secret, shared_secret_size,
6114 mbedtls_ctr_drbg_random,
6115 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006116 if( status != PSA_SUCCESS )
6117 goto exit;
6118 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6119 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006120
6121exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006122 if( status != PSA_SUCCESS )
6123 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006124 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006125 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006126 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006127
Jaeden Amero97271b32019-01-10 19:38:51 +00006128 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006129}
John Durkop6ba40d12020-11-10 08:50:04 -08006130#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006131
Gilles Peskine01d718c2018-09-18 12:01:02 +02006132#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6133
Gilles Peskine0216fe12019-04-11 21:23:21 +02006134static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6135 psa_key_slot_t *private_key,
6136 const uint8_t *peer_key,
6137 size_t peer_key_length,
6138 uint8_t *shared_secret,
6139 size_t shared_secret_size,
6140 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006141{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006142 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006143 {
John Durkop6ba40d12020-11-10 08:50:04 -08006144#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006145 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006146 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006147 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006148 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006149 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006150 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006151 private_key->data.key.data,
6152 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006153 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006154 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006155 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006156 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006157 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006158 shared_secret, shared_secret_size,
6159 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006160 mbedtls_ecp_keypair_free( ecp );
6161 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006162 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006163#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006164 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006165 (void) private_key;
6166 (void) peer_key;
6167 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006168 (void) shared_secret;
6169 (void) shared_secret_size;
6170 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006171 return( PSA_ERROR_NOT_SUPPORTED );
6172 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006173}
6174
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006175/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006176 * to potentially free embedded data structures and wipe confidential data.
6177 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006178static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006179 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006180 psa_key_slot_t *private_key,
6181 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006182 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006183{
6184 psa_status_t status;
6185 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6186 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006187 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006188
6189 /* Step 1: run the secret agreement algorithm to generate the shared
6190 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006191 status = psa_key_agreement_raw_internal( ka_alg,
6192 private_key,
6193 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006194 shared_secret,
6195 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006196 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006197 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006198 goto exit;
6199
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006200 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006201 * the shared secret. A shared secret is permitted wherever a key
6202 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006203 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006204 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006205 shared_secret,
6206 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006207exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006208 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006209 return( status );
6210}
6211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006212psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006213 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006214 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006215 const uint8_t *peer_key,
6216 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006217{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006219 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006220 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006221
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006222 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006223 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006224 status = psa_get_and_lock_transparent_key_slot_with_policy(
6225 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006226 if( status != PSA_SUCCESS )
6227 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006228 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006229 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006230 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006231 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006232 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006233 else
6234 {
6235 /* If a private key has been added as SECRET, we allow the derived
6236 * key material to be used as a key in PSA Crypto. */
6237 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6238 operation->can_output_key = 1;
6239 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006240
Ronald Cron5c522922020-11-14 16:35:34 +01006241 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006242
Ronald Cron5c522922020-11-14 16:35:34 +01006243 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006244}
6245
Gilles Peskinebe697d82019-05-16 18:00:41 +02006246psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006247 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006248 const uint8_t *peer_key,
6249 size_t peer_key_length,
6250 uint8_t *output,
6251 size_t output_size,
6252 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006253{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006255 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006256 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006257
6258 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6259 {
6260 status = PSA_ERROR_INVALID_ARGUMENT;
6261 goto exit;
6262 }
Ronald Cron5c522922020-11-14 16:35:34 +01006263 status = psa_get_and_lock_transparent_key_slot_with_policy(
6264 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006265 if( status != PSA_SUCCESS )
6266 goto exit;
6267
6268 status = psa_key_agreement_raw_internal( alg, slot,
6269 peer_key, peer_key_length,
6270 output, output_size,
6271 output_length );
6272
6273exit:
6274 if( status != PSA_SUCCESS )
6275 {
6276 /* If an error happens and is not handled properly, the output
6277 * may be used as a key to protect sensitive data. Arrange for such
6278 * a key to be random, which is likely to result in decryption or
6279 * verification errors. This is better than filling the buffer with
6280 * some constant data such as zeros, which would result in the data
6281 * being protected with a reproducible, easily knowable key.
6282 */
6283 psa_generate_random( output, output_size );
6284 *output_length = output_size;
6285 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006286
Ronald Cron5c522922020-11-14 16:35:34 +01006287 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006288
Ronald Cron5c522922020-11-14 16:35:34 +01006289 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006290}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006291
6292
6293/****************************************************************/
6294/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006295/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006296
Gilles Peskine4c317f42018-07-12 01:24:09 +02006297psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006298 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006299{
Janos Follath24eed8d2019-11-22 13:21:35 +00006300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006301 GUARD_MODULE_INITIALIZED;
6302
Gilles Peskinef181eca2019-08-07 13:49:00 +02006303 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6304 {
6305 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6306 output,
6307 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6308 if( ret != 0 )
6309 return( mbedtls_to_psa_error( ret ) );
6310 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6311 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6312 }
6313
Gilles Peskinee59236f2018-01-27 23:32:46 +01006314 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6315 return( mbedtls_to_psa_error( ret ) );
6316}
6317
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006318#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6319#include "mbedtls/entropy_poll.h"
6320
Gilles Peskine7228da22019-07-15 11:06:15 +02006321psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006322 size_t seed_size )
6323{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006324 if( global_data.initialized )
6325 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006326
6327 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6328 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6329 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6330 return( PSA_ERROR_INVALID_ARGUMENT );
6331
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006332 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006333}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006334#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006335
John Durkop07cc04a2020-11-16 22:08:34 -08006336#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006337static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6338 size_t domain_parameters_size,
6339 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006340{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006341 size_t i;
6342 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006343
Gilles Peskinee56e8782019-04-26 17:34:02 +02006344 if( domain_parameters_size == 0 )
6345 {
6346 *exponent = 65537;
6347 return( PSA_SUCCESS );
6348 }
6349
6350 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6351 * support values that fit in a 32-bit integer, which is larger than
6352 * int on just about every platform anyway. */
6353 if( domain_parameters_size > sizeof( acc ) )
6354 return( PSA_ERROR_NOT_SUPPORTED );
6355 for( i = 0; i < domain_parameters_size; i++ )
6356 acc = ( acc << 8 ) | domain_parameters[i];
6357 if( acc > INT_MAX )
6358 return( PSA_ERROR_NOT_SUPPORTED );
6359 *exponent = acc;
6360 return( PSA_SUCCESS );
6361}
John Durkop07cc04a2020-11-16 22:08:34 -08006362#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006363
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006364static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006365 psa_key_slot_t *slot, size_t bits,
6366 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006367{
Gilles Peskine8e338702019-07-30 20:06:31 +02006368 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369
Gilles Peskinee56e8782019-04-26 17:34:02 +02006370 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006371 return( PSA_ERROR_INVALID_ARGUMENT );
6372
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373 if( key_type_is_raw_bytes( type ) )
6374 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006375 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006376
6377 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006378 if( status != PSA_SUCCESS )
6379 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006380
6381 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006382 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6383 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006384 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006385
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006386 status = psa_generate_random( slot->data.key.data,
6387 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006388 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006389 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006390
6391 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006392#if defined(MBEDTLS_DES_C)
6393 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006394 psa_des_set_key_parity( slot->data.key.data,
6395 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006396#endif /* MBEDTLS_DES_C */
6397 }
6398 else
6399
John Durkop07cc04a2020-11-16 22:08:34 -08006400#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006401 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006402 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006403 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006405 int exponent;
6406 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006407 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6408 return( PSA_ERROR_NOT_SUPPORTED );
6409 /* Accept only byte-aligned keys, for the same reasons as
6410 * in psa_import_rsa_key(). */
6411 if( bits % 8 != 0 )
6412 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006413 status = psa_read_rsa_exponent( domain_parameters,
6414 domain_parameters_size,
6415 &exponent );
6416 if( status != PSA_SUCCESS )
6417 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006418 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6419 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006420 mbedtls_ctr_drbg_random,
6421 &global_data.ctr_drbg,
6422 (unsigned int) bits,
6423 exponent );
6424 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006425 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006426
6427 /* Make sure to always have an export representation available */
6428 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6429
Steven Cooreman75b74362020-07-28 14:30:13 +02006430 status = psa_allocate_buffer_to_slot( slot, bytes );
6431 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006432 {
6433 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006434 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006435 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006436
6437 status = psa_export_rsa_key( type,
6438 &rsa,
6439 slot->data.key.data,
6440 bytes,
6441 &slot->data.key.bytes );
6442 mbedtls_rsa_free( &rsa );
6443 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006444 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006445 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006446 }
6447 else
John Durkop07cc04a2020-11-16 22:08:34 -08006448#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006449
John Durkop9814fa22020-11-04 12:28:15 -08006450#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006451 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006452 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006453 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006454 mbedtls_ecp_group_id grp_id =
6455 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456 const mbedtls_ecp_curve_info *curve_info =
6457 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006458 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006460 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461 return( PSA_ERROR_NOT_SUPPORTED );
6462 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6463 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006464 mbedtls_ecp_keypair_init( &ecp );
6465 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466 mbedtls_ctr_drbg_random,
6467 &global_data.ctr_drbg );
6468 if( ret != 0 )
6469 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006470 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006471 return( mbedtls_to_psa_error( ret ) );
6472 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006473
6474
6475 /* Make sure to always have an export representation available */
6476 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006477 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6478 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006479 {
6480 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006481 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006482 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006483
6484 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006485 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6486
6487 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006488 if( status != PSA_SUCCESS ) {
6489 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006490 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006491 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006492 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006493 }
6494 else
John Durkop9814fa22020-11-04 12:28:15 -08006495#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006496 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006497 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006498 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006499
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006500 return( PSA_SUCCESS );
6501}
Darryl Green0c6575a2018-11-07 16:05:30 +00006502
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006503psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006504 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006505{
6506 psa_status_t status;
6507 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006508 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006509
Ronald Cron81709fc2020-11-14 12:10:32 +01006510 *key = MBEDTLS_SVC_KEY_ID_INIT;
6511
Gilles Peskine0f84d622019-09-12 19:03:13 +02006512 /* Reject any attempt to create a zero-length key so that we don't
6513 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6514 if( psa_get_key_bits( attributes ) == 0 )
6515 return( PSA_ERROR_INVALID_ARGUMENT );
6516
Ronald Cron81709fc2020-11-14 12:10:32 +01006517 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6518 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006519 if( status != PSA_SUCCESS )
6520 goto exit;
6521
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006522 status = psa_driver_wrapper_generate_key( attributes,
6523 slot );
6524 if( status != PSA_ERROR_NOT_SUPPORTED ||
6525 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6526 goto exit;
6527
6528 status = psa_generate_key_internal(
6529 slot, attributes->core.bits,
6530 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006531
6532exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006533 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006534 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006535 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006536 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006537
Darryl Green0c6575a2018-11-07 16:05:30 +00006538 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539}
6540
6541
Gilles Peskinee59236f2018-01-27 23:32:46 +01006542
6543/****************************************************************/
6544/* Module setup */
6545/****************************************************************/
6546
Gilles Peskine5e769522018-11-20 21:59:56 +01006547psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6548 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6549 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6550{
6551 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6552 return( PSA_ERROR_BAD_STATE );
6553 global_data.entropy_init = entropy_init;
6554 global_data.entropy_free = entropy_free;
6555 return( PSA_SUCCESS );
6556}
6557
Gilles Peskinee59236f2018-01-27 23:32:46 +01006558void mbedtls_psa_crypto_free( void )
6559{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006560 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006561 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6562 {
6563 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006564 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006565 }
6566 /* Wipe all remaining data, including configuration.
6567 * In particular, this sets all state indicator to the value
6568 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006569 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006570#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006571 /* Unregister all secure element drivers, so that we restart from
6572 * a pristine state. */
6573 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006574#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006575}
6576
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006577#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6578/** Recover a transaction that was interrupted by a power failure.
6579 *
6580 * This function is called during initialization, before psa_crypto_init()
6581 * returns. If this function returns a failure status, the initialization
6582 * fails.
6583 */
6584static psa_status_t psa_crypto_recover_transaction(
6585 const psa_crypto_transaction_t *transaction )
6586{
6587 switch( transaction->unknown.type )
6588 {
6589 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6590 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006591 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006592 * is implemented.
6593 * https://github.com/ARMmbed/mbed-crypto/issues/218
6594 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006595 default:
6596 /* We found an unsupported transaction in the storage.
6597 * We don't know what state the storage is in. Give up. */
6598 return( PSA_ERROR_STORAGE_FAILURE );
6599 }
6600}
6601#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6602
Gilles Peskinee59236f2018-01-27 23:32:46 +01006603psa_status_t psa_crypto_init( void )
6604{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006605 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006606 const unsigned char drbg_seed[] = "PSA";
6607
Gilles Peskinec6b69072018-11-20 21:42:52 +01006608 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006609 if( global_data.initialized != 0 )
6610 return( PSA_SUCCESS );
6611
Gilles Peskine5e769522018-11-20 21:59:56 +01006612 /* Set default configuration if
6613 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6614 if( global_data.entropy_init == NULL )
6615 global_data.entropy_init = mbedtls_entropy_init;
6616 if( global_data.entropy_free == NULL )
6617 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006618
Gilles Peskinec6b69072018-11-20 21:42:52 +01006619 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006620 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006621#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6622 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6623 /* The PSA entropy injection feature depends on using NV seed as an entropy
6624 * source. Add NV seed as an entropy source for PSA entropy injection. */
6625 mbedtls_entropy_add_source( &global_data.entropy,
6626 mbedtls_nv_seed_poll, NULL,
6627 MBEDTLS_ENTROPY_BLOCK_SIZE,
6628 MBEDTLS_ENTROPY_SOURCE_STRONG );
6629#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006630 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006631 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006632 status = mbedtls_to_psa_error(
6633 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6634 mbedtls_entropy_func,
6635 &global_data.entropy,
6636 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6637 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006638 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006639 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006640
Gilles Peskine66fb1262018-12-10 16:29:04 +01006641 status = psa_initialize_key_slots( );
6642 if( status != PSA_SUCCESS )
6643 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006644
Gilles Peskined9348f22019-10-01 15:22:29 +02006645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6646 status = psa_init_all_se_drivers( );
6647 if( status != PSA_SUCCESS )
6648 goto exit;
6649#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6650
Gilles Peskine4b734222019-07-24 15:56:31 +02006651#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006652 status = psa_crypto_load_transaction( );
6653 if( status == PSA_SUCCESS )
6654 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006655 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6656 if( status != PSA_SUCCESS )
6657 goto exit;
6658 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006659 }
6660 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6661 {
6662 /* There's no transaction to complete. It's all good. */
6663 status = PSA_SUCCESS;
6664 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006665#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006666
Gilles Peskinec6b69072018-11-20 21:42:52 +01006667 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006668 global_data.initialized = 1;
6669
6670exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006671 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006672 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006673 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006674}
6675
6676#endif /* MBEDTLS_PSA_CRYPTO_C */