blob: 48597b33c11944b3ac2ee27c6f81d734c9cf96ac [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 Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Steven Cooreman01164162020-07-20 15:31:37 +0200132psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200206 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
John Durkop07cc04a2020-11-16 22:08:34 -0800377/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
378 * current test driver in key_management.c is using this function
379 * when accelerators are used for ECC key pair and public key.
380 * Once that dependency is resolved these guards can be removed.
381 */
John Durkop9814fa22020-11-04 12:28:15 -0800382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800383 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
384 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
385 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100386mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100387 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200388{
389 switch( curve )
390 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100391 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100392 switch( byte_length )
393 {
394 case PSA_BITS_TO_BYTES( 192 ):
395 return( MBEDTLS_ECP_DP_SECP192R1 );
396 case PSA_BITS_TO_BYTES( 224 ):
397 return( MBEDTLS_ECP_DP_SECP224R1 );
398 case PSA_BITS_TO_BYTES( 256 ):
399 return( MBEDTLS_ECP_DP_SECP256R1 );
400 case PSA_BITS_TO_BYTES( 384 ):
401 return( MBEDTLS_ECP_DP_SECP384R1 );
402 case PSA_BITS_TO_BYTES( 521 ):
403 return( MBEDTLS_ECP_DP_SECP521R1 );
404 default:
405 return( MBEDTLS_ECP_DP_NONE );
406 }
407 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100408
Paul Elliott8ff510a2020-06-02 17:19:28 +0100409 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100410 switch( byte_length )
411 {
412 case PSA_BITS_TO_BYTES( 256 ):
413 return( MBEDTLS_ECP_DP_BP256R1 );
414 case PSA_BITS_TO_BYTES( 384 ):
415 return( MBEDTLS_ECP_DP_BP384R1 );
416 case PSA_BITS_TO_BYTES( 512 ):
417 return( MBEDTLS_ECP_DP_BP512R1 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 255 ):
427 return( MBEDTLS_ECP_DP_CURVE25519 );
428 case PSA_BITS_TO_BYTES( 448 ):
429 return( MBEDTLS_ECP_DP_CURVE448 );
430 default:
431 return( MBEDTLS_ECP_DP_NONE );
432 }
433 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100434
Paul Elliott8ff510a2020-06-02 17:19:28 +0100435 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100436 switch( byte_length )
437 {
438 case PSA_BITS_TO_BYTES( 192 ):
439 return( MBEDTLS_ECP_DP_SECP192K1 );
440 case PSA_BITS_TO_BYTES( 224 ):
441 return( MBEDTLS_ECP_DP_SECP224K1 );
442 case PSA_BITS_TO_BYTES( 256 ):
443 return( MBEDTLS_ECP_DP_SECP256K1 );
444 default:
445 return( MBEDTLS_ECP_DP_NONE );
446 }
447 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100448
Gilles Peskine12313cd2018-06-20 00:20:32 +0200449 default:
450 return( MBEDTLS_ECP_DP_NONE );
451 }
452}
John Durkop9814fa22020-11-04 12:28:15 -0800453#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800454 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
455 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
456 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200457
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200458static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
459 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460{
461 /* Check that the bit size is acceptable for the key type */
462 switch( type )
463 {
464 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200466 case PSA_KEY_TYPE_DERIVE:
467 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468#if defined(MBEDTLS_AES_C)
469 case PSA_KEY_TYPE_AES:
470 if( bits != 128 && bits != 192 && bits != 256 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_CAMELLIA_C)
475 case PSA_KEY_TYPE_CAMELLIA:
476 if( bits != 128 && bits != 192 && bits != 256 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
480#if defined(MBEDTLS_DES_C)
481 case PSA_KEY_TYPE_DES:
482 if( bits != 64 && bits != 128 && bits != 192 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
486#if defined(MBEDTLS_ARC4_C)
487 case PSA_KEY_TYPE_ARC4:
488 if( bits < 8 || bits > 2048 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
490 break;
491#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200492#if defined(MBEDTLS_CHACHA20_C)
493 case PSA_KEY_TYPE_CHACHA20:
494 if( bits != 256 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
496 break;
497#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 default:
499 return( PSA_ERROR_NOT_SUPPORTED );
500 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200501 if( bits % 8 != 0 )
502 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 return( PSA_SUCCESS );
505}
506
John Durkop0e005192020-10-31 22:06:54 -0700507#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
508 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
509 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800510 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
511 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
512 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200513
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Steven Cooreman7f391872020-07-30 14:57:44 +0200536/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200538 * \param[in] type The type of key contained in \p data.
539 * \param[in] data The buffer from which to load the representation.
540 * \param[in] data_length The size in bytes of \p data.
541 * \param[out] p_rsa Returns a pointer to an RSA context on success.
542 * The caller is responsible for freeing both the
543 * contents of the context and the context itself
544 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200545 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200546static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
547 const uint8_t *data,
548 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200549 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000551 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200552 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200554 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555
556 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200557 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200559 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000561 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200562 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( status != PSA_SUCCESS )
564 goto exit;
565
566 /* We have something that the pkparse module recognizes. If it is a
567 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200569 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000570 status = PSA_ERROR_INVALID_ARGUMENT;
571 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200572 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000573
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000574 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
575 * supports non-byte-aligned key sizes, but not well. For example,
576 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200577 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
579 {
580 status = PSA_ERROR_NOT_SUPPORTED;
581 goto exit;
582 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200583 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 if( status != PSA_SUCCESS )
585 goto exit;
586
Steven Cooremana2371e52020-07-28 14:30:39 +0200587 /* Copy out the pointer to the RSA context, and reset the PK context
588 * such that pk_free doesn't free the RSA context we just grabbed. */
589 *p_rsa = mbedtls_pk_rsa( ctx );
590 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000591
592exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200593 mbedtls_pk_free( &ctx );
594 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200595}
596
John Durkop6ba40d12020-11-10 08:50:04 -0800597#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
598 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
599 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
600 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
601 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
602 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
603
604#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
605 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
606
Steven Cooremana01795d2020-07-24 22:48:15 +0200607/** Export an RSA key to export representation
608 *
609 * \param[in] type The type of key (public/private) to export
610 * \param[in] rsa The internal RSA representation from which to export
611 * \param[out] data The buffer to export to
612 * \param[in] data_size The length of the buffer to export to
613 * \param[out] data_length The amount of bytes written to \p data
614 */
615static psa_status_t psa_export_rsa_key( psa_key_type_t type,
616 mbedtls_rsa_context *rsa,
617 uint8_t *data,
618 size_t data_size,
619 size_t *data_length )
620{
621#if defined(MBEDTLS_PK_WRITE_C)
622 int ret;
623 mbedtls_pk_context pk;
624 uint8_t *pos = data + data_size;
625
626 mbedtls_pk_init( &pk );
627 pk.pk_info = &mbedtls_rsa_info;
628 pk.pk_ctx = rsa;
629
630 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200631 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
632 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200633 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
634 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
635 else
636 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
637
638 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200639 {
640 /* Clean up in case pk_write failed halfway through. */
641 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200642 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200643 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200644
645 /* The mbedtls_pk_xxx functions write to the end of the buffer.
646 * Move the data to the beginning and erase remaining data
647 * at the original location. */
648 if( 2 * (size_t) ret <= data_size )
649 {
650 memcpy( data, data + data_size - ret, ret );
651 memset( data + data_size - ret, 0, ret );
652 }
653 else if( (size_t) ret < data_size )
654 {
655 memmove( data, data + data_size - ret, ret );
656 memset( data + ret, 0, data_size - ret );
657 }
658
659 *data_length = ret;
660 return( PSA_SUCCESS );
661#else
662 (void) type;
663 (void) rsa;
664 (void) data;
665 (void) data_size;
666 (void) data_length;
667 return( PSA_ERROR_NOT_SUPPORTED );
668#endif /* MBEDTLS_PK_WRITE_C */
669}
670
671/** Import an RSA key from import representation to a slot
672 *
673 * \param[in,out] slot The slot where to store the export representation to
674 * \param[in] data The buffer containing the import representation
675 * \param[in] data_length The amount of bytes in \p data
676 */
677static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
678 const uint8_t *data,
679 size_t data_length )
680{
681 psa_status_t status;
682 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200683 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200684
Steven Cooremana01795d2020-07-24 22:48:15 +0200685 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200686 status = psa_load_rsa_representation( slot->attr.type,
687 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200688 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200689 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200690 if( status != PSA_SUCCESS )
691 goto exit;
692
693 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200694 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200695
Steven Cooreman75b74362020-07-28 14:30:13 +0200696 /* Re-export the data to PSA export format, such that we can store export
697 * representation in the key slot. Export representation in case of RSA is
698 * the smallest representation that's allowed as input, so a straight-up
699 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200700 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200701 if( output == NULL )
702 {
703 status = PSA_ERROR_INSUFFICIENT_MEMORY;
704 goto exit;
705 }
706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200708 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200709 output,
710 data_length,
711 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200712exit:
713 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200714 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200715 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200716
717 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000718 if( status != PSA_SUCCESS )
719 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200720 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000721 return( status );
722 }
723
Steven Cooremana01795d2020-07-24 22:48:15 +0200724 /* On success, store the allocated export-formatted key. */
725 slot->data.key.data = output;
726 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000727
728 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200729}
John Durkop6ba40d12020-11-10 08:50:04 -0800730
731#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800732 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200733
John Durkop9814fa22020-11-04 12:28:15 -0800734#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800735 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
736 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
737 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
738 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200739/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200740 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200741 * \param[in] type The type of key contained in \p data.
742 * \param[in] data The buffer from which to load the representation.
743 * \param[in] data_length The size in bytes of \p data.
744 * \param[out] p_ecp Returns a pointer to an ECP context on success.
745 * The caller is responsible for freeing both the
746 * contents of the context and the context itself
747 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200748 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200749static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
750 const uint8_t *data,
751 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200752 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100753{
754 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200755 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200756 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200757 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100758
Steven Cooreman4fed4552020-08-03 14:46:03 +0200759 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
760 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100761 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200762 /* A Weierstrass public key is represented as:
763 * - The byte 0x04;
764 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
765 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200766 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200767 */
768 if( ( data_length & 1 ) == 0 )
769 return( PSA_ERROR_INVALID_ARGUMENT );
770 curve_size = data_length / 2;
771
772 /* Montgomery public keys are represented in compressed format, meaning
773 * their curve_size is equal to the amount of input. */
774
775 /* Private keys are represented in uncompressed private random integer
776 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100777 }
778
Steven Cooreman6d839f02020-07-30 11:36:45 +0200779 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200780 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200781 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200782 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200783 mbedtls_ecp_keypair_init( ecp );
784
Gilles Peskine4cd32772019-12-02 20:49:42 +0100785 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200786 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
787 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100788 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200789 {
790 status = PSA_ERROR_INVALID_ARGUMENT;
791 goto exit;
792 }
793
Steven Cooremanacda8342020-07-24 23:09:52 +0200794 status = mbedtls_to_psa_error(
795 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
796 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200797 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200798
Steven Cooreman6d839f02020-07-30 11:36:45 +0200799 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200800 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200801 {
802 /* Load the public value. */
803 status = mbedtls_to_psa_error(
804 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200805 data,
806 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200807 if( status != PSA_SUCCESS )
808 goto exit;
809
810 /* Check that the point is on the curve. */
811 status = mbedtls_to_psa_error(
812 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
813 if( status != PSA_SUCCESS )
814 goto exit;
815 }
816 else
817 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200818 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200819 status = mbedtls_to_psa_error(
820 mbedtls_ecp_read_key( ecp->grp.id,
821 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200822 data,
823 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200824 if( status != PSA_SUCCESS )
825 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200826 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200827
828 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200829exit:
830 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200831 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200833 mbedtls_free( ecp );
834 }
835
Steven Cooreman29149862020-08-05 15:43:42 +0200836 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100837}
John Durkop6ba40d12020-11-10 08:50:04 -0800838#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
839 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
840 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
841 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
842 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000843
John Durkop6ba40d12020-11-10 08:50:04 -0800844#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
845 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200846/** Export an ECP key to export representation
847 *
848 * \param[in] type The type of key (public/private) to export
849 * \param[in] ecp The internal ECP representation from which to export
850 * \param[out] data The buffer to export to
851 * \param[in] data_size The length of the buffer to export to
852 * \param[out] data_length The amount of bytes written to \p data
853 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200854static psa_status_t psa_export_ecp_key( psa_key_type_t type,
855 mbedtls_ecp_keypair *ecp,
856 uint8_t *data,
857 size_t data_size,
858 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200859{
Steven Cooremanacda8342020-07-24 23:09:52 +0200860 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000861
Steven Cooremanacda8342020-07-24 23:09:52 +0200862 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200863 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 /* Check whether the public part is loaded */
865 if( mbedtls_ecp_is_zero( &ecp->Q ) )
866 {
867 /* Calculate the public key */
868 status = mbedtls_to_psa_error(
869 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
870 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
871 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200872 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200873 }
874
Steven Cooreman4fed4552020-08-03 14:46:03 +0200875 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200876 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
877 MBEDTLS_ECP_PF_UNCOMPRESSED,
878 data_length,
879 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200880 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200881 if( status != PSA_SUCCESS )
882 memset( data, 0, data_size );
883
Steven Cooreman29149862020-08-05 15:43:42 +0200884 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200885 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 else
887 {
Steven Cooreman29149862020-08-05 15:43:42 +0200888 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 return( PSA_ERROR_BUFFER_TOO_SMALL );
890
891 status = mbedtls_to_psa_error(
892 mbedtls_ecp_write_key( ecp,
893 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200894 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200896 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200897 else
898 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200899
900 return( status );
901 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200902}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200903
Steven Cooreman4fed4552020-08-03 14:46:03 +0200904/** Import an ECP key from import representation to a slot
905 *
906 * \param[in,out] slot The slot where to store the export representation to
907 * \param[in] data The buffer containing the import representation
908 * \param[in] data_length The amount of bytes in \p data
909 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200910static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
911 const uint8_t *data,
912 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100913{
Steven Cooremanacda8342020-07-24 23:09:52 +0200914 psa_status_t status;
915 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100917
Steven Cooremanacda8342020-07-24 23:09:52 +0200918 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200919 status = psa_load_ecp_representation( slot->attr.type,
920 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200921 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200922 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100923 if( status != PSA_SUCCESS )
924 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100925
Steven Cooremanacda8342020-07-24 23:09:52 +0200926 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200927 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200928 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200929 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200930
Steven Cooremanacda8342020-07-24 23:09:52 +0200931 /* Re-export the data to PSA export format. There is currently no support
932 * for other input formats then the export format, so this is a 1-1
933 * copy operation. */
934 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200935 if( output == NULL )
936 {
937 status = PSA_ERROR_INSUFFICIENT_MEMORY;
938 goto exit;
939 }
940
941 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200942 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200943 output,
944 data_length,
945 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100946exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200947 /* Always free the PK object (will also free contained ECP context) */
948 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200949 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200950
951 /* Free the allocated buffer only on error. */
952 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100953 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200954 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200955 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100956 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200957
958 /* On success, store the allocated export-formatted key. */
959 slot->data.key.data = output;
960 slot->data.key.bytes = data_length;
961
962 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100963}
John Durkop9814fa22020-11-04 12:28:15 -0800964#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
965 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100966
Gilles Peskineb46bef22019-07-30 21:32:04 +0200967/** Return the size of the key in the given slot, in bits.
968 *
969 * \param[in] slot A key slot.
970 *
971 * \return The key size in bits, read from the metadata in the slot.
972 */
973static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
974{
975 return( slot->attr.bits );
976}
977
Steven Cooreman75b74362020-07-28 14:30:13 +0200978/** Try to allocate a buffer to an empty key slot.
979 *
980 * \param[in,out] slot Key slot to attach buffer to.
981 * \param[in] buffer_length Requested size of the buffer.
982 *
983 * \retval #PSA_SUCCESS
984 * The buffer has been successfully allocated.
985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
986 * Not enough memory was available for allocation.
987 * \retval #PSA_ERROR_ALREADY_EXISTS
988 * Trying to allocate a buffer to a non-empty key slot.
989 */
990static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
991 size_t buffer_length )
992{
993 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200994 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200995
996 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
997 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200998 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200999
1000 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001001 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001002}
1003
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001004psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1005 const uint8_t* data,
1006 size_t data_length )
1007{
1008 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1009 data_length );
1010 if( status != PSA_SUCCESS )
1011 return( status );
1012
1013 memcpy( slot->data.key.data, data, data_length );
1014 return( PSA_SUCCESS );
1015}
1016
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001017/** Import key data into a slot.
1018 *
1019 * `slot->type` must have been set previously.
1020 * This function assumes that the slot does not contain any key material yet.
1021 * On failure, the slot content is unchanged.
1022 *
1023 * Persistent storage is not affected.
1024 *
1025 * \param[in,out] slot The key slot to import data into.
1026 * Its `type` field must have previously been set to
1027 * the desired key type.
1028 * It must not contain any key material yet.
1029 * \param[in] data Buffer containing the key material to parse and import.
1030 * \param data_length Size of \p data in bytes.
1031 *
1032 * \retval #PSA_SUCCESS
1033 * \retval #PSA_ERROR_INVALID_ARGUMENT
1034 * \retval #PSA_ERROR_NOT_SUPPORTED
1035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1036 */
1037static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1038 const uint8_t *data,
1039 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001041 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001042 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001044 /* zero-length keys are never supported. */
1045 if( data_length == 0 )
1046 return( PSA_ERROR_NOT_SUPPORTED );
1047
Gilles Peskine8e338702019-07-30 20:06:31 +02001048 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 {
Steven Cooreman04524762020-10-13 17:43:44 +02001050 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001051
Steven Cooreman75b74362020-07-28 14:30:13 +02001052 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1053 if( data_length > SIZE_MAX / 8 )
1054 return( PSA_ERROR_NOT_SUPPORTED );
1055
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001056 /* Enforce a size limit, and in particular ensure that the bit
1057 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001058 if( bit_size > PSA_MAX_KEY_BITS )
1059 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001060
1061 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001062 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001063 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001064
1065 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001066 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001067 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001068 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001069
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001070 /* Write the actual key size to the slot.
1071 * psa_start_key_creation() wrote the size declared by the
1072 * caller, which may be 0 (meaning unspecified) or wrong. */
1073 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001074
1075 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001077 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001078 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001079 /* Try validation through accelerators first. */
1080 bit_size = slot->attr.bits;
1081 psa_key_attributes_t attributes = {
1082 .core = slot->attr
1083 };
1084 status = psa_driver_wrapper_validate_key( &attributes,
1085 data,
1086 data_length,
1087 &bit_size );
1088 if( status == PSA_SUCCESS )
1089 {
1090 /* Key has been validated successfully by an accelerator.
1091 * Copy key material into slot. */
1092 status = psa_copy_key_material_into_slot( slot, data, data_length );
1093 if( status != PSA_SUCCESS )
1094 return( status );
1095
1096 slot->attr.bits = (psa_key_bits_t) bit_size;
1097 return( PSA_SUCCESS );
1098 }
1099 else if( status != PSA_ERROR_NOT_SUPPORTED )
1100 return( status );
1101
1102 /* Key format is not supported by any accelerator, try software fallback
1103 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001104#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1105 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001106 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1107 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001108 return( psa_import_ecp_key( slot, data, data_length ) );
1109 }
John Durkop9814fa22020-11-04 12:28:15 -08001110#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1111 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001112#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1113 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001114 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001115 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001116 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001117 }
John Durkop9814fa22020-11-04 12:28:15 -08001118#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1119 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001120
1121 /* Fell through the fallback as well, so have nothing else to try. */
1122 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 }
1124 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001125 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001126 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001127 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001128 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001129}
1130
Gilles Peskinef603c712019-01-19 13:40:11 +01001131/** Calculate the intersection of two algorithm usage policies.
1132 *
1133 * Return 0 (which allows no operation) on incompatibility.
1134 */
1135static psa_algorithm_t psa_key_policy_algorithm_intersection(
1136 psa_algorithm_t alg1,
1137 psa_algorithm_t alg2 )
1138{
Gilles Peskine549ea862019-05-22 11:45:59 +02001139 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001140 if( alg1 == alg2 )
1141 return( alg1 );
1142 /* If the policies are from the same hash-and-sign family, check
1143 * if one is a wildcard. If so the other has the specific algorithm. */
1144 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1145 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1146 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1147 {
1148 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1149 return( alg2 );
1150 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1151 return( alg1 );
1152 }
1153 /* If the policies are incompatible, allow nothing. */
1154 return( 0 );
1155}
1156
Gilles Peskined6f371b2019-05-10 19:33:38 +02001157static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1158 psa_algorithm_t requested_alg )
1159{
Gilles Peskine549ea862019-05-22 11:45:59 +02001160 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001161 if( requested_alg == policy_alg )
1162 return( 1 );
1163 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001164 * and requested_alg is the same hash-and-sign family with any hash,
1165 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001166 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1167 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1168 {
1169 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1170 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1171 }
Steven Cooremance48e852020-10-05 16:02:45 +02001172 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001173 * a key derivation with that key agreement should also be allowed. This
1174 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001175 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1176 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1177 {
1178 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1179 policy_alg );
1180 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001181 /* If it isn't permitted, it's forbidden. */
1182 return( 0 );
1183}
1184
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001185/** Test whether a policy permits an algorithm.
1186 *
1187 * The caller must test usage flags separately.
1188 */
1189static int psa_key_policy_permits( const psa_key_policy_t *policy,
1190 psa_algorithm_t alg )
1191{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001192 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1193 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001194}
1195
Gilles Peskinef603c712019-01-19 13:40:11 +01001196/** Restrict a key policy based on a constraint.
1197 *
1198 * \param[in,out] policy The policy to restrict.
1199 * \param[in] constraint The policy constraint to apply.
1200 *
1201 * \retval #PSA_SUCCESS
1202 * \c *policy contains the intersection of the original value of
1203 * \c *policy and \c *constraint.
1204 * \retval #PSA_ERROR_INVALID_ARGUMENT
1205 * \c *policy and \c *constraint are incompatible.
1206 * \c *policy is unchanged.
1207 */
1208static psa_status_t psa_restrict_key_policy(
1209 psa_key_policy_t *policy,
1210 const psa_key_policy_t *constraint )
1211{
1212 psa_algorithm_t intersection_alg =
1213 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001214 psa_algorithm_t intersection_alg2 =
1215 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001216 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1217 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001218 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1219 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001220 policy->usage &= constraint->usage;
1221 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001222 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001223 return( PSA_SUCCESS );
1224}
1225
Ronald Cron5c522922020-11-14 16:35:34 +01001226/** Get the description of a key given its identifier and policy constraints
1227 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228 *
Ronald Cron5c522922020-11-14 16:35:34 +01001229 * The key must have allow all the usage flags set in \p usage. If \p alg is
1230 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001231 *
Ronald Cron5c522922020-11-14 16:35:34 +01001232 * In case of a persistent key, the function loads the description of the key
1233 * into a key slot if not already done.
1234 *
1235 * On success, the returned key slot is locked. It is the responsibility of
1236 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001237 */
Ronald Cron5c522922020-11-14 16:35:34 +01001238static psa_status_t psa_get_and_lock_key_slot_with_policy(
1239 mbedtls_svc_key_id_t key,
1240 psa_key_slot_t **p_slot,
1241 psa_key_usage_t usage,
1242 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001243{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1245 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001246
Ronald Cron5c522922020-11-14 16:35:34 +01001247 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001248 if( status != PSA_SUCCESS )
1249 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001250 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001251
1252 /* Enforce that usage policy for the key slot contains all the flags
1253 * required by the usage parameter. There is one exception: public
1254 * keys can always be exported, so we treat public key objects as
1255 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001256 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001257 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001258
1259 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001260 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001262
1263 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001265 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001266
Darryl Green06fd18d2018-07-16 11:21:11 +01001267 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001268
1269error:
1270 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001271 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001272
1273 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001274}
Darryl Green940d72c2018-07-13 13:18:51 +01001275
Ronald Cron5c522922020-11-14 16:35:34 +01001276/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001277 *
1278 * A transparent key is a key for which the key material is directly
1279 * available, as opposed to a key in a secure element.
1280 *
Ronald Cron5c522922020-11-14 16:35:34 +01001281 * This is a temporary function to use instead of
1282 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1283 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001284 *
Ronald Cron5c522922020-11-14 16:35:34 +01001285 * On success, the returned key slot is locked. It is the responsibility of the
1286 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001287 */
1288#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001289static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1290 mbedtls_svc_key_id_t key,
1291 psa_key_slot_t **p_slot,
1292 psa_key_usage_t usage,
1293 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001294{
Ronald Cron5c522922020-11-14 16:35:34 +01001295 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1296 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001297 if( status != PSA_SUCCESS )
1298 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299
Gilles Peskineadad8132019-07-25 11:31:23 +02001300 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001301 {
Ronald Cron5c522922020-11-14 16:35:34 +01001302 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001303 *p_slot = NULL;
1304 return( PSA_ERROR_NOT_SUPPORTED );
1305 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306
Gilles Peskine28f8f302019-07-24 13:30:31 +02001307 return( PSA_SUCCESS );
1308}
1309#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1310/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001311#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1312 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001313#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1314
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001315/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001316static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001317{
Gilles Peskine73167e12019-07-12 23:44:37 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1319 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001320 {
1321 /* No key material to clean. */
1322 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001323 else
1324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001325 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001326 /* Data pointer will always be either a valid pointer or NULL in an
1327 * initialized slot, so we can just free it. */
1328 if( slot->data.key.data != NULL )
1329 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001330 mbedtls_free( slot->data.key.data );
1331 slot->data.key.data = NULL;
1332 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001333 }
Darryl Green40225ba2018-11-15 14:48:15 +00001334
1335 return( PSA_SUCCESS );
1336}
1337
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001338/** Completely wipe a slot in memory, including its policy.
1339 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001340psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001341{
1342 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001343
1344 /*
1345 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001346 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001347 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1348 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001349 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001350 */
Ronald Cron5c522922020-11-14 16:35:34 +01001351 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001352 {
1353#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001354 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001355#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001356 status = PSA_ERROR_CORRUPTION_DETECTED;
1357 }
1358
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001359 /* Multipart operations may still be using the key. This is safe
1360 * because all multipart operation objects are independent from
1361 * the key slot: if they need to access the key after the setup
1362 * phase, they have a copy of the key. Note that this means that
1363 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001364 /* At this point, key material and other type-specific content has
1365 * been wiped. Clear remaining metadata. We can call memset and not
1366 * zeroize because the metadata is not particularly sensitive. */
1367 memset( slot, 0, sizeof( *slot ) );
1368 return( status );
1369}
1370
Ronald Croncf56a0a2020-08-04 09:51:30 +02001371psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001372{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001373 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 psa_status_t status; /* status of the last operation */
1375 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001376#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1377 psa_se_drv_table_entry_t *driver;
1378#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379
Ronald Croncf56a0a2020-08-04 09:51:30 +02001380 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001381 return( PSA_SUCCESS );
1382
Ronald Cronf2911112020-10-29 17:51:10 +01001383 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001384 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001385 * key, this will load the key description from persistent memory if not
1386 * done yet. We cannot avoid this loading as without it we don't know if
1387 * the key is operated by an SE or not and this information is needed by
1388 * the current implementation.
1389 */
Ronald Cron5c522922020-11-14 16:35:34 +01001390 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001391 if( status != PSA_SUCCESS )
1392 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001393
Ronald Cronf2911112020-10-29 17:51:10 +01001394 /*
1395 * If the key slot containing the key description is under access by the
1396 * library (apart from the present access), the key cannot be destroyed
1397 * yet. For the time being, just return in error. Eventually (to be
1398 * implemented), the key should be destroyed when all accesses have
1399 * stopped.
1400 */
Ronald Cron5c522922020-11-14 16:35:34 +01001401 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001402 {
Ronald Cron5c522922020-11-14 16:35:34 +01001403 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001404 return( PSA_ERROR_GENERIC_ERROR );
1405 }
1406
Gilles Peskine354f7672019-07-12 23:46:38 +02001407#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001408 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001409 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001410 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001411 /* For a key in a secure element, we need to do three things:
1412 * remove the key file in internal storage, destroy the
1413 * key inside the secure element, and update the driver's
1414 * persistent data. Start a transaction that will encompass these
1415 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001416 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001417 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001418 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001419 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001420 status = psa_crypto_save_transaction( );
1421 if( status != PSA_SUCCESS )
1422 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001423 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001424 /* We should still try to destroy the key in the secure
1425 * element and the key metadata in storage. This is especially
1426 * important if the error is that the storage is full.
1427 * But how to do it exactly without risking an inconsistent
1428 * state after a reset?
1429 * https://github.com/ARMmbed/mbed-crypto/issues/215
1430 */
1431 overall_status = status;
1432 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001433 }
1434
Gilles Peskine354f7672019-07-12 23:46:38 +02001435 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001436 if( overall_status == PSA_SUCCESS )
1437 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001438 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001439#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1440
Darryl Greend49a4992018-06-18 17:27:26 +01001441#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001442 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001443 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001444 status = psa_destroy_persistent_key( slot->attr.id );
1445 if( overall_status == PSA_SUCCESS )
1446 overall_status = status;
1447
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001448 /* TODO: other slots may have a copy of the same key. We should
1449 * invalidate them.
1450 * https://github.com/ARMmbed/mbed-crypto/issues/214
1451 */
Darryl Greend49a4992018-06-18 17:27:26 +01001452 }
1453#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001454
Gilles Peskinefc762652019-07-22 19:30:34 +02001455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1456 if( driver != NULL )
1457 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001458 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001459 if( overall_status == PSA_SUCCESS )
1460 overall_status = status;
1461 status = psa_crypto_stop_transaction( );
1462 if( overall_status == PSA_SUCCESS )
1463 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001464 }
1465#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1466
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001467#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1468exit:
1469#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001470 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001471 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1472 if( overall_status == PSA_SUCCESS )
1473 overall_status = status;
1474 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475}
1476
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001477void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001478{
Gilles Peskineb699f072019-04-26 16:06:02 +02001479 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001480 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001481}
1482
Gilles Peskineb699f072019-04-26 16:06:02 +02001483psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1484 psa_key_type_t type,
1485 const uint8_t *data,
1486 size_t data_length )
1487{
1488 uint8_t *copy = NULL;
1489
1490 if( data_length != 0 )
1491 {
1492 copy = mbedtls_calloc( 1, data_length );
1493 if( copy == NULL )
1494 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1495 memcpy( copy, data, data_length );
1496 }
1497 /* After this point, this function is guaranteed to succeed, so it
1498 * can start modifying `*attributes`. */
1499
1500 if( attributes->domain_parameters != NULL )
1501 {
1502 mbedtls_free( attributes->domain_parameters );
1503 attributes->domain_parameters = NULL;
1504 attributes->domain_parameters_size = 0;
1505 }
1506
1507 attributes->domain_parameters = copy;
1508 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001509 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001510 return( PSA_SUCCESS );
1511}
1512
1513psa_status_t psa_get_key_domain_parameters(
1514 const psa_key_attributes_t *attributes,
1515 uint8_t *data, size_t data_size, size_t *data_length )
1516{
1517 if( attributes->domain_parameters_size > data_size )
1518 return( PSA_ERROR_BUFFER_TOO_SMALL );
1519 *data_length = attributes->domain_parameters_size;
1520 if( attributes->domain_parameters_size != 0 )
1521 memcpy( data, attributes->domain_parameters,
1522 attributes->domain_parameters_size );
1523 return( PSA_SUCCESS );
1524}
1525
John Durkop07cc04a2020-11-16 22:08:34 -08001526#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001527 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001528static psa_status_t psa_get_rsa_public_exponent(
1529 const mbedtls_rsa_context *rsa,
1530 psa_key_attributes_t *attributes )
1531{
1532 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001534 uint8_t *buffer = NULL;
1535 size_t buflen;
1536 mbedtls_mpi_init( &mpi );
1537
1538 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1539 if( ret != 0 )
1540 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001541 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1542 {
1543 /* It's the default value, which is reported as an empty string,
1544 * so there's nothing to do. */
1545 goto exit;
1546 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001547
1548 buflen = mbedtls_mpi_size( &mpi );
1549 buffer = mbedtls_calloc( 1, buflen );
1550 if( buffer == NULL )
1551 {
1552 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1553 goto exit;
1554 }
1555 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1556 if( ret != 0 )
1557 goto exit;
1558 attributes->domain_parameters = buffer;
1559 attributes->domain_parameters_size = buflen;
1560
1561exit:
1562 mbedtls_mpi_free( &mpi );
1563 if( ret != 0 )
1564 mbedtls_free( buffer );
1565 return( mbedtls_to_psa_error( ret ) );
1566}
John Durkop07cc04a2020-11-16 22:08:34 -08001567#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001568 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001569
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001570/** Retrieve all the publicly-accessible attributes of a key.
1571 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001572psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001573 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001576 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001577 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001578
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001579 psa_reset_key_attributes( attributes );
1580
Ronald Cron5c522922020-11-14 16:35:34 +01001581 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001582 if( status != PSA_SUCCESS )
1583 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001584
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001586 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1587 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1588
1589#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1590 if( psa_key_slot_is_external( slot ) )
1591 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1592#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001593
Gilles Peskine8e338702019-07-30 20:06:31 +02001594 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001595 {
John Durkop07cc04a2020-11-16 22:08:34 -08001596#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001597 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001598 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001599 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001600#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001601 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001602 * is not yet implemented.
1603 * https://github.com/ARMmbed/mbed-crypto/issues/216
1604 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001605 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001606 break;
1607#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001608 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001609 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001610
Steven Cooreman4fed4552020-08-03 14:46:03 +02001611 status = psa_load_rsa_representation( slot->attr.type,
1612 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001613 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001614 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001615 if( status != PSA_SUCCESS )
1616 break;
1617
Steven Cooremana2371e52020-07-28 14:30:39 +02001618 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001619 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001620 mbedtls_rsa_free( rsa );
1621 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001622 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001623 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001624#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001625 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001626 default:
1627 /* Nothing else to do. */
1628 break;
1629 }
1630
1631 if( status != PSA_SUCCESS )
1632 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001633
Ronald Cron5c522922020-11-14 16:35:34 +01001634 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001635
Ronald Cron5c522922020-11-14 16:35:34 +01001636 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637}
1638
Gilles Peskinec8000c02019-08-02 20:15:51 +02001639#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1640psa_status_t psa_get_key_slot_number(
1641 const psa_key_attributes_t *attributes,
1642 psa_key_slot_number_t *slot_number )
1643{
1644 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1645 {
1646 *slot_number = attributes->slot_number;
1647 return( PSA_SUCCESS );
1648 }
1649 else
1650 return( PSA_ERROR_INVALID_ARGUMENT );
1651}
1652#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1653
Steven Cooremana01795d2020-07-24 22:48:15 +02001654static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1655 uint8_t *data,
1656 size_t data_size,
1657 size_t *data_length )
1658{
1659 if( slot->data.key.bytes > data_size )
1660 return( PSA_ERROR_BUFFER_TOO_SMALL );
1661 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1662 memset( data + slot->data.key.bytes, 0,
1663 data_size - slot->data.key.bytes );
1664 *data_length = slot->data.key.bytes;
1665 return( PSA_SUCCESS );
1666}
1667
Gilles Peskinef603c712019-01-19 13:40:11 +01001668static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1669 uint8_t *data,
1670 size_t data_size,
1671 size_t *data_length,
1672 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673{
Gilles Peskine5d309672019-07-12 23:47:28 +02001674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1675 const psa_drv_se_t *drv;
1676 psa_drv_se_context_t *drv_context;
1677#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1678
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001679 *data_length = 0;
1680
Gilles Peskine8e338702019-07-30 20:06:31 +02001681 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001682 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001683
Gilles Peskinef9168942019-09-12 19:20:29 +02001684 /* Reject a zero-length output buffer now, since this can never be a
1685 * valid key representation. This way we know that data must be a valid
1686 * pointer and we can do things like memset(data, ..., data_size). */
1687 if( data_size == 0 )
1688 return( PSA_ERROR_BUFFER_TOO_SMALL );
1689
Gilles Peskine5d309672019-07-12 23:47:28 +02001690#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001691 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001692 {
1693 psa_drv_se_export_key_t method;
1694 if( drv->key_management == NULL )
1695 return( PSA_ERROR_NOT_SUPPORTED );
1696 method = ( export_public_key ?
1697 drv->key_management->p_export_public :
1698 drv->key_management->p_export );
1699 if( method == NULL )
1700 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001701 return( method( drv_context,
1702 slot->data.se.slot_number,
1703 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001704 }
1705#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1706
Gilles Peskine8e338702019-07-30 20:06:31 +02001707 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001708 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001709 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001710 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001711 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1712 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001713 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001714 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001715 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001716 /* Exporting public -> public */
1717 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1718 }
1719 else if( !export_public_key )
1720 {
1721 /* Exporting private -> private */
1722 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1723 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001724
Steven Cooreman560c28a2020-07-24 23:20:24 +02001725 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001726 * so conversion is needed. Try the accelerators first. */
1727 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1728 data,
1729 data_size,
1730 data_length );
1731
1732 if( status != PSA_ERROR_NOT_SUPPORTED ||
1733 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1734 return( status );
1735
Steven Cooreman560c28a2020-07-24 23:20:24 +02001736 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1737 {
John Durkop0e005192020-10-31 22:06:54 -07001738#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1739 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001740 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001741 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001742 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001743 slot->data.key.data,
1744 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001745 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001746 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001747 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001748
Steven Cooreman560c28a2020-07-24 23:20:24 +02001749 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001750 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001751 data,
1752 data_size,
1753 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001754
Steven Cooremana2371e52020-07-28 14:30:39 +02001755 mbedtls_rsa_free( rsa );
1756 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001757
Steven Cooreman560c28a2020-07-24 23:20:24 +02001758 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001759#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001760 /* We don't know how to convert a private RSA key to public. */
1761 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001762#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1763 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001764 }
1765 else
Moran Pekera998bc62018-04-16 18:16:20 +03001766 {
John Durkop6ba40d12020-11-10 08:50:04 -08001767#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1768 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001769 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001770 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001771 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001772 slot->data.key.data,
1773 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001774 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001775 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001776 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001777
1778 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1779 PSA_KEY_TYPE_ECC_GET_FAMILY(
1780 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001781 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001782 data,
1783 data_size,
1784 data_length );
1785
Steven Cooremana2371e52020-07-28 14:30:39 +02001786 mbedtls_ecp_keypair_free( ecp );
1787 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001788 return( status );
1789#else
1790 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001791 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001792#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1793 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001794 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001795 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001796 else
1797 {
1798 /* This shouldn't happen in the reference implementation, but
1799 it is valid for a special-purpose implementation to omit
1800 support for exporting certain key types. */
1801 return( PSA_ERROR_NOT_SUPPORTED );
1802 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001803}
1804
Ronald Croncf56a0a2020-08-04 09:51:30 +02001805psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001806 uint8_t *data,
1807 size_t data_size,
1808 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001809{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001811 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001812 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001813
1814 /* Set the key to empty now, so that even when there are errors, we always
1815 * set data_length to a value between 0 and data_size. On error, setting
1816 * the key to empty is a good choice because an empty key representation is
1817 * unlikely to be accepted anywhere. */
1818 *data_length = 0;
1819
1820 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001821 * which don't require any flag, but
1822 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1823 */
1824 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1825 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001826 if( status != PSA_SUCCESS )
1827 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001828
1829 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001830 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001831
Ronald Cron5c522922020-11-14 16:35:34 +01001832 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001833}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001834
Ronald Croncf56a0a2020-08-04 09:51:30 +02001835psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001836 uint8_t *data,
1837 size_t data_size,
1838 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001839{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001841 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001842 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001843
1844 /* Set the key to empty now, so that even when there are errors, we always
1845 * set data_length to a value between 0 and data_size. On error, setting
1846 * the key to empty is a good choice because an empty key representation is
1847 * unlikely to be accepted anywhere. */
1848 *data_length = 0;
1849
1850 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001851 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001852 if( status != PSA_SUCCESS )
1853 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001854
1855 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001856 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001857
Ronald Cron5c522922020-11-14 16:35:34 +01001858 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001859}
1860
Gilles Peskine91e8c332019-08-02 19:19:39 +02001861#if defined(static_assert)
1862static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1863 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001864static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001865 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001866static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001867 "One or more key attribute flag is listed as both internal-only and external-only" );
1868#endif
1869
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001870/** Validate that a key policy is internally well-formed.
1871 *
1872 * This function only rejects invalid policies. It does not validate the
1873 * consistency of the policy with respect to other attributes of the key
1874 * such as the key type.
1875 */
1876static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001877{
1878 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001879 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 PSA_KEY_USAGE_ENCRYPT |
1881 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001882 PSA_KEY_USAGE_SIGN_HASH |
1883 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001884 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1885 return( PSA_ERROR_INVALID_ARGUMENT );
1886
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 return( PSA_SUCCESS );
1888}
1889
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001890/** Validate the internal consistency of key attributes.
1891 *
1892 * This function only rejects invalid attribute values. If does not
1893 * validate the consistency of the attributes with any key data that may
1894 * be involved in the creation of the key.
1895 *
1896 * Call this function early in the key creation process.
1897 *
1898 * \param[in] attributes Key attributes for the new key.
1899 * \param[out] p_drv On any return, the driver for the key, if any.
1900 * NULL for a transparent key.
1901 *
1902 */
1903static psa_status_t psa_validate_key_attributes(
1904 const psa_key_attributes_t *attributes,
1905 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001906{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001907 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001908 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001909 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001910
Ronald Cron54b90082020-10-29 15:26:43 +01001911 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001912 if( status != PSA_SUCCESS )
1913 return( status );
1914
Ronald Crond2ed4812020-07-17 16:11:30 +02001915 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001916 if( status != PSA_SUCCESS )
1917 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001918
Ronald Cron65f38a32020-10-23 17:11:13 +02001919 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1920 {
1921 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1922 return( PSA_ERROR_INVALID_ARGUMENT );
1923 }
1924 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001925 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001926 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001927 if( status != PSA_SUCCESS )
1928 return( status );
1929 }
1930
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001931 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001932 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001933 return( status );
1934
1935 /* Refuse to create overly large keys.
1936 * Note that this doesn't trigger on import if the attributes don't
1937 * explicitly specify a size (so psa_get_key_bits returns 0), so
1938 * psa_import_key() needs its own checks. */
1939 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1940 return( PSA_ERROR_NOT_SUPPORTED );
1941
Gilles Peskine91e8c332019-08-02 19:19:39 +02001942 /* Reject invalid flags. These should not be reachable through the API. */
1943 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1944 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1945 return( PSA_ERROR_INVALID_ARGUMENT );
1946
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001947 return( PSA_SUCCESS );
1948}
1949
Gilles Peskine4747d192019-04-17 15:05:45 +02001950/** Prepare a key slot to receive key material.
1951 *
1952 * This function allocates a key slot and sets its metadata.
1953 *
1954 * If this function fails, call psa_fail_key_creation().
1955 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001956 * This function is intended to be used as follows:
1957 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001958 * it with the specified attributes, and in case of a volatile key assign it
1959 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001960 * -# Populate the slot with the key material.
1961 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1962 * In case of failure at any step, stop the sequence and call
1963 * psa_fail_key_creation().
1964 *
Ronald Cron5c522922020-11-14 16:35:34 +01001965 * On success, the key slot is locked. It is the responsibility of the caller
1966 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001967 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001968 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001969 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001970 * \param[out] p_slot On success, a pointer to the prepared slot.
1971 * \param[out] p_drv On any return, the driver for the key, if any.
1972 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001973 *
1974 * \retval #PSA_SUCCESS
1975 * The key slot is ready to receive key material.
1976 * \return If this function fails, the key slot is an invalid state.
1977 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001978 */
1979static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001980 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001981 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001982 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001983 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001984{
1985 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001986 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001987 psa_key_slot_t *slot;
1988
Gilles Peskinedf179142019-07-15 22:02:14 +02001989 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 *p_drv = NULL;
1991
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001992 status = psa_validate_key_attributes( attributes, p_drv );
1993 if( status != PSA_SUCCESS )
1994 return( status );
1995
Ronald Cronc4d1b512020-07-31 11:26:37 +02001996 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001997 if( status != PSA_SUCCESS )
1998 return( status );
1999 slot = *p_slot;
2000
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002001 /* We're storing the declared bit-size of the key. It's up to each
2002 * creation mechanism to verify that this information is correct.
2003 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002004 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002005 * is optional (import, copy). In case of a volatile key, assign it the
2006 * volatile key identifier associated to the slot returned to contain its
2007 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002008
2009 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002010 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2011 {
2012#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2013 slot->attr.id = volatile_key_id;
2014#else
2015 slot->attr.id.key_id = volatile_key_id;
2016#endif
2017 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002018
Gilles Peskine91e8c332019-08-02 19:19:39 +02002019 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002020 * external-only flags, query `attributes`. Thanks to the check
2021 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002022 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002023 * may have set. */
2024 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002025
Gilles Peskinecbaff462019-07-12 23:46:04 +02002026#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002027 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002028 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002029 * create the key file in internal storage, create the
2030 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002031 * persistent data. This is done by starting a transaction that will
2032 * encompass these three actions.
2033 * For registering a volatile key, we just need to find an appropriate
2034 * slot number inside the SE. Since the key is designated volatile, creating
2035 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002036 /* The first thing to do is to find a slot number for the new key.
2037 * We save the slot number in persistent storage as part of the
2038 * transaction data. It will be needed to recover if the power
2039 * fails during the key creation process, to clean up on the secure
2040 * element side after restarting. Obtaining a slot number from the
2041 * secure element driver updates its persistent state, but we do not yet
2042 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002043 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002044 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002045 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002046 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002047 &slot->data.se.slot_number );
2048 if( status != PSA_SUCCESS )
2049 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002050
2051 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002052 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002053 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2054 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2055 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2056 psa_crypto_transaction.key.id = slot->attr.id;
2057 status = psa_crypto_save_transaction( );
2058 if( status != PSA_SUCCESS )
2059 {
2060 (void) psa_crypto_stop_transaction( );
2061 return( status );
2062 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002063 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002064 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002065
2066 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2067 {
2068 /* Key registration only makes sense with a secure element. */
2069 return( PSA_ERROR_INVALID_ARGUMENT );
2070 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002071#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2072
Ronald Cronc4d1b512020-07-31 11:26:37 +02002073 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002074}
Gilles Peskine4747d192019-04-17 15:05:45 +02002075
2076/** Finalize the creation of a key once its key material has been set.
2077 *
2078 * This entails writing the key to persistent storage.
2079 *
2080 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002081 * See the documentation of psa_start_key_creation() for the intended use
2082 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002083 *
Ronald Cron5c522922020-11-14 16:35:34 +01002084 * If the finalization succeeds, the function unlocks the key slot (it was
2085 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2086 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002087 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002088 * \param[in,out] slot Pointer to the slot with key material.
2089 * \param[in] driver The secure element driver for the key,
2090 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002091 * \param[out] key On success, identifier of the key. Note that the
2092 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002093 *
2094 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002095 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01002096 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2097 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2098 * \retval #PSA_ERROR_ALREADY_EXISTS
2099 * \retval #PSA_ERROR_DATA_INVALID
2100 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01002101 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01002102 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002103 * \return If this function fails, the key slot is an invalid state.
2104 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002105 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002106static psa_status_t psa_finish_key_creation(
2107 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002108 psa_se_drv_table_entry_t *driver,
2109 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002110{
2111 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002112 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002113 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002114
2115#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002116 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002117 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002118#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2119 if( driver != NULL )
2120 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002121 psa_se_key_data_storage_t data;
2122#if defined(static_assert)
2123 static_assert( sizeof( slot->data.se.slot_number ) ==
2124 sizeof( data.slot_number ),
2125 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002126#endif
2127 memcpy( &data.slot_number, &slot->data.se.slot_number,
2128 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002129 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002130 (uint8_t*) &data,
2131 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002132 }
2133 else
2134#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2135 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002136 /* Key material is saved in export representation in the slot, so
2137 * just pass the slot buffer for storage. */
2138 status = psa_save_persistent_key( &slot->attr,
2139 slot->data.key.data,
2140 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002141 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002142 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002143#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2144
Gilles Peskinecbaff462019-07-12 23:46:04 +02002145#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002146 /* Finish the transaction for a key creation. This does not
2147 * happen when registering an existing key. Detect this case
2148 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002149 * creation of a persistent key in a secure element requires a transaction,
2150 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002151 if( driver != NULL &&
2152 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002153 {
2154 status = psa_save_se_persistent_data( driver );
2155 if( status != PSA_SUCCESS )
2156 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002157 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002158 return( status );
2159 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002160 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002161 }
2162#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2163
Ronald Cron50972942020-11-14 11:28:25 +01002164 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002165 {
2166 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002167 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002168 if( status != PSA_SUCCESS )
2169 *key = MBEDTLS_SVC_KEY_ID_INIT;
2170 }
Ronald Cron50972942020-11-14 11:28:25 +01002171
Gilles Peskine4747d192019-04-17 15:05:45 +02002172 return( status );
2173}
2174
2175/** Abort the creation of a key.
2176 *
2177 * You may call this function after calling psa_start_key_creation(),
2178 * or after psa_finish_key_creation() fails. In other circumstances, this
2179 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002180 * See the documentation of psa_start_key_creation() for the intended use
2181 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002182 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002183 * \param[in,out] slot Pointer to the slot with key material.
2184 * \param[in] driver The secure element driver for the key,
2185 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002186 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002187static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002188 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002189{
Gilles Peskine011e4282019-06-26 18:34:38 +02002190 (void) driver;
2191
Gilles Peskine4747d192019-04-17 15:05:45 +02002192 if( slot == NULL )
2193 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002194
2195#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002196 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002197 * element, and the failure happened later (when saving metadata
2198 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002199 * element.
2200 * https://github.com/ARMmbed/mbed-crypto/issues/217
2201 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002202
Gilles Peskined7729582019-08-05 15:55:54 +02002203 /* Abort the ongoing transaction if any (there may not be one if
2204 * the creation process failed before starting one, or if the
2205 * key creation is a registration of a key in a secure element).
2206 * Earlier functions must already have done what it takes to undo any
2207 * partial creation. All that's left is to update the transaction data
2208 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002209 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002210#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2211
Gilles Peskine4747d192019-04-17 15:05:45 +02002212 psa_wipe_key_slot( slot );
2213}
2214
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002215/** Validate optional attributes during key creation.
2216 *
2217 * Some key attributes are optional during key creation. If they are
2218 * specified in the attributes structure, check that they are consistent
2219 * with the data in the slot.
2220 *
2221 * This function should be called near the end of key creation, after
2222 * the slot in memory is fully populated but before saving persistent data.
2223 */
2224static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002225 const psa_key_slot_t *slot,
2226 const psa_key_attributes_t *attributes )
2227{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002228 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002229 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002230 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002231 return( PSA_ERROR_INVALID_ARGUMENT );
2232 }
2233
2234 if( attributes->domain_parameters_size != 0 )
2235 {
John Durkop0e005192020-10-31 22:06:54 -07002236#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2237 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002238 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002239 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002240 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002241 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002243
Steven Cooreman7f391872020-07-30 14:57:44 +02002244 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002245 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002246 slot->data.key.data,
2247 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002248 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002249 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002250 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002251
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002252 mbedtls_mpi_init( &actual );
2253 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002254 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002255 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002256 mbedtls_rsa_free( rsa );
2257 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002258 if( ret != 0 )
2259 goto rsa_exit;
2260 ret = mbedtls_mpi_read_binary( &required,
2261 attributes->domain_parameters,
2262 attributes->domain_parameters_size );
2263 if( ret != 0 )
2264 goto rsa_exit;
2265 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2266 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2267 rsa_exit:
2268 mbedtls_mpi_free( &actual );
2269 mbedtls_mpi_free( &required );
2270 if( ret != 0)
2271 return( mbedtls_to_psa_error( ret ) );
2272 }
2273 else
John Durkop9814fa22020-11-04 12:28:15 -08002274#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2275 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002276 {
2277 return( PSA_ERROR_INVALID_ARGUMENT );
2278 }
2279 }
2280
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002281 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002282 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002283 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002284 return( PSA_ERROR_INVALID_ARGUMENT );
2285 }
2286
2287 return( PSA_SUCCESS );
2288}
2289
Gilles Peskine4747d192019-04-17 15:05:45 +02002290psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002291 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002292 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002293 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002294{
2295 psa_status_t status;
2296 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002297 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002298
Ronald Cron81709fc2020-11-14 12:10:32 +01002299 *key = MBEDTLS_SVC_KEY_ID_INIT;
2300
Gilles Peskine0f84d622019-09-12 19:03:13 +02002301 /* Reject zero-length symmetric keys (including raw data key objects).
2302 * This also rejects any key which might be encoded as an empty string,
2303 * which is never valid. */
2304 if( data_length == 0 )
2305 return( PSA_ERROR_INVALID_ARGUMENT );
2306
Gilles Peskinedf179142019-07-15 22:02:14 +02002307 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002308 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002309 if( status != PSA_SUCCESS )
2310 goto exit;
2311
Gilles Peskine5d309672019-07-12 23:47:28 +02002312#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2313 if( driver != NULL )
2314 {
2315 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002316 /* The driver should set the number of key bits, however in
2317 * case it doesn't, we initialize bits to an invalid value. */
2318 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002319 if( drv->key_management == NULL ||
2320 drv->key_management->p_import == NULL )
2321 {
2322 status = PSA_ERROR_NOT_SUPPORTED;
2323 goto exit;
2324 }
2325 status = drv->key_management->p_import(
2326 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002327 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002328 &bits );
2329 if( status != PSA_SUCCESS )
2330 goto exit;
2331 if( bits > PSA_MAX_KEY_BITS )
2332 {
2333 status = PSA_ERROR_NOT_SUPPORTED;
2334 goto exit;
2335 }
2336 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002337 }
2338 else
2339#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2340 {
2341 status = psa_import_key_into_slot( slot, data, data_length );
2342 if( status != PSA_SUCCESS )
2343 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002344 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002345 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002346 if( status != PSA_SUCCESS )
2347 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002348
Ronald Cron81709fc2020-11-14 12:10:32 +01002349 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002350exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002351 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002352 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002353
Gilles Peskine4747d192019-04-17 15:05:45 +02002354 return( status );
2355}
2356
Gilles Peskined7729582019-08-05 15:55:54 +02002357#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2358psa_status_t mbedtls_psa_register_se_key(
2359 const psa_key_attributes_t *attributes )
2360{
2361 psa_status_t status;
2362 psa_key_slot_t *slot = NULL;
2363 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002365
2366 /* Leaving attributes unspecified is not currently supported.
2367 * It could make sense to query the key type and size from the
2368 * secure element, but not all secure elements support this
2369 * and the driver HAL doesn't currently support it. */
2370 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2371 return( PSA_ERROR_NOT_SUPPORTED );
2372 if( psa_get_key_bits( attributes ) == 0 )
2373 return( PSA_ERROR_NOT_SUPPORTED );
2374
2375 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002376 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002377 if( status != PSA_SUCCESS )
2378 goto exit;
2379
Ronald Cron81709fc2020-11-14 12:10:32 +01002380 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002381
2382exit:
2383 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002384 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002385
Gilles Peskined7729582019-08-05 15:55:54 +02002386 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002387 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002388 return( status );
2389}
2390#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2391
Gilles Peskinef603c712019-01-19 13:40:11 +01002392static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002393 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002394{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002395 psa_status_t status = psa_copy_key_material_into_slot( target,
2396 source->data.key.data,
2397 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002398 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002399 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002400
Steven Cooreman398aee52020-10-13 14:35:45 +02002401 target->attr.type = source->attr.type;
2402 target->attr.bits = source->attr.bits;
2403
2404 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002405}
2406
Ronald Croncf56a0a2020-08-04 09:51:30 +02002407psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002408 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002409 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002410{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002412 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002413 psa_key_slot_t *source_slot = NULL;
2414 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002415 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002416 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002417
Ronald Cron81709fc2020-11-14 12:10:32 +01002418 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2419
Ronald Cron5c522922020-11-14 16:35:34 +01002420 status = psa_get_and_lock_transparent_key_slot_with_policy(
2421 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002422 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002423 goto exit;
2424
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002425 status = psa_validate_optional_attributes( source_slot,
2426 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002427 if( status != PSA_SUCCESS )
2428 goto exit;
2429
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002430 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002431 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002432 if( status != PSA_SUCCESS )
2433 goto exit;
2434
Ronald Cron81709fc2020-11-14 12:10:32 +01002435 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2436 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002437 if( status != PSA_SUCCESS )
2438 goto exit;
2439
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002440#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2441 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002442 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002443 /* Copying to a secure element is not implemented yet. */
2444 status = PSA_ERROR_NOT_SUPPORTED;
2445 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002446 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002447#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002448
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002449 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002450 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002451 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002452
Ronald Cron81709fc2020-11-14 12:10:32 +01002453 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002454exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002455 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002456 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002457
Ronald Cron5c522922020-11-14 16:35:34 +01002458 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002459
Ronald Cron5c522922020-11-14 16:35:34 +01002460 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002461}
2462
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002463
2464
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002465/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002466/* Message digests */
2467/****************************************************************/
2468
John Durkop07cc04a2020-11-16 22:08:34 -08002469#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002470 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2471 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002472 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002473static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002474{
2475 switch( alg )
2476 {
2477#if defined(MBEDTLS_MD2_C)
2478 case PSA_ALG_MD2:
2479 return( &mbedtls_md2_info );
2480#endif
2481#if defined(MBEDTLS_MD4_C)
2482 case PSA_ALG_MD4:
2483 return( &mbedtls_md4_info );
2484#endif
2485#if defined(MBEDTLS_MD5_C)
2486 case PSA_ALG_MD5:
2487 return( &mbedtls_md5_info );
2488#endif
2489#if defined(MBEDTLS_RIPEMD160_C)
2490 case PSA_ALG_RIPEMD160:
2491 return( &mbedtls_ripemd160_info );
2492#endif
2493#if defined(MBEDTLS_SHA1_C)
2494 case PSA_ALG_SHA_1:
2495 return( &mbedtls_sha1_info );
2496#endif
2497#if defined(MBEDTLS_SHA256_C)
2498 case PSA_ALG_SHA_224:
2499 return( &mbedtls_sha224_info );
2500 case PSA_ALG_SHA_256:
2501 return( &mbedtls_sha256_info );
2502#endif
2503#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002504#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002505 case PSA_ALG_SHA_384:
2506 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002507#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002508 case PSA_ALG_SHA_512:
2509 return( &mbedtls_sha512_info );
2510#endif
2511 default:
2512 return( NULL );
2513 }
2514}
John Durkop07cc04a2020-11-16 22:08:34 -08002515#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002516 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2517 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2518 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002519
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2521{
2522 switch( operation->alg )
2523 {
Gilles Peskine81736312018-06-26 15:04:31 +02002524 case 0:
2525 /* The object has (apparently) been initialized but it is not
2526 * in use. It's ok to call abort on such an object, and there's
2527 * nothing to do. */
2528 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002529#if defined(MBEDTLS_MD2_C)
2530 case PSA_ALG_MD2:
2531 mbedtls_md2_free( &operation->ctx.md2 );
2532 break;
2533#endif
2534#if defined(MBEDTLS_MD4_C)
2535 case PSA_ALG_MD4:
2536 mbedtls_md4_free( &operation->ctx.md4 );
2537 break;
2538#endif
2539#if defined(MBEDTLS_MD5_C)
2540 case PSA_ALG_MD5:
2541 mbedtls_md5_free( &operation->ctx.md5 );
2542 break;
2543#endif
2544#if defined(MBEDTLS_RIPEMD160_C)
2545 case PSA_ALG_RIPEMD160:
2546 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2547 break;
2548#endif
2549#if defined(MBEDTLS_SHA1_C)
2550 case PSA_ALG_SHA_1:
2551 mbedtls_sha1_free( &operation->ctx.sha1 );
2552 break;
2553#endif
2554#if defined(MBEDTLS_SHA256_C)
2555 case PSA_ALG_SHA_224:
2556 case PSA_ALG_SHA_256:
2557 mbedtls_sha256_free( &operation->ctx.sha256 );
2558 break;
2559#endif
2560#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002561#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002563#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564 case PSA_ALG_SHA_512:
2565 mbedtls_sha512_free( &operation->ctx.sha512 );
2566 break;
2567#endif
2568 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002569 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002570 }
2571 operation->alg = 0;
2572 return( PSA_SUCCESS );
2573}
2574
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002575psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 psa_algorithm_t alg )
2577{
Janos Follath24eed8d2019-11-22 13:21:35 +00002578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002579
2580 /* A context must be freshly initialized before it can be set up. */
2581 if( operation->alg != 0 )
2582 {
2583 return( PSA_ERROR_BAD_STATE );
2584 }
2585
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 switch( alg )
2587 {
2588#if defined(MBEDTLS_MD2_C)
2589 case PSA_ALG_MD2:
2590 mbedtls_md2_init( &operation->ctx.md2 );
2591 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2592 break;
2593#endif
2594#if defined(MBEDTLS_MD4_C)
2595 case PSA_ALG_MD4:
2596 mbedtls_md4_init( &operation->ctx.md4 );
2597 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2598 break;
2599#endif
2600#if defined(MBEDTLS_MD5_C)
2601 case PSA_ALG_MD5:
2602 mbedtls_md5_init( &operation->ctx.md5 );
2603 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2604 break;
2605#endif
2606#if defined(MBEDTLS_RIPEMD160_C)
2607 case PSA_ALG_RIPEMD160:
2608 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2609 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2610 break;
2611#endif
2612#if defined(MBEDTLS_SHA1_C)
2613 case PSA_ALG_SHA_1:
2614 mbedtls_sha1_init( &operation->ctx.sha1 );
2615 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2616 break;
2617#endif
2618#if defined(MBEDTLS_SHA256_C)
2619 case PSA_ALG_SHA_224:
2620 mbedtls_sha256_init( &operation->ctx.sha256 );
2621 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2622 break;
2623 case PSA_ALG_SHA_256:
2624 mbedtls_sha256_init( &operation->ctx.sha256 );
2625 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2626 break;
2627#endif
2628#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002629#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002630 case PSA_ALG_SHA_384:
2631 mbedtls_sha512_init( &operation->ctx.sha512 );
2632 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2633 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002634#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002635 case PSA_ALG_SHA_512:
2636 mbedtls_sha512_init( &operation->ctx.sha512 );
2637 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2638 break;
2639#endif
2640 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002641 return( PSA_ALG_IS_HASH( alg ) ?
2642 PSA_ERROR_NOT_SUPPORTED :
2643 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002644 }
2645 if( ret == 0 )
2646 operation->alg = alg;
2647 else
2648 psa_hash_abort( operation );
2649 return( mbedtls_to_psa_error( ret ) );
2650}
2651
2652psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2653 const uint8_t *input,
2654 size_t input_length )
2655{
Janos Follath24eed8d2019-11-22 13:21:35 +00002656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002657
2658 /* Don't require hash implementations to behave correctly on a
2659 * zero-length input, which may have an invalid pointer. */
2660 if( input_length == 0 )
2661 return( PSA_SUCCESS );
2662
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663 switch( operation->alg )
2664 {
2665#if defined(MBEDTLS_MD2_C)
2666 case PSA_ALG_MD2:
2667 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2668 input, input_length );
2669 break;
2670#endif
2671#if defined(MBEDTLS_MD4_C)
2672 case PSA_ALG_MD4:
2673 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2674 input, input_length );
2675 break;
2676#endif
2677#if defined(MBEDTLS_MD5_C)
2678 case PSA_ALG_MD5:
2679 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2680 input, input_length );
2681 break;
2682#endif
2683#if defined(MBEDTLS_RIPEMD160_C)
2684 case PSA_ALG_RIPEMD160:
2685 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2686 input, input_length );
2687 break;
2688#endif
2689#if defined(MBEDTLS_SHA1_C)
2690 case PSA_ALG_SHA_1:
2691 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2692 input, input_length );
2693 break;
2694#endif
2695#if defined(MBEDTLS_SHA256_C)
2696 case PSA_ALG_SHA_224:
2697 case PSA_ALG_SHA_256:
2698 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2699 input, input_length );
2700 break;
2701#endif
2702#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002703#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002704 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002705#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002706 case PSA_ALG_SHA_512:
2707 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2708 input, input_length );
2709 break;
2710#endif
2711 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002712 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002713 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002714
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715 if( ret != 0 )
2716 psa_hash_abort( operation );
2717 return( mbedtls_to_psa_error( ret ) );
2718}
2719
2720psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2721 uint8_t *hash,
2722 size_t hash_size,
2723 size_t *hash_length )
2724{
itayzafrir40835d42018-08-02 13:14:17 +03002725 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002727 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002728
2729 /* Fill the output buffer with something that isn't a valid hash
2730 * (barring an attack on the hash and deliberately-crafted input),
2731 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002732 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002733 /* If hash_size is 0 then hash may be NULL and then the
2734 * call to memset would have undefined behavior. */
2735 if( hash_size != 0 )
2736 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002737
2738 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002739 {
2740 status = PSA_ERROR_BUFFER_TOO_SMALL;
2741 goto exit;
2742 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002743
2744 switch( operation->alg )
2745 {
2746#if defined(MBEDTLS_MD2_C)
2747 case PSA_ALG_MD2:
2748 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2749 break;
2750#endif
2751#if defined(MBEDTLS_MD4_C)
2752 case PSA_ALG_MD4:
2753 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2754 break;
2755#endif
2756#if defined(MBEDTLS_MD5_C)
2757 case PSA_ALG_MD5:
2758 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2759 break;
2760#endif
2761#if defined(MBEDTLS_RIPEMD160_C)
2762 case PSA_ALG_RIPEMD160:
2763 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2764 break;
2765#endif
2766#if defined(MBEDTLS_SHA1_C)
2767 case PSA_ALG_SHA_1:
2768 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2769 break;
2770#endif
2771#if defined(MBEDTLS_SHA256_C)
2772 case PSA_ALG_SHA_224:
2773 case PSA_ALG_SHA_256:
2774 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2775 break;
2776#endif
2777#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002778#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002779 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002780#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002781 case PSA_ALG_SHA_512:
2782 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2783 break;
2784#endif
2785 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002786 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 }
itayzafrir40835d42018-08-02 13:14:17 +03002788 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789
itayzafrir40835d42018-08-02 13:14:17 +03002790exit:
2791 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002792 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002793 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002794 return( psa_hash_abort( operation ) );
2795 }
2796 else
2797 {
2798 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002799 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002800 }
2801}
2802
Gilles Peskine2d277862018-06-18 15:41:12 +02002803psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2804 const uint8_t *hash,
2805 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002806{
2807 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2808 size_t actual_hash_length;
2809 psa_status_t status = psa_hash_finish( operation,
2810 actual_hash, sizeof( actual_hash ),
2811 &actual_hash_length );
2812 if( status != PSA_SUCCESS )
2813 return( status );
2814 if( actual_hash_length != hash_length )
2815 return( PSA_ERROR_INVALID_SIGNATURE );
2816 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2817 return( PSA_ERROR_INVALID_SIGNATURE );
2818 return( PSA_SUCCESS );
2819}
2820
Gilles Peskine0a749c82019-11-28 19:33:58 +01002821psa_status_t psa_hash_compute( psa_algorithm_t alg,
2822 const uint8_t *input, size_t input_length,
2823 uint8_t *hash, size_t hash_size,
2824 size_t *hash_length )
2825{
2826 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2828
2829 *hash_length = hash_size;
2830 status = psa_hash_setup( &operation, alg );
2831 if( status != PSA_SUCCESS )
2832 goto exit;
2833 status = psa_hash_update( &operation, input, input_length );
2834 if( status != PSA_SUCCESS )
2835 goto exit;
2836 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2837 if( status != PSA_SUCCESS )
2838 goto exit;
2839
2840exit:
2841 if( status == PSA_SUCCESS )
2842 status = psa_hash_abort( &operation );
2843 else
2844 psa_hash_abort( &operation );
2845 return( status );
2846}
2847
2848psa_status_t psa_hash_compare( psa_algorithm_t alg,
2849 const uint8_t *input, size_t input_length,
2850 const uint8_t *hash, size_t hash_length )
2851{
2852 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2854
2855 status = psa_hash_setup( &operation, alg );
2856 if( status != PSA_SUCCESS )
2857 goto exit;
2858 status = psa_hash_update( &operation, input, input_length );
2859 if( status != PSA_SUCCESS )
2860 goto exit;
2861 status = psa_hash_verify( &operation, hash, hash_length );
2862 if( status != PSA_SUCCESS )
2863 goto exit;
2864
2865exit:
2866 if( status == PSA_SUCCESS )
2867 status = psa_hash_abort( &operation );
2868 else
2869 psa_hash_abort( &operation );
2870 return( status );
2871}
2872
Gilles Peskineeb35d782019-01-22 17:56:16 +01002873psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2874 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002875{
2876 if( target_operation->alg != 0 )
2877 return( PSA_ERROR_BAD_STATE );
2878
2879 switch( source_operation->alg )
2880 {
2881 case 0:
2882 return( PSA_ERROR_BAD_STATE );
2883#if defined(MBEDTLS_MD2_C)
2884 case PSA_ALG_MD2:
2885 mbedtls_md2_clone( &target_operation->ctx.md2,
2886 &source_operation->ctx.md2 );
2887 break;
2888#endif
2889#if defined(MBEDTLS_MD4_C)
2890 case PSA_ALG_MD4:
2891 mbedtls_md4_clone( &target_operation->ctx.md4,
2892 &source_operation->ctx.md4 );
2893 break;
2894#endif
2895#if defined(MBEDTLS_MD5_C)
2896 case PSA_ALG_MD5:
2897 mbedtls_md5_clone( &target_operation->ctx.md5,
2898 &source_operation->ctx.md5 );
2899 break;
2900#endif
2901#if defined(MBEDTLS_RIPEMD160_C)
2902 case PSA_ALG_RIPEMD160:
2903 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2904 &source_operation->ctx.ripemd160 );
2905 break;
2906#endif
2907#if defined(MBEDTLS_SHA1_C)
2908 case PSA_ALG_SHA_1:
2909 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2910 &source_operation->ctx.sha1 );
2911 break;
2912#endif
2913#if defined(MBEDTLS_SHA256_C)
2914 case PSA_ALG_SHA_224:
2915 case PSA_ALG_SHA_256:
2916 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2917 &source_operation->ctx.sha256 );
2918 break;
2919#endif
2920#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002921#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002922 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002923#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002924 case PSA_ALG_SHA_512:
2925 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2926 &source_operation->ctx.sha512 );
2927 break;
2928#endif
2929 default:
2930 return( PSA_ERROR_NOT_SUPPORTED );
2931 }
2932
2933 target_operation->alg = source_operation->alg;
2934 return( PSA_SUCCESS );
2935}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002936
2937
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002938/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939/* MAC */
2940/****************************************************************/
2941
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002942static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943 psa_algorithm_t alg,
2944 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002945 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002946 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002949 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002951 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002952 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002953
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2955 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002956 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002958 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002959 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960 mode = MBEDTLS_MODE_STREAM;
2961 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962 case PSA_ALG_CTR:
2963 mode = MBEDTLS_MODE_CTR;
2964 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002965 case PSA_ALG_CFB:
2966 mode = MBEDTLS_MODE_CFB;
2967 break;
2968 case PSA_ALG_OFB:
2969 mode = MBEDTLS_MODE_OFB;
2970 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002971 case PSA_ALG_ECB_NO_PADDING:
2972 mode = MBEDTLS_MODE_ECB;
2973 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002974 case PSA_ALG_CBC_NO_PADDING:
2975 mode = MBEDTLS_MODE_CBC;
2976 break;
2977 case PSA_ALG_CBC_PKCS7:
2978 mode = MBEDTLS_MODE_CBC;
2979 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002980 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981 mode = MBEDTLS_MODE_CCM;
2982 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002983 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984 mode = MBEDTLS_MODE_GCM;
2985 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002986 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2987 mode = MBEDTLS_MODE_CHACHAPOLY;
2988 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989 default:
2990 return( NULL );
2991 }
2992 }
2993 else if( alg == PSA_ALG_CMAC )
2994 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002995 else
2996 return( NULL );
2997
2998 switch( key_type )
2999 {
3000 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003001 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002 break;
3003 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003004 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3005 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003007 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003009 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003010 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3011 * but two-key Triple-DES is functionally three-key Triple-DES
3012 * with K1=K3, so that's how we present it to mbedtls. */
3013 if( key_bits == 128 )
3014 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015 break;
3016 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003017 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018 break;
3019 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003020 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003022 case PSA_KEY_TYPE_CHACHA20:
3023 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3024 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003025 default:
3026 return( NULL );
3027 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003028 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003029 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030
Jaeden Amero23bbb752018-06-26 14:16:54 +01003031 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3032 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033}
3034
John Durkop6ba40d12020-11-10 08:50:04 -08003035#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003036static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003037{
Gilles Peskine2d277862018-06-18 15:41:12 +02003038 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003039 {
3040 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003041 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003042 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003043 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003044 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003045 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003046 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003047 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003048 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003049 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003050 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003051 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003052 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003053 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003054 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003055 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003056 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003057 return( 128 );
3058 default:
3059 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003060 }
3061}
John Durkop07cc04a2020-11-16 22:08:34 -08003062#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003063
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003064/* Initialize the MAC operation structure. Once this function has been
3065 * called, psa_mac_abort can run and will do the right thing. */
3066static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3067 psa_algorithm_t alg )
3068{
3069 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3070
3071 operation->alg = alg;
3072 operation->key_set = 0;
3073 operation->iv_set = 0;
3074 operation->iv_required = 0;
3075 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003076 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003077
3078#if defined(MBEDTLS_CMAC_C)
3079 if( alg == PSA_ALG_CMAC )
3080 {
3081 operation->iv_required = 0;
3082 mbedtls_cipher_init( &operation->ctx.cmac );
3083 status = PSA_SUCCESS;
3084 }
3085 else
3086#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003088 if( PSA_ALG_IS_HMAC( operation->alg ) )
3089 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003090 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3091 operation->ctx.hmac.hash_ctx.alg = 0;
3092 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003093 }
3094 else
John Durkopd0321952020-10-29 21:37:36 -07003095#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003096 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003097 if( ! PSA_ALG_IS_MAC( alg ) )
3098 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003099 }
3100
3101 if( status != PSA_SUCCESS )
3102 memset( operation, 0, sizeof( *operation ) );
3103 return( status );
3104}
3105
John Durkop6ba40d12020-11-10 08:50:04 -08003106#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003107static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3108{
Gilles Peskine3f108122018-12-07 18:14:53 +01003109 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003110 return( psa_hash_abort( &hmac->hash_ctx ) );
3111}
John Durkop6ba40d12020-11-10 08:50:04 -08003112#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003113
Gilles Peskine8c9def32018-02-08 10:02:12 +01003114psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3115{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003116 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003118 /* The object has (apparently) been initialized but it is not
3119 * in use. It's ok to call abort on such an object, and there's
3120 * nothing to do. */
3121 return( PSA_SUCCESS );
3122 }
3123 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003125 if( operation->alg == PSA_ALG_CMAC )
3126 {
3127 mbedtls_cipher_free( &operation->ctx.cmac );
3128 }
3129 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003131#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003132 if( PSA_ALG_IS_HMAC( operation->alg ) )
3133 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003134 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003135 }
3136 else
John Durkopd0321952020-10-29 21:37:36 -07003137#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003138 {
3139 /* Sanity check (shouldn't happen: operation->alg should
3140 * always have been initialized to a valid value). */
3141 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 }
Moran Peker41deec42018-04-04 15:43:05 +03003143
Gilles Peskine8c9def32018-02-08 10:02:12 +01003144 operation->alg = 0;
3145 operation->key_set = 0;
3146 operation->iv_set = 0;
3147 operation->iv_required = 0;
3148 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003149 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003150
Gilles Peskine8c9def32018-02-08 10:02:12 +01003151 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003152
3153bad_state:
3154 /* If abort is called on an uninitialized object, we can't trust
3155 * anything. Wipe the object in case it contains confidential data.
3156 * This may result in a memory leak if a pointer gets overwritten,
3157 * but it's too late to do anything about this. */
3158 memset( operation, 0, sizeof( *operation ) );
3159 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003160}
3161
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003162#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003163static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003164 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003165 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003166 const mbedtls_cipher_info_t *cipher_info )
3167{
Janos Follath24eed8d2019-11-22 13:21:35 +00003168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003169
3170 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003171
3172 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3173 if( ret != 0 )
3174 return( ret );
3175
3176 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003177 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003178 key_bits );
3179 return( ret );
3180}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003181#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003182
John Durkop6ba40d12020-11-10 08:50:04 -08003183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003184static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3185 const uint8_t *key,
3186 size_t key_length,
3187 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003188{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003189 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003190 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003191 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003192 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003193 psa_status_t status;
3194
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003195 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3196 * overflow below. This should never trigger if the hash algorithm
3197 * is implemented correctly. */
3198 /* The size checks against the ipad and opad buffers cannot be written
3199 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3200 * because that triggers -Wlogical-op on GCC 7.3. */
3201 if( block_size > sizeof( ipad ) )
3202 return( PSA_ERROR_NOT_SUPPORTED );
3203 if( block_size > sizeof( hmac->opad ) )
3204 return( PSA_ERROR_NOT_SUPPORTED );
3205 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003206 return( PSA_ERROR_NOT_SUPPORTED );
3207
Gilles Peskined223b522018-06-11 18:12:58 +02003208 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003209 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003210 status = psa_hash_compute( hash_alg, key, key_length,
3211 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003212 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003213 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003214 }
Gilles Peskine96889972018-07-12 17:07:03 +02003215 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3216 * but it is permitted. It is common when HMAC is used in HKDF, for
3217 * example. Don't call `memcpy` in the 0-length because `key` could be
3218 * an invalid pointer which would make the behavior undefined. */
3219 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003220 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003221
Gilles Peskined223b522018-06-11 18:12:58 +02003222 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3223 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003224 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003225 ipad[i] ^= 0x36;
3226 memset( ipad + key_length, 0x36, block_size - key_length );
3227
3228 /* Copy the key material from ipad to opad, flipping the requisite bits,
3229 * and filling the rest of opad with the requisite constant. */
3230 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003231 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3232 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003233
Gilles Peskine01126fa2018-07-12 17:04:55 +02003234 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003235 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003236 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003237
Gilles Peskine01126fa2018-07-12 17:04:55 +02003238 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003239
3240cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003241 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003242
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243 return( status );
3244}
John Durkop6ba40d12020-11-10 08:50:04 -08003245#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003246
Gilles Peskine89167cb2018-07-08 20:12:23 +02003247static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003248 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003249 psa_algorithm_t alg,
3250 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003251{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003253 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003254 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003255 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003256 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003257 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003258 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003259 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003261 /* A context must be freshly initialized before it can be set up. */
3262 if( operation->alg != 0 )
3263 {
3264 return( PSA_ERROR_BAD_STATE );
3265 }
3266
Gilles Peskined911eb72018-08-14 15:18:45 +02003267 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003268 if( status != PSA_SUCCESS )
3269 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003270 if( is_sign )
3271 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003272
Ronald Cron5c522922020-11-14 16:35:34 +01003273 status = psa_get_and_lock_transparent_key_slot_with_policy(
3274 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003275 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003276 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003277 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003278
Gilles Peskine8c9def32018-02-08 10:02:12 +01003279#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003280 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003281 {
3282 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003283 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003284 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003286 if( cipher_info == NULL )
3287 {
3288 status = PSA_ERROR_NOT_SUPPORTED;
3289 goto exit;
3290 }
3291 operation->mac_size = cipher_info->block_size;
3292 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3293 status = mbedtls_to_psa_error( ret );
3294 }
3295 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003296#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003297#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003298 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003299 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003300 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003301 if( hash_alg == 0 )
3302 {
3303 status = PSA_ERROR_NOT_SUPPORTED;
3304 goto exit;
3305 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003306
3307 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3308 /* Sanity check. This shouldn't fail on a valid configuration. */
3309 if( operation->mac_size == 0 ||
3310 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3311 {
3312 status = PSA_ERROR_NOT_SUPPORTED;
3313 goto exit;
3314 }
3315
Gilles Peskine8e338702019-07-30 20:06:31 +02003316 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003317 {
3318 status = PSA_ERROR_INVALID_ARGUMENT;
3319 goto exit;
3320 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003321
Gilles Peskine01126fa2018-07-12 17:04:55 +02003322 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003323 slot->data.key.data,
3324 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003325 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003326 }
3327 else
John Durkopd0321952020-10-29 21:37:36 -07003328#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003329 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003330 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003331 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003332 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003333
Gilles Peskined911eb72018-08-14 15:18:45 +02003334 if( truncated == 0 )
3335 {
3336 /* The "normal" case: untruncated algorithm. Nothing to do. */
3337 }
3338 else if( truncated < 4 )
3339 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003340 /* A very short MAC is too short for security since it can be
3341 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3342 * so we make this our minimum, even though 32 bits is still
3343 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003344 status = PSA_ERROR_NOT_SUPPORTED;
3345 }
3346 else if( truncated > operation->mac_size )
3347 {
3348 /* It's impossible to "truncate" to a larger length. */
3349 status = PSA_ERROR_INVALID_ARGUMENT;
3350 }
3351 else
3352 operation->mac_size = truncated;
3353
Gilles Peskinefbfac682018-07-08 20:51:54 +02003354exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003355 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003356 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003357 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003358 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003359 else
3360 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003361 operation->key_set = 1;
3362 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003363
Ronald Cron5c522922020-11-14 16:35:34 +01003364 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003365
Ronald Cron5c522922020-11-14 16:35:34 +01003366 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003367}
3368
Gilles Peskine89167cb2018-07-08 20:12:23 +02003369psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003370 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003371 psa_algorithm_t alg )
3372{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003373 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003374}
3375
3376psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003377 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003378 psa_algorithm_t alg )
3379{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003380 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003381}
3382
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3384 const uint8_t *input,
3385 size_t input_length )
3386{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003387 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003388 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003389 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003390 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003391 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003392 operation->has_input = 1;
3393
Gilles Peskine8c9def32018-02-08 10:02:12 +01003394#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003395 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003396 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003397 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3398 input, input_length );
3399 status = mbedtls_to_psa_error( ret );
3400 }
3401 else
3402#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003403#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003404 if( PSA_ALG_IS_HMAC( operation->alg ) )
3405 {
3406 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3407 input_length );
3408 }
3409 else
John Durkopd0321952020-10-29 21:37:36 -07003410#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003411 {
3412 /* This shouldn't happen if `operation` was initialized by
3413 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003414 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003415 }
3416
Gilles Peskinefbfac682018-07-08 20:51:54 +02003417 if( status != PSA_SUCCESS )
3418 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003419 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003420}
3421
John Durkop6ba40d12020-11-10 08:50:04 -08003422#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003423static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3424 uint8_t *mac,
3425 size_t mac_size )
3426{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003427 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003428 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3429 size_t hash_size = 0;
3430 size_t block_size = psa_get_hash_block_size( hash_alg );
3431 psa_status_t status;
3432
Gilles Peskine01126fa2018-07-12 17:04:55 +02003433 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3434 if( status != PSA_SUCCESS )
3435 return( status );
3436 /* From here on, tmp needs to be wiped. */
3437
3438 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3439 if( status != PSA_SUCCESS )
3440 goto exit;
3441
3442 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3443 if( status != PSA_SUCCESS )
3444 goto exit;
3445
3446 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3447 if( status != PSA_SUCCESS )
3448 goto exit;
3449
Gilles Peskined911eb72018-08-14 15:18:45 +02003450 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3451 if( status != PSA_SUCCESS )
3452 goto exit;
3453
3454 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003455
3456exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003457 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003458 return( status );
3459}
John Durkop6ba40d12020-11-10 08:50:04 -08003460#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003461
mohammad16036df908f2018-04-02 08:34:15 -07003462static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003463 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003464 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003465{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003466 if( ! operation->key_set )
3467 return( PSA_ERROR_BAD_STATE );
3468 if( operation->iv_required && ! operation->iv_set )
3469 return( PSA_ERROR_BAD_STATE );
3470
Gilles Peskine8c9def32018-02-08 10:02:12 +01003471 if( mac_size < operation->mac_size )
3472 return( PSA_ERROR_BUFFER_TOO_SMALL );
3473
Gilles Peskine8c9def32018-02-08 10:02:12 +01003474#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003475 if( operation->alg == PSA_ALG_CMAC )
3476 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003477 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3478 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3479 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003480 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003481 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003482 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003483 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003484 else
3485#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003486#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003487 if( PSA_ALG_IS_HMAC( operation->alg ) )
3488 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003489 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003490 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003491 }
3492 else
John Durkopd0321952020-10-29 21:37:36 -07003493#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003494 {
3495 /* This shouldn't happen if `operation` was initialized by
3496 * a setup function. */
3497 return( PSA_ERROR_BAD_STATE );
3498 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003499}
3500
Gilles Peskineacd4be32018-07-08 19:56:25 +02003501psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3502 uint8_t *mac,
3503 size_t mac_size,
3504 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003505{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003506 psa_status_t status;
3507
Jaeden Amero252ef282019-02-15 14:05:35 +00003508 if( operation->alg == 0 )
3509 {
3510 return( PSA_ERROR_BAD_STATE );
3511 }
3512
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003513 /* Fill the output buffer with something that isn't a valid mac
3514 * (barring an attack on the mac and deliberately-crafted input),
3515 * in case the caller doesn't check the return status properly. */
3516 *mac_length = mac_size;
3517 /* If mac_size is 0 then mac may be NULL and then the
3518 * call to memset would have undefined behavior. */
3519 if( mac_size != 0 )
3520 memset( mac, '!', mac_size );
3521
Gilles Peskine89167cb2018-07-08 20:12:23 +02003522 if( ! operation->is_sign )
3523 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003524 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003525 }
mohammad16036df908f2018-04-02 08:34:15 -07003526
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003527 status = psa_mac_finish_internal( operation, mac, mac_size );
3528
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003529 if( status == PSA_SUCCESS )
3530 {
3531 status = psa_mac_abort( operation );
3532 if( status == PSA_SUCCESS )
3533 *mac_length = operation->mac_size;
3534 else
3535 memset( mac, '!', mac_size );
3536 }
3537 else
3538 psa_mac_abort( operation );
3539 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003540}
3541
Gilles Peskineacd4be32018-07-08 19:56:25 +02003542psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3543 const uint8_t *mac,
3544 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003545{
Gilles Peskine828ed142018-06-18 23:25:51 +02003546 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003547 psa_status_t status;
3548
Jaeden Amero252ef282019-02-15 14:05:35 +00003549 if( operation->alg == 0 )
3550 {
3551 return( PSA_ERROR_BAD_STATE );
3552 }
3553
Gilles Peskine89167cb2018-07-08 20:12:23 +02003554 if( operation->is_sign )
3555 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003556 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003557 }
3558 if( operation->mac_size != mac_length )
3559 {
3560 status = PSA_ERROR_INVALID_SIGNATURE;
3561 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003562 }
mohammad16036df908f2018-04-02 08:34:15 -07003563
3564 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003565 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003566 if( status != PSA_SUCCESS )
3567 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003568
3569 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3570 status = PSA_ERROR_INVALID_SIGNATURE;
3571
3572cleanup:
3573 if( status == PSA_SUCCESS )
3574 status = psa_mac_abort( operation );
3575 else
3576 psa_mac_abort( operation );
3577
Gilles Peskine3f108122018-12-07 18:14:53 +01003578 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003579
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003580 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003581}
3582
3583
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
Gilles Peskine20035e32018-02-03 22:44:14 +01003585/****************************************************************/
3586/* Asymmetric cryptography */
3587/****************************************************************/
3588
John Durkop6ba40d12020-11-10 08:50:04 -08003589#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3590 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003591/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003592 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003593static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3594 size_t hash_length,
3595 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003596{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003597 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003598 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003599 *md_alg = mbedtls_md_get_type( md_info );
3600
3601 /* The Mbed TLS RSA module uses an unsigned int for hash length
3602 * parameters. Validate that it fits so that we don't risk an
3603 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003604#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003605 if( hash_length > UINT_MAX )
3606 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003607#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003608
John Durkop0e005192020-10-31 22:06:54 -07003609#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003610 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3611 * must be correct. */
3612 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3613 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003614 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003615 if( md_info == NULL )
3616 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003617 if( mbedtls_md_get_size( md_info ) != hash_length )
3618 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003619 }
John Durkop0e005192020-10-31 22:06:54 -07003620#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003621
John Durkop0e005192020-10-31 22:06:54 -07003622#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003623 /* PSS requires a hash internally. */
3624 if( PSA_ALG_IS_RSA_PSS( alg ) )
3625 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003626 if( md_info == NULL )
3627 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003628 }
John Durkop0e005192020-10-31 22:06:54 -07003629#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003630
Gilles Peskine61b91d42018-06-08 16:09:36 +02003631 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003632}
3633
Gilles Peskine2b450e32018-06-27 15:42:46 +02003634static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3635 psa_algorithm_t alg,
3636 const uint8_t *hash,
3637 size_t hash_length,
3638 uint8_t *signature,
3639 size_t signature_size,
3640 size_t *signature_length )
3641{
3642 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003644 mbedtls_md_type_t md_alg;
3645
3646 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3647 if( status != PSA_SUCCESS )
3648 return( status );
3649
Gilles Peskine630a18a2018-06-29 17:49:35 +02003650 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003651 return( PSA_ERROR_BUFFER_TOO_SMALL );
3652
John Durkop0e005192020-10-31 22:06:54 -07003653#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003654 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3655 {
3656 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3657 MBEDTLS_MD_NONE );
3658 ret = mbedtls_rsa_pkcs1_sign( rsa,
3659 mbedtls_ctr_drbg_random,
3660 &global_data.ctr_drbg,
3661 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003662 md_alg,
3663 (unsigned int) hash_length,
3664 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003665 signature );
3666 }
3667 else
John Durkop0e005192020-10-31 22:06:54 -07003668#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3669#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003670 if( PSA_ALG_IS_RSA_PSS( alg ) )
3671 {
3672 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3673 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3674 mbedtls_ctr_drbg_random,
3675 &global_data.ctr_drbg,
3676 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003677 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003678 (unsigned int) hash_length,
3679 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003680 signature );
3681 }
3682 else
John Durkop0e005192020-10-31 22:06:54 -07003683#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003684 {
3685 return( PSA_ERROR_INVALID_ARGUMENT );
3686 }
3687
3688 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003689 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003690 return( mbedtls_to_psa_error( ret ) );
3691}
3692
3693static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3694 psa_algorithm_t alg,
3695 const uint8_t *hash,
3696 size_t hash_length,
3697 const uint8_t *signature,
3698 size_t signature_length )
3699{
3700 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003702 mbedtls_md_type_t md_alg;
3703
3704 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3705 if( status != PSA_SUCCESS )
3706 return( status );
3707
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003708 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3709 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003710
John Durkop0e005192020-10-31 22:06:54 -07003711#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003712 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3713 {
3714 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3715 MBEDTLS_MD_NONE );
3716 ret = mbedtls_rsa_pkcs1_verify( rsa,
3717 mbedtls_ctr_drbg_random,
3718 &global_data.ctr_drbg,
3719 MBEDTLS_RSA_PUBLIC,
3720 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003721 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003722 hash,
3723 signature );
3724 }
3725 else
John Durkop0e005192020-10-31 22:06:54 -07003726#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3727#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003728 if( PSA_ALG_IS_RSA_PSS( alg ) )
3729 {
3730 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3731 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3732 mbedtls_ctr_drbg_random,
3733 &global_data.ctr_drbg,
3734 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003735 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003736 (unsigned int) hash_length,
3737 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003738 signature );
3739 }
3740 else
John Durkop0e005192020-10-31 22:06:54 -07003741#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003742 {
3743 return( PSA_ERROR_INVALID_ARGUMENT );
3744 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003745
3746 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3747 * the rest of the signature is invalid". This has little use in
3748 * practice and PSA doesn't report this distinction. */
3749 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3750 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003751 return( mbedtls_to_psa_error( ret ) );
3752}
John Durkop6ba40d12020-11-10 08:50:04 -08003753#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3754 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003755
John Durkop6ba40d12020-11-10 08:50:04 -08003756#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3757 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003758/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3759 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3760 * (even though these functions don't modify it). */
3761static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3762 psa_algorithm_t alg,
3763 const uint8_t *hash,
3764 size_t hash_length,
3765 uint8_t *signature,
3766 size_t signature_size,
3767 size_t *signature_length )
3768{
Janos Follath24eed8d2019-11-22 13:21:35 +00003769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003770 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003771 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003772 mbedtls_mpi_init( &r );
3773 mbedtls_mpi_init( &s );
3774
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003775 if( signature_size < 2 * curve_bytes )
3776 {
3777 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3778 goto cleanup;
3779 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003780
John Durkop0ea39e02020-10-13 19:58:20 -07003781#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003782 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3783 {
3784 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3785 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3786 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003787 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3788 &ecp->d, hash,
3789 hash_length, md_alg,
3790 mbedtls_ctr_drbg_random,
3791 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003792 }
3793 else
John Durkop0ea39e02020-10-13 19:58:20 -07003794#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003795 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003796 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3798 hash, hash_length,
3799 mbedtls_ctr_drbg_random,
3800 &global_data.ctr_drbg ) );
3801 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003802
3803 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3804 signature,
3805 curve_bytes ) );
3806 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3807 signature + curve_bytes,
3808 curve_bytes ) );
3809
3810cleanup:
3811 mbedtls_mpi_free( &r );
3812 mbedtls_mpi_free( &s );
3813 if( ret == 0 )
3814 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003815 return( mbedtls_to_psa_error( ret ) );
3816}
3817
3818static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3819 const uint8_t *hash,
3820 size_t hash_length,
3821 const uint8_t *signature,
3822 size_t signature_length )
3823{
Janos Follath24eed8d2019-11-22 13:21:35 +00003824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003825 mbedtls_mpi r, s;
3826 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3827 mbedtls_mpi_init( &r );
3828 mbedtls_mpi_init( &s );
3829
3830 if( signature_length != 2 * curve_bytes )
3831 return( PSA_ERROR_INVALID_SIGNATURE );
3832
3833 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3834 signature,
3835 curve_bytes ) );
3836 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3837 signature + curve_bytes,
3838 curve_bytes ) );
3839
Steven Cooremanacda8342020-07-24 23:09:52 +02003840 /* Check whether the public part is loaded. If not, load it. */
3841 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3842 {
3843 MBEDTLS_MPI_CHK(
3844 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3845 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3846 }
3847
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003848 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3849 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003850
3851cleanup:
3852 mbedtls_mpi_free( &r );
3853 mbedtls_mpi_free( &s );
3854 return( mbedtls_to_psa_error( ret ) );
3855}
John Durkop6ba40d12020-11-10 08:50:04 -08003856#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3857 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003858
Ronald Croncf56a0a2020-08-04 09:51:30 +02003859psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003860 psa_algorithm_t alg,
3861 const uint8_t *hash,
3862 size_t hash_length,
3863 uint8_t *signature,
3864 size_t signature_size,
3865 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003866{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003868 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003869 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003870
3871 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003872 /* Immediately reject a zero-length signature buffer. This guarantees
3873 * that signature must be a valid pointer. (On the other hand, the hash
3874 * buffer can in principle be empty since it doesn't actually have
3875 * to be a hash.) */
3876 if( signature_size == 0 )
3877 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003878
Ronald Cron5c522922020-11-14 16:35:34 +01003879 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3880 PSA_KEY_USAGE_SIGN_HASH,
3881 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003882 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003883 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003884 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003885 {
3886 status = PSA_ERROR_INVALID_ARGUMENT;
3887 goto exit;
3888 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003889
Steven Cooremancd84cb42020-07-16 20:28:36 +02003890 /* Try any of the available accelerators first */
3891 status = psa_driver_wrapper_sign_hash( slot,
3892 alg,
3893 hash,
3894 hash_length,
3895 signature,
3896 signature_size,
3897 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003898 if( status != PSA_ERROR_NOT_SUPPORTED ||
3899 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003900 goto exit;
3901
Steven Cooreman7a250572020-07-17 16:43:05 +02003902 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003903#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3904 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003905 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003906 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003907 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003908
Steven Cooreman4fed4552020-08-03 14:46:03 +02003909 status = psa_load_rsa_representation( slot->attr.type,
3910 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003911 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003912 &rsa );
3913 if( status != PSA_SUCCESS )
3914 goto exit;
3915
Steven Cooremana2371e52020-07-28 14:30:39 +02003916 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003917 alg,
3918 hash, hash_length,
3919 signature, signature_size,
3920 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003921
Steven Cooremana2371e52020-07-28 14:30:39 +02003922 mbedtls_rsa_free( rsa );
3923 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003924 }
3925 else
John Durkop6ba40d12020-11-10 08:50:04 -08003926#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3927 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003928 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003929 {
John Durkop9814fa22020-11-04 12:28:15 -08003930#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3931 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003932 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003933#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003934 PSA_ALG_IS_ECDSA( alg )
3935#else
3936 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3937#endif
3938 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003939 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003940 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003941 status = psa_load_ecp_representation( slot->attr.type,
3942 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003943 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003944 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003945 if( status != PSA_SUCCESS )
3946 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003947 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003948 alg,
3949 hash, hash_length,
3950 signature, signature_size,
3951 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003952 mbedtls_ecp_keypair_free( ecp );
3953 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003954 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003955 else
John Durkop9814fa22020-11-04 12:28:15 -08003956#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3957 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003958 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003959 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003960 }
itayzafrir5c753392018-05-08 11:18:38 +03003961 }
3962 else
itayzafrir5c753392018-05-08 11:18:38 +03003963 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003964 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003965 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003966
3967exit:
3968 /* Fill the unused part of the output buffer (the whole buffer on error,
3969 * the trailing part on success) with something that isn't a valid mac
3970 * (barring an attack on the mac and deliberately-crafted input),
3971 * in case the caller doesn't check the return status properly. */
3972 if( status == PSA_SUCCESS )
3973 memset( signature + *signature_length, '!',
3974 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003975 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003976 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003977 /* If signature_size is 0 then we have nothing to do. We must not call
3978 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003979
Ronald Cron5c522922020-11-14 16:35:34 +01003980 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003981
Ronald Cron5c522922020-11-14 16:35:34 +01003982 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003983}
3984
Ronald Croncf56a0a2020-08-04 09:51:30 +02003985psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003986 psa_algorithm_t alg,
3987 const uint8_t *hash,
3988 size_t hash_length,
3989 const uint8_t *signature,
3990 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003991{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003993 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003994 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003995
Ronald Cron5c522922020-11-14 16:35:34 +01003996 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3997 PSA_KEY_USAGE_VERIFY_HASH,
3998 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003999 if( status != PSA_SUCCESS )
4000 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004001
Steven Cooreman55ae2172020-07-17 19:46:15 +02004002 /* Try any of the available accelerators first */
4003 status = psa_driver_wrapper_verify_hash( slot,
4004 alg,
4005 hash,
4006 hash_length,
4007 signature,
4008 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004009 if( status != PSA_ERROR_NOT_SUPPORTED ||
4010 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004011 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004012
John Durkop6ba40d12020-11-10 08:50:04 -08004013#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4014 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004015 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004017 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004018
Steven Cooreman4fed4552020-08-03 14:46:03 +02004019 status = psa_load_rsa_representation( slot->attr.type,
4020 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004021 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004022 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004023 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004024 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004025
Steven Cooremana2371e52020-07-28 14:30:39 +02004026 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004027 alg,
4028 hash, hash_length,
4029 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004030 mbedtls_rsa_free( rsa );
4031 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004032 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 }
4034 else
John Durkop6ba40d12020-11-10 08:50:04 -08004035#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4036 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004037 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004038 {
John Durkop9814fa22020-11-04 12:28:15 -08004039#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4040 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004041 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004042 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004043 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004044 status = psa_load_ecp_representation( slot->attr.type,
4045 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004046 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004047 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004048 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004049 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004050 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004051 hash, hash_length,
4052 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004053 mbedtls_ecp_keypair_free( ecp );
4054 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004055 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004056 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004057 else
John Durkop9814fa22020-11-04 12:28:15 -08004058#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4059 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004060 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004061 status = PSA_ERROR_INVALID_ARGUMENT;
4062 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004063 }
itayzafrir5c753392018-05-08 11:18:38 +03004064 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004065 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004066 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004067 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004068 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004069
4070exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004071 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004072
Ronald Cron5c522922020-11-14 16:35:34 +01004073 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004074}
4075
John Durkop0e005192020-10-31 22:06:54 -07004076#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004077static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4078 mbedtls_rsa_context *rsa )
4079{
4080 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4081 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4082 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4083 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4084}
John Durkop0e005192020-10-31 22:06:54 -07004085#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004086
Ronald Croncf56a0a2020-08-04 09:51:30 +02004087psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004088 psa_algorithm_t alg,
4089 const uint8_t *input,
4090 size_t input_length,
4091 const uint8_t *salt,
4092 size_t salt_length,
4093 uint8_t *output,
4094 size_t output_size,
4095 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004098 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004099 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004100
Darryl Green5cc689a2018-07-24 15:34:10 +01004101 (void) input;
4102 (void) input_length;
4103 (void) salt;
4104 (void) output;
4105 (void) output_size;
4106
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004107 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004108
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004109 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4110 return( PSA_ERROR_INVALID_ARGUMENT );
4111
Ronald Cron5c522922020-11-14 16:35:34 +01004112 status = psa_get_and_lock_transparent_key_slot_with_policy(
4113 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004114 if( status != PSA_SUCCESS )
4115 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004116 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4117 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004118 {
4119 status = PSA_ERROR_INVALID_ARGUMENT;
4120 goto exit;
4121 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004122
John Durkop6ba40d12020-11-10 08:50:04 -08004123#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4124 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004125 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004126 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004127 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004128 status = psa_load_rsa_representation( slot->attr.type,
4129 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004130 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004131 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004132 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004133 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004134
4135 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004136 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004137 status = PSA_ERROR_BUFFER_TOO_SMALL;
4138 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004139 }
John Durkop0e005192020-10-31 22:06:54 -07004140#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004141 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004142 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004143 status = mbedtls_to_psa_error(
4144 mbedtls_rsa_pkcs1_encrypt( rsa,
4145 mbedtls_ctr_drbg_random,
4146 &global_data.ctr_drbg,
4147 MBEDTLS_RSA_PUBLIC,
4148 input_length,
4149 input,
4150 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 }
4152 else
John Durkop0e005192020-10-31 22:06:54 -07004153#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4154#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004155 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004157 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004158 status = mbedtls_to_psa_error(
4159 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4160 mbedtls_ctr_drbg_random,
4161 &global_data.ctr_drbg,
4162 MBEDTLS_RSA_PUBLIC,
4163 salt, salt_length,
4164 input_length,
4165 input,
4166 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167 }
4168 else
John Durkop0e005192020-10-31 22:06:54 -07004169#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004170 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004171 status = PSA_ERROR_INVALID_ARGUMENT;
4172 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004173 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004174rsa_exit:
4175 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004176 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004177
Steven Cooremana2371e52020-07-28 14:30:39 +02004178 mbedtls_rsa_free( rsa );
4179 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004180 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181 else
John Durkop9814fa22020-11-04 12:28:15 -08004182#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004183 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004185 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004187
4188exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004189 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004190
Ronald Cron5c522922020-11-14 16:35:34 +01004191 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004192}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004193
Ronald Croncf56a0a2020-08-04 09:51:30 +02004194psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004195 psa_algorithm_t alg,
4196 const uint8_t *input,
4197 size_t input_length,
4198 const uint8_t *salt,
4199 size_t salt_length,
4200 uint8_t *output,
4201 size_t output_size,
4202 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004204 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004205 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004206 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004207
Darryl Green5cc689a2018-07-24 15:34:10 +01004208 (void) input;
4209 (void) input_length;
4210 (void) salt;
4211 (void) output;
4212 (void) output_size;
4213
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004214 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004216 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4217 return( PSA_ERROR_INVALID_ARGUMENT );
4218
Ronald Cron5c522922020-11-14 16:35:34 +01004219 status = psa_get_and_lock_transparent_key_slot_with_policy(
4220 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004221 if( status != PSA_SUCCESS )
4222 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004223 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004224 {
4225 status = PSA_ERROR_INVALID_ARGUMENT;
4226 goto exit;
4227 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228
John Durkop6ba40d12020-11-10 08:50:04 -08004229#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4230 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004231 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004232 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004233 mbedtls_rsa_context *rsa = NULL;
4234 status = psa_load_rsa_representation( slot->attr.type,
4235 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004236 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004237 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004238 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004239 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240
Steven Cooremana2371e52020-07-28 14:30:39 +02004241 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004242 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004243 status = PSA_ERROR_INVALID_ARGUMENT;
4244 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004245 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246
John Durkop0e005192020-10-31 22:06:54 -07004247#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004248 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004249 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004250 status = mbedtls_to_psa_error(
4251 mbedtls_rsa_pkcs1_decrypt( rsa,
4252 mbedtls_ctr_drbg_random,
4253 &global_data.ctr_drbg,
4254 MBEDTLS_RSA_PRIVATE,
4255 output_length,
4256 input,
4257 output,
4258 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 }
4260 else
John Durkop0e005192020-10-31 22:06:54 -07004261#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4262#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004263 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004265 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004266 status = mbedtls_to_psa_error(
4267 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4268 mbedtls_ctr_drbg_random,
4269 &global_data.ctr_drbg,
4270 MBEDTLS_RSA_PRIVATE,
4271 salt, salt_length,
4272 output_length,
4273 input,
4274 output,
4275 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276 }
4277 else
John Durkop0e005192020-10-31 22:06:54 -07004278#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004280 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004282
Steven Cooreman4fed4552020-08-03 14:46:03 +02004283rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004284 mbedtls_rsa_free( rsa );
4285 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004286 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004287 else
John Durkop6ba40d12020-11-10 08:50:04 -08004288#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4289 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004290 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004291 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004292 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004293
4294exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004295 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004296
Ronald Cron5c522922020-11-14 16:35:34 +01004297 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004298}
Gilles Peskine20035e32018-02-03 22:44:14 +01004299
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004300
4301
mohammad1603503973b2018-03-12 15:59:30 +02004302/****************************************************************/
4303/* Symmetric cryptography */
4304/****************************************************************/
4305
Gilles Peskinee553c652018-06-04 16:22:46 +02004306static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004307 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004308 psa_algorithm_t alg,
4309 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004310{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004312 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004313 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004314 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004315 size_t key_bits;
4316 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004317 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4318 PSA_KEY_USAGE_ENCRYPT :
4319 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004320
Steven Cooremand3feccd2020-09-01 15:56:14 +02004321 /* A context must be freshly initialized before it can be set up. */
4322 if( operation->alg != 0 )
4323 return( PSA_ERROR_BAD_STATE );
4324
Steven Cooremana07b9972020-09-10 14:54:14 +02004325 /* The requested algorithm must be one that can be processed by cipher. */
4326 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004327 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004328
Steven Cooremanef8575e2020-09-11 11:44:50 +02004329 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004330 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004331 if( status != PSA_SUCCESS )
4332 goto exit;
4333
4334 /* Initialize the operation struct members, except for alg. The alg member
4335 * is used to indicate to psa_cipher_abort that there are resources to free,
4336 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004337 operation->key_set = 0;
4338 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004339 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004340 operation->iv_size = 0;
4341 operation->block_size = 0;
4342 if( alg == PSA_ALG_ECB_NO_PADDING )
4343 operation->iv_required = 0;
4344 else
4345 operation->iv_required = 1;
4346
Steven Cooremana07b9972020-09-10 14:54:14 +02004347 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004348 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004349 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004350 slot,
4351 alg );
4352 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004353 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004354 slot,
4355 alg );
4356
Steven Cooremanef8575e2020-09-11 11:44:50 +02004357 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004358 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004359 /* Once the driver context is initialised, it needs to be freed using
4360 * psa_cipher_abort. Indicate this through setting alg. */
4361 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004362 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004363
Steven Cooremand3feccd2020-09-01 15:56:14 +02004364 if( status != PSA_ERROR_NOT_SUPPORTED ||
4365 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4366 goto exit;
4367
Steven Cooremana07b9972020-09-10 14:54:14 +02004368 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004369 * available for the given algorithm & key. */
4370 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004371
Steven Cooremana07b9972020-09-10 14:54:14 +02004372 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004373 * psa_cipher_abort. Indicate there is something to be freed through setting
4374 * alg, and indicate the operation is being done using mbedtls crypto through
4375 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004376 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004377 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004378
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004379 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004380 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004381 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004382 {
4383 status = PSA_ERROR_NOT_SUPPORTED;
4384 goto exit;
4385 }
mohammad1603503973b2018-03-12 15:59:30 +02004386
mohammad1603503973b2018-03-12 15:59:30 +02004387 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004388 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004389 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004390
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004391#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004392 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004393 {
4394 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004395 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004396 memcpy( keys, slot->data.key.data, 16 );
4397 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004398 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4399 keys,
4400 192, cipher_operation );
4401 }
4402 else
4403#endif
4404 {
4405 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004406 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004407 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004408 }
Moran Peker41deec42018-04-04 15:43:05 +03004409 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004410 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004411
mohammad16038481e742018-03-18 13:57:31 +02004412#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004413 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004414 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004415 case PSA_ALG_CBC_NO_PADDING:
4416 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4417 MBEDTLS_PADDING_NONE );
4418 break;
4419 case PSA_ALG_CBC_PKCS7:
4420 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4421 MBEDTLS_PADDING_PKCS7 );
4422 break;
4423 default:
4424 /* The algorithm doesn't involve padding. */
4425 ret = 0;
4426 break;
4427 }
4428 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004429 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004430#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4431
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004432 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004433 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004434 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4435 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004436 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004437 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004438 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004439#if defined(MBEDTLS_CHACHA20_C)
4440 else
4441 if( alg == PSA_ALG_CHACHA20 )
4442 operation->iv_size = 12;
4443#endif
mohammad1603503973b2018-03-12 15:59:30 +02004444
Steven Cooreman7df02922020-09-09 15:28:49 +02004445 status = PSA_SUCCESS;
4446
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004447exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004448 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004449 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004450 if( status == PSA_SUCCESS )
4451 {
4452 /* Update operation flags for both driver and software implementations */
4453 operation->key_set = 1;
4454 }
4455 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004456 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004457
Ronald Cron5c522922020-11-14 16:35:34 +01004458 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004459
Ronald Cron5c522922020-11-14 16:35:34 +01004460 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004461}
4462
Gilles Peskinefe119512018-07-08 21:39:34 +02004463psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004464 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004465 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004466{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004467 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004468}
4469
Gilles Peskinefe119512018-07-08 21:39:34 +02004470psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004471 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004472 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004473{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004474 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004475}
4476
Gilles Peskinefe119512018-07-08 21:39:34 +02004477psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004478 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004479 size_t iv_size,
4480 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004481{
itayzafrir534bd7c2018-08-02 13:56:32 +03004482 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004484 if( operation->iv_set || ! operation->iv_required )
4485 {
4486 return( PSA_ERROR_BAD_STATE );
4487 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004488
Steven Cooremanef8575e2020-09-11 11:44:50 +02004489 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004490 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004491 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004492 iv,
4493 iv_size,
4494 iv_length );
4495 goto exit;
4496 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004497
Moran Peker41deec42018-04-04 15:43:05 +03004498 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004499 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004500 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004501 goto exit;
4502 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004503 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4504 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004505 if( ret != 0 )
4506 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004507 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004508 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004509 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004510
mohammad16038481e742018-03-18 13:57:31 +02004511 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004512 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004513
Moran Peker395db872018-05-31 14:07:14 +03004514exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004515 if( status == PSA_SUCCESS )
4516 operation->iv_set = 1;
4517 else
Moran Peker395db872018-05-31 14:07:14 +03004518 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004519 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004520}
4521
Gilles Peskinefe119512018-07-08 21:39:34 +02004522psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004523 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004524 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004525{
itayzafrir534bd7c2018-08-02 13:56:32 +03004526 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004528 if( operation->iv_set || ! operation->iv_required )
4529 {
4530 return( PSA_ERROR_BAD_STATE );
4531 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004532
Steven Cooremanef8575e2020-09-11 11:44:50 +02004533 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004534 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004535 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004536 iv,
4537 iv_length );
4538 goto exit;
4539 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004540
Moran Pekera28258c2018-05-29 16:25:04 +03004541 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004542 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004543 status = PSA_ERROR_INVALID_ARGUMENT;
4544 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004545 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004546 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4547 status = mbedtls_to_psa_error( ret );
4548exit:
4549 if( status == PSA_SUCCESS )
4550 operation->iv_set = 1;
4551 else
mohammad1603503973b2018-03-12 15:59:30 +02004552 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004553 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004554}
4555
Steven Cooreman177deba2020-09-07 17:14:14 +02004556/* Process input for which the algorithm is set to ECB mode. This requires
4557 * manual processing, since the PSA API is defined as being able to process
4558 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4559 * underlying mbedtls_cipher_update only takes full blocks. */
4560static psa_status_t psa_cipher_update_ecb_internal(
4561 mbedtls_cipher_context_t *ctx,
4562 const uint8_t *input,
4563 size_t input_length,
4564 uint8_t *output,
4565 size_t output_size,
4566 size_t *output_length )
4567{
4568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4569 size_t block_size = ctx->cipher_info->block_size;
4570 size_t internal_output_length = 0;
4571 *output_length = 0;
4572
4573 if( input_length == 0 )
4574 {
4575 status = PSA_SUCCESS;
4576 goto exit;
4577 }
4578
4579 if( ctx->unprocessed_len > 0 )
4580 {
4581 /* Fill up to block size, and run the block if there's a full one. */
4582 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4583
4584 if( input_length < bytes_to_copy )
4585 bytes_to_copy = input_length;
4586
4587 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4588 input, bytes_to_copy );
4589 input_length -= bytes_to_copy;
4590 input += bytes_to_copy;
4591 ctx->unprocessed_len += bytes_to_copy;
4592
4593 if( ctx->unprocessed_len == block_size )
4594 {
4595 status = mbedtls_to_psa_error(
4596 mbedtls_cipher_update( ctx,
4597 ctx->unprocessed_data,
4598 block_size,
4599 output, &internal_output_length ) );
4600
4601 if( status != PSA_SUCCESS )
4602 goto exit;
4603
4604 output += internal_output_length;
4605 output_size -= internal_output_length;
4606 *output_length += internal_output_length;
4607 ctx->unprocessed_len = 0;
4608 }
4609 }
4610
4611 while( input_length >= block_size )
4612 {
4613 /* Run all full blocks we have, one by one */
4614 status = mbedtls_to_psa_error(
4615 mbedtls_cipher_update( ctx, input,
4616 block_size,
4617 output, &internal_output_length ) );
4618
4619 if( status != PSA_SUCCESS )
4620 goto exit;
4621
4622 input_length -= block_size;
4623 input += block_size;
4624
4625 output += internal_output_length;
4626 output_size -= internal_output_length;
4627 *output_length += internal_output_length;
4628 }
4629
4630 if( input_length > 0 )
4631 {
4632 /* Save unprocessed bytes for later processing */
4633 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4634 input, input_length );
4635 ctx->unprocessed_len += input_length;
4636 }
4637
4638 status = PSA_SUCCESS;
4639
4640exit:
4641 return( status );
4642}
4643
Gilles Peskinee553c652018-06-04 16:22:46 +02004644psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4645 const uint8_t *input,
4646 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004647 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004648 size_t output_size,
4649 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004650{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004652 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004653 if( operation->alg == 0 )
4654 {
4655 return( PSA_ERROR_BAD_STATE );
4656 }
4657 if( operation->iv_required && ! operation->iv_set )
4658 {
4659 return( PSA_ERROR_BAD_STATE );
4660 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004661
Steven Cooremanef8575e2020-09-11 11:44:50 +02004662 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004663 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004664 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004665 input,
4666 input_length,
4667 output,
4668 output_size,
4669 output_length );
4670 goto exit;
4671 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004672
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004673 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004674 {
4675 /* Take the unprocessed partial block left over from previous
4676 * update calls, if any, plus the input to this call. Remove
4677 * the last partial block, if any. You get the data that will be
4678 * output in this call. */
4679 expected_output_size =
4680 ( operation->ctx.cipher.unprocessed_len + input_length )
4681 / operation->block_size * operation->block_size;
4682 }
4683 else
4684 {
4685 expected_output_size = input_length;
4686 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004687
Gilles Peskine89d789c2018-06-04 17:17:16 +02004688 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004689 {
4690 status = PSA_ERROR_BUFFER_TOO_SMALL;
4691 goto exit;
4692 }
mohammad160382759612018-03-12 18:16:40 +02004693
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004694 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4695 {
4696 /* mbedtls_cipher_update has an API inconsistency: it will only
4697 * process a single block at a time in ECB mode. Abstract away that
4698 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004699 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4700 input,
4701 input_length,
4702 output,
4703 output_size,
4704 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004705 }
4706 else
4707 {
4708 status = mbedtls_to_psa_error(
4709 mbedtls_cipher_update( &operation->ctx.cipher, input,
4710 input_length, output, output_length ) );
4711 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004712exit:
4713 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004714 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004715 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004716}
4717
Gilles Peskinee553c652018-06-04 16:22:46 +02004718psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4719 uint8_t *output,
4720 size_t output_size,
4721 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004722{
David Saadab4ecc272019-02-14 13:48:10 +02004723 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004724 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004725 if( operation->alg == 0 )
4726 {
4727 return( PSA_ERROR_BAD_STATE );
4728 }
4729 if( operation->iv_required && ! operation->iv_set )
4730 {
4731 return( PSA_ERROR_BAD_STATE );
4732 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004733
Steven Cooremanef8575e2020-09-11 11:44:50 +02004734 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004735 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004736 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004737 output,
4738 output_size,
4739 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004740 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004741 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004742
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004743 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004744 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004745 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004746 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004747 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004748 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004749 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004750 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004751 }
4752
Steven Cooremanef8575e2020-09-11 11:44:50 +02004753 status = mbedtls_to_psa_error(
4754 mbedtls_cipher_finish( &operation->ctx.cipher,
4755 temp_output_buffer,
4756 output_length ) );
4757 if( status != PSA_SUCCESS )
4758 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004759
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004760 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004761 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004762 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004763 memcpy( output, temp_output_buffer, *output_length );
4764 else
Janos Follath315b51c2018-07-09 16:04:51 +01004765 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004766
Steven Cooremanef8575e2020-09-11 11:44:50 +02004767exit:
4768 if( operation->mbedtls_in_use == 1 )
4769 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004770
Steven Cooremanef8575e2020-09-11 11:44:50 +02004771 if( status == PSA_SUCCESS )
4772 return( psa_cipher_abort( operation ) );
4773 else
4774 {
4775 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004776 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004777
Steven Cooremanef8575e2020-09-11 11:44:50 +02004778 return( status );
4779 }
mohammad1603503973b2018-03-12 15:59:30 +02004780}
4781
Gilles Peskinee553c652018-06-04 16:22:46 +02004782psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4783{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004784 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004785 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004786 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004787 * in use. It's ok to call abort on such an object, and there's
4788 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004789 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004790 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004791
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004792 /* Sanity check (shouldn't happen: operation->alg should
4793 * always have been initialized to a valid value). */
4794 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4795 return( PSA_ERROR_BAD_STATE );
4796
Steven Cooremanef8575e2020-09-11 11:44:50 +02004797 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004798 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004799 else
4800 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004801
Moran Peker41deec42018-04-04 15:43:05 +03004802 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004803 operation->key_set = 0;
4804 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004805 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004806 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004807 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004808 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004809
Moran Peker395db872018-05-31 14:07:14 +03004810 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004811}
4812
Gilles Peskinea0655c32018-04-30 17:06:50 +02004813
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004814
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004815
mohammad16035955c982018-04-26 00:53:03 +03004816/****************************************************************/
4817/* AEAD */
4818/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004819
Gilles Peskineedf9a652018-08-17 18:11:56 +02004820typedef struct
4821{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004822 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004823 const mbedtls_cipher_info_t *cipher_info;
4824 union
4825 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004826 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004827#if defined(MBEDTLS_CCM_C)
4828 mbedtls_ccm_context ccm;
4829#endif /* MBEDTLS_CCM_C */
4830#if defined(MBEDTLS_GCM_C)
4831 mbedtls_gcm_context gcm;
4832#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004833#if defined(MBEDTLS_CHACHAPOLY_C)
4834 mbedtls_chachapoly_context chachapoly;
4835#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004836 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004837 psa_algorithm_t core_alg;
4838 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004839 uint8_t tag_length;
4840} aead_operation_t;
4841
Ronald Cronf95a2b12020-10-22 15:24:49 +02004842#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4843
Gilles Peskine30a9e412019-01-14 18:36:12 +01004844static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004845{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004846 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004847 {
4848#if defined(MBEDTLS_CCM_C)
4849 case PSA_ALG_CCM:
4850 mbedtls_ccm_free( &operation->ctx.ccm );
4851 break;
4852#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004853#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004854 case PSA_ALG_GCM:
4855 mbedtls_gcm_free( &operation->ctx.gcm );
4856 break;
4857#endif /* MBEDTLS_GCM_C */
4858 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004859
Ronald Cron5c522922020-11-14 16:35:34 +01004860 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004861}
4862
4863static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004864 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004865 psa_key_usage_t usage,
4866 psa_algorithm_t alg )
4867{
4868 psa_status_t status;
4869 size_t key_bits;
4870 mbedtls_cipher_id_t cipher_id;
4871
Ronald Cron5c522922020-11-14 16:35:34 +01004872 status = psa_get_and_lock_transparent_key_slot_with_policy(
4873 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004874 if( status != PSA_SUCCESS )
4875 return( status );
4876
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004877 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878
4879 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004880 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004881 &cipher_id );
4882 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004883 {
4884 status = PSA_ERROR_NOT_SUPPORTED;
4885 goto cleanup;
4886 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004887
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004888 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889 {
4890#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004891 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4892 operation->core_alg = PSA_ALG_CCM;
4893 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004894 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4895 * The call to mbedtls_ccm_encrypt_and_tag or
4896 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004897 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004898 {
4899 status = PSA_ERROR_INVALID_ARGUMENT;
4900 goto cleanup;
4901 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004902 mbedtls_ccm_init( &operation->ctx.ccm );
4903 status = mbedtls_to_psa_error(
4904 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004905 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004906 (unsigned int) key_bits ) );
4907 if( status != 0 )
4908 goto cleanup;
4909 break;
4910#endif /* MBEDTLS_CCM_C */
4911
4912#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004913 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4914 operation->core_alg = PSA_ALG_GCM;
4915 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004916 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4917 * The call to mbedtls_gcm_crypt_and_tag or
4918 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004919 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004920 {
4921 status = PSA_ERROR_INVALID_ARGUMENT;
4922 goto cleanup;
4923 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004924 mbedtls_gcm_init( &operation->ctx.gcm );
4925 status = mbedtls_to_psa_error(
4926 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004927 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004928 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004929 if( status != 0 )
4930 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004931 break;
4932#endif /* MBEDTLS_GCM_C */
4933
Gilles Peskine26869f22019-05-06 15:25:00 +02004934#if defined(MBEDTLS_CHACHAPOLY_C)
4935 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4936 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4937 operation->full_tag_length = 16;
4938 /* We only support the default tag length. */
4939 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004940 {
4941 status = PSA_ERROR_NOT_SUPPORTED;
4942 goto cleanup;
4943 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004944 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4945 status = mbedtls_to_psa_error(
4946 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004947 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004948 if( status != 0 )
4949 goto cleanup;
4950 break;
4951#endif /* MBEDTLS_CHACHAPOLY_C */
4952
Gilles Peskineedf9a652018-08-17 18:11:56 +02004953 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004954 status = PSA_ERROR_NOT_SUPPORTED;
4955 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004956 }
4957
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004958 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4959 {
4960 status = PSA_ERROR_INVALID_ARGUMENT;
4961 goto cleanup;
4962 }
4963 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004964
Gilles Peskineedf9a652018-08-17 18:11:56 +02004965 return( PSA_SUCCESS );
4966
4967cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004968 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004969 return( status );
4970}
4971
Ronald Croncf56a0a2020-08-04 09:51:30 +02004972psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004973 psa_algorithm_t alg,
4974 const uint8_t *nonce,
4975 size_t nonce_length,
4976 const uint8_t *additional_data,
4977 size_t additional_data_length,
4978 const uint8_t *plaintext,
4979 size_t plaintext_length,
4980 uint8_t *ciphertext,
4981 size_t ciphertext_size,
4982 size_t *ciphertext_length )
4983{
mohammad16035955c982018-04-26 00:53:03 +03004984 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004985 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004986 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004987
mohammad1603f08a5502018-06-03 15:05:47 +03004988 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004989
Ronald Croncf56a0a2020-08-04 09:51:30 +02004990 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004991 if( status != PSA_SUCCESS )
4992 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004993
Gilles Peskineedf9a652018-08-17 18:11:56 +02004994 /* For all currently supported modes, the tag is at the end of the
4995 * ciphertext. */
4996 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4997 {
4998 status = PSA_ERROR_BUFFER_TOO_SMALL;
4999 goto exit;
5000 }
5001 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005002
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005003#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005004 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005005 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005006 status = mbedtls_to_psa_error(
5007 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5008 MBEDTLS_GCM_ENCRYPT,
5009 plaintext_length,
5010 nonce, nonce_length,
5011 additional_data, additional_data_length,
5012 plaintext, ciphertext,
5013 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005014 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005015 else
5016#endif /* MBEDTLS_GCM_C */
5017#if defined(MBEDTLS_CCM_C)
5018 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005019 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005020 status = mbedtls_to_psa_error(
5021 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5022 plaintext_length,
5023 nonce, nonce_length,
5024 additional_data,
5025 additional_data_length,
5026 plaintext, ciphertext,
5027 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005028 }
mohammad16035c8845f2018-05-09 05:40:09 -07005029 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005030#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005031#if defined(MBEDTLS_CHACHAPOLY_C)
5032 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5033 {
5034 if( nonce_length != 12 || operation.tag_length != 16 )
5035 {
5036 status = PSA_ERROR_NOT_SUPPORTED;
5037 goto exit;
5038 }
5039 status = mbedtls_to_psa_error(
5040 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5041 plaintext_length,
5042 nonce,
5043 additional_data,
5044 additional_data_length,
5045 plaintext,
5046 ciphertext,
5047 tag ) );
5048 }
5049 else
5050#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005051 {
mohammad1603554faad2018-06-03 15:07:38 +03005052 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005053 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005054
Gilles Peskineedf9a652018-08-17 18:11:56 +02005055 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5056 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005057
Gilles Peskineedf9a652018-08-17 18:11:56 +02005058exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005059 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005060 if( status == PSA_SUCCESS )
5061 *ciphertext_length = plaintext_length + operation.tag_length;
5062 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005063}
5064
Gilles Peskineee652a32018-06-01 19:23:52 +02005065/* Locate the tag in a ciphertext buffer containing the encrypted data
5066 * followed by the tag. Return the length of the part preceding the tag in
5067 * *plaintext_length. This is the size of the plaintext in modes where
5068 * the encrypted data has the same size as the plaintext, such as
5069 * CCM and GCM. */
5070static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5071 const uint8_t *ciphertext,
5072 size_t ciphertext_length,
5073 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005074 const uint8_t **p_tag )
5075{
5076 size_t payload_length;
5077 if( tag_length > ciphertext_length )
5078 return( PSA_ERROR_INVALID_ARGUMENT );
5079 payload_length = ciphertext_length - tag_length;
5080 if( payload_length > plaintext_size )
5081 return( PSA_ERROR_BUFFER_TOO_SMALL );
5082 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005083 return( PSA_SUCCESS );
5084}
5085
Ronald Croncf56a0a2020-08-04 09:51:30 +02005086psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005087 psa_algorithm_t alg,
5088 const uint8_t *nonce,
5089 size_t nonce_length,
5090 const uint8_t *additional_data,
5091 size_t additional_data_length,
5092 const uint8_t *ciphertext,
5093 size_t ciphertext_length,
5094 uint8_t *plaintext,
5095 size_t plaintext_size,
5096 size_t *plaintext_length )
5097{
mohammad16035955c982018-04-26 00:53:03 +03005098 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005099 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005100 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005101
Gilles Peskineee652a32018-06-01 19:23:52 +02005102 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005103
Ronald Croncf56a0a2020-08-04 09:51:30 +02005104 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005105 if( status != PSA_SUCCESS )
5106 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005107
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005108 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5109 ciphertext, ciphertext_length,
5110 plaintext_size, &tag );
5111 if( status != PSA_SUCCESS )
5112 goto exit;
5113
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005114#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005115 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005116 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005117 status = mbedtls_to_psa_error(
5118 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5119 ciphertext_length - operation.tag_length,
5120 nonce, nonce_length,
5121 additional_data,
5122 additional_data_length,
5123 tag, operation.tag_length,
5124 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005125 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005126 else
5127#endif /* MBEDTLS_GCM_C */
5128#if defined(MBEDTLS_CCM_C)
5129 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005130 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005131 status = mbedtls_to_psa_error(
5132 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5133 ciphertext_length - operation.tag_length,
5134 nonce, nonce_length,
5135 additional_data,
5136 additional_data_length,
5137 ciphertext, plaintext,
5138 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005139 }
mohammad160339574652018-06-01 04:39:53 -07005140 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005141#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005142#if defined(MBEDTLS_CHACHAPOLY_C)
5143 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5144 {
5145 if( nonce_length != 12 || operation.tag_length != 16 )
5146 {
5147 status = PSA_ERROR_NOT_SUPPORTED;
5148 goto exit;
5149 }
5150 status = mbedtls_to_psa_error(
5151 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5152 ciphertext_length - operation.tag_length,
5153 nonce,
5154 additional_data,
5155 additional_data_length,
5156 tag,
5157 ciphertext,
5158 plaintext ) );
5159 }
5160 else
5161#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005162 {
mohammad1603554faad2018-06-03 15:07:38 +03005163 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005164 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005165
Gilles Peskineedf9a652018-08-17 18:11:56 +02005166 if( status != PSA_SUCCESS && plaintext_size != 0 )
5167 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005168
Gilles Peskineedf9a652018-08-17 18:11:56 +02005169exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005170 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005171 if( status == PSA_SUCCESS )
5172 *plaintext_length = ciphertext_length - operation.tag_length;
5173 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005174}
5175
Gilles Peskinea0655c32018-04-30 17:06:50 +02005176
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005177
Gilles Peskine20035e32018-02-03 22:44:14 +01005178/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005179/* Generators */
5180/****************************************************************/
5181
John Durkop07cc04a2020-11-16 22:08:34 -08005182#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5183 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5184 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5185#define AT_LEAST_ONE_BUILTIN_KDF
5186#endif
5187
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188#define HKDF_STATE_INIT 0 /* no input yet */
5189#define HKDF_STATE_STARTED 1 /* got salt */
5190#define HKDF_STATE_KEYED 2 /* got key */
5191#define HKDF_STATE_OUTPUT 3 /* output started */
5192
Gilles Peskinecbe66502019-05-16 16:59:18 +02005193static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005195{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5197 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005198 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005200}
5201
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005203{
5204 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005206 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005207 {
5208 /* The object has (apparently) been initialized but it is not
5209 * in use. It's ok to call abort on such an object, and there's
5210 * nothing to do. */
5211 }
5212 else
John Durkopd0321952020-10-29 21:37:36 -07005213#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005215 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 mbedtls_free( operation->ctx.hkdf.info );
5217 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005218 }
John Durkop07cc04a2020-11-16 22:08:34 -08005219 else
5220#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5221#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5222 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5223 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005225 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005226 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005227 if( operation->ctx.tls12_prf.seed != NULL )
5228 {
5229 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5230 operation->ctx.tls12_prf.seed_length );
5231 mbedtls_free( operation->ctx.tls12_prf.seed );
5232 }
5233
5234 if( operation->ctx.tls12_prf.label != NULL )
5235 {
5236 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5237 operation->ctx.tls12_prf.label_length );
5238 mbedtls_free( operation->ctx.tls12_prf.label );
5239 }
5240
5241 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5242
5243 /* We leave the fields Ai and output_block to be erased safely by the
5244 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005245 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005246 else
John Durkop07cc04a2020-11-16 22:08:34 -08005247#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5248 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005249 {
5250 status = PSA_ERROR_BAD_STATE;
5251 }
Janos Follath083036a2019-06-11 10:22:26 +01005252 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005253 return( status );
5254}
5255
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005257 size_t *capacity)
5258{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005260 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005262 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005263 }
5264
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 return( PSA_SUCCESS );
5267}
5268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005270 size_t capacity )
5271{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005277 return( PSA_SUCCESS );
5278}
5279
John Durkopd0321952020-10-29 21:37:36 -07005280#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005282 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005283static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005284 psa_algorithm_t hash_alg,
5285 uint8_t *output,
5286 size_t output_length )
5287{
5288 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5289 psa_status_t status;
5290
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005291 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5292 return( PSA_ERROR_BAD_STATE );
5293 hkdf->state = HKDF_STATE_OUTPUT;
5294
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295 while( output_length != 0 )
5296 {
5297 /* Copy what remains of the current block */
5298 uint8_t n = hash_length - hkdf->offset_in_block;
5299 if( n > output_length )
5300 n = (uint8_t) output_length;
5301 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5302 output += n;
5303 output_length -= n;
5304 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005305 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005306 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005307 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005308 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005310 * object was corrupted or if this function is called directly
5311 * inside the library. */
5312 if( hkdf->block_number == 0xff )
5313 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005314
5315 /* We need a new block */
5316 ++hkdf->block_number;
5317 hkdf->offset_in_block = 0;
5318 status = psa_hmac_setup_internal( &hkdf->hmac,
5319 hkdf->prk, hash_length,
5320 hash_alg );
5321 if( status != PSA_SUCCESS )
5322 return( status );
5323 if( hkdf->block_number != 1 )
5324 {
5325 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5326 hkdf->output_block,
5327 hash_length );
5328 if( status != PSA_SUCCESS )
5329 return( status );
5330 }
5331 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5332 hkdf->info,
5333 hkdf->info_length );
5334 if( status != PSA_SUCCESS )
5335 return( status );
5336 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5337 &hkdf->block_number, 1 );
5338 if( status != PSA_SUCCESS )
5339 return( status );
5340 status = psa_hmac_finish_internal( &hkdf->hmac,
5341 hkdf->output_block,
5342 sizeof( hkdf->output_block ) );
5343 if( status != PSA_SUCCESS )
5344 return( status );
5345 }
5346
5347 return( PSA_SUCCESS );
5348}
John Durkop07cc04a2020-11-16 22:08:34 -08005349#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005350
John Durkop07cc04a2020-11-16 22:08:34 -08005351#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5352 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005353static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5354 psa_tls12_prf_key_derivation_t *tls12_prf,
5355 psa_algorithm_t alg )
5356{
5357 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5358 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005359 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5360 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005361
Janos Follath7742fee2019-06-17 12:58:10 +01005362 /* We can't be wanting more output after block 0xff, otherwise
5363 * the capacity check in psa_key_derivation_output_bytes() would have
5364 * prevented this call. It could happen only if the operation
5365 * object was corrupted or if this function is called directly
5366 * inside the library. */
5367 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005368 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005369
5370 /* We need a new block */
5371 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005372 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005373
5374 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5375 *
5376 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5377 *
5378 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5379 * HMAC_hash(secret, A(2) + seed) +
5380 * HMAC_hash(secret, A(3) + seed) + ...
5381 *
5382 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005383 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005384 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005385 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005386 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005387 * is currently extracted as `output_block` and where i is
5388 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005389 */
5390
Janos Follathea29bfb2019-06-19 12:21:20 +01005391 /* Save the hash context before using it, to preserve the hash state with
5392 * only the inner padding in it. We need this, because inner padding depends
5393 * on the key (secret in the RFC's terminology). */
5394 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5395 if( status != PSA_SUCCESS )
5396 goto cleanup;
5397
5398 /* Calculate A(i) where i = tls12_prf->block_number. */
5399 if( tls12_prf->block_number == 1 )
5400 {
5401 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5402 * the variable seed and in this instance means it in the context of the
5403 * P_hash function, where seed = label + seed.) */
5404 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5405 tls12_prf->label, tls12_prf->label_length );
5406 if( status != PSA_SUCCESS )
5407 goto cleanup;
5408 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5409 tls12_prf->seed, tls12_prf->seed_length );
5410 if( status != PSA_SUCCESS )
5411 goto cleanup;
5412 }
5413 else
5414 {
5415 /* A(i) = HMAC_hash(secret, A(i-1)) */
5416 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5417 tls12_prf->Ai, hash_length );
5418 if( status != PSA_SUCCESS )
5419 goto cleanup;
5420 }
5421
5422 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5423 tls12_prf->Ai, hash_length );
5424 if( status != PSA_SUCCESS )
5425 goto cleanup;
5426 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5427 if( status != PSA_SUCCESS )
5428 goto cleanup;
5429
5430 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5431 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5432 tls12_prf->Ai, hash_length );
5433 if( status != PSA_SUCCESS )
5434 goto cleanup;
5435 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5436 tls12_prf->label, tls12_prf->label_length );
5437 if( status != PSA_SUCCESS )
5438 goto cleanup;
5439 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5440 tls12_prf->seed, tls12_prf->seed_length );
5441 if( status != PSA_SUCCESS )
5442 goto cleanup;
5443 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5444 tls12_prf->output_block, hash_length );
5445 if( status != PSA_SUCCESS )
5446 goto cleanup;
5447 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5448 if( status != PSA_SUCCESS )
5449 goto cleanup;
5450
Janos Follath7742fee2019-06-17 12:58:10 +01005451
5452cleanup:
5453
Janos Follathea29bfb2019-06-19 12:21:20 +01005454 cleanup_status = psa_hash_abort( &backup );
5455 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5456 status = cleanup_status;
5457
5458 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005459}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005460
Janos Follath844eb0e2019-06-19 12:10:49 +01005461static psa_status_t psa_key_derivation_tls12_prf_read(
5462 psa_tls12_prf_key_derivation_t *tls12_prf,
5463 psa_algorithm_t alg,
5464 uint8_t *output,
5465 size_t output_length )
5466{
5467 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5468 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5469 psa_status_t status;
5470 uint8_t offset, length;
5471
5472 while( output_length != 0 )
5473 {
5474 /* Check if we have fully processed the current block. */
5475 if( tls12_prf->left_in_block == 0 )
5476 {
5477 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5478 alg );
5479 if( status != PSA_SUCCESS )
5480 return( status );
5481
5482 continue;
5483 }
5484
5485 if( tls12_prf->left_in_block > output_length )
5486 length = (uint8_t) output_length;
5487 else
5488 length = tls12_prf->left_in_block;
5489
5490 offset = hash_length - tls12_prf->left_in_block;
5491 memcpy( output, tls12_prf->output_block + offset, length );
5492 output += length;
5493 output_length -= length;
5494 tls12_prf->left_in_block -= length;
5495 }
5496
5497 return( PSA_SUCCESS );
5498}
John Durkop07cc04a2020-11-16 22:08:34 -08005499#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5500 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005501
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005502psa_status_t psa_key_derivation_output_bytes(
5503 psa_key_derivation_operation_t *operation,
5504 uint8_t *output,
5505 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005506{
5507 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005509
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005511 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005512 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005513 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005514 }
5515
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005520 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005521 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005522 goto exit;
5523 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005525 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005527 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005528 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5529 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005531 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005532 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005533 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005534 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005535
John Durkopd0321952020-10-29 21:37:36 -07005536#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005537 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005538 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005539 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005541 output, output_length );
5542 }
Janos Follathadbec812019-06-14 11:05:39 +01005543 else
John Durkop07cc04a2020-11-16 22:08:34 -08005544#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5545#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5546 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005547 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005548 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005549 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005551 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005552 output_length );
5553 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005554 else
John Durkop07cc04a2020-11-16 22:08:34 -08005555#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5556 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005557 {
5558 return( PSA_ERROR_BAD_STATE );
5559 }
5560
5561exit:
5562 if( status != PSA_SUCCESS )
5563 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005564 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 * This allows us to differentiate between exhausted operations and
5566 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5567 * operations. */
5568 psa_algorithm_t alg = operation->alg;
5569 psa_key_derivation_abort( operation );
5570 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005571 memset( output, '!', output_length );
5572 }
5573 return( status );
5574}
5575
Gilles Peskine08542d82018-07-19 17:05:42 +02005576#if defined(MBEDTLS_DES_C)
5577static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5578{
5579 if( data_size >= 8 )
5580 mbedtls_des_key_set_parity( data );
5581 if( data_size >= 16 )
5582 mbedtls_des_key_set_parity( data + 8 );
5583 if( data_size >= 24 )
5584 mbedtls_des_key_set_parity( data + 16 );
5585}
5586#endif /* MBEDTLS_DES_C */
5587
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005588static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005589 psa_key_slot_t *slot,
5590 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005592{
5593 uint8_t *data = NULL;
5594 size_t bytes = PSA_BITS_TO_BYTES( bits );
5595 psa_status_t status;
5596
Gilles Peskine8e338702019-07-30 20:06:31 +02005597 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005598 return( PSA_ERROR_INVALID_ARGUMENT );
5599 if( bits % 8 != 0 )
5600 return( PSA_ERROR_INVALID_ARGUMENT );
5601 data = mbedtls_calloc( 1, bytes );
5602 if( data == NULL )
5603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5604
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005605 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005606 if( status != PSA_SUCCESS )
5607 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005608#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005609 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005610 psa_des_set_key_parity( data, bytes );
5611#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005612 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005613
5614exit:
5615 mbedtls_free( data );
5616 return( status );
5617}
5618
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005619psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005620 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005621 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005622{
5623 psa_status_t status;
5624 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005625 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005626
Ronald Cron81709fc2020-11-14 12:10:32 +01005627 *key = MBEDTLS_SVC_KEY_ID_INIT;
5628
Gilles Peskine0f84d622019-09-12 19:03:13 +02005629 /* Reject any attempt to create a zero-length key so that we don't
5630 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5631 if( psa_get_key_bits( attributes ) == 0 )
5632 return( PSA_ERROR_INVALID_ARGUMENT );
5633
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005634 if( ! operation->can_output_key )
5635 return( PSA_ERROR_NOT_PERMITTED );
5636
Ronald Cron81709fc2020-11-14 12:10:32 +01005637 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5638 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005639#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5640 if( driver != NULL )
5641 {
5642 /* Deriving a key in a secure element is not implemented yet. */
5643 status = PSA_ERROR_NOT_SUPPORTED;
5644 }
5645#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005646 if( status == PSA_SUCCESS )
5647 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005648 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005649 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005651 }
5652 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005653 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005654 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005655 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005656
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005657 return( status );
5658}
5659
Gilles Peskineeab56e42018-07-12 17:12:33 +02005660
5661
5662/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005663/* Key derivation */
5664/****************************************************************/
5665
John Durkop07cc04a2020-11-16 22:08:34 -08005666#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005667static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005668 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005669 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005670{
John Durkop07cc04a2020-11-16 22:08:34 -08005671 int is_kdf_alg_supported;
5672
Janos Follath5fe19732019-06-20 15:09:30 +01005673 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5674 * initialisers for this union leave some bytes unspecified.) */
5675 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5676
Gilles Peskine969c5d62019-01-16 15:53:06 +01005677 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005678#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005679 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5680 is_kdf_alg_supported = 1;
5681 else
5682#endif
5683#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5684 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5685 is_kdf_alg_supported = 1;
5686 else
5687#endif
5688#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5689 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5690 is_kdf_alg_supported = 1;
5691 else
5692#endif
5693 is_kdf_alg_supported = 0;
5694
5695 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005696 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005697 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005698 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5699 if( hash_size == 0 )
5700 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005701 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5702 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005703 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005704 {
5705 return( PSA_ERROR_NOT_SUPPORTED );
5706 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005707 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005708 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005709 }
John Durkop07cc04a2020-11-16 22:08:34 -08005710
5711 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005712}
John Durkop07cc04a2020-11-16 22:08:34 -08005713#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005715psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005716 psa_algorithm_t alg )
5717{
5718 psa_status_t status;
5719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721 return( PSA_ERROR_BAD_STATE );
5722
Gilles Peskine6843c292019-01-18 16:44:49 +01005723 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5724 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005725#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005726 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005727 {
5728 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005730 }
5731 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5732 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005734 }
John Durkop07cc04a2020-11-16 22:08:34 -08005735#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005736 else
5737 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005738
5739 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005741 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005742}
5743
John Durkopd0321952020-10-29 21:37:36 -07005744#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005745static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005746 psa_algorithm_t hash_alg,
5747 psa_key_derivation_step_t step,
5748 const uint8_t *data,
5749 size_t data_length )
5750{
5751 psa_status_t status;
5752 switch( step )
5753 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005754 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005755 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005756 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005757 status = psa_hmac_setup_internal( &hkdf->hmac,
5758 data, data_length,
5759 hash_alg );
5760 if( status != PSA_SUCCESS )
5761 return( status );
5762 hkdf->state = HKDF_STATE_STARTED;
5763 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005764 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005765 /* If no salt was provided, use an empty salt. */
5766 if( hkdf->state == HKDF_STATE_INIT )
5767 {
5768 status = psa_hmac_setup_internal( &hkdf->hmac,
5769 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005770 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 if( status != PSA_SUCCESS )
5772 return( status );
5773 hkdf->state = HKDF_STATE_STARTED;
5774 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005775 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005776 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005777 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5778 data, data_length );
5779 if( status != PSA_SUCCESS )
5780 return( status );
5781 status = psa_hmac_finish_internal( &hkdf->hmac,
5782 hkdf->prk,
5783 sizeof( hkdf->prk ) );
5784 if( status != PSA_SUCCESS )
5785 return( status );
5786 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5787 hkdf->block_number = 0;
5788 hkdf->state = HKDF_STATE_KEYED;
5789 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005790 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005791 if( hkdf->state == HKDF_STATE_OUTPUT )
5792 return( PSA_ERROR_BAD_STATE );
5793 if( hkdf->info_set )
5794 return( PSA_ERROR_BAD_STATE );
5795 hkdf->info_length = data_length;
5796 if( data_length != 0 )
5797 {
5798 hkdf->info = mbedtls_calloc( 1, data_length );
5799 if( hkdf->info == NULL )
5800 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5801 memcpy( hkdf->info, data, data_length );
5802 }
5803 hkdf->info_set = 1;
5804 return( PSA_SUCCESS );
5805 default:
5806 return( PSA_ERROR_INVALID_ARGUMENT );
5807 }
5808}
John Durkop07cc04a2020-11-16 22:08:34 -08005809#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005810
John Durkop07cc04a2020-11-16 22:08:34 -08005811#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5812 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005813static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5814 const uint8_t *data,
5815 size_t data_length )
5816{
5817 if( prf->state != TLS12_PRF_STATE_INIT )
5818 return( PSA_ERROR_BAD_STATE );
5819
Janos Follathd6dce9f2019-07-04 09:11:38 +01005820 if( data_length != 0 )
5821 {
5822 prf->seed = mbedtls_calloc( 1, data_length );
5823 if( prf->seed == NULL )
5824 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005825
Janos Follathd6dce9f2019-07-04 09:11:38 +01005826 memcpy( prf->seed, data, data_length );
5827 prf->seed_length = data_length;
5828 }
Janos Follathf08e2652019-06-13 09:05:41 +01005829
5830 prf->state = TLS12_PRF_STATE_SEED_SET;
5831
5832 return( PSA_SUCCESS );
5833}
5834
Janos Follath81550542019-06-13 14:26:34 +01005835static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5836 psa_algorithm_t hash_alg,
5837 const uint8_t *data,
5838 size_t data_length )
5839{
5840 psa_status_t status;
5841 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5842 return( PSA_ERROR_BAD_STATE );
5843
5844 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5845 if( status != PSA_SUCCESS )
5846 return( status );
5847
5848 prf->state = TLS12_PRF_STATE_KEY_SET;
5849
5850 return( PSA_SUCCESS );
5851}
5852
Janos Follath63028dd2019-06-13 09:15:47 +01005853static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5854 const uint8_t *data,
5855 size_t data_length )
5856{
5857 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5858 return( PSA_ERROR_BAD_STATE );
5859
Janos Follathd6dce9f2019-07-04 09:11:38 +01005860 if( data_length != 0 )
5861 {
5862 prf->label = mbedtls_calloc( 1, data_length );
5863 if( prf->label == NULL )
5864 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005865
Janos Follathd6dce9f2019-07-04 09:11:38 +01005866 memcpy( prf->label, data, data_length );
5867 prf->label_length = data_length;
5868 }
Janos Follath63028dd2019-06-13 09:15:47 +01005869
5870 prf->state = TLS12_PRF_STATE_LABEL_SET;
5871
5872 return( PSA_SUCCESS );
5873}
5874
Janos Follathb03233e2019-06-11 15:30:30 +01005875static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5876 psa_algorithm_t hash_alg,
5877 psa_key_derivation_step_t step,
5878 const uint8_t *data,
5879 size_t data_length )
5880{
Janos Follathb03233e2019-06-11 15:30:30 +01005881 switch( step )
5882 {
Janos Follathf08e2652019-06-13 09:05:41 +01005883 case PSA_KEY_DERIVATION_INPUT_SEED:
5884 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005885 case PSA_KEY_DERIVATION_INPUT_SECRET:
5886 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005887 case PSA_KEY_DERIVATION_INPUT_LABEL:
5888 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005889 default:
5890 return( PSA_ERROR_INVALID_ARGUMENT );
5891 }
5892}
John Durkop07cc04a2020-11-16 22:08:34 -08005893#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5894 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5895
5896#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5897static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5898 psa_tls12_prf_key_derivation_t *prf,
5899 psa_algorithm_t hash_alg,
5900 const uint8_t *data,
5901 size_t data_length )
5902{
5903 psa_status_t status;
5904 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5905 uint8_t *cur = pms;
5906
5907 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5908 return( PSA_ERROR_INVALID_ARGUMENT );
5909
5910 /* Quoting RFC 4279, Section 2:
5911 *
5912 * The premaster secret is formed as follows: if the PSK is N octets
5913 * long, concatenate a uint16 with the value N, N zero octets, a second
5914 * uint16 with the value N, and the PSK itself.
5915 */
5916
5917 *cur++ = ( data_length >> 8 ) & 0xff;
5918 *cur++ = ( data_length >> 0 ) & 0xff;
5919 memset( cur, 0, data_length );
5920 cur += data_length;
5921 *cur++ = pms[0];
5922 *cur++ = pms[1];
5923 memcpy( cur, data, data_length );
5924 cur += data_length;
5925
5926 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5927
5928 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5929 return( status );
5930}
Janos Follath6660f0e2019-06-17 08:44:03 +01005931
5932static psa_status_t psa_tls12_prf_psk_to_ms_input(
5933 psa_tls12_prf_key_derivation_t *prf,
5934 psa_algorithm_t hash_alg,
5935 psa_key_derivation_step_t step,
5936 const uint8_t *data,
5937 size_t data_length )
5938{
5939 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005940 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005941 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5942 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005943 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005944
5945 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5946}
John Durkop07cc04a2020-11-16 22:08:34 -08005947#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005948
Gilles Peskineb8965192019-09-24 16:21:10 +02005949/** Check whether the given key type is acceptable for the given
5950 * input step of a key derivation.
5951 *
5952 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5953 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5954 * Both secret and non-secret inputs can alternatively have the type
5955 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5956 * that the input was passed as a buffer rather than via a key object.
5957 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005958static int psa_key_derivation_check_input_type(
5959 psa_key_derivation_step_t step,
5960 psa_key_type_t key_type )
5961{
5962 switch( step )
5963 {
5964 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005965 if( key_type == PSA_KEY_TYPE_DERIVE )
5966 return( PSA_SUCCESS );
5967 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005968 return( PSA_SUCCESS );
5969 break;
5970 case PSA_KEY_DERIVATION_INPUT_LABEL:
5971 case PSA_KEY_DERIVATION_INPUT_SALT:
5972 case PSA_KEY_DERIVATION_INPUT_INFO:
5973 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005974 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5975 return( PSA_SUCCESS );
5976 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005977 return( PSA_SUCCESS );
5978 break;
5979 }
5980 return( PSA_ERROR_INVALID_ARGUMENT );
5981}
5982
Janos Follathb80a94e2019-06-12 15:54:46 +01005983static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005984 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005985 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005986 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005987 const uint8_t *data,
5988 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005989{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005990 psa_status_t status;
5991 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5992
5993 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005994 if( status != PSA_SUCCESS )
5995 goto exit;
5996
John Durkopd0321952020-10-29 21:37:36 -07005997#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005998 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005999 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006000 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006001 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006002 step, data, data_length );
6003 }
John Durkop07cc04a2020-11-16 22:08:34 -08006004 else
6005#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6006#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6007 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006008 {
Janos Follathb03233e2019-06-11 15:30:30 +01006009 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6010 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6011 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006012 }
John Durkop07cc04a2020-11-16 22:08:34 -08006013 else
6014#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6015#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6016 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006017 {
6018 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6019 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6020 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021 }
6022 else
John Durkop07cc04a2020-11-16 22:08:34 -08006023#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006024 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006025 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006026 return( PSA_ERROR_BAD_STATE );
6027 }
6028
Gilles Peskine224b0d62019-09-23 18:13:17 +02006029exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006030 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006031 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006032 return( status );
6033}
6034
Janos Follath51f4a0f2019-06-14 11:35:55 +01006035psa_status_t psa_key_derivation_input_bytes(
6036 psa_key_derivation_operation_t *operation,
6037 psa_key_derivation_step_t step,
6038 const uint8_t *data,
6039 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006040{
Gilles Peskineb8965192019-09-24 16:21:10 +02006041 return( psa_key_derivation_input_internal( operation, step,
6042 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006043 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006044}
6045
Janos Follath51f4a0f2019-06-14 11:35:55 +01006046psa_status_t psa_key_derivation_input_key(
6047 psa_key_derivation_operation_t *operation,
6048 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006049 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006050{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006052 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006053 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006054
Ronald Cron5c522922020-11-14 16:35:34 +01006055 status = psa_get_and_lock_transparent_key_slot_with_policy(
6056 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006057 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006058 {
6059 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006060 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006061 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006062
6063 /* Passing a key object as a SECRET input unlocks the permission
6064 * to output to a key object. */
6065 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6066 operation->can_output_key = 1;
6067
Ronald Cronf95a2b12020-10-22 15:24:49 +02006068 status = psa_key_derivation_input_internal( operation,
6069 step, slot->attr.type,
6070 slot->data.key.data,
6071 slot->data.key.bytes );
6072
Ronald Cron5c522922020-11-14 16:35:34 +01006073 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006074
Ronald Cron5c522922020-11-14 16:35:34 +01006075 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006076}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006077
6078
6079
6080/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006081/* Key agreement */
6082/****************************************************************/
6083
John Durkop6ba40d12020-11-10 08:50:04 -08006084#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006085static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6086 size_t peer_key_length,
6087 const mbedtls_ecp_keypair *our_key,
6088 uint8_t *shared_secret,
6089 size_t shared_secret_size,
6090 size_t *shared_secret_length )
6091{
Steven Cooremand4867872020-08-05 16:31:39 +02006092 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006093 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006094 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006095 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006096 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006097 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006098
Steven Cooreman4fed4552020-08-03 14:46:03 +02006099 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6100 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006101 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006102 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006103 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006104 goto exit;
6105
Jaeden Amero97271b32019-01-10 19:38:51 +00006106 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006107 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006108 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006109 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006110 status = mbedtls_to_psa_error(
6111 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6112 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006113 goto exit;
6114
Jaeden Amero97271b32019-01-10 19:38:51 +00006115 status = mbedtls_to_psa_error(
6116 mbedtls_ecdh_calc_secret( &ecdh,
6117 shared_secret_length,
6118 shared_secret, shared_secret_size,
6119 mbedtls_ctr_drbg_random,
6120 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006121 if( status != PSA_SUCCESS )
6122 goto exit;
6123 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6124 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006125
6126exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006127 if( status != PSA_SUCCESS )
6128 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006129 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006130 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006131 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006132
Jaeden Amero97271b32019-01-10 19:38:51 +00006133 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006134}
John Durkop6ba40d12020-11-10 08:50:04 -08006135#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006136
Gilles Peskine01d718c2018-09-18 12:01:02 +02006137#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6138
Gilles Peskine0216fe12019-04-11 21:23:21 +02006139static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6140 psa_key_slot_t *private_key,
6141 const uint8_t *peer_key,
6142 size_t peer_key_length,
6143 uint8_t *shared_secret,
6144 size_t shared_secret_size,
6145 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006146{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006147 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006148 {
John Durkop6ba40d12020-11-10 08:50:04 -08006149#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006150 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006151 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006152 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006153 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006154 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006155 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006156 private_key->data.key.data,
6157 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006158 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006159 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006160 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006161 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006162 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006163 shared_secret, shared_secret_size,
6164 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006165 mbedtls_ecp_keypair_free( ecp );
6166 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006167 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006168#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006169 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006170 (void) private_key;
6171 (void) peer_key;
6172 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006173 (void) shared_secret;
6174 (void) shared_secret_size;
6175 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006176 return( PSA_ERROR_NOT_SUPPORTED );
6177 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006178}
6179
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006180/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006181 * to potentially free embedded data structures and wipe confidential data.
6182 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006183static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006184 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006185 psa_key_slot_t *private_key,
6186 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006187 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006188{
6189 psa_status_t status;
6190 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6191 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006192 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006193
6194 /* Step 1: run the secret agreement algorithm to generate the shared
6195 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006196 status = psa_key_agreement_raw_internal( ka_alg,
6197 private_key,
6198 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006199 shared_secret,
6200 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006201 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006202 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006203 goto exit;
6204
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006205 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006206 * the shared secret. A shared secret is permitted wherever a key
6207 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006208 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006209 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006210 shared_secret,
6211 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006212exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006213 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006214 return( status );
6215}
6216
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006217psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006218 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006219 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006220 const uint8_t *peer_key,
6221 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006222{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006224 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006225 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006227 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006228 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006229 status = psa_get_and_lock_transparent_key_slot_with_policy(
6230 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006231 if( status != PSA_SUCCESS )
6232 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006233 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006234 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006235 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006236 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006237 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006238 else
6239 {
6240 /* If a private key has been added as SECRET, we allow the derived
6241 * key material to be used as a key in PSA Crypto. */
6242 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6243 operation->can_output_key = 1;
6244 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006245
Ronald Cron5c522922020-11-14 16:35:34 +01006246 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006247
Ronald Cron5c522922020-11-14 16:35:34 +01006248 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006249}
6250
Gilles Peskinebe697d82019-05-16 18:00:41 +02006251psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006252 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006253 const uint8_t *peer_key,
6254 size_t peer_key_length,
6255 uint8_t *output,
6256 size_t output_size,
6257 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006258{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006261 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006262
6263 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6264 {
6265 status = PSA_ERROR_INVALID_ARGUMENT;
6266 goto exit;
6267 }
Ronald Cron5c522922020-11-14 16:35:34 +01006268 status = psa_get_and_lock_transparent_key_slot_with_policy(
6269 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006270 if( status != PSA_SUCCESS )
6271 goto exit;
6272
6273 status = psa_key_agreement_raw_internal( alg, slot,
6274 peer_key, peer_key_length,
6275 output, output_size,
6276 output_length );
6277
6278exit:
6279 if( status != PSA_SUCCESS )
6280 {
6281 /* If an error happens and is not handled properly, the output
6282 * may be used as a key to protect sensitive data. Arrange for such
6283 * a key to be random, which is likely to result in decryption or
6284 * verification errors. This is better than filling the buffer with
6285 * some constant data such as zeros, which would result in the data
6286 * being protected with a reproducible, easily knowable key.
6287 */
6288 psa_generate_random( output, output_size );
6289 *output_length = output_size;
6290 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006291
Ronald Cron5c522922020-11-14 16:35:34 +01006292 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006293
Ronald Cron5c522922020-11-14 16:35:34 +01006294 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006295}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006296
6297
6298/****************************************************************/
6299/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006300/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006301
Gilles Peskine4c317f42018-07-12 01:24:09 +02006302psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006303 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006304{
Janos Follath24eed8d2019-11-22 13:21:35 +00006305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006306 GUARD_MODULE_INITIALIZED;
6307
Gilles Peskinef181eca2019-08-07 13:49:00 +02006308 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6309 {
6310 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6311 output,
6312 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6313 if( ret != 0 )
6314 return( mbedtls_to_psa_error( ret ) );
6315 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6316 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6317 }
6318
Gilles Peskinee59236f2018-01-27 23:32:46 +01006319 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6320 return( mbedtls_to_psa_error( ret ) );
6321}
6322
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006323#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6324#include "mbedtls/entropy_poll.h"
6325
Gilles Peskine7228da22019-07-15 11:06:15 +02006326psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006327 size_t seed_size )
6328{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006329 if( global_data.initialized )
6330 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006331
6332 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6333 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6334 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6335 return( PSA_ERROR_INVALID_ARGUMENT );
6336
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006337 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006338}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006339#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006340
John Durkop07cc04a2020-11-16 22:08:34 -08006341#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006342static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6343 size_t domain_parameters_size,
6344 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006345{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006346 size_t i;
6347 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006348
Gilles Peskinee56e8782019-04-26 17:34:02 +02006349 if( domain_parameters_size == 0 )
6350 {
6351 *exponent = 65537;
6352 return( PSA_SUCCESS );
6353 }
6354
6355 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6356 * support values that fit in a 32-bit integer, which is larger than
6357 * int on just about every platform anyway. */
6358 if( domain_parameters_size > sizeof( acc ) )
6359 return( PSA_ERROR_NOT_SUPPORTED );
6360 for( i = 0; i < domain_parameters_size; i++ )
6361 acc = ( acc << 8 ) | domain_parameters[i];
6362 if( acc > INT_MAX )
6363 return( PSA_ERROR_NOT_SUPPORTED );
6364 *exponent = acc;
6365 return( PSA_SUCCESS );
6366}
John Durkop07cc04a2020-11-16 22:08:34 -08006367#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006368
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006369static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006370 psa_key_slot_t *slot, size_t bits,
6371 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006372{
Gilles Peskine8e338702019-07-30 20:06:31 +02006373 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006374
Gilles Peskinee56e8782019-04-26 17:34:02 +02006375 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376 return( PSA_ERROR_INVALID_ARGUMENT );
6377
Gilles Peskinee59236f2018-01-27 23:32:46 +01006378 if( key_type_is_raw_bytes( type ) )
6379 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006380 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006381
6382 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006383 if( status != PSA_SUCCESS )
6384 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006385
6386 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006387 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6388 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006389 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006390
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006391 status = psa_generate_random( slot->data.key.data,
6392 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006393 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006394 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006395
6396 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006397#if defined(MBEDTLS_DES_C)
6398 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006399 psa_des_set_key_parity( slot->data.key.data,
6400 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006401#endif /* MBEDTLS_DES_C */
6402 }
6403 else
6404
John Durkop07cc04a2020-11-16 22:08:34 -08006405#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006406 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006407 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006408 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006410 int exponent;
6411 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6413 return( PSA_ERROR_NOT_SUPPORTED );
6414 /* Accept only byte-aligned keys, for the same reasons as
6415 * in psa_import_rsa_key(). */
6416 if( bits % 8 != 0 )
6417 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006418 status = psa_read_rsa_exponent( domain_parameters,
6419 domain_parameters_size,
6420 &exponent );
6421 if( status != PSA_SUCCESS )
6422 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006423 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6424 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006425 mbedtls_ctr_drbg_random,
6426 &global_data.ctr_drbg,
6427 (unsigned int) bits,
6428 exponent );
6429 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006431
6432 /* Make sure to always have an export representation available */
6433 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6434
Steven Cooreman75b74362020-07-28 14:30:13 +02006435 status = psa_allocate_buffer_to_slot( slot, bytes );
6436 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006437 {
6438 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006439 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006441
6442 status = psa_export_rsa_key( type,
6443 &rsa,
6444 slot->data.key.data,
6445 bytes,
6446 &slot->data.key.bytes );
6447 mbedtls_rsa_free( &rsa );
6448 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006449 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006450 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006451 }
6452 else
John Durkop07cc04a2020-11-16 22:08:34 -08006453#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006454
John Durkop9814fa22020-11-04 12:28:15 -08006455#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006456 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006457 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006458 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006459 mbedtls_ecp_group_id grp_id =
6460 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461 const mbedtls_ecp_curve_info *curve_info =
6462 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006463 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006465 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466 return( PSA_ERROR_NOT_SUPPORTED );
6467 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6468 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006469 mbedtls_ecp_keypair_init( &ecp );
6470 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006471 mbedtls_ctr_drbg_random,
6472 &global_data.ctr_drbg );
6473 if( ret != 0 )
6474 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006475 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006476 return( mbedtls_to_psa_error( ret ) );
6477 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006478
6479
6480 /* Make sure to always have an export representation available */
6481 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006482 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6483 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006484 {
6485 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006486 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006487 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006488
6489 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006490 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6491
6492 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006493 if( status != PSA_SUCCESS ) {
6494 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006495 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006496 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006497 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498 }
6499 else
John Durkop9814fa22020-11-04 12:28:15 -08006500#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006501 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006502 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006503 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006504
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006505 return( PSA_SUCCESS );
6506}
Darryl Green0c6575a2018-11-07 16:05:30 +00006507
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006508psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006509 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006510{
6511 psa_status_t status;
6512 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006513 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006514
Ronald Cron81709fc2020-11-14 12:10:32 +01006515 *key = MBEDTLS_SVC_KEY_ID_INIT;
6516
Gilles Peskine0f84d622019-09-12 19:03:13 +02006517 /* Reject any attempt to create a zero-length key so that we don't
6518 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6519 if( psa_get_key_bits( attributes ) == 0 )
6520 return( PSA_ERROR_INVALID_ARGUMENT );
6521
Ronald Cron81709fc2020-11-14 12:10:32 +01006522 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6523 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006524 if( status != PSA_SUCCESS )
6525 goto exit;
6526
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006527 status = psa_driver_wrapper_generate_key( attributes,
6528 slot );
6529 if( status != PSA_ERROR_NOT_SUPPORTED ||
6530 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6531 goto exit;
6532
6533 status = psa_generate_key_internal(
6534 slot, attributes->core.bits,
6535 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006536
6537exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006538 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006539 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006540 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006541 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006542
Darryl Green0c6575a2018-11-07 16:05:30 +00006543 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544}
6545
6546
Gilles Peskinee59236f2018-01-27 23:32:46 +01006547
6548/****************************************************************/
6549/* Module setup */
6550/****************************************************************/
6551
Gilles Peskine5e769522018-11-20 21:59:56 +01006552psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6553 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6554 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6555{
6556 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6557 return( PSA_ERROR_BAD_STATE );
6558 global_data.entropy_init = entropy_init;
6559 global_data.entropy_free = entropy_free;
6560 return( PSA_SUCCESS );
6561}
6562
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563void mbedtls_psa_crypto_free( void )
6564{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006565 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006566 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6567 {
6568 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006569 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006570 }
6571 /* Wipe all remaining data, including configuration.
6572 * In particular, this sets all state indicator to the value
6573 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006574 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006575#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006576 /* Unregister all secure element drivers, so that we restart from
6577 * a pristine state. */
6578 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006579#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006580}
6581
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006582#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6583/** Recover a transaction that was interrupted by a power failure.
6584 *
6585 * This function is called during initialization, before psa_crypto_init()
6586 * returns. If this function returns a failure status, the initialization
6587 * fails.
6588 */
6589static psa_status_t psa_crypto_recover_transaction(
6590 const psa_crypto_transaction_t *transaction )
6591{
6592 switch( transaction->unknown.type )
6593 {
6594 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6595 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006596 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006597 * is implemented.
6598 * https://github.com/ARMmbed/mbed-crypto/issues/218
6599 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006600 default:
6601 /* We found an unsupported transaction in the storage.
6602 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006603 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006604 }
6605}
6606#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6607
Gilles Peskinee59236f2018-01-27 23:32:46 +01006608psa_status_t psa_crypto_init( void )
6609{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006610 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006611 const unsigned char drbg_seed[] = "PSA";
6612
Gilles Peskinec6b69072018-11-20 21:42:52 +01006613 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006614 if( global_data.initialized != 0 )
6615 return( PSA_SUCCESS );
6616
Gilles Peskine5e769522018-11-20 21:59:56 +01006617 /* Set default configuration if
6618 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6619 if( global_data.entropy_init == NULL )
6620 global_data.entropy_init = mbedtls_entropy_init;
6621 if( global_data.entropy_free == NULL )
6622 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006623
Gilles Peskinec6b69072018-11-20 21:42:52 +01006624 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006625 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006626#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6627 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6628 /* The PSA entropy injection feature depends on using NV seed as an entropy
6629 * source. Add NV seed as an entropy source for PSA entropy injection. */
6630 mbedtls_entropy_add_source( &global_data.entropy,
6631 mbedtls_nv_seed_poll, NULL,
6632 MBEDTLS_ENTROPY_BLOCK_SIZE,
6633 MBEDTLS_ENTROPY_SOURCE_STRONG );
6634#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006635 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006636 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006637 status = mbedtls_to_psa_error(
6638 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6639 mbedtls_entropy_func,
6640 &global_data.entropy,
6641 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6642 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006643 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006644 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006645
Gilles Peskine66fb1262018-12-10 16:29:04 +01006646 status = psa_initialize_key_slots( );
6647 if( status != PSA_SUCCESS )
6648 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006649
Gilles Peskined9348f22019-10-01 15:22:29 +02006650#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6651 status = psa_init_all_se_drivers( );
6652 if( status != PSA_SUCCESS )
6653 goto exit;
6654#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6655
Gilles Peskine4b734222019-07-24 15:56:31 +02006656#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006657 status = psa_crypto_load_transaction( );
6658 if( status == PSA_SUCCESS )
6659 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006660 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6661 if( status != PSA_SUCCESS )
6662 goto exit;
6663 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006664 }
6665 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6666 {
6667 /* There's no transaction to complete. It's all good. */
6668 status = PSA_SUCCESS;
6669 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006670#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006671
Gilles Peskinec6b69072018-11-20 21:42:52 +01006672 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006673 global_data.initialized = 1;
6674
6675exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006676 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006677 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006678 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006679}
6680
6681#endif /* MBEDTLS_PSA_CRYPTO_C */