blob: 82b95dc6dd1674b8aad87283bf3ae3e155c6323a [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.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002096 * \return If this function fails, the key slot is an invalid state.
2097 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002098 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002099static psa_status_t psa_finish_key_creation(
2100 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002101 psa_se_drv_table_entry_t *driver,
2102 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002103{
2104 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002105 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002106 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002107
2108#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002109 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002110 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002111#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2112 if( driver != NULL )
2113 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002114 psa_se_key_data_storage_t data;
2115#if defined(static_assert)
2116 static_assert( sizeof( slot->data.se.slot_number ) ==
2117 sizeof( data.slot_number ),
2118 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002119#endif
2120 memcpy( &data.slot_number, &slot->data.se.slot_number,
2121 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002122 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002123 (uint8_t*) &data,
2124 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002125 }
2126 else
2127#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2128 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002129 /* Key material is saved in export representation in the slot, so
2130 * just pass the slot buffer for storage. */
2131 status = psa_save_persistent_key( &slot->attr,
2132 slot->data.key.data,
2133 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002134 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002135 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002136#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2137
Gilles Peskinecbaff462019-07-12 23:46:04 +02002138#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002139 /* Finish the transaction for a key creation. This does not
2140 * happen when registering an existing key. Detect this case
2141 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002142 * creation of a persistent key in a secure element requires a transaction,
2143 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002144 if( driver != NULL &&
2145 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002146 {
2147 status = psa_save_se_persistent_data( driver );
2148 if( status != PSA_SUCCESS )
2149 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002150 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002151 return( status );
2152 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002153 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002154 }
2155#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2156
Ronald Cron50972942020-11-14 11:28:25 +01002157 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002158 {
2159 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002160 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002161 if( status != PSA_SUCCESS )
2162 *key = MBEDTLS_SVC_KEY_ID_INIT;
2163 }
Ronald Cron50972942020-11-14 11:28:25 +01002164
Gilles Peskine4747d192019-04-17 15:05:45 +02002165 return( status );
2166}
2167
2168/** Abort the creation of a key.
2169 *
2170 * You may call this function after calling psa_start_key_creation(),
2171 * or after psa_finish_key_creation() fails. In other circumstances, this
2172 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002173 * See the documentation of psa_start_key_creation() for the intended use
2174 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002175 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002176 * \param[in,out] slot Pointer to the slot with key material.
2177 * \param[in] driver The secure element driver for the key,
2178 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002179 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002180static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002181 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002182{
Gilles Peskine011e4282019-06-26 18:34:38 +02002183 (void) driver;
2184
Gilles Peskine4747d192019-04-17 15:05:45 +02002185 if( slot == NULL )
2186 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002187
2188#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002189 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002190 * element, and the failure happened later (when saving metadata
2191 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002192 * element.
2193 * https://github.com/ARMmbed/mbed-crypto/issues/217
2194 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002195
Gilles Peskined7729582019-08-05 15:55:54 +02002196 /* Abort the ongoing transaction if any (there may not be one if
2197 * the creation process failed before starting one, or if the
2198 * key creation is a registration of a key in a secure element).
2199 * Earlier functions must already have done what it takes to undo any
2200 * partial creation. All that's left is to update the transaction data
2201 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002202 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002203#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2204
Gilles Peskine4747d192019-04-17 15:05:45 +02002205 psa_wipe_key_slot( slot );
2206}
2207
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002208/** Validate optional attributes during key creation.
2209 *
2210 * Some key attributes are optional during key creation. If they are
2211 * specified in the attributes structure, check that they are consistent
2212 * with the data in the slot.
2213 *
2214 * This function should be called near the end of key creation, after
2215 * the slot in memory is fully populated but before saving persistent data.
2216 */
2217static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002218 const psa_key_slot_t *slot,
2219 const psa_key_attributes_t *attributes )
2220{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002221 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002222 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002223 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002224 return( PSA_ERROR_INVALID_ARGUMENT );
2225 }
2226
2227 if( attributes->domain_parameters_size != 0 )
2228 {
John Durkop0e005192020-10-31 22:06:54 -07002229#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2230 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002231 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002232 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002233 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002234 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002236
Steven Cooreman7f391872020-07-30 14:57:44 +02002237 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002238 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002239 slot->data.key.data,
2240 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002241 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002242 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002243 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002244
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002245 mbedtls_mpi_init( &actual );
2246 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002247 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002248 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002249 mbedtls_rsa_free( rsa );
2250 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002251 if( ret != 0 )
2252 goto rsa_exit;
2253 ret = mbedtls_mpi_read_binary( &required,
2254 attributes->domain_parameters,
2255 attributes->domain_parameters_size );
2256 if( ret != 0 )
2257 goto rsa_exit;
2258 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2259 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2260 rsa_exit:
2261 mbedtls_mpi_free( &actual );
2262 mbedtls_mpi_free( &required );
2263 if( ret != 0)
2264 return( mbedtls_to_psa_error( ret ) );
2265 }
2266 else
John Durkop9814fa22020-11-04 12:28:15 -08002267#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2268 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002269 {
2270 return( PSA_ERROR_INVALID_ARGUMENT );
2271 }
2272 }
2273
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002274 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002275 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002276 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002277 return( PSA_ERROR_INVALID_ARGUMENT );
2278 }
2279
2280 return( PSA_SUCCESS );
2281}
2282
Gilles Peskine4747d192019-04-17 15:05:45 +02002283psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002284 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002285 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002286 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002287{
2288 psa_status_t status;
2289 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002290 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002291
Ronald Cron81709fc2020-11-14 12:10:32 +01002292 *key = MBEDTLS_SVC_KEY_ID_INIT;
2293
Gilles Peskine0f84d622019-09-12 19:03:13 +02002294 /* Reject zero-length symmetric keys (including raw data key objects).
2295 * This also rejects any key which might be encoded as an empty string,
2296 * which is never valid. */
2297 if( data_length == 0 )
2298 return( PSA_ERROR_INVALID_ARGUMENT );
2299
Gilles Peskinedf179142019-07-15 22:02:14 +02002300 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002301 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002302 if( status != PSA_SUCCESS )
2303 goto exit;
2304
Gilles Peskine5d309672019-07-12 23:47:28 +02002305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2306 if( driver != NULL )
2307 {
2308 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002309 /* The driver should set the number of key bits, however in
2310 * case it doesn't, we initialize bits to an invalid value. */
2311 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002312 if( drv->key_management == NULL ||
2313 drv->key_management->p_import == NULL )
2314 {
2315 status = PSA_ERROR_NOT_SUPPORTED;
2316 goto exit;
2317 }
2318 status = drv->key_management->p_import(
2319 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002320 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002321 &bits );
2322 if( status != PSA_SUCCESS )
2323 goto exit;
2324 if( bits > PSA_MAX_KEY_BITS )
2325 {
2326 status = PSA_ERROR_NOT_SUPPORTED;
2327 goto exit;
2328 }
2329 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002330 }
2331 else
2332#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2333 {
2334 status = psa_import_key_into_slot( slot, data, data_length );
2335 if( status != PSA_SUCCESS )
2336 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002337 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002338 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002339 if( status != PSA_SUCCESS )
2340 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002341
Ronald Cron81709fc2020-11-14 12:10:32 +01002342 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002343exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002344 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002345 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002346
Gilles Peskine4747d192019-04-17 15:05:45 +02002347 return( status );
2348}
2349
Gilles Peskined7729582019-08-05 15:55:54 +02002350#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2351psa_status_t mbedtls_psa_register_se_key(
2352 const psa_key_attributes_t *attributes )
2353{
2354 psa_status_t status;
2355 psa_key_slot_t *slot = NULL;
2356 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002358
2359 /* Leaving attributes unspecified is not currently supported.
2360 * It could make sense to query the key type and size from the
2361 * secure element, but not all secure elements support this
2362 * and the driver HAL doesn't currently support it. */
2363 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2364 return( PSA_ERROR_NOT_SUPPORTED );
2365 if( psa_get_key_bits( attributes ) == 0 )
2366 return( PSA_ERROR_NOT_SUPPORTED );
2367
2368 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002369 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002370 if( status != PSA_SUCCESS )
2371 goto exit;
2372
Ronald Cron81709fc2020-11-14 12:10:32 +01002373 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002374
2375exit:
2376 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002377 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002378
Gilles Peskined7729582019-08-05 15:55:54 +02002379 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002380 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002381 return( status );
2382}
2383#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2384
Gilles Peskinef603c712019-01-19 13:40:11 +01002385static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002386 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002387{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002388 psa_status_t status = psa_copy_key_material_into_slot( target,
2389 source->data.key.data,
2390 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002391 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002392 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002393
Steven Cooreman398aee52020-10-13 14:35:45 +02002394 target->attr.type = source->attr.type;
2395 target->attr.bits = source->attr.bits;
2396
2397 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002398}
2399
Ronald Croncf56a0a2020-08-04 09:51:30 +02002400psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002401 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002402 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002403{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002405 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002406 psa_key_slot_t *source_slot = NULL;
2407 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002408 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002409 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002410
Ronald Cron81709fc2020-11-14 12:10:32 +01002411 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2412
Ronald Cron5c522922020-11-14 16:35:34 +01002413 status = psa_get_and_lock_transparent_key_slot_with_policy(
2414 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002415 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002416 goto exit;
2417
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002418 status = psa_validate_optional_attributes( source_slot,
2419 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002420 if( status != PSA_SUCCESS )
2421 goto exit;
2422
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002423 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002424 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002425 if( status != PSA_SUCCESS )
2426 goto exit;
2427
Ronald Cron81709fc2020-11-14 12:10:32 +01002428 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2429 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002430 if( status != PSA_SUCCESS )
2431 goto exit;
2432
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002433#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2434 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002435 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002436 /* Copying to a secure element is not implemented yet. */
2437 status = PSA_ERROR_NOT_SUPPORTED;
2438 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002439 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002440#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002441
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002442 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002443 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002444 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002445
Ronald Cron81709fc2020-11-14 12:10:32 +01002446 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002447exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002448 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002449 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002450
Ronald Cron5c522922020-11-14 16:35:34 +01002451 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002452
Ronald Cron5c522922020-11-14 16:35:34 +01002453 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002454}
2455
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002456
2457
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002458/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002459/* Message digests */
2460/****************************************************************/
2461
John Durkop07cc04a2020-11-16 22:08:34 -08002462#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002463 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2464 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002465 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002466static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002467{
2468 switch( alg )
2469 {
John Durkopee4e6602020-11-27 08:48:46 -08002470#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002471 case PSA_ALG_MD2:
2472 return( &mbedtls_md2_info );
2473#endif
John Durkopee4e6602020-11-27 08:48:46 -08002474#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002475 case PSA_ALG_MD4:
2476 return( &mbedtls_md4_info );
2477#endif
John Durkopee4e6602020-11-27 08:48:46 -08002478#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 case PSA_ALG_MD5:
2480 return( &mbedtls_md5_info );
2481#endif
John Durkopee4e6602020-11-27 08:48:46 -08002482#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002483 case PSA_ALG_RIPEMD160:
2484 return( &mbedtls_ripemd160_info );
2485#endif
John Durkopee4e6602020-11-27 08:48:46 -08002486#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002487 case PSA_ALG_SHA_1:
2488 return( &mbedtls_sha1_info );
2489#endif
John Durkopee4e6602020-11-27 08:48:46 -08002490#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 case PSA_ALG_SHA_224:
2492 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002493#endif
2494#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002495 case PSA_ALG_SHA_256:
2496 return( &mbedtls_sha256_info );
2497#endif
John Durkopee4e6602020-11-27 08:48:46 -08002498#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002499 case PSA_ALG_SHA_384:
2500 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002501#endif
John Durkopee4e6602020-11-27 08:48:46 -08002502#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 case PSA_ALG_SHA_512:
2504 return( &mbedtls_sha512_info );
2505#endif
2506 default:
2507 return( NULL );
2508 }
2509}
John Durkop07cc04a2020-11-16 22:08:34 -08002510#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002511 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2512 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2513 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002514
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002515psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2516{
2517 switch( operation->alg )
2518 {
Gilles Peskine81736312018-06-26 15:04:31 +02002519 case 0:
2520 /* The object has (apparently) been initialized but it is not
2521 * in use. It's ok to call abort on such an object, and there's
2522 * nothing to do. */
2523 break;
John Durkopee4e6602020-11-27 08:48:46 -08002524#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002525 case PSA_ALG_MD2:
2526 mbedtls_md2_free( &operation->ctx.md2 );
2527 break;
2528#endif
John Durkopee4e6602020-11-27 08:48:46 -08002529#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002530 case PSA_ALG_MD4:
2531 mbedtls_md4_free( &operation->ctx.md4 );
2532 break;
2533#endif
John Durkopee4e6602020-11-27 08:48:46 -08002534#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535 case PSA_ALG_MD5:
2536 mbedtls_md5_free( &operation->ctx.md5 );
2537 break;
2538#endif
John Durkopee4e6602020-11-27 08:48:46 -08002539#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002540 case PSA_ALG_RIPEMD160:
2541 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2542 break;
2543#endif
John Durkopee4e6602020-11-27 08:48:46 -08002544#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002545 case PSA_ALG_SHA_1:
2546 mbedtls_sha1_free( &operation->ctx.sha1 );
2547 break;
2548#endif
John Durkop6ca23272020-12-03 06:01:32 -08002549#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002550 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002551 mbedtls_sha256_free( &operation->ctx.sha256 );
2552 break;
2553#endif
2554#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002555 case PSA_ALG_SHA_256:
2556 mbedtls_sha256_free( &operation->ctx.sha256 );
2557 break;
2558#endif
John Durkop6ca23272020-12-03 06:01:32 -08002559#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002561 mbedtls_sha512_free( &operation->ctx.sha512 );
2562 break;
2563#endif
2564#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002565 case PSA_ALG_SHA_512:
2566 mbedtls_sha512_free( &operation->ctx.sha512 );
2567 break;
2568#endif
2569 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002570 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 }
2572 operation->alg = 0;
2573 return( PSA_SUCCESS );
2574}
2575
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002576psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577 psa_algorithm_t alg )
2578{
Janos Follath24eed8d2019-11-22 13:21:35 +00002579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002580
2581 /* A context must be freshly initialized before it can be set up. */
2582 if( operation->alg != 0 )
2583 {
2584 return( PSA_ERROR_BAD_STATE );
2585 }
2586
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002587 switch( alg )
2588 {
John Durkopee4e6602020-11-27 08:48:46 -08002589#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002590 case PSA_ALG_MD2:
2591 mbedtls_md2_init( &operation->ctx.md2 );
2592 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2593 break;
2594#endif
John Durkopee4e6602020-11-27 08:48:46 -08002595#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596 case PSA_ALG_MD4:
2597 mbedtls_md4_init( &operation->ctx.md4 );
2598 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2599 break;
2600#endif
John Durkopee4e6602020-11-27 08:48:46 -08002601#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002602 case PSA_ALG_MD5:
2603 mbedtls_md5_init( &operation->ctx.md5 );
2604 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2605 break;
2606#endif
John Durkopee4e6602020-11-27 08:48:46 -08002607#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002608 case PSA_ALG_RIPEMD160:
2609 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2610 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2611 break;
2612#endif
John Durkopee4e6602020-11-27 08:48:46 -08002613#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002614 case PSA_ALG_SHA_1:
2615 mbedtls_sha1_init( &operation->ctx.sha1 );
2616 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2617 break;
2618#endif
John Durkopee4e6602020-11-27 08:48:46 -08002619#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002620 case PSA_ALG_SHA_224:
2621 mbedtls_sha256_init( &operation->ctx.sha256 );
2622 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2623 break;
John Durkopee4e6602020-11-27 08:48:46 -08002624#endif
2625#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626 case PSA_ALG_SHA_256:
2627 mbedtls_sha256_init( &operation->ctx.sha256 );
2628 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2629 break;
2630#endif
John Durkopee4e6602020-11-27 08:48:46 -08002631#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002632 case PSA_ALG_SHA_384:
2633 mbedtls_sha512_init( &operation->ctx.sha512 );
2634 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2635 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002636#endif
John Durkopee4e6602020-11-27 08:48:46 -08002637#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638 case PSA_ALG_SHA_512:
2639 mbedtls_sha512_init( &operation->ctx.sha512 );
2640 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2641 break;
2642#endif
2643 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002644 return( PSA_ALG_IS_HASH( alg ) ?
2645 PSA_ERROR_NOT_SUPPORTED :
2646 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002647 }
2648 if( ret == 0 )
2649 operation->alg = alg;
2650 else
2651 psa_hash_abort( operation );
2652 return( mbedtls_to_psa_error( ret ) );
2653}
2654
2655psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2656 const uint8_t *input,
2657 size_t input_length )
2658{
Janos Follath24eed8d2019-11-22 13:21:35 +00002659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002660
2661 /* Don't require hash implementations to behave correctly on a
2662 * zero-length input, which may have an invalid pointer. */
2663 if( input_length == 0 )
2664 return( PSA_SUCCESS );
2665
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002666 switch( operation->alg )
2667 {
John Durkopee4e6602020-11-27 08:48:46 -08002668#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 case PSA_ALG_MD2:
2670 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2671 input, input_length );
2672 break;
2673#endif
John Durkopee4e6602020-11-27 08:48:46 -08002674#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675 case PSA_ALG_MD4:
2676 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2677 input, input_length );
2678 break;
2679#endif
John Durkopee4e6602020-11-27 08:48:46 -08002680#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002681 case PSA_ALG_MD5:
2682 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2683 input, input_length );
2684 break;
2685#endif
John Durkopee4e6602020-11-27 08:48:46 -08002686#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002687 case PSA_ALG_RIPEMD160:
2688 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2689 input, input_length );
2690 break;
2691#endif
John Durkopee4e6602020-11-27 08:48:46 -08002692#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002693 case PSA_ALG_SHA_1:
2694 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2695 input, input_length );
2696 break;
2697#endif
John Durkop6ca23272020-12-03 06:01:32 -08002698#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002699 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002700 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2701 input, input_length );
2702 break;
2703#endif
2704#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002705 case PSA_ALG_SHA_256:
2706 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2707 input, input_length );
2708 break;
2709#endif
John Durkop6ca23272020-12-03 06:01:32 -08002710#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002711 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002712 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2713 input, input_length );
2714 break;
2715#endif
2716#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717 case PSA_ALG_SHA_512:
2718 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2719 input, input_length );
2720 break;
2721#endif
2722 default:
John Durkopee4e6602020-11-27 08:48:46 -08002723 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002724 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002725 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002726
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002727 if( ret != 0 )
2728 psa_hash_abort( operation );
2729 return( mbedtls_to_psa_error( ret ) );
2730}
2731
2732psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2733 uint8_t *hash,
2734 size_t hash_size,
2735 size_t *hash_length )
2736{
itayzafrir40835d42018-08-02 13:14:17 +03002737 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002739 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002740
2741 /* Fill the output buffer with something that isn't a valid hash
2742 * (barring an attack on the hash and deliberately-crafted input),
2743 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002744 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002745 /* If hash_size is 0 then hash may be NULL and then the
2746 * call to memset would have undefined behavior. */
2747 if( hash_size != 0 )
2748 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002749
2750 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002751 {
2752 status = PSA_ERROR_BUFFER_TOO_SMALL;
2753 goto exit;
2754 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002755
2756 switch( operation->alg )
2757 {
John Durkopee4e6602020-11-27 08:48:46 -08002758#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002759 case PSA_ALG_MD2:
2760 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2761 break;
2762#endif
John Durkopee4e6602020-11-27 08:48:46 -08002763#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002764 case PSA_ALG_MD4:
2765 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2766 break;
2767#endif
John Durkopee4e6602020-11-27 08:48:46 -08002768#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002769 case PSA_ALG_MD5:
2770 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2771 break;
2772#endif
John Durkopee4e6602020-11-27 08:48:46 -08002773#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002774 case PSA_ALG_RIPEMD160:
2775 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2776 break;
2777#endif
John Durkopee4e6602020-11-27 08:48:46 -08002778#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002779 case PSA_ALG_SHA_1:
2780 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2781 break;
2782#endif
John Durkop6ca23272020-12-03 06:01:32 -08002783#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002784 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002785 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2786 break;
2787#endif
2788#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789 case PSA_ALG_SHA_256:
2790 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2791 break;
2792#endif
John Durkop6ca23272020-12-03 06:01:32 -08002793#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002794 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002795 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2796 break;
2797#endif
2798#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002799 case PSA_ALG_SHA_512:
2800 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2801 break;
2802#endif
2803 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002804 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002805 }
itayzafrir40835d42018-08-02 13:14:17 +03002806 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807
itayzafrir40835d42018-08-02 13:14:17 +03002808exit:
2809 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002810 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002811 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002812 return( psa_hash_abort( operation ) );
2813 }
2814 else
2815 {
2816 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002817 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002818 }
2819}
2820
Gilles Peskine2d277862018-06-18 15:41:12 +02002821psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2822 const uint8_t *hash,
2823 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002824{
2825 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2826 size_t actual_hash_length;
2827 psa_status_t status = psa_hash_finish( operation,
2828 actual_hash, sizeof( actual_hash ),
2829 &actual_hash_length );
2830 if( status != PSA_SUCCESS )
2831 return( status );
2832 if( actual_hash_length != hash_length )
2833 return( PSA_ERROR_INVALID_SIGNATURE );
2834 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2835 return( PSA_ERROR_INVALID_SIGNATURE );
2836 return( PSA_SUCCESS );
2837}
2838
Gilles Peskine0a749c82019-11-28 19:33:58 +01002839psa_status_t psa_hash_compute( psa_algorithm_t alg,
2840 const uint8_t *input, size_t input_length,
2841 uint8_t *hash, size_t hash_size,
2842 size_t *hash_length )
2843{
2844 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2845 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2846
2847 *hash_length = hash_size;
2848 status = psa_hash_setup( &operation, alg );
2849 if( status != PSA_SUCCESS )
2850 goto exit;
2851 status = psa_hash_update( &operation, input, input_length );
2852 if( status != PSA_SUCCESS )
2853 goto exit;
2854 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2855 if( status != PSA_SUCCESS )
2856 goto exit;
2857
2858exit:
2859 if( status == PSA_SUCCESS )
2860 status = psa_hash_abort( &operation );
2861 else
2862 psa_hash_abort( &operation );
2863 return( status );
2864}
2865
2866psa_status_t psa_hash_compare( psa_algorithm_t alg,
2867 const uint8_t *input, size_t input_length,
2868 const uint8_t *hash, size_t hash_length )
2869{
2870 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2871 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2872
2873 status = psa_hash_setup( &operation, alg );
2874 if( status != PSA_SUCCESS )
2875 goto exit;
2876 status = psa_hash_update( &operation, input, input_length );
2877 if( status != PSA_SUCCESS )
2878 goto exit;
2879 status = psa_hash_verify( &operation, hash, hash_length );
2880 if( status != PSA_SUCCESS )
2881 goto exit;
2882
2883exit:
2884 if( status == PSA_SUCCESS )
2885 status = psa_hash_abort( &operation );
2886 else
2887 psa_hash_abort( &operation );
2888 return( status );
2889}
2890
Gilles Peskineeb35d782019-01-22 17:56:16 +01002891psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2892 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002893{
2894 if( target_operation->alg != 0 )
2895 return( PSA_ERROR_BAD_STATE );
2896
2897 switch( source_operation->alg )
2898 {
2899 case 0:
2900 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002901#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002902 case PSA_ALG_MD2:
2903 mbedtls_md2_clone( &target_operation->ctx.md2,
2904 &source_operation->ctx.md2 );
2905 break;
2906#endif
John Durkopee4e6602020-11-27 08:48:46 -08002907#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002908 case PSA_ALG_MD4:
2909 mbedtls_md4_clone( &target_operation->ctx.md4,
2910 &source_operation->ctx.md4 );
2911 break;
2912#endif
John Durkopee4e6602020-11-27 08:48:46 -08002913#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002914 case PSA_ALG_MD5:
2915 mbedtls_md5_clone( &target_operation->ctx.md5,
2916 &source_operation->ctx.md5 );
2917 break;
2918#endif
John Durkopee4e6602020-11-27 08:48:46 -08002919#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002920 case PSA_ALG_RIPEMD160:
2921 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2922 &source_operation->ctx.ripemd160 );
2923 break;
2924#endif
John Durkopee4e6602020-11-27 08:48:46 -08002925#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002926 case PSA_ALG_SHA_1:
2927 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2928 &source_operation->ctx.sha1 );
2929 break;
2930#endif
John Durkop6ca23272020-12-03 06:01:32 -08002931#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002932 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002933 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2934 &source_operation->ctx.sha256 );
2935 break;
2936#endif
2937#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002938 case PSA_ALG_SHA_256:
2939 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2940 &source_operation->ctx.sha256 );
2941 break;
2942#endif
John Durkop6ca23272020-12-03 06:01:32 -08002943#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002944 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002945 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2946 &source_operation->ctx.sha512 );
2947 break;
2948#endif
2949#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002950 case PSA_ALG_SHA_512:
2951 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2952 &source_operation->ctx.sha512 );
2953 break;
2954#endif
2955 default:
2956 return( PSA_ERROR_NOT_SUPPORTED );
2957 }
2958
2959 target_operation->alg = source_operation->alg;
2960 return( PSA_SUCCESS );
2961}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002962
2963
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002964/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965/* MAC */
2966/****************************************************************/
2967
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002968static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969 psa_algorithm_t alg,
2970 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002971 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002972 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002973{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002975 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002977 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002978 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002979
Gilles Peskine8c9def32018-02-08 10:02:12 +01002980 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2981 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002982 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002983 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002984 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002985 mode = MBEDTLS_MODE_STREAM;
2986 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002987 case PSA_ALG_CTR:
2988 mode = MBEDTLS_MODE_CTR;
2989 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002990 case PSA_ALG_CFB:
2991 mode = MBEDTLS_MODE_CFB;
2992 break;
2993 case PSA_ALG_OFB:
2994 mode = MBEDTLS_MODE_OFB;
2995 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002996 case PSA_ALG_ECB_NO_PADDING:
2997 mode = MBEDTLS_MODE_ECB;
2998 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002999 case PSA_ALG_CBC_NO_PADDING:
3000 mode = MBEDTLS_MODE_CBC;
3001 break;
3002 case PSA_ALG_CBC_PKCS7:
3003 mode = MBEDTLS_MODE_CBC;
3004 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003005 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006 mode = MBEDTLS_MODE_CCM;
3007 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003008 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009 mode = MBEDTLS_MODE_GCM;
3010 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003011 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3012 mode = MBEDTLS_MODE_CHACHAPOLY;
3013 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 default:
3015 return( NULL );
3016 }
3017 }
3018 else if( alg == PSA_ALG_CMAC )
3019 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 else
3021 return( NULL );
3022
3023 switch( key_type )
3024 {
3025 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003026 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027 break;
3028 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003029 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3030 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003032 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003034 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003035 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3036 * but two-key Triple-DES is functionally three-key Triple-DES
3037 * with K1=K3, so that's how we present it to mbedtls. */
3038 if( key_bits == 128 )
3039 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040 break;
3041 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003042 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 break;
3044 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003045 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003047 case PSA_KEY_TYPE_CHACHA20:
3048 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3049 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050 default:
3051 return( NULL );
3052 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003053 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003054 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003055
Jaeden Amero23bbb752018-06-26 14:16:54 +01003056 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3057 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058}
3059
John Durkop6ba40d12020-11-10 08:50:04 -08003060#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003061static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003062{
Gilles Peskine2d277862018-06-18 15:41:12 +02003063 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003064 {
3065 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003066 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003067 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003068 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003069 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003070 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003071 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003072 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003073 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003074 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003075 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003076 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003077 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003078 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003079 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003080 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003081 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003082 return( 128 );
3083 default:
3084 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003085 }
3086}
John Durkop07cc04a2020-11-16 22:08:34 -08003087#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003088
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003089/* Initialize the MAC operation structure. Once this function has been
3090 * called, psa_mac_abort can run and will do the right thing. */
3091static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3092 psa_algorithm_t alg )
3093{
3094 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3095
3096 operation->alg = alg;
3097 operation->key_set = 0;
3098 operation->iv_set = 0;
3099 operation->iv_required = 0;
3100 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003101 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003102
3103#if defined(MBEDTLS_CMAC_C)
3104 if( alg == PSA_ALG_CMAC )
3105 {
3106 operation->iv_required = 0;
3107 mbedtls_cipher_init( &operation->ctx.cmac );
3108 status = PSA_SUCCESS;
3109 }
3110 else
3111#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003112#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003113 if( PSA_ALG_IS_HMAC( operation->alg ) )
3114 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003115 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3116 operation->ctx.hmac.hash_ctx.alg = 0;
3117 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003118 }
3119 else
John Durkopd0321952020-10-29 21:37:36 -07003120#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003121 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003122 if( ! PSA_ALG_IS_MAC( alg ) )
3123 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003124 }
3125
3126 if( status != PSA_SUCCESS )
3127 memset( operation, 0, sizeof( *operation ) );
3128 return( status );
3129}
3130
John Durkop6ba40d12020-11-10 08:50:04 -08003131#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003132static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3133{
Gilles Peskine3f108122018-12-07 18:14:53 +01003134 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003135 return( psa_hash_abort( &hmac->hash_ctx ) );
3136}
John Durkop6ba40d12020-11-10 08:50:04 -08003137#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003138
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3140{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003141 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143 /* The object has (apparently) been initialized but it is not
3144 * in use. It's ok to call abort on such an object, and there's
3145 * nothing to do. */
3146 return( PSA_SUCCESS );
3147 }
3148 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003149#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003150 if( operation->alg == PSA_ALG_CMAC )
3151 {
3152 mbedtls_cipher_free( &operation->ctx.cmac );
3153 }
3154 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003155#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003156#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003157 if( PSA_ALG_IS_HMAC( operation->alg ) )
3158 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003159 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003160 }
3161 else
John Durkopd0321952020-10-29 21:37:36 -07003162#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003163 {
3164 /* Sanity check (shouldn't happen: operation->alg should
3165 * always have been initialized to a valid value). */
3166 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003167 }
Moran Peker41deec42018-04-04 15:43:05 +03003168
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169 operation->alg = 0;
3170 operation->key_set = 0;
3171 operation->iv_set = 0;
3172 operation->iv_required = 0;
3173 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003174 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003175
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003177
3178bad_state:
3179 /* If abort is called on an uninitialized object, we can't trust
3180 * anything. Wipe the object in case it contains confidential data.
3181 * This may result in a memory leak if a pointer gets overwritten,
3182 * but it's too late to do anything about this. */
3183 memset( operation, 0, sizeof( *operation ) );
3184 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003185}
3186
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003187#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003188static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003189 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003190 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003191 const mbedtls_cipher_info_t *cipher_info )
3192{
Janos Follath24eed8d2019-11-22 13:21:35 +00003193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003194
3195 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003196
3197 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3198 if( ret != 0 )
3199 return( ret );
3200
3201 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003202 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003203 key_bits );
3204 return( ret );
3205}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003206#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003207
John Durkop6ba40d12020-11-10 08:50:04 -08003208#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003209static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3210 const uint8_t *key,
3211 size_t key_length,
3212 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003213{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003214 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003215 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003216 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003217 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003218 psa_status_t status;
3219
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003220 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3221 * overflow below. This should never trigger if the hash algorithm
3222 * is implemented correctly. */
3223 /* The size checks against the ipad and opad buffers cannot be written
3224 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3225 * because that triggers -Wlogical-op on GCC 7.3. */
3226 if( block_size > sizeof( ipad ) )
3227 return( PSA_ERROR_NOT_SUPPORTED );
3228 if( block_size > sizeof( hmac->opad ) )
3229 return( PSA_ERROR_NOT_SUPPORTED );
3230 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003231 return( PSA_ERROR_NOT_SUPPORTED );
3232
Gilles Peskined223b522018-06-11 18:12:58 +02003233 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003234 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003235 status = psa_hash_compute( hash_alg, key, key_length,
3236 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003237 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003238 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003239 }
Gilles Peskine96889972018-07-12 17:07:03 +02003240 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3241 * but it is permitted. It is common when HMAC is used in HKDF, for
3242 * example. Don't call `memcpy` in the 0-length because `key` could be
3243 * an invalid pointer which would make the behavior undefined. */
3244 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003245 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003246
Gilles Peskined223b522018-06-11 18:12:58 +02003247 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3248 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003249 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003250 ipad[i] ^= 0x36;
3251 memset( ipad + key_length, 0x36, block_size - key_length );
3252
3253 /* Copy the key material from ipad to opad, flipping the requisite bits,
3254 * and filling the rest of opad with the requisite constant. */
3255 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003256 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3257 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003258
Gilles Peskine01126fa2018-07-12 17:04:55 +02003259 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003260 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003261 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003262
Gilles Peskine01126fa2018-07-12 17:04:55 +02003263 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003264
3265cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003266 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003267
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003268 return( status );
3269}
John Durkop6ba40d12020-11-10 08:50:04 -08003270#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003271
Gilles Peskine89167cb2018-07-08 20:12:23 +02003272static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003273 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003274 psa_algorithm_t alg,
3275 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003276{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003277 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003278 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003279 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003280 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003281 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003282 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003283 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003284 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003285
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003286 /* A context must be freshly initialized before it can be set up. */
3287 if( operation->alg != 0 )
3288 {
3289 return( PSA_ERROR_BAD_STATE );
3290 }
3291
Gilles Peskined911eb72018-08-14 15:18:45 +02003292 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003293 if( status != PSA_SUCCESS )
3294 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003295 if( is_sign )
3296 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003297
Ronald Cron5c522922020-11-14 16:35:34 +01003298 status = psa_get_and_lock_transparent_key_slot_with_policy(
3299 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003300 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003301 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003302 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003303
Gilles Peskine8c9def32018-02-08 10:02:12 +01003304#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003305 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003306 {
3307 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003308 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003309 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003311 if( cipher_info == NULL )
3312 {
3313 status = PSA_ERROR_NOT_SUPPORTED;
3314 goto exit;
3315 }
3316 operation->mac_size = cipher_info->block_size;
3317 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3318 status = mbedtls_to_psa_error( ret );
3319 }
3320 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003321#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003322#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003323 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003324 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003325 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003326 if( hash_alg == 0 )
3327 {
3328 status = PSA_ERROR_NOT_SUPPORTED;
3329 goto exit;
3330 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003331
3332 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3333 /* Sanity check. This shouldn't fail on a valid configuration. */
3334 if( operation->mac_size == 0 ||
3335 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3336 {
3337 status = PSA_ERROR_NOT_SUPPORTED;
3338 goto exit;
3339 }
3340
Gilles Peskine8e338702019-07-30 20:06:31 +02003341 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003342 {
3343 status = PSA_ERROR_INVALID_ARGUMENT;
3344 goto exit;
3345 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003346
Gilles Peskine01126fa2018-07-12 17:04:55 +02003347 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003348 slot->data.key.data,
3349 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003350 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003351 }
3352 else
John Durkopd0321952020-10-29 21:37:36 -07003353#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003354 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003355 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003356 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003357 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003358
Gilles Peskined911eb72018-08-14 15:18:45 +02003359 if( truncated == 0 )
3360 {
3361 /* The "normal" case: untruncated algorithm. Nothing to do. */
3362 }
3363 else if( truncated < 4 )
3364 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003365 /* A very short MAC is too short for security since it can be
3366 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3367 * so we make this our minimum, even though 32 bits is still
3368 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003369 status = PSA_ERROR_NOT_SUPPORTED;
3370 }
3371 else if( truncated > operation->mac_size )
3372 {
3373 /* It's impossible to "truncate" to a larger length. */
3374 status = PSA_ERROR_INVALID_ARGUMENT;
3375 }
3376 else
3377 operation->mac_size = truncated;
3378
Gilles Peskinefbfac682018-07-08 20:51:54 +02003379exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003380 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003381 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003382 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003384 else
3385 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003386 operation->key_set = 1;
3387 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003388
Ronald Cron5c522922020-11-14 16:35:34 +01003389 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003390
Ronald Cron5c522922020-11-14 16:35:34 +01003391 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003392}
3393
Gilles Peskine89167cb2018-07-08 20:12:23 +02003394psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003395 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003396 psa_algorithm_t alg )
3397{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003398 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003399}
3400
3401psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003402 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003403 psa_algorithm_t alg )
3404{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003405 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003406}
3407
Gilles Peskine8c9def32018-02-08 10:02:12 +01003408psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3409 const uint8_t *input,
3410 size_t input_length )
3411{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003412 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003413 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003414 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003416 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003417 operation->has_input = 1;
3418
Gilles Peskine8c9def32018-02-08 10:02:12 +01003419#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003420 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003421 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003422 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3423 input, input_length );
3424 status = mbedtls_to_psa_error( ret );
3425 }
3426 else
3427#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003428#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003429 if( PSA_ALG_IS_HMAC( operation->alg ) )
3430 {
3431 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3432 input_length );
3433 }
3434 else
John Durkopd0321952020-10-29 21:37:36 -07003435#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003436 {
3437 /* This shouldn't happen if `operation` was initialized by
3438 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003439 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003440 }
3441
Gilles Peskinefbfac682018-07-08 20:51:54 +02003442 if( status != PSA_SUCCESS )
3443 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003444 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003445}
3446
John Durkop6ba40d12020-11-10 08:50:04 -08003447#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003448static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3449 uint8_t *mac,
3450 size_t mac_size )
3451{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003452 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003453 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3454 size_t hash_size = 0;
3455 size_t block_size = psa_get_hash_block_size( hash_alg );
3456 psa_status_t status;
3457
Gilles Peskine01126fa2018-07-12 17:04:55 +02003458 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3459 if( status != PSA_SUCCESS )
3460 return( status );
3461 /* From here on, tmp needs to be wiped. */
3462
3463 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3464 if( status != PSA_SUCCESS )
3465 goto exit;
3466
3467 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3468 if( status != PSA_SUCCESS )
3469 goto exit;
3470
3471 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3472 if( status != PSA_SUCCESS )
3473 goto exit;
3474
Gilles Peskined911eb72018-08-14 15:18:45 +02003475 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3476 if( status != PSA_SUCCESS )
3477 goto exit;
3478
3479 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003480
3481exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003482 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003483 return( status );
3484}
John Durkop6ba40d12020-11-10 08:50:04 -08003485#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003486
mohammad16036df908f2018-04-02 08:34:15 -07003487static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003488 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003489 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003490{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003491 if( ! operation->key_set )
3492 return( PSA_ERROR_BAD_STATE );
3493 if( operation->iv_required && ! operation->iv_set )
3494 return( PSA_ERROR_BAD_STATE );
3495
Gilles Peskine8c9def32018-02-08 10:02:12 +01003496 if( mac_size < operation->mac_size )
3497 return( PSA_ERROR_BUFFER_TOO_SMALL );
3498
Gilles Peskine8c9def32018-02-08 10:02:12 +01003499#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003500 if( operation->alg == PSA_ALG_CMAC )
3501 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003502 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3503 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3504 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003505 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003506 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003507 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003508 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003509 else
3510#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003511#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003512 if( PSA_ALG_IS_HMAC( operation->alg ) )
3513 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003514 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003515 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003516 }
3517 else
John Durkopd0321952020-10-29 21:37:36 -07003518#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003519 {
3520 /* This shouldn't happen if `operation` was initialized by
3521 * a setup function. */
3522 return( PSA_ERROR_BAD_STATE );
3523 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003524}
3525
Gilles Peskineacd4be32018-07-08 19:56:25 +02003526psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3527 uint8_t *mac,
3528 size_t mac_size,
3529 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003530{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003531 psa_status_t status;
3532
Jaeden Amero252ef282019-02-15 14:05:35 +00003533 if( operation->alg == 0 )
3534 {
3535 return( PSA_ERROR_BAD_STATE );
3536 }
3537
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003538 /* Fill the output buffer with something that isn't a valid mac
3539 * (barring an attack on the mac and deliberately-crafted input),
3540 * in case the caller doesn't check the return status properly. */
3541 *mac_length = mac_size;
3542 /* If mac_size is 0 then mac may be NULL and then the
3543 * call to memset would have undefined behavior. */
3544 if( mac_size != 0 )
3545 memset( mac, '!', mac_size );
3546
Gilles Peskine89167cb2018-07-08 20:12:23 +02003547 if( ! operation->is_sign )
3548 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003549 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003550 }
mohammad16036df908f2018-04-02 08:34:15 -07003551
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003552 status = psa_mac_finish_internal( operation, mac, mac_size );
3553
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003554 if( status == PSA_SUCCESS )
3555 {
3556 status = psa_mac_abort( operation );
3557 if( status == PSA_SUCCESS )
3558 *mac_length = operation->mac_size;
3559 else
3560 memset( mac, '!', mac_size );
3561 }
3562 else
3563 psa_mac_abort( operation );
3564 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003565}
3566
Gilles Peskineacd4be32018-07-08 19:56:25 +02003567psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3568 const uint8_t *mac,
3569 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003570{
Gilles Peskine828ed142018-06-18 23:25:51 +02003571 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003572 psa_status_t status;
3573
Jaeden Amero252ef282019-02-15 14:05:35 +00003574 if( operation->alg == 0 )
3575 {
3576 return( PSA_ERROR_BAD_STATE );
3577 }
3578
Gilles Peskine89167cb2018-07-08 20:12:23 +02003579 if( operation->is_sign )
3580 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003581 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003582 }
3583 if( operation->mac_size != mac_length )
3584 {
3585 status = PSA_ERROR_INVALID_SIGNATURE;
3586 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003587 }
mohammad16036df908f2018-04-02 08:34:15 -07003588
3589 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003590 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003591 if( status != PSA_SUCCESS )
3592 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003593
3594 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3595 status = PSA_ERROR_INVALID_SIGNATURE;
3596
3597cleanup:
3598 if( status == PSA_SUCCESS )
3599 status = psa_mac_abort( operation );
3600 else
3601 psa_mac_abort( operation );
3602
Gilles Peskine3f108122018-12-07 18:14:53 +01003603 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003604
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003605 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003606}
3607
3608
Gilles Peskine20035e32018-02-03 22:44:14 +01003609
Gilles Peskine20035e32018-02-03 22:44:14 +01003610/****************************************************************/
3611/* Asymmetric cryptography */
3612/****************************************************************/
3613
John Durkop6ba40d12020-11-10 08:50:04 -08003614#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3615 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003616/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003617 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003618static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3619 size_t hash_length,
3620 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003621{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003622 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003623 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003624 *md_alg = mbedtls_md_get_type( md_info );
3625
3626 /* The Mbed TLS RSA module uses an unsigned int for hash length
3627 * parameters. Validate that it fits so that we don't risk an
3628 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003629#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003630 if( hash_length > UINT_MAX )
3631 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003632#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003633
John Durkop0e005192020-10-31 22:06:54 -07003634#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003635 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3636 * must be correct. */
3637 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3638 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003639 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003640 if( md_info == NULL )
3641 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003642 if( mbedtls_md_get_size( md_info ) != hash_length )
3643 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003644 }
John Durkop0e005192020-10-31 22:06:54 -07003645#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003646
John Durkop0e005192020-10-31 22:06:54 -07003647#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003648 /* PSS requires a hash internally. */
3649 if( PSA_ALG_IS_RSA_PSS( alg ) )
3650 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003651 if( md_info == NULL )
3652 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003653 }
John Durkop0e005192020-10-31 22:06:54 -07003654#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003655
Gilles Peskine61b91d42018-06-08 16:09:36 +02003656 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003657}
3658
Gilles Peskine2b450e32018-06-27 15:42:46 +02003659static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3660 psa_algorithm_t alg,
3661 const uint8_t *hash,
3662 size_t hash_length,
3663 uint8_t *signature,
3664 size_t signature_size,
3665 size_t *signature_length )
3666{
3667 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003669 mbedtls_md_type_t md_alg;
3670
3671 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3672 if( status != PSA_SUCCESS )
3673 return( status );
3674
Gilles Peskine630a18a2018-06-29 17:49:35 +02003675 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003676 return( PSA_ERROR_BUFFER_TOO_SMALL );
3677
John Durkop0e005192020-10-31 22:06:54 -07003678#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003679 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3680 {
3681 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3682 MBEDTLS_MD_NONE );
3683 ret = mbedtls_rsa_pkcs1_sign( rsa,
3684 mbedtls_ctr_drbg_random,
3685 &global_data.ctr_drbg,
3686 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003687 md_alg,
3688 (unsigned int) hash_length,
3689 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003690 signature );
3691 }
3692 else
John Durkop0e005192020-10-31 22:06:54 -07003693#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3694#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003695 if( PSA_ALG_IS_RSA_PSS( alg ) )
3696 {
3697 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3698 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3699 mbedtls_ctr_drbg_random,
3700 &global_data.ctr_drbg,
3701 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003702 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003703 (unsigned int) hash_length,
3704 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003705 signature );
3706 }
3707 else
John Durkop0e005192020-10-31 22:06:54 -07003708#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003709 {
3710 return( PSA_ERROR_INVALID_ARGUMENT );
3711 }
3712
3713 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003714 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003715 return( mbedtls_to_psa_error( ret ) );
3716}
3717
3718static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3719 psa_algorithm_t alg,
3720 const uint8_t *hash,
3721 size_t hash_length,
3722 const uint8_t *signature,
3723 size_t signature_length )
3724{
3725 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003727 mbedtls_md_type_t md_alg;
3728
3729 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3730 if( status != PSA_SUCCESS )
3731 return( status );
3732
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003733 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3734 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003735
John Durkop0e005192020-10-31 22:06:54 -07003736#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003737 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3738 {
3739 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3740 MBEDTLS_MD_NONE );
3741 ret = mbedtls_rsa_pkcs1_verify( rsa,
3742 mbedtls_ctr_drbg_random,
3743 &global_data.ctr_drbg,
3744 MBEDTLS_RSA_PUBLIC,
3745 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003746 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003747 hash,
3748 signature );
3749 }
3750 else
John Durkop0e005192020-10-31 22:06:54 -07003751#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3752#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003753 if( PSA_ALG_IS_RSA_PSS( alg ) )
3754 {
3755 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3756 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3757 mbedtls_ctr_drbg_random,
3758 &global_data.ctr_drbg,
3759 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003760 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003761 (unsigned int) hash_length,
3762 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003763 signature );
3764 }
3765 else
John Durkop0e005192020-10-31 22:06:54 -07003766#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003767 {
3768 return( PSA_ERROR_INVALID_ARGUMENT );
3769 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003770
3771 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3772 * the rest of the signature is invalid". This has little use in
3773 * practice and PSA doesn't report this distinction. */
3774 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3775 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003776 return( mbedtls_to_psa_error( ret ) );
3777}
John Durkop6ba40d12020-11-10 08:50:04 -08003778#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3779 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003780
John Durkop6ba40d12020-11-10 08:50:04 -08003781#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3782 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003783/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3784 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3785 * (even though these functions don't modify it). */
3786static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3787 psa_algorithm_t alg,
3788 const uint8_t *hash,
3789 size_t hash_length,
3790 uint8_t *signature,
3791 size_t signature_size,
3792 size_t *signature_length )
3793{
Janos Follath24eed8d2019-11-22 13:21:35 +00003794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003795 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003796 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797 mbedtls_mpi_init( &r );
3798 mbedtls_mpi_init( &s );
3799
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003800 if( signature_size < 2 * curve_bytes )
3801 {
3802 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3803 goto cleanup;
3804 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003805
John Durkop0ea39e02020-10-13 19:58:20 -07003806#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003807 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3808 {
3809 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3810 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3811 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003812 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3813 &ecp->d, hash,
3814 hash_length, md_alg,
3815 mbedtls_ctr_drbg_random,
3816 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003817 }
3818 else
John Durkop0ea39e02020-10-13 19:58:20 -07003819#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003820 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003821 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003822 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3823 hash, hash_length,
3824 mbedtls_ctr_drbg_random,
3825 &global_data.ctr_drbg ) );
3826 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003827
3828 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3829 signature,
3830 curve_bytes ) );
3831 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3832 signature + curve_bytes,
3833 curve_bytes ) );
3834
3835cleanup:
3836 mbedtls_mpi_free( &r );
3837 mbedtls_mpi_free( &s );
3838 if( ret == 0 )
3839 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003840 return( mbedtls_to_psa_error( ret ) );
3841}
3842
3843static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3844 const uint8_t *hash,
3845 size_t hash_length,
3846 const uint8_t *signature,
3847 size_t signature_length )
3848{
Janos Follath24eed8d2019-11-22 13:21:35 +00003849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003850 mbedtls_mpi r, s;
3851 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3852 mbedtls_mpi_init( &r );
3853 mbedtls_mpi_init( &s );
3854
3855 if( signature_length != 2 * curve_bytes )
3856 return( PSA_ERROR_INVALID_SIGNATURE );
3857
3858 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3859 signature,
3860 curve_bytes ) );
3861 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3862 signature + curve_bytes,
3863 curve_bytes ) );
3864
Steven Cooremanacda8342020-07-24 23:09:52 +02003865 /* Check whether the public part is loaded. If not, load it. */
3866 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3867 {
3868 MBEDTLS_MPI_CHK(
3869 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3870 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3871 }
3872
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003873 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3874 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003875
3876cleanup:
3877 mbedtls_mpi_free( &r );
3878 mbedtls_mpi_free( &s );
3879 return( mbedtls_to_psa_error( ret ) );
3880}
John Durkop6ba40d12020-11-10 08:50:04 -08003881#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3882 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003883
Ronald Croncf56a0a2020-08-04 09:51:30 +02003884psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003885 psa_algorithm_t alg,
3886 const uint8_t *hash,
3887 size_t hash_length,
3888 uint8_t *signature,
3889 size_t signature_size,
3890 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003891{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003893 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003894 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003895
3896 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003897 /* Immediately reject a zero-length signature buffer. This guarantees
3898 * that signature must be a valid pointer. (On the other hand, the hash
3899 * buffer can in principle be empty since it doesn't actually have
3900 * to be a hash.) */
3901 if( signature_size == 0 )
3902 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003903
Ronald Cron5c522922020-11-14 16:35:34 +01003904 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3905 PSA_KEY_USAGE_SIGN_HASH,
3906 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003907 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003908 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003909 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003910 {
3911 status = PSA_ERROR_INVALID_ARGUMENT;
3912 goto exit;
3913 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003914
Steven Cooremancd84cb42020-07-16 20:28:36 +02003915 /* Try any of the available accelerators first */
3916 status = psa_driver_wrapper_sign_hash( slot,
3917 alg,
3918 hash,
3919 hash_length,
3920 signature,
3921 signature_size,
3922 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003923 if( status != PSA_ERROR_NOT_SUPPORTED ||
3924 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003925 goto exit;
3926
Steven Cooreman7a250572020-07-17 16:43:05 +02003927 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003928#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3929 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003930 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003931 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003932 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003933
Steven Cooreman4fed4552020-08-03 14:46:03 +02003934 status = psa_load_rsa_representation( slot->attr.type,
3935 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003936 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003937 &rsa );
3938 if( status != PSA_SUCCESS )
3939 goto exit;
3940
Steven Cooremana2371e52020-07-28 14:30:39 +02003941 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003942 alg,
3943 hash, hash_length,
3944 signature, signature_size,
3945 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003946
Steven Cooremana2371e52020-07-28 14:30:39 +02003947 mbedtls_rsa_free( rsa );
3948 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003949 }
3950 else
John Durkop6ba40d12020-11-10 08:50:04 -08003951#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3952 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003953 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003954 {
John Durkop9814fa22020-11-04 12:28:15 -08003955#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3956 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003957 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003958#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003959 PSA_ALG_IS_ECDSA( alg )
3960#else
3961 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3962#endif
3963 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003964 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003965 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003966 status = psa_load_ecp_representation( slot->attr.type,
3967 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003968 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003969 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003970 if( status != PSA_SUCCESS )
3971 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003972 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003973 alg,
3974 hash, hash_length,
3975 signature, signature_size,
3976 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003977 mbedtls_ecp_keypair_free( ecp );
3978 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003979 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003980 else
John Durkop9814fa22020-11-04 12:28:15 -08003981#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3982 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003983 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003984 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003985 }
itayzafrir5c753392018-05-08 11:18:38 +03003986 }
3987 else
itayzafrir5c753392018-05-08 11:18:38 +03003988 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003989 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003990 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003991
3992exit:
3993 /* Fill the unused part of the output buffer (the whole buffer on error,
3994 * the trailing part on success) with something that isn't a valid mac
3995 * (barring an attack on the mac and deliberately-crafted input),
3996 * in case the caller doesn't check the return status properly. */
3997 if( status == PSA_SUCCESS )
3998 memset( signature + *signature_length, '!',
3999 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004000 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004001 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004002 /* If signature_size is 0 then we have nothing to do. We must not call
4003 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004004
Ronald Cron5c522922020-11-14 16:35:34 +01004005 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004006
Ronald Cron5c522922020-11-14 16:35:34 +01004007 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004008}
4009
Ronald Croncf56a0a2020-08-04 09:51:30 +02004010psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004011 psa_algorithm_t alg,
4012 const uint8_t *hash,
4013 size_t hash_length,
4014 const uint8_t *signature,
4015 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004016{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004018 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004019 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004020
Ronald Cron5c522922020-11-14 16:35:34 +01004021 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4022 PSA_KEY_USAGE_VERIFY_HASH,
4023 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004024 if( status != PSA_SUCCESS )
4025 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004026
Steven Cooreman55ae2172020-07-17 19:46:15 +02004027 /* Try any of the available accelerators first */
4028 status = psa_driver_wrapper_verify_hash( slot,
4029 alg,
4030 hash,
4031 hash_length,
4032 signature,
4033 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004034 if( status != PSA_ERROR_NOT_SUPPORTED ||
4035 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004036 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004037
John Durkop6ba40d12020-11-10 08:50:04 -08004038#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4039 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004040 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004042 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004043
Steven Cooreman4fed4552020-08-03 14:46:03 +02004044 status = psa_load_rsa_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 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004048 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004049 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004050
Steven Cooremana2371e52020-07-28 14:30:39 +02004051 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004052 alg,
4053 hash, hash_length,
4054 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004055 mbedtls_rsa_free( rsa );
4056 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004057 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058 }
4059 else
John Durkop6ba40d12020-11-10 08:50:04 -08004060#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4061 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004062 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004063 {
John Durkop9814fa22020-11-04 12:28:15 -08004064#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4065 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004066 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004067 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004068 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004069 status = psa_load_ecp_representation( slot->attr.type,
4070 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004071 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004072 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004073 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004074 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004075 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004076 hash, hash_length,
4077 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004078 mbedtls_ecp_keypair_free( ecp );
4079 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004080 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004081 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004082 else
John Durkop9814fa22020-11-04 12:28:15 -08004083#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4084 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004085 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004086 status = PSA_ERROR_INVALID_ARGUMENT;
4087 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004088 }
itayzafrir5c753392018-05-08 11:18:38 +03004089 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004090 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004091 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004092 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004093 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004094
4095exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004096 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004097
Ronald Cron5c522922020-11-14 16:35:34 +01004098 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004099}
4100
John Durkop0e005192020-10-31 22:06:54 -07004101#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004102static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4103 mbedtls_rsa_context *rsa )
4104{
4105 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4106 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4107 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4108 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4109}
John Durkop0e005192020-10-31 22:06:54 -07004110#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004111
Ronald Croncf56a0a2020-08-04 09:51:30 +02004112psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004113 psa_algorithm_t alg,
4114 const uint8_t *input,
4115 size_t input_length,
4116 const uint8_t *salt,
4117 size_t salt_length,
4118 uint8_t *output,
4119 size_t output_size,
4120 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004123 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004124 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004125
Darryl Green5cc689a2018-07-24 15:34:10 +01004126 (void) input;
4127 (void) input_length;
4128 (void) salt;
4129 (void) output;
4130 (void) output_size;
4131
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004132 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004134 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4135 return( PSA_ERROR_INVALID_ARGUMENT );
4136
Ronald Cron5c522922020-11-14 16:35:34 +01004137 status = psa_get_and_lock_transparent_key_slot_with_policy(
4138 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004139 if( status != PSA_SUCCESS )
4140 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004141 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4142 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004143 {
4144 status = PSA_ERROR_INVALID_ARGUMENT;
4145 goto exit;
4146 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147
John Durkop6ba40d12020-11-10 08:50:04 -08004148#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4149 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004150 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004152 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004153 status = psa_load_rsa_representation( slot->attr.type,
4154 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004155 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004156 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004157 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004158 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004159
4160 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004161 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004162 status = PSA_ERROR_BUFFER_TOO_SMALL;
4163 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004164 }
John Durkop0e005192020-10-31 22:06:54 -07004165#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004166 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004168 status = mbedtls_to_psa_error(
4169 mbedtls_rsa_pkcs1_encrypt( rsa,
4170 mbedtls_ctr_drbg_random,
4171 &global_data.ctr_drbg,
4172 MBEDTLS_RSA_PUBLIC,
4173 input_length,
4174 input,
4175 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176 }
4177 else
John Durkop0e005192020-10-31 22:06:54 -07004178#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4179#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004180 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004182 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004183 status = mbedtls_to_psa_error(
4184 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4185 mbedtls_ctr_drbg_random,
4186 &global_data.ctr_drbg,
4187 MBEDTLS_RSA_PUBLIC,
4188 salt, salt_length,
4189 input_length,
4190 input,
4191 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004192 }
4193 else
John Durkop0e005192020-10-31 22:06:54 -07004194#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004196 status = PSA_ERROR_INVALID_ARGUMENT;
4197 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004199rsa_exit:
4200 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004201 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004202
Steven Cooremana2371e52020-07-28 14:30:39 +02004203 mbedtls_rsa_free( rsa );
4204 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004206 else
John Durkop9814fa22020-11-04 12:28:15 -08004207#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004208 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004210 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004211 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004212
4213exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004214 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004215
Ronald Cron5c522922020-11-14 16:35:34 +01004216 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004217}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218
Ronald Croncf56a0a2020-08-04 09:51:30 +02004219psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004220 psa_algorithm_t alg,
4221 const uint8_t *input,
4222 size_t input_length,
4223 const uint8_t *salt,
4224 size_t salt_length,
4225 uint8_t *output,
4226 size_t output_size,
4227 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004231 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004232
Darryl Green5cc689a2018-07-24 15:34:10 +01004233 (void) input;
4234 (void) input_length;
4235 (void) salt;
4236 (void) output;
4237 (void) output_size;
4238
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004239 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004241 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4242 return( PSA_ERROR_INVALID_ARGUMENT );
4243
Ronald Cron5c522922020-11-14 16:35:34 +01004244 status = psa_get_and_lock_transparent_key_slot_with_policy(
4245 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004246 if( status != PSA_SUCCESS )
4247 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004248 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004249 {
4250 status = PSA_ERROR_INVALID_ARGUMENT;
4251 goto exit;
4252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253
John Durkop6ba40d12020-11-10 08:50:04 -08004254#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4255 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004256 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004258 mbedtls_rsa_context *rsa = NULL;
4259 status = psa_load_rsa_representation( slot->attr.type,
4260 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004261 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004262 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004263 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004264 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004265
Steven Cooremana2371e52020-07-28 14:30:39 +02004266 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004267 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004268 status = PSA_ERROR_INVALID_ARGUMENT;
4269 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004270 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004271
John Durkop0e005192020-10-31 22:06:54 -07004272#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004273 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004274 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004275 status = mbedtls_to_psa_error(
4276 mbedtls_rsa_pkcs1_decrypt( rsa,
4277 mbedtls_ctr_drbg_random,
4278 &global_data.ctr_drbg,
4279 MBEDTLS_RSA_PRIVATE,
4280 output_length,
4281 input,
4282 output,
4283 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004284 }
4285 else
John Durkop0e005192020-10-31 22:06:54 -07004286#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4287#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004288 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004290 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004291 status = mbedtls_to_psa_error(
4292 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4293 mbedtls_ctr_drbg_random,
4294 &global_data.ctr_drbg,
4295 MBEDTLS_RSA_PRIVATE,
4296 salt, salt_length,
4297 output_length,
4298 input,
4299 output,
4300 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301 }
4302 else
John Durkop0e005192020-10-31 22:06:54 -07004303#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004304 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004305 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004307
Steven Cooreman4fed4552020-08-03 14:46:03 +02004308rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004309 mbedtls_rsa_free( rsa );
4310 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004311 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004312 else
John Durkop6ba40d12020-11-10 08:50:04 -08004313#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4314 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004315 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004316 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004317 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004318
4319exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004320 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004321
Ronald Cron5c522922020-11-14 16:35:34 +01004322 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004323}
Gilles Peskine20035e32018-02-03 22:44:14 +01004324
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004325
4326
mohammad1603503973b2018-03-12 15:59:30 +02004327/****************************************************************/
4328/* Symmetric cryptography */
4329/****************************************************************/
4330
Gilles Peskinee553c652018-06-04 16:22:46 +02004331static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004332 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004333 psa_algorithm_t alg,
4334 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004335{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004337 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004338 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004339 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004340 size_t key_bits;
4341 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004342 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4343 PSA_KEY_USAGE_ENCRYPT :
4344 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004345
Steven Cooremand3feccd2020-09-01 15:56:14 +02004346 /* A context must be freshly initialized before it can be set up. */
4347 if( operation->alg != 0 )
4348 return( PSA_ERROR_BAD_STATE );
4349
Steven Cooremana07b9972020-09-10 14:54:14 +02004350 /* The requested algorithm must be one that can be processed by cipher. */
4351 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004352 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004353
Steven Cooremanef8575e2020-09-11 11:44:50 +02004354 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004355 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004356 if( status != PSA_SUCCESS )
4357 goto exit;
4358
4359 /* Initialize the operation struct members, except for alg. The alg member
4360 * is used to indicate to psa_cipher_abort that there are resources to free,
4361 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004362 operation->key_set = 0;
4363 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004364 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004365 operation->iv_size = 0;
4366 operation->block_size = 0;
4367 if( alg == PSA_ALG_ECB_NO_PADDING )
4368 operation->iv_required = 0;
4369 else
4370 operation->iv_required = 1;
4371
Steven Cooremana07b9972020-09-10 14:54:14 +02004372 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004373 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004374 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004375 slot,
4376 alg );
4377 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004378 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004379 slot,
4380 alg );
4381
Steven Cooremanef8575e2020-09-11 11:44:50 +02004382 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004383 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004384 /* Once the driver context is initialised, it needs to be freed using
4385 * psa_cipher_abort. Indicate this through setting alg. */
4386 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004387 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004388
Steven Cooremand3feccd2020-09-01 15:56:14 +02004389 if( status != PSA_ERROR_NOT_SUPPORTED ||
4390 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4391 goto exit;
4392
Steven Cooremana07b9972020-09-10 14:54:14 +02004393 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004394 * available for the given algorithm & key. */
4395 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004396
Steven Cooremana07b9972020-09-10 14:54:14 +02004397 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004398 * psa_cipher_abort. Indicate there is something to be freed through setting
4399 * alg, and indicate the operation is being done using mbedtls crypto through
4400 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004401 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004402 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004403
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004404 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004405 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004406 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004407 {
4408 status = PSA_ERROR_NOT_SUPPORTED;
4409 goto exit;
4410 }
mohammad1603503973b2018-03-12 15:59:30 +02004411
mohammad1603503973b2018-03-12 15:59:30 +02004412 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004413 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004414 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004415
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004416#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004417 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004418 {
4419 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004420 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004421 memcpy( keys, slot->data.key.data, 16 );
4422 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004423 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4424 keys,
4425 192, cipher_operation );
4426 }
4427 else
4428#endif
4429 {
4430 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004431 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004432 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004433 }
Moran Peker41deec42018-04-04 15:43:05 +03004434 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004435 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004436
mohammad16038481e742018-03-18 13:57:31 +02004437#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004438 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004439 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004440 case PSA_ALG_CBC_NO_PADDING:
4441 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4442 MBEDTLS_PADDING_NONE );
4443 break;
4444 case PSA_ALG_CBC_PKCS7:
4445 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4446 MBEDTLS_PADDING_PKCS7 );
4447 break;
4448 default:
4449 /* The algorithm doesn't involve padding. */
4450 ret = 0;
4451 break;
4452 }
4453 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004454 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004455#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4456
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004457 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004458 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004459 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4460 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004461 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004462 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004463 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004464#if defined(MBEDTLS_CHACHA20_C)
4465 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004466 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004467 operation->iv_size = 12;
4468#endif
mohammad1603503973b2018-03-12 15:59:30 +02004469
Steven Cooreman7df02922020-09-09 15:28:49 +02004470 status = PSA_SUCCESS;
4471
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004472exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004473 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004474 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004475 if( status == PSA_SUCCESS )
4476 {
4477 /* Update operation flags for both driver and software implementations */
4478 operation->key_set = 1;
4479 }
4480 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004481 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004482
Ronald Cron5c522922020-11-14 16:35:34 +01004483 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004484
Ronald Cron5c522922020-11-14 16:35:34 +01004485 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004486}
4487
Gilles Peskinefe119512018-07-08 21:39:34 +02004488psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004489 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004490 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004491{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004492 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004493}
4494
Gilles Peskinefe119512018-07-08 21:39:34 +02004495psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004496 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004497 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004498{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004499 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004500}
4501
Gilles Peskinefe119512018-07-08 21:39:34 +02004502psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004503 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004504 size_t iv_size,
4505 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004506{
itayzafrir534bd7c2018-08-02 13:56:32 +03004507 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004509 if( operation->iv_set || ! operation->iv_required )
4510 {
4511 return( PSA_ERROR_BAD_STATE );
4512 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004513
Steven Cooremanef8575e2020-09-11 11:44:50 +02004514 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004515 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004516 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004517 iv,
4518 iv_size,
4519 iv_length );
4520 goto exit;
4521 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004522
Moran Peker41deec42018-04-04 15:43:05 +03004523 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004524 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004525 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004526 goto exit;
4527 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004528 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4529 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004530 if( ret != 0 )
4531 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004532 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004533 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004534 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004535
mohammad16038481e742018-03-18 13:57:31 +02004536 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004537 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004538
Moran Peker395db872018-05-31 14:07:14 +03004539exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004540 if( status == PSA_SUCCESS )
4541 operation->iv_set = 1;
4542 else
Moran Peker395db872018-05-31 14:07:14 +03004543 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004544 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004545}
4546
Gilles Peskinefe119512018-07-08 21:39:34 +02004547psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004548 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004549 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004550{
itayzafrir534bd7c2018-08-02 13:56:32 +03004551 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004553 if( operation->iv_set || ! operation->iv_required )
4554 {
4555 return( PSA_ERROR_BAD_STATE );
4556 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004557
Steven Cooremanef8575e2020-09-11 11:44:50 +02004558 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004559 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004560 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004561 iv,
4562 iv_length );
4563 goto exit;
4564 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004565
Moran Pekera28258c2018-05-29 16:25:04 +03004566 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004567 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004568 status = PSA_ERROR_INVALID_ARGUMENT;
4569 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004570 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004571 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4572 status = mbedtls_to_psa_error( ret );
4573exit:
4574 if( status == PSA_SUCCESS )
4575 operation->iv_set = 1;
4576 else
mohammad1603503973b2018-03-12 15:59:30 +02004577 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004578 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004579}
4580
Steven Cooreman177deba2020-09-07 17:14:14 +02004581/* Process input for which the algorithm is set to ECB mode. This requires
4582 * manual processing, since the PSA API is defined as being able to process
4583 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4584 * underlying mbedtls_cipher_update only takes full blocks. */
4585static psa_status_t psa_cipher_update_ecb_internal(
4586 mbedtls_cipher_context_t *ctx,
4587 const uint8_t *input,
4588 size_t input_length,
4589 uint8_t *output,
4590 size_t output_size,
4591 size_t *output_length )
4592{
4593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4594 size_t block_size = ctx->cipher_info->block_size;
4595 size_t internal_output_length = 0;
4596 *output_length = 0;
4597
4598 if( input_length == 0 )
4599 {
4600 status = PSA_SUCCESS;
4601 goto exit;
4602 }
4603
4604 if( ctx->unprocessed_len > 0 )
4605 {
4606 /* Fill up to block size, and run the block if there's a full one. */
4607 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4608
4609 if( input_length < bytes_to_copy )
4610 bytes_to_copy = input_length;
4611
4612 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4613 input, bytes_to_copy );
4614 input_length -= bytes_to_copy;
4615 input += bytes_to_copy;
4616 ctx->unprocessed_len += bytes_to_copy;
4617
4618 if( ctx->unprocessed_len == block_size )
4619 {
4620 status = mbedtls_to_psa_error(
4621 mbedtls_cipher_update( ctx,
4622 ctx->unprocessed_data,
4623 block_size,
4624 output, &internal_output_length ) );
4625
4626 if( status != PSA_SUCCESS )
4627 goto exit;
4628
4629 output += internal_output_length;
4630 output_size -= internal_output_length;
4631 *output_length += internal_output_length;
4632 ctx->unprocessed_len = 0;
4633 }
4634 }
4635
4636 while( input_length >= block_size )
4637 {
4638 /* Run all full blocks we have, one by one */
4639 status = mbedtls_to_psa_error(
4640 mbedtls_cipher_update( ctx, input,
4641 block_size,
4642 output, &internal_output_length ) );
4643
4644 if( status != PSA_SUCCESS )
4645 goto exit;
4646
4647 input_length -= block_size;
4648 input += block_size;
4649
4650 output += internal_output_length;
4651 output_size -= internal_output_length;
4652 *output_length += internal_output_length;
4653 }
4654
4655 if( input_length > 0 )
4656 {
4657 /* Save unprocessed bytes for later processing */
4658 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4659 input, input_length );
4660 ctx->unprocessed_len += input_length;
4661 }
4662
4663 status = PSA_SUCCESS;
4664
4665exit:
4666 return( status );
4667}
4668
Gilles Peskinee553c652018-06-04 16:22:46 +02004669psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4670 const uint8_t *input,
4671 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004672 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004673 size_t output_size,
4674 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004675{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004677 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004678 if( operation->alg == 0 )
4679 {
4680 return( PSA_ERROR_BAD_STATE );
4681 }
4682 if( operation->iv_required && ! operation->iv_set )
4683 {
4684 return( PSA_ERROR_BAD_STATE );
4685 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004686
Steven Cooremanef8575e2020-09-11 11:44:50 +02004687 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004688 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004689 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004690 input,
4691 input_length,
4692 output,
4693 output_size,
4694 output_length );
4695 goto exit;
4696 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004697
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004698 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004699 {
4700 /* Take the unprocessed partial block left over from previous
4701 * update calls, if any, plus the input to this call. Remove
4702 * the last partial block, if any. You get the data that will be
4703 * output in this call. */
4704 expected_output_size =
4705 ( operation->ctx.cipher.unprocessed_len + input_length )
4706 / operation->block_size * operation->block_size;
4707 }
4708 else
4709 {
4710 expected_output_size = input_length;
4711 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004712
Gilles Peskine89d789c2018-06-04 17:17:16 +02004713 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004714 {
4715 status = PSA_ERROR_BUFFER_TOO_SMALL;
4716 goto exit;
4717 }
mohammad160382759612018-03-12 18:16:40 +02004718
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004719 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4720 {
4721 /* mbedtls_cipher_update has an API inconsistency: it will only
4722 * process a single block at a time in ECB mode. Abstract away that
4723 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004724 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4725 input,
4726 input_length,
4727 output,
4728 output_size,
4729 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004730 }
4731 else
4732 {
4733 status = mbedtls_to_psa_error(
4734 mbedtls_cipher_update( &operation->ctx.cipher, input,
4735 input_length, output, output_length ) );
4736 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004737exit:
4738 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004739 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004740 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004741}
4742
Gilles Peskinee553c652018-06-04 16:22:46 +02004743psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4744 uint8_t *output,
4745 size_t output_size,
4746 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004747{
David Saadab4ecc272019-02-14 13:48:10 +02004748 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004749 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004750 if( operation->alg == 0 )
4751 {
4752 return( PSA_ERROR_BAD_STATE );
4753 }
4754 if( operation->iv_required && ! operation->iv_set )
4755 {
4756 return( PSA_ERROR_BAD_STATE );
4757 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004758
Steven Cooremanef8575e2020-09-11 11:44:50 +02004759 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004760 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004761 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004762 output,
4763 output_size,
4764 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004765 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004766 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004767
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004768 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004769 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004770 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004771 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004772 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004773 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004774 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004775 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004776 }
4777
Steven Cooremanef8575e2020-09-11 11:44:50 +02004778 status = mbedtls_to_psa_error(
4779 mbedtls_cipher_finish( &operation->ctx.cipher,
4780 temp_output_buffer,
4781 output_length ) );
4782 if( status != PSA_SUCCESS )
4783 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004784
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004785 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004786 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004787 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004788 memcpy( output, temp_output_buffer, *output_length );
4789 else
Janos Follath315b51c2018-07-09 16:04:51 +01004790 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004791
Steven Cooremanef8575e2020-09-11 11:44:50 +02004792exit:
4793 if( operation->mbedtls_in_use == 1 )
4794 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004795
Steven Cooremanef8575e2020-09-11 11:44:50 +02004796 if( status == PSA_SUCCESS )
4797 return( psa_cipher_abort( operation ) );
4798 else
4799 {
4800 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004801 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004802
Steven Cooremanef8575e2020-09-11 11:44:50 +02004803 return( status );
4804 }
mohammad1603503973b2018-03-12 15:59:30 +02004805}
4806
Gilles Peskinee553c652018-06-04 16:22:46 +02004807psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4808{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004809 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004810 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004811 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004812 * in use. It's ok to call abort on such an object, and there's
4813 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004814 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004815 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004816
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004817 /* Sanity check (shouldn't happen: operation->alg should
4818 * always have been initialized to a valid value). */
4819 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4820 return( PSA_ERROR_BAD_STATE );
4821
Steven Cooremanef8575e2020-09-11 11:44:50 +02004822 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004823 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004824 else
4825 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004826
Moran Peker41deec42018-04-04 15:43:05 +03004827 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004828 operation->key_set = 0;
4829 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004830 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004831 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004832 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004833 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004834
Moran Peker395db872018-05-31 14:07:14 +03004835 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004836}
4837
Gilles Peskinea0655c32018-04-30 17:06:50 +02004838
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004839
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004840
mohammad16035955c982018-04-26 00:53:03 +03004841/****************************************************************/
4842/* AEAD */
4843/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004844
Gilles Peskineedf9a652018-08-17 18:11:56 +02004845typedef struct
4846{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004847 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004848 const mbedtls_cipher_info_t *cipher_info;
4849 union
4850 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004851 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004852#if defined(MBEDTLS_CCM_C)
4853 mbedtls_ccm_context ccm;
4854#endif /* MBEDTLS_CCM_C */
4855#if defined(MBEDTLS_GCM_C)
4856 mbedtls_gcm_context gcm;
4857#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004858#if defined(MBEDTLS_CHACHAPOLY_C)
4859 mbedtls_chachapoly_context chachapoly;
4860#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004861 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004862 psa_algorithm_t core_alg;
4863 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004864 uint8_t tag_length;
4865} aead_operation_t;
4866
Ronald Cronf95a2b12020-10-22 15:24:49 +02004867#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4868
Gilles Peskine30a9e412019-01-14 18:36:12 +01004869static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004870{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004871 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 {
4873#if defined(MBEDTLS_CCM_C)
4874 case PSA_ALG_CCM:
4875 mbedtls_ccm_free( &operation->ctx.ccm );
4876 break;
4877#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004878#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004879 case PSA_ALG_GCM:
4880 mbedtls_gcm_free( &operation->ctx.gcm );
4881 break;
4882#endif /* MBEDTLS_GCM_C */
4883 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004884
Ronald Cron5c522922020-11-14 16:35:34 +01004885 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004886}
4887
4888static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004889 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004890 psa_key_usage_t usage,
4891 psa_algorithm_t alg )
4892{
4893 psa_status_t status;
4894 size_t key_bits;
4895 mbedtls_cipher_id_t cipher_id;
4896
Ronald Cron5c522922020-11-14 16:35:34 +01004897 status = psa_get_and_lock_transparent_key_slot_with_policy(
4898 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004899 if( status != PSA_SUCCESS )
4900 return( status );
4901
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004902 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004903
4904 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004905 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004906 &cipher_id );
4907 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004908 {
4909 status = PSA_ERROR_NOT_SUPPORTED;
4910 goto cleanup;
4911 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004912
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004913 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004914 {
4915#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004916 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4917 operation->core_alg = PSA_ALG_CCM;
4918 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004919 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4920 * The call to mbedtls_ccm_encrypt_and_tag or
4921 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004922 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004923 {
4924 status = PSA_ERROR_INVALID_ARGUMENT;
4925 goto cleanup;
4926 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004927 mbedtls_ccm_init( &operation->ctx.ccm );
4928 status = mbedtls_to_psa_error(
4929 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004930 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004931 (unsigned int) key_bits ) );
4932 if( status != 0 )
4933 goto cleanup;
4934 break;
4935#endif /* MBEDTLS_CCM_C */
4936
4937#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004938 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4939 operation->core_alg = PSA_ALG_GCM;
4940 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004941 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4942 * The call to mbedtls_gcm_crypt_and_tag or
4943 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004944 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004945 {
4946 status = PSA_ERROR_INVALID_ARGUMENT;
4947 goto cleanup;
4948 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004949 mbedtls_gcm_init( &operation->ctx.gcm );
4950 status = mbedtls_to_psa_error(
4951 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004952 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004953 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004954 if( status != 0 )
4955 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004956 break;
4957#endif /* MBEDTLS_GCM_C */
4958
Gilles Peskine26869f22019-05-06 15:25:00 +02004959#if defined(MBEDTLS_CHACHAPOLY_C)
4960 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4961 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4962 operation->full_tag_length = 16;
4963 /* We only support the default tag length. */
4964 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004965 {
4966 status = PSA_ERROR_NOT_SUPPORTED;
4967 goto cleanup;
4968 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004969 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4970 status = mbedtls_to_psa_error(
4971 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004972 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004973 if( status != 0 )
4974 goto cleanup;
4975 break;
4976#endif /* MBEDTLS_CHACHAPOLY_C */
4977
Gilles Peskineedf9a652018-08-17 18:11:56 +02004978 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004979 status = PSA_ERROR_NOT_SUPPORTED;
4980 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004981 }
4982
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004983 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4984 {
4985 status = PSA_ERROR_INVALID_ARGUMENT;
4986 goto cleanup;
4987 }
4988 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004989
Gilles Peskineedf9a652018-08-17 18:11:56 +02004990 return( PSA_SUCCESS );
4991
4992cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004993 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004994 return( status );
4995}
4996
Ronald Croncf56a0a2020-08-04 09:51:30 +02004997psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004998 psa_algorithm_t alg,
4999 const uint8_t *nonce,
5000 size_t nonce_length,
5001 const uint8_t *additional_data,
5002 size_t additional_data_length,
5003 const uint8_t *plaintext,
5004 size_t plaintext_length,
5005 uint8_t *ciphertext,
5006 size_t ciphertext_size,
5007 size_t *ciphertext_length )
5008{
mohammad16035955c982018-04-26 00:53:03 +03005009 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005010 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005011 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005012
mohammad1603f08a5502018-06-03 15:05:47 +03005013 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005014
Ronald Croncf56a0a2020-08-04 09:51:30 +02005015 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005016 if( status != PSA_SUCCESS )
5017 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005018
Gilles Peskineedf9a652018-08-17 18:11:56 +02005019 /* For all currently supported modes, the tag is at the end of the
5020 * ciphertext. */
5021 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5022 {
5023 status = PSA_ERROR_BUFFER_TOO_SMALL;
5024 goto exit;
5025 }
5026 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005027
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005028#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005029 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005030 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005031 status = mbedtls_to_psa_error(
5032 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5033 MBEDTLS_GCM_ENCRYPT,
5034 plaintext_length,
5035 nonce, nonce_length,
5036 additional_data, additional_data_length,
5037 plaintext, ciphertext,
5038 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005039 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005040 else
5041#endif /* MBEDTLS_GCM_C */
5042#if defined(MBEDTLS_CCM_C)
5043 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005044 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005045 status = mbedtls_to_psa_error(
5046 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5047 plaintext_length,
5048 nonce, nonce_length,
5049 additional_data,
5050 additional_data_length,
5051 plaintext, ciphertext,
5052 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005053 }
mohammad16035c8845f2018-05-09 05:40:09 -07005054 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005055#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005056#if defined(MBEDTLS_CHACHAPOLY_C)
5057 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5058 {
5059 if( nonce_length != 12 || operation.tag_length != 16 )
5060 {
5061 status = PSA_ERROR_NOT_SUPPORTED;
5062 goto exit;
5063 }
5064 status = mbedtls_to_psa_error(
5065 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5066 plaintext_length,
5067 nonce,
5068 additional_data,
5069 additional_data_length,
5070 plaintext,
5071 ciphertext,
5072 tag ) );
5073 }
5074 else
5075#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005076 {
mohammad1603554faad2018-06-03 15:07:38 +03005077 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005078 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005079
Gilles Peskineedf9a652018-08-17 18:11:56 +02005080 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5081 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005082
Gilles Peskineedf9a652018-08-17 18:11:56 +02005083exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005084 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005085 if( status == PSA_SUCCESS )
5086 *ciphertext_length = plaintext_length + operation.tag_length;
5087 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005088}
5089
Gilles Peskineee652a32018-06-01 19:23:52 +02005090/* Locate the tag in a ciphertext buffer containing the encrypted data
5091 * followed by the tag. Return the length of the part preceding the tag in
5092 * *plaintext_length. This is the size of the plaintext in modes where
5093 * the encrypted data has the same size as the plaintext, such as
5094 * CCM and GCM. */
5095static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5096 const uint8_t *ciphertext,
5097 size_t ciphertext_length,
5098 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005099 const uint8_t **p_tag )
5100{
5101 size_t payload_length;
5102 if( tag_length > ciphertext_length )
5103 return( PSA_ERROR_INVALID_ARGUMENT );
5104 payload_length = ciphertext_length - tag_length;
5105 if( payload_length > plaintext_size )
5106 return( PSA_ERROR_BUFFER_TOO_SMALL );
5107 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005108 return( PSA_SUCCESS );
5109}
5110
Ronald Croncf56a0a2020-08-04 09:51:30 +02005111psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005112 psa_algorithm_t alg,
5113 const uint8_t *nonce,
5114 size_t nonce_length,
5115 const uint8_t *additional_data,
5116 size_t additional_data_length,
5117 const uint8_t *ciphertext,
5118 size_t ciphertext_length,
5119 uint8_t *plaintext,
5120 size_t plaintext_size,
5121 size_t *plaintext_length )
5122{
mohammad16035955c982018-04-26 00:53:03 +03005123 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005124 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005125 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005126
Gilles Peskineee652a32018-06-01 19:23:52 +02005127 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005128
Ronald Croncf56a0a2020-08-04 09:51:30 +02005129 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005130 if( status != PSA_SUCCESS )
5131 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005132
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005133 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5134 ciphertext, ciphertext_length,
5135 plaintext_size, &tag );
5136 if( status != PSA_SUCCESS )
5137 goto exit;
5138
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005139#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005140 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005141 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005142 status = mbedtls_to_psa_error(
5143 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5144 ciphertext_length - operation.tag_length,
5145 nonce, nonce_length,
5146 additional_data,
5147 additional_data_length,
5148 tag, operation.tag_length,
5149 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005150 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005151 else
5152#endif /* MBEDTLS_GCM_C */
5153#if defined(MBEDTLS_CCM_C)
5154 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005155 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005156 status = mbedtls_to_psa_error(
5157 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5158 ciphertext_length - operation.tag_length,
5159 nonce, nonce_length,
5160 additional_data,
5161 additional_data_length,
5162 ciphertext, plaintext,
5163 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005164 }
mohammad160339574652018-06-01 04:39:53 -07005165 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005166#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005167#if defined(MBEDTLS_CHACHAPOLY_C)
5168 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5169 {
5170 if( nonce_length != 12 || operation.tag_length != 16 )
5171 {
5172 status = PSA_ERROR_NOT_SUPPORTED;
5173 goto exit;
5174 }
5175 status = mbedtls_to_psa_error(
5176 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5177 ciphertext_length - operation.tag_length,
5178 nonce,
5179 additional_data,
5180 additional_data_length,
5181 tag,
5182 ciphertext,
5183 plaintext ) );
5184 }
5185 else
5186#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005187 {
mohammad1603554faad2018-06-03 15:07:38 +03005188 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005189 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005190
Gilles Peskineedf9a652018-08-17 18:11:56 +02005191 if( status != PSA_SUCCESS && plaintext_size != 0 )
5192 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005193
Gilles Peskineedf9a652018-08-17 18:11:56 +02005194exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005195 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005196 if( status == PSA_SUCCESS )
5197 *plaintext_length = ciphertext_length - operation.tag_length;
5198 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005199}
5200
Gilles Peskinea0655c32018-04-30 17:06:50 +02005201
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005202
Gilles Peskine20035e32018-02-03 22:44:14 +01005203/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204/* Generators */
5205/****************************************************************/
5206
John Durkop07cc04a2020-11-16 22:08:34 -08005207#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5208 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5209 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5210#define AT_LEAST_ONE_BUILTIN_KDF
5211#endif
5212
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005213#define HKDF_STATE_INIT 0 /* no input yet */
5214#define HKDF_STATE_STARTED 1 /* got salt */
5215#define HKDF_STATE_KEYED 2 /* got key */
5216#define HKDF_STATE_OUTPUT 3 /* output started */
5217
Gilles Peskinecbe66502019-05-16 16:59:18 +02005218static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5222 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005223 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005225}
5226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005228{
5229 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005231 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 {
5233 /* The object has (apparently) been initialized but it is not
5234 * in use. It's ok to call abort on such an object, and there's
5235 * nothing to do. */
5236 }
5237 else
John Durkopd0321952020-10-29 21:37:36 -07005238#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005239 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005240 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 mbedtls_free( operation->ctx.hkdf.info );
5242 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005243 }
John Durkop07cc04a2020-11-16 22:08:34 -08005244 else
5245#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5246#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5247 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5248 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005250 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005251 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005252 if( operation->ctx.tls12_prf.seed != NULL )
5253 {
5254 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5255 operation->ctx.tls12_prf.seed_length );
5256 mbedtls_free( operation->ctx.tls12_prf.seed );
5257 }
5258
5259 if( operation->ctx.tls12_prf.label != NULL )
5260 {
5261 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5262 operation->ctx.tls12_prf.label_length );
5263 mbedtls_free( operation->ctx.tls12_prf.label );
5264 }
5265
5266 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5267
5268 /* We leave the fields Ai and output_block to be erased safely by the
5269 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005270 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005271 else
John Durkop07cc04a2020-11-16 22:08:34 -08005272#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5273 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005274 {
5275 status = PSA_ERROR_BAD_STATE;
5276 }
Janos Follath083036a2019-06-11 10:22:26 +01005277 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278 return( status );
5279}
5280
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005282 size_t *capacity)
5283{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005285 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005287 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005288 }
5289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005291 return( PSA_SUCCESS );
5292}
5293
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295 size_t capacity )
5296{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005297 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005302 return( PSA_SUCCESS );
5303}
5304
John Durkopd0321952020-10-29 21:37:36 -07005305#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005307 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005308static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309 psa_algorithm_t hash_alg,
5310 uint8_t *output,
5311 size_t output_length )
5312{
5313 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5314 psa_status_t status;
5315
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5317 return( PSA_ERROR_BAD_STATE );
5318 hkdf->state = HKDF_STATE_OUTPUT;
5319
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320 while( output_length != 0 )
5321 {
5322 /* Copy what remains of the current block */
5323 uint8_t n = hash_length - hkdf->offset_in_block;
5324 if( n > output_length )
5325 n = (uint8_t) output_length;
5326 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5327 output += n;
5328 output_length -= n;
5329 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005330 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005331 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005332 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005333 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005335 * object was corrupted or if this function is called directly
5336 * inside the library. */
5337 if( hkdf->block_number == 0xff )
5338 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005339
5340 /* We need a new block */
5341 ++hkdf->block_number;
5342 hkdf->offset_in_block = 0;
5343 status = psa_hmac_setup_internal( &hkdf->hmac,
5344 hkdf->prk, hash_length,
5345 hash_alg );
5346 if( status != PSA_SUCCESS )
5347 return( status );
5348 if( hkdf->block_number != 1 )
5349 {
5350 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5351 hkdf->output_block,
5352 hash_length );
5353 if( status != PSA_SUCCESS )
5354 return( status );
5355 }
5356 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5357 hkdf->info,
5358 hkdf->info_length );
5359 if( status != PSA_SUCCESS )
5360 return( status );
5361 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5362 &hkdf->block_number, 1 );
5363 if( status != PSA_SUCCESS )
5364 return( status );
5365 status = psa_hmac_finish_internal( &hkdf->hmac,
5366 hkdf->output_block,
5367 sizeof( hkdf->output_block ) );
5368 if( status != PSA_SUCCESS )
5369 return( status );
5370 }
5371
5372 return( PSA_SUCCESS );
5373}
John Durkop07cc04a2020-11-16 22:08:34 -08005374#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005375
John Durkop07cc04a2020-11-16 22:08:34 -08005376#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5377 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005378static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5379 psa_tls12_prf_key_derivation_t *tls12_prf,
5380 psa_algorithm_t alg )
5381{
5382 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5383 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005384 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5385 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005386
Janos Follath7742fee2019-06-17 12:58:10 +01005387 /* We can't be wanting more output after block 0xff, otherwise
5388 * the capacity check in psa_key_derivation_output_bytes() would have
5389 * prevented this call. It could happen only if the operation
5390 * object was corrupted or if this function is called directly
5391 * inside the library. */
5392 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005393 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005394
5395 /* We need a new block */
5396 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005397 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005398
5399 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5400 *
5401 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5402 *
5403 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5404 * HMAC_hash(secret, A(2) + seed) +
5405 * HMAC_hash(secret, A(3) + seed) + ...
5406 *
5407 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005408 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005409 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005410 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005411 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005412 * is currently extracted as `output_block` and where i is
5413 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005414 */
5415
Janos Follathea29bfb2019-06-19 12:21:20 +01005416 /* Save the hash context before using it, to preserve the hash state with
5417 * only the inner padding in it. We need this, because inner padding depends
5418 * on the key (secret in the RFC's terminology). */
5419 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5420 if( status != PSA_SUCCESS )
5421 goto cleanup;
5422
5423 /* Calculate A(i) where i = tls12_prf->block_number. */
5424 if( tls12_prf->block_number == 1 )
5425 {
5426 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5427 * the variable seed and in this instance means it in the context of the
5428 * P_hash function, where seed = label + seed.) */
5429 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5430 tls12_prf->label, tls12_prf->label_length );
5431 if( status != PSA_SUCCESS )
5432 goto cleanup;
5433 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5434 tls12_prf->seed, tls12_prf->seed_length );
5435 if( status != PSA_SUCCESS )
5436 goto cleanup;
5437 }
5438 else
5439 {
5440 /* A(i) = HMAC_hash(secret, A(i-1)) */
5441 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5442 tls12_prf->Ai, hash_length );
5443 if( status != PSA_SUCCESS )
5444 goto cleanup;
5445 }
5446
5447 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5448 tls12_prf->Ai, hash_length );
5449 if( status != PSA_SUCCESS )
5450 goto cleanup;
5451 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5452 if( status != PSA_SUCCESS )
5453 goto cleanup;
5454
5455 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5456 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5457 tls12_prf->Ai, hash_length );
5458 if( status != PSA_SUCCESS )
5459 goto cleanup;
5460 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5461 tls12_prf->label, tls12_prf->label_length );
5462 if( status != PSA_SUCCESS )
5463 goto cleanup;
5464 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5465 tls12_prf->seed, tls12_prf->seed_length );
5466 if( status != PSA_SUCCESS )
5467 goto cleanup;
5468 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5469 tls12_prf->output_block, hash_length );
5470 if( status != PSA_SUCCESS )
5471 goto cleanup;
5472 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5473 if( status != PSA_SUCCESS )
5474 goto cleanup;
5475
Janos Follath7742fee2019-06-17 12:58:10 +01005476
5477cleanup:
5478
Janos Follathea29bfb2019-06-19 12:21:20 +01005479 cleanup_status = psa_hash_abort( &backup );
5480 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5481 status = cleanup_status;
5482
5483 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005484}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005485
Janos Follath844eb0e2019-06-19 12:10:49 +01005486static psa_status_t psa_key_derivation_tls12_prf_read(
5487 psa_tls12_prf_key_derivation_t *tls12_prf,
5488 psa_algorithm_t alg,
5489 uint8_t *output,
5490 size_t output_length )
5491{
5492 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5493 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5494 psa_status_t status;
5495 uint8_t offset, length;
5496
5497 while( output_length != 0 )
5498 {
5499 /* Check if we have fully processed the current block. */
5500 if( tls12_prf->left_in_block == 0 )
5501 {
5502 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5503 alg );
5504 if( status != PSA_SUCCESS )
5505 return( status );
5506
5507 continue;
5508 }
5509
5510 if( tls12_prf->left_in_block > output_length )
5511 length = (uint8_t) output_length;
5512 else
5513 length = tls12_prf->left_in_block;
5514
5515 offset = hash_length - tls12_prf->left_in_block;
5516 memcpy( output, tls12_prf->output_block + offset, length );
5517 output += length;
5518 output_length -= length;
5519 tls12_prf->left_in_block -= length;
5520 }
5521
5522 return( PSA_SUCCESS );
5523}
John Durkop07cc04a2020-11-16 22:08:34 -08005524#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5525 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005526
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005527psa_status_t psa_key_derivation_output_bytes(
5528 psa_key_derivation_operation_t *operation,
5529 uint8_t *output,
5530 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005531{
5532 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005536 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005538 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005539 }
5540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005542 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005544 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005546 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005547 goto exit;
5548 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005549 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005550 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005552 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005553 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5554 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005556 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005557 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005558 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005560
John Durkopd0321952020-10-29 21:37:36 -07005561#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005562 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005563 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005564 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005566 output, output_length );
5567 }
Janos Follathadbec812019-06-14 11:05:39 +01005568 else
John Durkop07cc04a2020-11-16 22:08:34 -08005569#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5570#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5571 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005572 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005573 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005574 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005576 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005577 output_length );
5578 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005579 else
John Durkop07cc04a2020-11-16 22:08:34 -08005580#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5581 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005582 {
5583 return( PSA_ERROR_BAD_STATE );
5584 }
5585
5586exit:
5587 if( status != PSA_SUCCESS )
5588 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005589 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005590 * This allows us to differentiate between exhausted operations and
5591 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5592 * operations. */
5593 psa_algorithm_t alg = operation->alg;
5594 psa_key_derivation_abort( operation );
5595 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005596 memset( output, '!', output_length );
5597 }
5598 return( status );
5599}
5600
Gilles Peskine08542d82018-07-19 17:05:42 +02005601#if defined(MBEDTLS_DES_C)
5602static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5603{
5604 if( data_size >= 8 )
5605 mbedtls_des_key_set_parity( data );
5606 if( data_size >= 16 )
5607 mbedtls_des_key_set_parity( data + 8 );
5608 if( data_size >= 24 )
5609 mbedtls_des_key_set_parity( data + 16 );
5610}
5611#endif /* MBEDTLS_DES_C */
5612
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005613static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005614 psa_key_slot_t *slot,
5615 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005616 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005617{
5618 uint8_t *data = NULL;
5619 size_t bytes = PSA_BITS_TO_BYTES( bits );
5620 psa_status_t status;
5621
Gilles Peskine8e338702019-07-30 20:06:31 +02005622 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005623 return( PSA_ERROR_INVALID_ARGUMENT );
5624 if( bits % 8 != 0 )
5625 return( PSA_ERROR_INVALID_ARGUMENT );
5626 data = mbedtls_calloc( 1, bytes );
5627 if( data == NULL )
5628 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005630 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005631 if( status != PSA_SUCCESS )
5632 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005633#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005634 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005635 psa_des_set_key_parity( data, bytes );
5636#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005638
5639exit:
5640 mbedtls_free( data );
5641 return( status );
5642}
5643
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005644psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005645 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005646 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005647{
5648 psa_status_t status;
5649 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005650 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005651
Ronald Cron81709fc2020-11-14 12:10:32 +01005652 *key = MBEDTLS_SVC_KEY_ID_INIT;
5653
Gilles Peskine0f84d622019-09-12 19:03:13 +02005654 /* Reject any attempt to create a zero-length key so that we don't
5655 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5656 if( psa_get_key_bits( attributes ) == 0 )
5657 return( PSA_ERROR_INVALID_ARGUMENT );
5658
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005659 if( ! operation->can_output_key )
5660 return( PSA_ERROR_NOT_PERMITTED );
5661
Ronald Cron81709fc2020-11-14 12:10:32 +01005662 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5663 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005664#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5665 if( driver != NULL )
5666 {
5667 /* Deriving a key in a secure element is not implemented yet. */
5668 status = PSA_ERROR_NOT_SUPPORTED;
5669 }
5670#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 if( status == PSA_SUCCESS )
5672 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005673 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005674 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005676 }
5677 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005678 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005679 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005680 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005681
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005682 return( status );
5683}
5684
Gilles Peskineeab56e42018-07-12 17:12:33 +02005685
5686
5687/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005688/* Key derivation */
5689/****************************************************************/
5690
John Durkop07cc04a2020-11-16 22:08:34 -08005691#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005692static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005694 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005695{
John Durkop07cc04a2020-11-16 22:08:34 -08005696 int is_kdf_alg_supported;
5697
Janos Follath5fe19732019-06-20 15:09:30 +01005698 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5699 * initialisers for this union leave some bytes unspecified.) */
5700 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5701
Gilles Peskine969c5d62019-01-16 15:53:06 +01005702 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005703#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005704 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5705 is_kdf_alg_supported = 1;
5706 else
5707#endif
5708#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5709 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5710 is_kdf_alg_supported = 1;
5711 else
5712#endif
5713#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5714 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5715 is_kdf_alg_supported = 1;
5716 else
5717#endif
5718 is_kdf_alg_supported = 0;
5719
5720 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005721 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005723 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5724 if( hash_size == 0 )
5725 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005726 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5727 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005728 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729 {
5730 return( PSA_ERROR_NOT_SUPPORTED );
5731 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005732 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005733 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005734 }
John Durkop07cc04a2020-11-16 22:08:34 -08005735
5736 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005737}
John Durkop07cc04a2020-11-16 22:08:34 -08005738#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005741 psa_algorithm_t alg )
5742{
5743 psa_status_t status;
5744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005746 return( PSA_ERROR_BAD_STATE );
5747
Gilles Peskine6843c292019-01-18 16:44:49 +01005748 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5749 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005750#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005751 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005752 {
5753 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005755 }
5756 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5757 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005759 }
John Durkop07cc04a2020-11-16 22:08:34 -08005760#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005761 else
5762 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005763
5764 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005765 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005766 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005767}
5768
John Durkopd0321952020-10-29 21:37:36 -07005769#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005770static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 psa_algorithm_t hash_alg,
5772 psa_key_derivation_step_t step,
5773 const uint8_t *data,
5774 size_t data_length )
5775{
5776 psa_status_t status;
5777 switch( step )
5778 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005779 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005780 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005781 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005782 status = psa_hmac_setup_internal( &hkdf->hmac,
5783 data, data_length,
5784 hash_alg );
5785 if( status != PSA_SUCCESS )
5786 return( status );
5787 hkdf->state = HKDF_STATE_STARTED;
5788 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005789 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005790 /* If no salt was provided, use an empty salt. */
5791 if( hkdf->state == HKDF_STATE_INIT )
5792 {
5793 status = psa_hmac_setup_internal( &hkdf->hmac,
5794 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005795 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005796 if( status != PSA_SUCCESS )
5797 return( status );
5798 hkdf->state = HKDF_STATE_STARTED;
5799 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005800 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005801 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005802 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5803 data, data_length );
5804 if( status != PSA_SUCCESS )
5805 return( status );
5806 status = psa_hmac_finish_internal( &hkdf->hmac,
5807 hkdf->prk,
5808 sizeof( hkdf->prk ) );
5809 if( status != PSA_SUCCESS )
5810 return( status );
5811 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5812 hkdf->block_number = 0;
5813 hkdf->state = HKDF_STATE_KEYED;
5814 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005815 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005816 if( hkdf->state == HKDF_STATE_OUTPUT )
5817 return( PSA_ERROR_BAD_STATE );
5818 if( hkdf->info_set )
5819 return( PSA_ERROR_BAD_STATE );
5820 hkdf->info_length = data_length;
5821 if( data_length != 0 )
5822 {
5823 hkdf->info = mbedtls_calloc( 1, data_length );
5824 if( hkdf->info == NULL )
5825 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5826 memcpy( hkdf->info, data, data_length );
5827 }
5828 hkdf->info_set = 1;
5829 return( PSA_SUCCESS );
5830 default:
5831 return( PSA_ERROR_INVALID_ARGUMENT );
5832 }
5833}
John Durkop07cc04a2020-11-16 22:08:34 -08005834#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005835
John Durkop07cc04a2020-11-16 22:08:34 -08005836#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5837 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005838static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5839 const uint8_t *data,
5840 size_t data_length )
5841{
5842 if( prf->state != TLS12_PRF_STATE_INIT )
5843 return( PSA_ERROR_BAD_STATE );
5844
Janos Follathd6dce9f2019-07-04 09:11:38 +01005845 if( data_length != 0 )
5846 {
5847 prf->seed = mbedtls_calloc( 1, data_length );
5848 if( prf->seed == NULL )
5849 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005850
Janos Follathd6dce9f2019-07-04 09:11:38 +01005851 memcpy( prf->seed, data, data_length );
5852 prf->seed_length = data_length;
5853 }
Janos Follathf08e2652019-06-13 09:05:41 +01005854
5855 prf->state = TLS12_PRF_STATE_SEED_SET;
5856
5857 return( PSA_SUCCESS );
5858}
5859
Janos Follath81550542019-06-13 14:26:34 +01005860static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5861 psa_algorithm_t hash_alg,
5862 const uint8_t *data,
5863 size_t data_length )
5864{
5865 psa_status_t status;
5866 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5867 return( PSA_ERROR_BAD_STATE );
5868
5869 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5870 if( status != PSA_SUCCESS )
5871 return( status );
5872
5873 prf->state = TLS12_PRF_STATE_KEY_SET;
5874
5875 return( PSA_SUCCESS );
5876}
5877
Janos Follath63028dd2019-06-13 09:15:47 +01005878static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5879 const uint8_t *data,
5880 size_t data_length )
5881{
5882 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5883 return( PSA_ERROR_BAD_STATE );
5884
Janos Follathd6dce9f2019-07-04 09:11:38 +01005885 if( data_length != 0 )
5886 {
5887 prf->label = mbedtls_calloc( 1, data_length );
5888 if( prf->label == NULL )
5889 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005890
Janos Follathd6dce9f2019-07-04 09:11:38 +01005891 memcpy( prf->label, data, data_length );
5892 prf->label_length = data_length;
5893 }
Janos Follath63028dd2019-06-13 09:15:47 +01005894
5895 prf->state = TLS12_PRF_STATE_LABEL_SET;
5896
5897 return( PSA_SUCCESS );
5898}
5899
Janos Follathb03233e2019-06-11 15:30:30 +01005900static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5901 psa_algorithm_t hash_alg,
5902 psa_key_derivation_step_t step,
5903 const uint8_t *data,
5904 size_t data_length )
5905{
Janos Follathb03233e2019-06-11 15:30:30 +01005906 switch( step )
5907 {
Janos Follathf08e2652019-06-13 09:05:41 +01005908 case PSA_KEY_DERIVATION_INPUT_SEED:
5909 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005910 case PSA_KEY_DERIVATION_INPUT_SECRET:
5911 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005912 case PSA_KEY_DERIVATION_INPUT_LABEL:
5913 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005914 default:
5915 return( PSA_ERROR_INVALID_ARGUMENT );
5916 }
5917}
John Durkop07cc04a2020-11-16 22:08:34 -08005918#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5919 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5920
5921#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5922static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5923 psa_tls12_prf_key_derivation_t *prf,
5924 psa_algorithm_t hash_alg,
5925 const uint8_t *data,
5926 size_t data_length )
5927{
5928 psa_status_t status;
5929 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5930 uint8_t *cur = pms;
5931
5932 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5933 return( PSA_ERROR_INVALID_ARGUMENT );
5934
5935 /* Quoting RFC 4279, Section 2:
5936 *
5937 * The premaster secret is formed as follows: if the PSK is N octets
5938 * long, concatenate a uint16 with the value N, N zero octets, a second
5939 * uint16 with the value N, and the PSK itself.
5940 */
5941
5942 *cur++ = ( data_length >> 8 ) & 0xff;
5943 *cur++ = ( data_length >> 0 ) & 0xff;
5944 memset( cur, 0, data_length );
5945 cur += data_length;
5946 *cur++ = pms[0];
5947 *cur++ = pms[1];
5948 memcpy( cur, data, data_length );
5949 cur += data_length;
5950
5951 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5952
5953 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5954 return( status );
5955}
Janos Follath6660f0e2019-06-17 08:44:03 +01005956
5957static psa_status_t psa_tls12_prf_psk_to_ms_input(
5958 psa_tls12_prf_key_derivation_t *prf,
5959 psa_algorithm_t hash_alg,
5960 psa_key_derivation_step_t step,
5961 const uint8_t *data,
5962 size_t data_length )
5963{
5964 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005965 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005966 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5967 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005968 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005969
5970 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5971}
John Durkop07cc04a2020-11-16 22:08:34 -08005972#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005973
Gilles Peskineb8965192019-09-24 16:21:10 +02005974/** Check whether the given key type is acceptable for the given
5975 * input step of a key derivation.
5976 *
5977 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5978 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5979 * Both secret and non-secret inputs can alternatively have the type
5980 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5981 * that the input was passed as a buffer rather than via a key object.
5982 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005983static int psa_key_derivation_check_input_type(
5984 psa_key_derivation_step_t step,
5985 psa_key_type_t key_type )
5986{
5987 switch( step )
5988 {
5989 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005990 if( key_type == PSA_KEY_TYPE_DERIVE )
5991 return( PSA_SUCCESS );
5992 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005993 return( PSA_SUCCESS );
5994 break;
5995 case PSA_KEY_DERIVATION_INPUT_LABEL:
5996 case PSA_KEY_DERIVATION_INPUT_SALT:
5997 case PSA_KEY_DERIVATION_INPUT_INFO:
5998 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005999 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6000 return( PSA_SUCCESS );
6001 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006002 return( PSA_SUCCESS );
6003 break;
6004 }
6005 return( PSA_ERROR_INVALID_ARGUMENT );
6006}
6007
Janos Follathb80a94e2019-06-12 15:54:46 +01006008static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006009 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006010 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006011 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006012 const uint8_t *data,
6013 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006014{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006015 psa_status_t status;
6016 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6017
6018 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006019 if( status != PSA_SUCCESS )
6020 goto exit;
6021
John Durkopd0321952020-10-29 21:37:36 -07006022#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006023 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006024 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006025 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006026 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006027 step, data, data_length );
6028 }
John Durkop07cc04a2020-11-16 22:08:34 -08006029 else
6030#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6031#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6032 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006033 {
Janos Follathb03233e2019-06-11 15:30:30 +01006034 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6035 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6036 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006037 }
John Durkop07cc04a2020-11-16 22:08:34 -08006038 else
6039#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6040#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6041 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006042 {
6043 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6044 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6045 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006046 }
6047 else
John Durkop07cc04a2020-11-16 22:08:34 -08006048#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006049 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006050 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006051 return( PSA_ERROR_BAD_STATE );
6052 }
6053
Gilles Peskine224b0d62019-09-23 18:13:17 +02006054exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006055 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006056 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006057 return( status );
6058}
6059
Janos Follath51f4a0f2019-06-14 11:35:55 +01006060psa_status_t psa_key_derivation_input_bytes(
6061 psa_key_derivation_operation_t *operation,
6062 psa_key_derivation_step_t step,
6063 const uint8_t *data,
6064 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006065{
Gilles Peskineb8965192019-09-24 16:21:10 +02006066 return( psa_key_derivation_input_internal( operation, step,
6067 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006068 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006069}
6070
Janos Follath51f4a0f2019-06-14 11:35:55 +01006071psa_status_t psa_key_derivation_input_key(
6072 psa_key_derivation_operation_t *operation,
6073 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006074 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006075{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006076 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006077 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006078 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006079
Ronald Cron5c522922020-11-14 16:35:34 +01006080 status = psa_get_and_lock_transparent_key_slot_with_policy(
6081 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006082 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006083 {
6084 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006085 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006086 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006087
6088 /* Passing a key object as a SECRET input unlocks the permission
6089 * to output to a key object. */
6090 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6091 operation->can_output_key = 1;
6092
Ronald Cronf95a2b12020-10-22 15:24:49 +02006093 status = psa_key_derivation_input_internal( operation,
6094 step, slot->attr.type,
6095 slot->data.key.data,
6096 slot->data.key.bytes );
6097
Ronald Cron5c522922020-11-14 16:35:34 +01006098 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006099
Ronald Cron5c522922020-11-14 16:35:34 +01006100 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006101}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006102
6103
6104
6105/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006106/* Key agreement */
6107/****************************************************************/
6108
John Durkop6ba40d12020-11-10 08:50:04 -08006109#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006110static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6111 size_t peer_key_length,
6112 const mbedtls_ecp_keypair *our_key,
6113 uint8_t *shared_secret,
6114 size_t shared_secret_size,
6115 size_t *shared_secret_length )
6116{
Steven Cooremand4867872020-08-05 16:31:39 +02006117 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006118 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006119 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006120 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006121 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006122 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006123
Steven Cooreman4fed4552020-08-03 14:46:03 +02006124 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6125 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006126 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006127 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006128 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006129 goto exit;
6130
Jaeden Amero97271b32019-01-10 19:38:51 +00006131 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006132 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006133 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006134 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006135 status = mbedtls_to_psa_error(
6136 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6137 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006138 goto exit;
6139
Jaeden Amero97271b32019-01-10 19:38:51 +00006140 status = mbedtls_to_psa_error(
6141 mbedtls_ecdh_calc_secret( &ecdh,
6142 shared_secret_length,
6143 shared_secret, shared_secret_size,
6144 mbedtls_ctr_drbg_random,
6145 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006146 if( status != PSA_SUCCESS )
6147 goto exit;
6148 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6149 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006150
6151exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006152 if( status != PSA_SUCCESS )
6153 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006154 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006155 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006156 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006157
Jaeden Amero97271b32019-01-10 19:38:51 +00006158 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006159}
John Durkop6ba40d12020-11-10 08:50:04 -08006160#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006161
Gilles Peskine01d718c2018-09-18 12:01:02 +02006162#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6163
Gilles Peskine0216fe12019-04-11 21:23:21 +02006164static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6165 psa_key_slot_t *private_key,
6166 const uint8_t *peer_key,
6167 size_t peer_key_length,
6168 uint8_t *shared_secret,
6169 size_t shared_secret_size,
6170 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006171{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006172 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006173 {
John Durkop6ba40d12020-11-10 08:50:04 -08006174#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006175 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006176 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006177 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006178 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006179 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006180 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006181 private_key->data.key.data,
6182 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006183 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006184 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006185 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006186 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006187 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006188 shared_secret, shared_secret_size,
6189 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006190 mbedtls_ecp_keypair_free( ecp );
6191 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006192 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006193#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006194 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006195 (void) private_key;
6196 (void) peer_key;
6197 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006198 (void) shared_secret;
6199 (void) shared_secret_size;
6200 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006201 return( PSA_ERROR_NOT_SUPPORTED );
6202 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006203}
6204
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006205/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006206 * to potentially free embedded data structures and wipe confidential data.
6207 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006208static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006209 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006210 psa_key_slot_t *private_key,
6211 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006212 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006213{
6214 psa_status_t status;
6215 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6216 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006217 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006218
6219 /* Step 1: run the secret agreement algorithm to generate the shared
6220 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006221 status = psa_key_agreement_raw_internal( ka_alg,
6222 private_key,
6223 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006224 shared_secret,
6225 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006226 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006227 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006228 goto exit;
6229
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006230 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006231 * the shared secret. A shared secret is permitted wherever a key
6232 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006233 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006234 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006235 shared_secret,
6236 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006237exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006238 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006239 return( status );
6240}
6241
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006242psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006243 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006244 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006245 const uint8_t *peer_key,
6246 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006247{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006249 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006250 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006251
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006252 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006253 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006254 status = psa_get_and_lock_transparent_key_slot_with_policy(
6255 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006256 if( status != PSA_SUCCESS )
6257 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006258 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006259 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006260 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006261 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006262 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006263 else
6264 {
6265 /* If a private key has been added as SECRET, we allow the derived
6266 * key material to be used as a key in PSA Crypto. */
6267 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6268 operation->can_output_key = 1;
6269 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006270
Ronald Cron5c522922020-11-14 16:35:34 +01006271 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006272
Ronald Cron5c522922020-11-14 16:35:34 +01006273 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006274}
6275
Gilles Peskinebe697d82019-05-16 18:00:41 +02006276psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006277 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006278 const uint8_t *peer_key,
6279 size_t peer_key_length,
6280 uint8_t *output,
6281 size_t output_size,
6282 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006283{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006285 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006286 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006287
6288 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6289 {
6290 status = PSA_ERROR_INVALID_ARGUMENT;
6291 goto exit;
6292 }
Ronald Cron5c522922020-11-14 16:35:34 +01006293 status = psa_get_and_lock_transparent_key_slot_with_policy(
6294 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006295 if( status != PSA_SUCCESS )
6296 goto exit;
6297
6298 status = psa_key_agreement_raw_internal( alg, slot,
6299 peer_key, peer_key_length,
6300 output, output_size,
6301 output_length );
6302
6303exit:
6304 if( status != PSA_SUCCESS )
6305 {
6306 /* If an error happens and is not handled properly, the output
6307 * may be used as a key to protect sensitive data. Arrange for such
6308 * a key to be random, which is likely to result in decryption or
6309 * verification errors. This is better than filling the buffer with
6310 * some constant data such as zeros, which would result in the data
6311 * being protected with a reproducible, easily knowable key.
6312 */
6313 psa_generate_random( output, output_size );
6314 *output_length = output_size;
6315 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006316
Ronald Cron5c522922020-11-14 16:35:34 +01006317 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006318
Ronald Cron5c522922020-11-14 16:35:34 +01006319 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006320}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006321
6322
6323/****************************************************************/
6324/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006325/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006326
Gilles Peskine4c317f42018-07-12 01:24:09 +02006327psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006328 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006329{
Janos Follath24eed8d2019-11-22 13:21:35 +00006330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006331 GUARD_MODULE_INITIALIZED;
6332
Gilles Peskinef181eca2019-08-07 13:49:00 +02006333 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6334 {
6335 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6336 output,
6337 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6338 if( ret != 0 )
6339 return( mbedtls_to_psa_error( ret ) );
6340 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6341 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6342 }
6343
Gilles Peskinee59236f2018-01-27 23:32:46 +01006344 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6345 return( mbedtls_to_psa_error( ret ) );
6346}
6347
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006348#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6349#include "mbedtls/entropy_poll.h"
6350
Gilles Peskine7228da22019-07-15 11:06:15 +02006351psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006352 size_t seed_size )
6353{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006354 if( global_data.initialized )
6355 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006356
6357 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6358 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6359 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6360 return( PSA_ERROR_INVALID_ARGUMENT );
6361
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006362 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006363}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006364#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006365
John Durkop07cc04a2020-11-16 22:08:34 -08006366#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006367static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6368 size_t domain_parameters_size,
6369 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006370{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006371 size_t i;
6372 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373
Gilles Peskinee56e8782019-04-26 17:34:02 +02006374 if( domain_parameters_size == 0 )
6375 {
6376 *exponent = 65537;
6377 return( PSA_SUCCESS );
6378 }
6379
6380 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6381 * support values that fit in a 32-bit integer, which is larger than
6382 * int on just about every platform anyway. */
6383 if( domain_parameters_size > sizeof( acc ) )
6384 return( PSA_ERROR_NOT_SUPPORTED );
6385 for( i = 0; i < domain_parameters_size; i++ )
6386 acc = ( acc << 8 ) | domain_parameters[i];
6387 if( acc > INT_MAX )
6388 return( PSA_ERROR_NOT_SUPPORTED );
6389 *exponent = acc;
6390 return( PSA_SUCCESS );
6391}
John Durkop07cc04a2020-11-16 22:08:34 -08006392#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006393
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006394static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006395 psa_key_slot_t *slot, size_t bits,
6396 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006397{
Gilles Peskine8e338702019-07-30 20:06:31 +02006398 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006399
Gilles Peskinee56e8782019-04-26 17:34:02 +02006400 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006401 return( PSA_ERROR_INVALID_ARGUMENT );
6402
Gilles Peskinee59236f2018-01-27 23:32:46 +01006403 if( key_type_is_raw_bytes( type ) )
6404 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006405 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006406
6407 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006408 if( status != PSA_SUCCESS )
6409 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006410
6411 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006412 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6413 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006414 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006415
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006416 status = psa_generate_random( slot->data.key.data,
6417 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006418 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006420
6421 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006422#if defined(MBEDTLS_DES_C)
6423 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006424 psa_des_set_key_parity( slot->data.key.data,
6425 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006426#endif /* MBEDTLS_DES_C */
6427 }
6428 else
6429
John Durkop07cc04a2020-11-16 22:08:34 -08006430#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006431 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006433 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006435 int exponent;
6436 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006437 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6438 return( PSA_ERROR_NOT_SUPPORTED );
6439 /* Accept only byte-aligned keys, for the same reasons as
6440 * in psa_import_rsa_key(). */
6441 if( bits % 8 != 0 )
6442 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006443 status = psa_read_rsa_exponent( domain_parameters,
6444 domain_parameters_size,
6445 &exponent );
6446 if( status != PSA_SUCCESS )
6447 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006448 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6449 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006450 mbedtls_ctr_drbg_random,
6451 &global_data.ctr_drbg,
6452 (unsigned int) bits,
6453 exponent );
6454 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006455 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006456
6457 /* Make sure to always have an export representation available */
6458 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6459
Steven Cooreman75b74362020-07-28 14:30:13 +02006460 status = psa_allocate_buffer_to_slot( slot, bytes );
6461 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006462 {
6463 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006464 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006466
6467 status = psa_export_rsa_key( type,
6468 &rsa,
6469 slot->data.key.data,
6470 bytes,
6471 &slot->data.key.bytes );
6472 mbedtls_rsa_free( &rsa );
6473 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006474 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006475 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006476 }
6477 else
John Durkop07cc04a2020-11-16 22:08:34 -08006478#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006479
John Durkop9814fa22020-11-04 12:28:15 -08006480#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006481 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006482 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006483 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006484 mbedtls_ecp_group_id grp_id =
6485 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006486 const mbedtls_ecp_curve_info *curve_info =
6487 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006488 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006490 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006491 return( PSA_ERROR_NOT_SUPPORTED );
6492 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6493 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006494 mbedtls_ecp_keypair_init( &ecp );
6495 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006496 mbedtls_ctr_drbg_random,
6497 &global_data.ctr_drbg );
6498 if( ret != 0 )
6499 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006500 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006501 return( mbedtls_to_psa_error( ret ) );
6502 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006503
6504
6505 /* Make sure to always have an export representation available */
6506 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006507 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6508 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006509 {
6510 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006511 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006512 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006513
6514 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006515 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6516
6517 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006518 if( status != PSA_SUCCESS ) {
6519 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006520 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006521 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006522 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006523 }
6524 else
John Durkop9814fa22020-11-04 12:28:15 -08006525#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006526 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006528 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006529
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006530 return( PSA_SUCCESS );
6531}
Darryl Green0c6575a2018-11-07 16:05:30 +00006532
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006533psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006534 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006535{
6536 psa_status_t status;
6537 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006538 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006539
Ronald Cron81709fc2020-11-14 12:10:32 +01006540 *key = MBEDTLS_SVC_KEY_ID_INIT;
6541
Gilles Peskine0f84d622019-09-12 19:03:13 +02006542 /* Reject any attempt to create a zero-length key so that we don't
6543 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6544 if( psa_get_key_bits( attributes ) == 0 )
6545 return( PSA_ERROR_INVALID_ARGUMENT );
6546
Ronald Cron81709fc2020-11-14 12:10:32 +01006547 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6548 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006549 if( status != PSA_SUCCESS )
6550 goto exit;
6551
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006552 status = psa_driver_wrapper_generate_key( attributes,
6553 slot );
6554 if( status != PSA_ERROR_NOT_SUPPORTED ||
6555 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6556 goto exit;
6557
6558 status = psa_generate_key_internal(
6559 slot, attributes->core.bits,
6560 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006561
6562exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006563 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006564 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006565 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006566 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006567
Darryl Green0c6575a2018-11-07 16:05:30 +00006568 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006569}
6570
6571
Gilles Peskinee59236f2018-01-27 23:32:46 +01006572
6573/****************************************************************/
6574/* Module setup */
6575/****************************************************************/
6576
Gilles Peskine5e769522018-11-20 21:59:56 +01006577psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6578 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6579 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6580{
6581 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6582 return( PSA_ERROR_BAD_STATE );
6583 global_data.entropy_init = entropy_init;
6584 global_data.entropy_free = entropy_free;
6585 return( PSA_SUCCESS );
6586}
6587
Gilles Peskinee59236f2018-01-27 23:32:46 +01006588void mbedtls_psa_crypto_free( void )
6589{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006590 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006591 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6592 {
6593 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006594 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006595 }
6596 /* Wipe all remaining data, including configuration.
6597 * In particular, this sets all state indicator to the value
6598 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006599 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006600#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006601 /* Unregister all secure element drivers, so that we restart from
6602 * a pristine state. */
6603 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006604#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006605}
6606
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006607#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6608/** Recover a transaction that was interrupted by a power failure.
6609 *
6610 * This function is called during initialization, before psa_crypto_init()
6611 * returns. If this function returns a failure status, the initialization
6612 * fails.
6613 */
6614static psa_status_t psa_crypto_recover_transaction(
6615 const psa_crypto_transaction_t *transaction )
6616{
6617 switch( transaction->unknown.type )
6618 {
6619 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6620 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006621 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006622 * is implemented.
6623 * https://github.com/ARMmbed/mbed-crypto/issues/218
6624 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006625 default:
6626 /* We found an unsupported transaction in the storage.
6627 * We don't know what state the storage is in. Give up. */
6628 return( PSA_ERROR_STORAGE_FAILURE );
6629 }
6630}
6631#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6632
Gilles Peskinee59236f2018-01-27 23:32:46 +01006633psa_status_t psa_crypto_init( void )
6634{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006635 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006636 const unsigned char drbg_seed[] = "PSA";
6637
Gilles Peskinec6b69072018-11-20 21:42:52 +01006638 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006639 if( global_data.initialized != 0 )
6640 return( PSA_SUCCESS );
6641
Gilles Peskine5e769522018-11-20 21:59:56 +01006642 /* Set default configuration if
6643 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6644 if( global_data.entropy_init == NULL )
6645 global_data.entropy_init = mbedtls_entropy_init;
6646 if( global_data.entropy_free == NULL )
6647 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006648
Gilles Peskinec6b69072018-11-20 21:42:52 +01006649 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006650 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006651#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6652 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6653 /* The PSA entropy injection feature depends on using NV seed as an entropy
6654 * source. Add NV seed as an entropy source for PSA entropy injection. */
6655 mbedtls_entropy_add_source( &global_data.entropy,
6656 mbedtls_nv_seed_poll, NULL,
6657 MBEDTLS_ENTROPY_BLOCK_SIZE,
6658 MBEDTLS_ENTROPY_SOURCE_STRONG );
6659#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006660 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006661 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006662 status = mbedtls_to_psa_error(
6663 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6664 mbedtls_entropy_func,
6665 &global_data.entropy,
6666 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6667 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006668 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006669 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006670
Gilles Peskine66fb1262018-12-10 16:29:04 +01006671 status = psa_initialize_key_slots( );
6672 if( status != PSA_SUCCESS )
6673 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006674
Gilles Peskined9348f22019-10-01 15:22:29 +02006675#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6676 status = psa_init_all_se_drivers( );
6677 if( status != PSA_SUCCESS )
6678 goto exit;
6679#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6680
Gilles Peskine4b734222019-07-24 15:56:31 +02006681#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006682 status = psa_crypto_load_transaction( );
6683 if( status == PSA_SUCCESS )
6684 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006685 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6686 if( status != PSA_SUCCESS )
6687 goto exit;
6688 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006689 }
6690 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6691 {
6692 /* There's no transaction to complete. It's all good. */
6693 status = PSA_SUCCESS;
6694 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006695#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006696
Gilles Peskinec6b69072018-11-20 21:42:52 +01006697 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006698 global_data.initialized = 1;
6699
6700exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006701 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006702 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006703 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006704}
6705
6706#endif /* MBEDTLS_PSA_CRYPTO_C */