blob: 97b522dd54b59305ca44dff3d4220c3f6ad07b9b [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
Darryl Green06fd18d2018-07-16 11:21:11 +01001226/** Retrieve a slot which must contain a key. The key must have allow all the
1227 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1228 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001229static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001230 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001231 psa_key_usage_t usage,
1232 psa_algorithm_t alg )
1233{
1234 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001235 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001236
1237 *p_slot = NULL;
1238
Gilles Peskinec5487a82018-12-03 18:08:14 +01001239 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001240 if( status != PSA_SUCCESS )
1241 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001242
1243 /* Enforce that usage policy for the key slot contains all the flags
1244 * required by the usage parameter. There is one exception: public
1245 * keys can always be exported, so we treat public key objects as
1246 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001247 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001248 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001249 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001250 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001251
1252 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001253 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001254 return( PSA_ERROR_NOT_PERMITTED );
1255
1256 *p_slot = slot;
1257 return( PSA_SUCCESS );
1258}
Darryl Green940d72c2018-07-13 13:18:51 +01001259
Gilles Peskine28f8f302019-07-24 13:30:31 +02001260/** Retrieve a slot which must contain a transparent key.
1261 *
1262 * A transparent key is a key for which the key material is directly
1263 * available, as opposed to a key in a secure element.
1264 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001265 * This is a temporary function to use instead of psa_get_key_from_slot()
1266 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001267 */
1268#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1269static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1270 psa_key_slot_t **p_slot,
1271 psa_key_usage_t usage,
1272 psa_algorithm_t alg )
1273{
1274 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1275 if( status != PSA_SUCCESS )
1276 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001277 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001278 {
1279 *p_slot = NULL;
1280 return( PSA_ERROR_NOT_SUPPORTED );
1281 }
1282 return( PSA_SUCCESS );
1283}
1284#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1285/* With no secure element support, all keys are transparent. */
1286#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1287 psa_get_key_from_slot( handle, p_slot, usage, alg )
1288#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1289
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001290/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001291static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001292{
Gilles Peskine73167e12019-07-12 23:44:37 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001295 {
1296 /* No key material to clean. */
1297 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001298 else
1299#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001300 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001301 /* Data pointer will always be either a valid pointer or NULL in an
1302 * initialized slot, so we can just free it. */
1303 if( slot->data.key.data != NULL )
1304 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001305 mbedtls_free( slot->data.key.data );
1306 slot->data.key.data = NULL;
1307 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001308 }
Darryl Green40225ba2018-11-15 14:48:15 +00001309
1310 return( PSA_SUCCESS );
1311}
1312
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001313/** Completely wipe a slot in memory, including its policy.
1314 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001315psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001316{
1317 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001318 /* Multipart operations may still be using the key. This is safe
1319 * because all multipart operation objects are independent from
1320 * the key slot: if they need to access the key after the setup
1321 * phase, they have a copy of the key. Note that this means that
1322 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001323 /* At this point, key material and other type-specific content has
1324 * been wiped. Clear remaining metadata. We can call memset and not
1325 * zeroize because the metadata is not particularly sensitive. */
1326 memset( slot, 0, sizeof( *slot ) );
1327 return( status );
1328}
1329
Gilles Peskinec5487a82018-12-03 18:08:14 +01001330psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001332 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001333 psa_status_t status; /* status of the last operation */
1334 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001335#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1336 psa_se_drv_table_entry_t *driver;
1337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338
Gilles Peskine1841cf42019-10-08 15:48:25 +02001339 if( handle == 0 )
1340 return( PSA_SUCCESS );
1341
Gilles Peskinec5487a82018-12-03 18:08:14 +01001342 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001343 if( status != PSA_SUCCESS )
1344 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001345
1346#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001347 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001348 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001349 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001350 /* For a key in a secure element, we need to do three things:
1351 * remove the key file in internal storage, destroy the
1352 * key inside the secure element, and update the driver's
1353 * persistent data. Start a transaction that will encompass these
1354 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001355 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001356 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001357 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001358 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001359 status = psa_crypto_save_transaction( );
1360 if( status != PSA_SUCCESS )
1361 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001362 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001363 /* We should still try to destroy the key in the secure
1364 * element and the key metadata in storage. This is especially
1365 * important if the error is that the storage is full.
1366 * But how to do it exactly without risking an inconsistent
1367 * state after a reset?
1368 * https://github.com/ARMmbed/mbed-crypto/issues/215
1369 */
1370 overall_status = status;
1371 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001372 }
1373
Gilles Peskine354f7672019-07-12 23:46:38 +02001374 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001375 if( overall_status == PSA_SUCCESS )
1376 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001377 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001378#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1379
Darryl Greend49a4992018-06-18 17:27:26 +01001380#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001381 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001382 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001383 status = psa_destroy_persistent_key( slot->attr.id );
1384 if( overall_status == PSA_SUCCESS )
1385 overall_status = status;
1386
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001387 /* TODO: other slots may have a copy of the same key. We should
1388 * invalidate them.
1389 * https://github.com/ARMmbed/mbed-crypto/issues/214
1390 */
Darryl Greend49a4992018-06-18 17:27:26 +01001391 }
1392#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001393
Gilles Peskinefc762652019-07-22 19:30:34 +02001394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1395 if( driver != NULL )
1396 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001397 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001398 if( overall_status == PSA_SUCCESS )
1399 overall_status = status;
1400 status = psa_crypto_stop_transaction( );
1401 if( overall_status == PSA_SUCCESS )
1402 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001403 }
1404#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1405
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001406#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1407exit:
1408#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001409 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001410 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1411 if( overall_status == PSA_SUCCESS )
1412 overall_status = status;
1413 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001414}
1415
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001416void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001417{
Gilles Peskineb699f072019-04-26 16:06:02 +02001418 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001419 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001420}
1421
Gilles Peskineb699f072019-04-26 16:06:02 +02001422psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1423 psa_key_type_t type,
1424 const uint8_t *data,
1425 size_t data_length )
1426{
1427 uint8_t *copy = NULL;
1428
1429 if( data_length != 0 )
1430 {
1431 copy = mbedtls_calloc( 1, data_length );
1432 if( copy == NULL )
1433 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1434 memcpy( copy, data, data_length );
1435 }
1436 /* After this point, this function is guaranteed to succeed, so it
1437 * can start modifying `*attributes`. */
1438
1439 if( attributes->domain_parameters != NULL )
1440 {
1441 mbedtls_free( attributes->domain_parameters );
1442 attributes->domain_parameters = NULL;
1443 attributes->domain_parameters_size = 0;
1444 }
1445
1446 attributes->domain_parameters = copy;
1447 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001448 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001449 return( PSA_SUCCESS );
1450}
1451
1452psa_status_t psa_get_key_domain_parameters(
1453 const psa_key_attributes_t *attributes,
1454 uint8_t *data, size_t data_size, size_t *data_length )
1455{
1456 if( attributes->domain_parameters_size > data_size )
1457 return( PSA_ERROR_BUFFER_TOO_SMALL );
1458 *data_length = attributes->domain_parameters_size;
1459 if( attributes->domain_parameters_size != 0 )
1460 memcpy( data, attributes->domain_parameters,
1461 attributes->domain_parameters_size );
1462 return( PSA_SUCCESS );
1463}
1464
John Durkop07cc04a2020-11-16 22:08:34 -08001465#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001467static psa_status_t psa_get_rsa_public_exponent(
1468 const mbedtls_rsa_context *rsa,
1469 psa_key_attributes_t *attributes )
1470{
1471 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001473 uint8_t *buffer = NULL;
1474 size_t buflen;
1475 mbedtls_mpi_init( &mpi );
1476
1477 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1478 if( ret != 0 )
1479 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001480 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1481 {
1482 /* It's the default value, which is reported as an empty string,
1483 * so there's nothing to do. */
1484 goto exit;
1485 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001486
1487 buflen = mbedtls_mpi_size( &mpi );
1488 buffer = mbedtls_calloc( 1, buflen );
1489 if( buffer == NULL )
1490 {
1491 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1492 goto exit;
1493 }
1494 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1495 if( ret != 0 )
1496 goto exit;
1497 attributes->domain_parameters = buffer;
1498 attributes->domain_parameters_size = buflen;
1499
1500exit:
1501 mbedtls_mpi_free( &mpi );
1502 if( ret != 0 )
1503 mbedtls_free( buffer );
1504 return( mbedtls_to_psa_error( ret ) );
1505}
John Durkop07cc04a2020-11-16 22:08:34 -08001506#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001507 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001508
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001509/** Retrieve all the publicly-accessible attributes of a key.
1510 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001511psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1512 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001514 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001515 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001517 psa_reset_key_attributes( attributes );
1518
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001519 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001520 if( status != PSA_SUCCESS )
1521 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001522
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001523 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001524 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1525 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1526
1527#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1528 if( psa_key_slot_is_external( slot ) )
1529 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1530#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001531
Gilles Peskine8e338702019-07-30 20:06:31 +02001532 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001533 {
John Durkop07cc04a2020-11-16 22:08:34 -08001534#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001535 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001536 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001537 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001538#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001539 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001540 * is not yet implemented.
1541 * https://github.com/ARMmbed/mbed-crypto/issues/216
1542 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001543 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001544 break;
1545#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001546 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001547 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001548
Steven Cooreman4fed4552020-08-03 14:46:03 +02001549 status = psa_load_rsa_representation( slot->attr.type,
1550 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001551 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001552 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001553 if( status != PSA_SUCCESS )
1554 break;
1555
Steven Cooremana2371e52020-07-28 14:30:39 +02001556 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001557 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001558 mbedtls_rsa_free( rsa );
1559 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001560 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001561 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001562#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001563 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001564 default:
1565 /* Nothing else to do. */
1566 break;
1567 }
1568
1569 if( status != PSA_SUCCESS )
1570 psa_reset_key_attributes( attributes );
1571 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572}
1573
Gilles Peskinec8000c02019-08-02 20:15:51 +02001574#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1575psa_status_t psa_get_key_slot_number(
1576 const psa_key_attributes_t *attributes,
1577 psa_key_slot_number_t *slot_number )
1578{
1579 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1580 {
1581 *slot_number = attributes->slot_number;
1582 return( PSA_SUCCESS );
1583 }
1584 else
1585 return( PSA_ERROR_INVALID_ARGUMENT );
1586}
1587#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1588
Steven Cooremana01795d2020-07-24 22:48:15 +02001589static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1590 uint8_t *data,
1591 size_t data_size,
1592 size_t *data_length )
1593{
1594 if( slot->data.key.bytes > data_size )
1595 return( PSA_ERROR_BUFFER_TOO_SMALL );
1596 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1597 memset( data + slot->data.key.bytes, 0,
1598 data_size - slot->data.key.bytes );
1599 *data_length = slot->data.key.bytes;
1600 return( PSA_SUCCESS );
1601}
1602
Gilles Peskinef603c712019-01-19 13:40:11 +01001603static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1604 uint8_t *data,
1605 size_t data_size,
1606 size_t *data_length,
1607 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608{
Gilles Peskine5d309672019-07-12 23:47:28 +02001609#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1610 const psa_drv_se_t *drv;
1611 psa_drv_se_context_t *drv_context;
1612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1613
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001614 *data_length = 0;
1615
Gilles Peskine8e338702019-07-30 20:06:31 +02001616 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001617 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001618
Gilles Peskinef9168942019-09-12 19:20:29 +02001619 /* Reject a zero-length output buffer now, since this can never be a
1620 * valid key representation. This way we know that data must be a valid
1621 * pointer and we can do things like memset(data, ..., data_size). */
1622 if( data_size == 0 )
1623 return( PSA_ERROR_BUFFER_TOO_SMALL );
1624
Gilles Peskine5d309672019-07-12 23:47:28 +02001625#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001626 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001627 {
1628 psa_drv_se_export_key_t method;
1629 if( drv->key_management == NULL )
1630 return( PSA_ERROR_NOT_SUPPORTED );
1631 method = ( export_public_key ?
1632 drv->key_management->p_export_public :
1633 drv->key_management->p_export );
1634 if( method == NULL )
1635 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001636 return( method( drv_context,
1637 slot->data.se.slot_number,
1638 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001639 }
1640#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1641
Gilles Peskine8e338702019-07-30 20:06:31 +02001642 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001643 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001644 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001645 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001646 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1647 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001648 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001649 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001650 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001651 /* Exporting public -> public */
1652 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1653 }
1654 else if( !export_public_key )
1655 {
1656 /* Exporting private -> private */
1657 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1658 }
1659 /* Need to export the public part of a private key,
1660 * so conversion is needed */
1661 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1662 {
John Durkop0e005192020-10-31 22:06:54 -07001663#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1664 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001665 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001666 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001667 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001668 slot->data.key.data,
1669 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001670 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001671 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001672 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001673
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001675 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001676 data,
1677 data_size,
1678 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001679
Steven Cooremana2371e52020-07-28 14:30:39 +02001680 mbedtls_rsa_free( rsa );
1681 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001682
Steven Cooreman560c28a2020-07-24 23:20:24 +02001683 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001684#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001685 /* We don't know how to convert a private RSA key to public. */
1686 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001687#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1688 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001689 }
1690 else
Moran Pekera998bc62018-04-16 18:16:20 +03001691 {
John Durkop6ba40d12020-11-10 08:50:04 -08001692#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1693 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001694 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001695 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001696 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001697 slot->data.key.data,
1698 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001699 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001700 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001701 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001702
1703 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1704 PSA_KEY_TYPE_ECC_GET_FAMILY(
1705 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001706 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001707 data,
1708 data_size,
1709 data_length );
1710
Steven Cooremana2371e52020-07-28 14:30:39 +02001711 mbedtls_ecp_keypair_free( ecp );
1712 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001713 return( status );
1714#else
1715 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001716 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001717#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1718 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001719 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001720 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001721 else
1722 {
1723 /* This shouldn't happen in the reference implementation, but
1724 it is valid for a special-purpose implementation to omit
1725 support for exporting certain key types. */
1726 return( PSA_ERROR_NOT_SUPPORTED );
1727 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001728}
1729
Gilles Peskinec5487a82018-12-03 18:08:14 +01001730psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001731 uint8_t *data,
1732 size_t data_size,
1733 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001734{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001735 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001736 psa_status_t status;
1737
1738 /* Set the key to empty now, so that even when there are errors, we always
1739 * set data_length to a value between 0 and data_size. On error, setting
1740 * the key to empty is a good choice because an empty key representation is
1741 * unlikely to be accepted anywhere. */
1742 *data_length = 0;
1743
1744 /* Export requires the EXPORT flag. There is an exception for public keys,
1745 * which don't require any flag, but psa_get_key_from_slot takes
1746 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001747 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001748 if( status != PSA_SUCCESS )
1749 return( status );
1750 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001751 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001752}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001753
Gilles Peskinec5487a82018-12-03 18:08:14 +01001754psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001755 uint8_t *data,
1756 size_t data_size,
1757 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001758{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001759 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001760 psa_status_t status;
1761
1762 /* Set the key to empty now, so that even when there are errors, we always
1763 * set data_length to a value between 0 and data_size. On error, setting
1764 * the key to empty is a good choice because an empty key representation is
1765 * unlikely to be accepted anywhere. */
1766 *data_length = 0;
1767
1768 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001769 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001770 if( status != PSA_SUCCESS )
1771 return( status );
1772 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001773 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001774}
1775
Gilles Peskine91e8c332019-08-02 19:19:39 +02001776#if defined(static_assert)
1777static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1778 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001779static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001780 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001781static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001782 "One or more key attribute flag is listed as both internal-only and external-only" );
1783#endif
1784
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001785/** Validate that a key policy is internally well-formed.
1786 *
1787 * This function only rejects invalid policies. It does not validate the
1788 * consistency of the policy with respect to other attributes of the key
1789 * such as the key type.
1790 */
1791static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001792{
1793 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001794 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001795 PSA_KEY_USAGE_ENCRYPT |
1796 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001797 PSA_KEY_USAGE_SIGN_HASH |
1798 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1800 return( PSA_ERROR_INVALID_ARGUMENT );
1801
Gilles Peskine4747d192019-04-17 15:05:45 +02001802 return( PSA_SUCCESS );
1803}
1804
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001805/** Validate the internal consistency of key attributes.
1806 *
1807 * This function only rejects invalid attribute values. If does not
1808 * validate the consistency of the attributes with any key data that may
1809 * be involved in the creation of the key.
1810 *
1811 * Call this function early in the key creation process.
1812 *
1813 * \param[in] attributes Key attributes for the new key.
1814 * \param[out] p_drv On any return, the driver for the key, if any.
1815 * NULL for a transparent key.
1816 *
1817 */
1818static psa_status_t psa_validate_key_attributes(
1819 const psa_key_attributes_t *attributes,
1820 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001821{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001822 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001823
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001824 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1825 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001826 if( status != PSA_SUCCESS )
1827 return( status );
1828
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001829 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1830 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001831 if( status != PSA_SUCCESS )
1832 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001833
1834 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001835 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001836 return( status );
1837
1838 /* Refuse to create overly large keys.
1839 * Note that this doesn't trigger on import if the attributes don't
1840 * explicitly specify a size (so psa_get_key_bits returns 0), so
1841 * psa_import_key() needs its own checks. */
1842 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1843 return( PSA_ERROR_NOT_SUPPORTED );
1844
Gilles Peskine91e8c332019-08-02 19:19:39 +02001845 /* Reject invalid flags. These should not be reachable through the API. */
1846 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1847 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1848 return( PSA_ERROR_INVALID_ARGUMENT );
1849
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001850 return( PSA_SUCCESS );
1851}
1852
Gilles Peskine4747d192019-04-17 15:05:45 +02001853/** Prepare a key slot to receive key material.
1854 *
1855 * This function allocates a key slot and sets its metadata.
1856 *
1857 * If this function fails, call psa_fail_key_creation().
1858 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001859 * This function is intended to be used as follows:
1860 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1861 * it with the specified attributes, and assign it a handle.
1862 * -# Populate the slot with the key material.
1863 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1864 * In case of failure at any step, stop the sequence and call
1865 * psa_fail_key_creation().
1866 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001867 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001868 * \param[in] attributes Key attributes for the new key.
1869 * \param[out] handle On success, a handle for the allocated slot.
1870 * \param[out] p_slot On success, a pointer to the prepared slot.
1871 * \param[out] p_drv On any return, the driver for the key, if any.
1872 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001873 *
1874 * \retval #PSA_SUCCESS
1875 * The key slot is ready to receive key material.
1876 * \return If this function fails, the key slot is an invalid state.
1877 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 */
1879static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001880 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 const psa_key_attributes_t *attributes,
1882 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001883 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001884 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001885{
1886 psa_status_t status;
1887 psa_key_slot_t *slot;
1888
Gilles Peskinedf179142019-07-15 22:02:14 +02001889 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001890 *p_drv = NULL;
1891
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001892 status = psa_validate_key_attributes( attributes, p_drv );
1893 if( status != PSA_SUCCESS )
1894 return( status );
1895
Gilles Peskineedbed562019-08-07 18:19:59 +02001896 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 if( status != PSA_SUCCESS )
1898 return( status );
1899 slot = *p_slot;
1900
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001901 /* We're storing the declared bit-size of the key. It's up to each
1902 * creation mechanism to verify that this information is correct.
1903 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001904 * an input (generate, device) but not for those where the bit-size
1905 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001906
1907 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001908
Gilles Peskine91e8c332019-08-02 19:19:39 +02001909 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001910 * external-only flags, query `attributes`. Thanks to the check
1911 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001912 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001913 * may have set. */
1914 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001915
Gilles Peskinecbaff462019-07-12 23:46:04 +02001916#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001917 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001918 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001919 * create the key file in internal storage, create the
1920 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001921 * persistent data. This is done by starting a transaction that will
1922 * encompass these three actions.
1923 * For registering a volatile key, we just need to find an appropriate
1924 * slot number inside the SE. Since the key is designated volatile, creating
1925 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001926 /* The first thing to do is to find a slot number for the new key.
1927 * We save the slot number in persistent storage as part of the
1928 * transaction data. It will be needed to recover if the power
1929 * fails during the key creation process, to clean up on the secure
1930 * element side after restarting. Obtaining a slot number from the
1931 * secure element driver updates its persistent state, but we do not yet
1932 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001933 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001934 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001935 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001936 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001937 &slot->data.se.slot_number );
1938 if( status != PSA_SUCCESS )
1939 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001940
1941 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001942 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001943 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1944 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1945 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1946 psa_crypto_transaction.key.id = slot->attr.id;
1947 status = psa_crypto_save_transaction( );
1948 if( status != PSA_SUCCESS )
1949 {
1950 (void) psa_crypto_stop_transaction( );
1951 return( status );
1952 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001953 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001954 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001955
1956 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1957 {
1958 /* Key registration only makes sense with a secure element. */
1959 return( PSA_ERROR_INVALID_ARGUMENT );
1960 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001961#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1962
Darryl Green0c6575a2018-11-07 16:05:30 +00001963 return( status );
1964}
Gilles Peskine4747d192019-04-17 15:05:45 +02001965
1966/** Finalize the creation of a key once its key material has been set.
1967 *
1968 * This entails writing the key to persistent storage.
1969 *
1970 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001971 * See the documentation of psa_start_key_creation() for the intended use
1972 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001973 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001974 * \param[in,out] slot Pointer to the slot with key material.
1975 * \param[in] driver The secure element driver for the key,
1976 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001977 *
1978 * \retval #PSA_SUCCESS
1979 * The key was successfully created. The handle is now valid.
1980 * \return If this function fails, the key slot is an invalid state.
1981 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001982 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001983static psa_status_t psa_finish_key_creation(
1984 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001985 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001986{
1987 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001988 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001989 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001990
1991#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001992 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001993 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001994#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1995 if( driver != NULL )
1996 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001997 psa_se_key_data_storage_t data;
1998#if defined(static_assert)
1999 static_assert( sizeof( slot->data.se.slot_number ) ==
2000 sizeof( data.slot_number ),
2001 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002002#endif
2003 memcpy( &data.slot_number, &slot->data.se.slot_number,
2004 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002005 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002006 (uint8_t*) &data,
2007 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002008 }
2009 else
2010#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2011 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002012 /* Key material is saved in export representation in the slot, so
2013 * just pass the slot buffer for storage. */
2014 status = psa_save_persistent_key( &slot->attr,
2015 slot->data.key.data,
2016 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002017 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002018 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002019#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2020
Gilles Peskinecbaff462019-07-12 23:46:04 +02002021#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002022 /* Finish the transaction for a key creation. This does not
2023 * happen when registering an existing key. Detect this case
2024 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002025 * creation of a persistent key in a secure element requires a transaction,
2026 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002027 if( driver != NULL &&
2028 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002029 {
2030 status = psa_save_se_persistent_data( driver );
2031 if( status != PSA_SUCCESS )
2032 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002033 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002034 return( status );
2035 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002036 status = psa_crypto_stop_transaction( );
2037 if( status != PSA_SUCCESS )
2038 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002039 }
2040#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2041
Gilles Peskine4747d192019-04-17 15:05:45 +02002042 return( status );
2043}
2044
2045/** Abort the creation of a key.
2046 *
2047 * You may call this function after calling psa_start_key_creation(),
2048 * or after psa_finish_key_creation() fails. In other circumstances, this
2049 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002050 * See the documentation of psa_start_key_creation() for the intended use
2051 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002052 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002053 * \param[in,out] slot Pointer to the slot with key material.
2054 * \param[in] driver The secure element driver for the key,
2055 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002056 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002057static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002058 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002059{
Gilles Peskine011e4282019-06-26 18:34:38 +02002060 (void) driver;
2061
Gilles Peskine4747d192019-04-17 15:05:45 +02002062 if( slot == NULL )
2063 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002064
2065#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002066 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002067 * element, and the failure happened later (when saving metadata
2068 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002069 * element.
2070 * https://github.com/ARMmbed/mbed-crypto/issues/217
2071 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002072
Gilles Peskined7729582019-08-05 15:55:54 +02002073 /* Abort the ongoing transaction if any (there may not be one if
2074 * the creation process failed before starting one, or if the
2075 * key creation is a registration of a key in a secure element).
2076 * Earlier functions must already have done what it takes to undo any
2077 * partial creation. All that's left is to update the transaction data
2078 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002079 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002080#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2081
Gilles Peskine4747d192019-04-17 15:05:45 +02002082 psa_wipe_key_slot( slot );
2083}
2084
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002085/** Validate optional attributes during key creation.
2086 *
2087 * Some key attributes are optional during key creation. If they are
2088 * specified in the attributes structure, check that they are consistent
2089 * with the data in the slot.
2090 *
2091 * This function should be called near the end of key creation, after
2092 * the slot in memory is fully populated but before saving persistent data.
2093 */
2094static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002095 const psa_key_slot_t *slot,
2096 const psa_key_attributes_t *attributes )
2097{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002098 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002099 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002100 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002101 return( PSA_ERROR_INVALID_ARGUMENT );
2102 }
2103
2104 if( attributes->domain_parameters_size != 0 )
2105 {
John Durkop0e005192020-10-31 22:06:54 -07002106#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2107 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002108 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002110 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002111 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002113
Steven Cooreman7f391872020-07-30 14:57:44 +02002114 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002115 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002116 slot->data.key.data,
2117 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002118 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002119 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002120 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002121
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002122 mbedtls_mpi_init( &actual );
2123 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002124 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002125 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002126 mbedtls_rsa_free( rsa );
2127 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 if( ret != 0 )
2129 goto rsa_exit;
2130 ret = mbedtls_mpi_read_binary( &required,
2131 attributes->domain_parameters,
2132 attributes->domain_parameters_size );
2133 if( ret != 0 )
2134 goto rsa_exit;
2135 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2136 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2137 rsa_exit:
2138 mbedtls_mpi_free( &actual );
2139 mbedtls_mpi_free( &required );
2140 if( ret != 0)
2141 return( mbedtls_to_psa_error( ret ) );
2142 }
2143 else
John Durkop9814fa22020-11-04 12:28:15 -08002144#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2145 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002146 {
2147 return( PSA_ERROR_INVALID_ARGUMENT );
2148 }
2149 }
2150
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002151 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002152 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002153 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002154 return( PSA_ERROR_INVALID_ARGUMENT );
2155 }
2156
2157 return( PSA_SUCCESS );
2158}
2159
Gilles Peskine4747d192019-04-17 15:05:45 +02002160psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002161 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002162 size_t data_length,
2163 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002164{
2165 psa_status_t status;
2166 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002167 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002168
Gilles Peskine0f84d622019-09-12 19:03:13 +02002169 /* Reject zero-length symmetric keys (including raw data key objects).
2170 * This also rejects any key which might be encoded as an empty string,
2171 * which is never valid. */
2172 if( data_length == 0 )
2173 return( PSA_ERROR_INVALID_ARGUMENT );
2174
Gilles Peskinedf179142019-07-15 22:02:14 +02002175 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2176 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002177 if( status != PSA_SUCCESS )
2178 goto exit;
2179
Gilles Peskine5d309672019-07-12 23:47:28 +02002180#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2181 if( driver != NULL )
2182 {
2183 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002184 /* The driver should set the number of key bits, however in
2185 * case it doesn't, we initialize bits to an invalid value. */
2186 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002187 if( drv->key_management == NULL ||
2188 drv->key_management->p_import == NULL )
2189 {
2190 status = PSA_ERROR_NOT_SUPPORTED;
2191 goto exit;
2192 }
2193 status = drv->key_management->p_import(
2194 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002195 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002196 &bits );
2197 if( status != PSA_SUCCESS )
2198 goto exit;
2199 if( bits > PSA_MAX_KEY_BITS )
2200 {
2201 status = PSA_ERROR_NOT_SUPPORTED;
2202 goto exit;
2203 }
2204 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002205 }
2206 else
2207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2208 {
2209 status = psa_import_key_into_slot( slot, data, data_length );
2210 if( status != PSA_SUCCESS )
2211 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002212 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002213 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002214 if( status != PSA_SUCCESS )
2215 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002216
Gilles Peskine011e4282019-06-26 18:34:38 +02002217 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002218exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002219 if( status != PSA_SUCCESS )
2220 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002221 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002222 *handle = 0;
2223 }
2224 return( status );
2225}
2226
Gilles Peskined7729582019-08-05 15:55:54 +02002227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2228psa_status_t mbedtls_psa_register_se_key(
2229 const psa_key_attributes_t *attributes )
2230{
2231 psa_status_t status;
2232 psa_key_slot_t *slot = NULL;
2233 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002234 psa_key_handle_t handle = 0;
2235
2236 /* Leaving attributes unspecified is not currently supported.
2237 * It could make sense to query the key type and size from the
2238 * secure element, but not all secure elements support this
2239 * and the driver HAL doesn't currently support it. */
2240 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2241 return( PSA_ERROR_NOT_SUPPORTED );
2242 if( psa_get_key_bits( attributes ) == 0 )
2243 return( PSA_ERROR_NOT_SUPPORTED );
2244
2245 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2246 &handle, &slot, &driver );
2247 if( status != PSA_SUCCESS )
2248 goto exit;
2249
Gilles Peskined7729582019-08-05 15:55:54 +02002250 status = psa_finish_key_creation( slot, driver );
2251
2252exit:
2253 if( status != PSA_SUCCESS )
2254 {
2255 psa_fail_key_creation( slot, driver );
2256 }
2257 /* Registration doesn't keep the key in RAM. */
2258 psa_close_key( handle );
2259 return( status );
2260}
2261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2262
Gilles Peskinef603c712019-01-19 13:40:11 +01002263static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002264 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002265{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002266 psa_status_t status = psa_copy_key_material_into_slot( target,
2267 source->data.key.data,
2268 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002269 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002270 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002271
Steven Cooreman398aee52020-10-13 14:35:45 +02002272 target->attr.type = source->attr.type;
2273 target->attr.bits = source->attr.bits;
2274
2275 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002276}
2277
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002278psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2279 const psa_key_attributes_t *specified_attributes,
2280 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002281{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002282 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002283 psa_key_slot_t *source_slot = NULL;
2284 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002285 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002286 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002287
Gilles Peskine28f8f302019-07-24 13:30:31 +02002288 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002289 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002290 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002291 goto exit;
2292
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002293 status = psa_validate_optional_attributes( source_slot,
2294 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002295 if( status != PSA_SUCCESS )
2296 goto exit;
2297
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002298 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002299 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002300 if( status != PSA_SUCCESS )
2301 goto exit;
2302
Gilles Peskinedf179142019-07-15 22:02:14 +02002303 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2304 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002305 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002306 if( status != PSA_SUCCESS )
2307 goto exit;
2308
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002309#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2310 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002311 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002312 /* Copying to a secure element is not implemented yet. */
2313 status = PSA_ERROR_NOT_SUPPORTED;
2314 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002315 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002316#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002317
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002318 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002319 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002320 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002321
Gilles Peskine011e4282019-06-26 18:34:38 +02002322 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002323exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002324 if( status != PSA_SUCCESS )
2325 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002326 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002327 *target_handle = 0;
2328 }
2329 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002330}
2331
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002332
2333
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002334/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002335/* Message digests */
2336/****************************************************************/
2337
John Durkop07cc04a2020-11-16 22:08:34 -08002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002339 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2340 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002341 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002342static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002343{
2344 switch( alg )
2345 {
2346#if defined(MBEDTLS_MD2_C)
2347 case PSA_ALG_MD2:
2348 return( &mbedtls_md2_info );
2349#endif
2350#if defined(MBEDTLS_MD4_C)
2351 case PSA_ALG_MD4:
2352 return( &mbedtls_md4_info );
2353#endif
2354#if defined(MBEDTLS_MD5_C)
2355 case PSA_ALG_MD5:
2356 return( &mbedtls_md5_info );
2357#endif
2358#if defined(MBEDTLS_RIPEMD160_C)
2359 case PSA_ALG_RIPEMD160:
2360 return( &mbedtls_ripemd160_info );
2361#endif
2362#if defined(MBEDTLS_SHA1_C)
2363 case PSA_ALG_SHA_1:
2364 return( &mbedtls_sha1_info );
2365#endif
2366#if defined(MBEDTLS_SHA256_C)
2367 case PSA_ALG_SHA_224:
2368 return( &mbedtls_sha224_info );
2369 case PSA_ALG_SHA_256:
2370 return( &mbedtls_sha256_info );
2371#endif
2372#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002373#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002374 case PSA_ALG_SHA_384:
2375 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002376#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002377 case PSA_ALG_SHA_512:
2378 return( &mbedtls_sha512_info );
2379#endif
2380 default:
2381 return( NULL );
2382 }
2383}
John Durkop07cc04a2020-11-16 22:08:34 -08002384#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002385 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2386 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2387 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002388
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2390{
2391 switch( operation->alg )
2392 {
Gilles Peskine81736312018-06-26 15:04:31 +02002393 case 0:
2394 /* The object has (apparently) been initialized but it is not
2395 * in use. It's ok to call abort on such an object, and there's
2396 * nothing to do. */
2397 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398#if defined(MBEDTLS_MD2_C)
2399 case PSA_ALG_MD2:
2400 mbedtls_md2_free( &operation->ctx.md2 );
2401 break;
2402#endif
2403#if defined(MBEDTLS_MD4_C)
2404 case PSA_ALG_MD4:
2405 mbedtls_md4_free( &operation->ctx.md4 );
2406 break;
2407#endif
2408#if defined(MBEDTLS_MD5_C)
2409 case PSA_ALG_MD5:
2410 mbedtls_md5_free( &operation->ctx.md5 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_RIPEMD160_C)
2414 case PSA_ALG_RIPEMD160:
2415 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2416 break;
2417#endif
2418#if defined(MBEDTLS_SHA1_C)
2419 case PSA_ALG_SHA_1:
2420 mbedtls_sha1_free( &operation->ctx.sha1 );
2421 break;
2422#endif
2423#if defined(MBEDTLS_SHA256_C)
2424 case PSA_ALG_SHA_224:
2425 case PSA_ALG_SHA_256:
2426 mbedtls_sha256_free( &operation->ctx.sha256 );
2427 break;
2428#endif
2429#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002430#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002432#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433 case PSA_ALG_SHA_512:
2434 mbedtls_sha512_free( &operation->ctx.sha512 );
2435 break;
2436#endif
2437 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002438 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439 }
2440 operation->alg = 0;
2441 return( PSA_SUCCESS );
2442}
2443
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002444psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002445 psa_algorithm_t alg )
2446{
Janos Follath24eed8d2019-11-22 13:21:35 +00002447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002448
2449 /* A context must be freshly initialized before it can be set up. */
2450 if( operation->alg != 0 )
2451 {
2452 return( PSA_ERROR_BAD_STATE );
2453 }
2454
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002455 switch( alg )
2456 {
2457#if defined(MBEDTLS_MD2_C)
2458 case PSA_ALG_MD2:
2459 mbedtls_md2_init( &operation->ctx.md2 );
2460 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2461 break;
2462#endif
2463#if defined(MBEDTLS_MD4_C)
2464 case PSA_ALG_MD4:
2465 mbedtls_md4_init( &operation->ctx.md4 );
2466 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2467 break;
2468#endif
2469#if defined(MBEDTLS_MD5_C)
2470 case PSA_ALG_MD5:
2471 mbedtls_md5_init( &operation->ctx.md5 );
2472 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2473 break;
2474#endif
2475#if defined(MBEDTLS_RIPEMD160_C)
2476 case PSA_ALG_RIPEMD160:
2477 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2478 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2479 break;
2480#endif
2481#if defined(MBEDTLS_SHA1_C)
2482 case PSA_ALG_SHA_1:
2483 mbedtls_sha1_init( &operation->ctx.sha1 );
2484 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2485 break;
2486#endif
2487#if defined(MBEDTLS_SHA256_C)
2488 case PSA_ALG_SHA_224:
2489 mbedtls_sha256_init( &operation->ctx.sha256 );
2490 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2491 break;
2492 case PSA_ALG_SHA_256:
2493 mbedtls_sha256_init( &operation->ctx.sha256 );
2494 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2495 break;
2496#endif
2497#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002498#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499 case PSA_ALG_SHA_384:
2500 mbedtls_sha512_init( &operation->ctx.sha512 );
2501 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2502 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002503#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002504 case PSA_ALG_SHA_512:
2505 mbedtls_sha512_init( &operation->ctx.sha512 );
2506 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2507 break;
2508#endif
2509 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002510 return( PSA_ALG_IS_HASH( alg ) ?
2511 PSA_ERROR_NOT_SUPPORTED :
2512 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002513 }
2514 if( ret == 0 )
2515 operation->alg = alg;
2516 else
2517 psa_hash_abort( operation );
2518 return( mbedtls_to_psa_error( ret ) );
2519}
2520
2521psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2522 const uint8_t *input,
2523 size_t input_length )
2524{
Janos Follath24eed8d2019-11-22 13:21:35 +00002525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002526
2527 /* Don't require hash implementations to behave correctly on a
2528 * zero-length input, which may have an invalid pointer. */
2529 if( input_length == 0 )
2530 return( PSA_SUCCESS );
2531
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002532 switch( operation->alg )
2533 {
2534#if defined(MBEDTLS_MD2_C)
2535 case PSA_ALG_MD2:
2536 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2537 input, input_length );
2538 break;
2539#endif
2540#if defined(MBEDTLS_MD4_C)
2541 case PSA_ALG_MD4:
2542 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2543 input, input_length );
2544 break;
2545#endif
2546#if defined(MBEDTLS_MD5_C)
2547 case PSA_ALG_MD5:
2548 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2549 input, input_length );
2550 break;
2551#endif
2552#if defined(MBEDTLS_RIPEMD160_C)
2553 case PSA_ALG_RIPEMD160:
2554 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2555 input, input_length );
2556 break;
2557#endif
2558#if defined(MBEDTLS_SHA1_C)
2559 case PSA_ALG_SHA_1:
2560 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2561 input, input_length );
2562 break;
2563#endif
2564#if defined(MBEDTLS_SHA256_C)
2565 case PSA_ALG_SHA_224:
2566 case PSA_ALG_SHA_256:
2567 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2568 input, input_length );
2569 break;
2570#endif
2571#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002572#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002574#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 case PSA_ALG_SHA_512:
2576 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2577 input, input_length );
2578 break;
2579#endif
2580 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002581 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002582 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002583
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 if( ret != 0 )
2585 psa_hash_abort( operation );
2586 return( mbedtls_to_psa_error( ret ) );
2587}
2588
2589psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2590 uint8_t *hash,
2591 size_t hash_size,
2592 size_t *hash_length )
2593{
itayzafrir40835d42018-08-02 13:14:17 +03002594 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002596 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002597
2598 /* Fill the output buffer with something that isn't a valid hash
2599 * (barring an attack on the hash and deliberately-crafted input),
2600 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002601 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002602 /* If hash_size is 0 then hash may be NULL and then the
2603 * call to memset would have undefined behavior. */
2604 if( hash_size != 0 )
2605 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002606
2607 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002608 {
2609 status = PSA_ERROR_BUFFER_TOO_SMALL;
2610 goto exit;
2611 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002612
2613 switch( operation->alg )
2614 {
2615#if defined(MBEDTLS_MD2_C)
2616 case PSA_ALG_MD2:
2617 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2618 break;
2619#endif
2620#if defined(MBEDTLS_MD4_C)
2621 case PSA_ALG_MD4:
2622 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2623 break;
2624#endif
2625#if defined(MBEDTLS_MD5_C)
2626 case PSA_ALG_MD5:
2627 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2628 break;
2629#endif
2630#if defined(MBEDTLS_RIPEMD160_C)
2631 case PSA_ALG_RIPEMD160:
2632 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2633 break;
2634#endif
2635#if defined(MBEDTLS_SHA1_C)
2636 case PSA_ALG_SHA_1:
2637 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2638 break;
2639#endif
2640#if defined(MBEDTLS_SHA256_C)
2641 case PSA_ALG_SHA_224:
2642 case PSA_ALG_SHA_256:
2643 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2644 break;
2645#endif
2646#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002647#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002648 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002649#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002650 case PSA_ALG_SHA_512:
2651 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2652 break;
2653#endif
2654 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002655 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002656 }
itayzafrir40835d42018-08-02 13:14:17 +03002657 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002658
itayzafrir40835d42018-08-02 13:14:17 +03002659exit:
2660 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002661 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002662 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663 return( psa_hash_abort( operation ) );
2664 }
2665 else
2666 {
2667 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002668 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 }
2670}
2671
Gilles Peskine2d277862018-06-18 15:41:12 +02002672psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2673 const uint8_t *hash,
2674 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675{
2676 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2677 size_t actual_hash_length;
2678 psa_status_t status = psa_hash_finish( operation,
2679 actual_hash, sizeof( actual_hash ),
2680 &actual_hash_length );
2681 if( status != PSA_SUCCESS )
2682 return( status );
2683 if( actual_hash_length != hash_length )
2684 return( PSA_ERROR_INVALID_SIGNATURE );
2685 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2686 return( PSA_ERROR_INVALID_SIGNATURE );
2687 return( PSA_SUCCESS );
2688}
2689
Gilles Peskine0a749c82019-11-28 19:33:58 +01002690psa_status_t psa_hash_compute( psa_algorithm_t alg,
2691 const uint8_t *input, size_t input_length,
2692 uint8_t *hash, size_t hash_size,
2693 size_t *hash_length )
2694{
2695 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2697
2698 *hash_length = hash_size;
2699 status = psa_hash_setup( &operation, alg );
2700 if( status != PSA_SUCCESS )
2701 goto exit;
2702 status = psa_hash_update( &operation, input, input_length );
2703 if( status != PSA_SUCCESS )
2704 goto exit;
2705 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2706 if( status != PSA_SUCCESS )
2707 goto exit;
2708
2709exit:
2710 if( status == PSA_SUCCESS )
2711 status = psa_hash_abort( &operation );
2712 else
2713 psa_hash_abort( &operation );
2714 return( status );
2715}
2716
2717psa_status_t psa_hash_compare( psa_algorithm_t alg,
2718 const uint8_t *input, size_t input_length,
2719 const uint8_t *hash, size_t hash_length )
2720{
2721 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2722 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2723
2724 status = psa_hash_setup( &operation, alg );
2725 if( status != PSA_SUCCESS )
2726 goto exit;
2727 status = psa_hash_update( &operation, input, input_length );
2728 if( status != PSA_SUCCESS )
2729 goto exit;
2730 status = psa_hash_verify( &operation, hash, hash_length );
2731 if( status != PSA_SUCCESS )
2732 goto exit;
2733
2734exit:
2735 if( status == PSA_SUCCESS )
2736 status = psa_hash_abort( &operation );
2737 else
2738 psa_hash_abort( &operation );
2739 return( status );
2740}
2741
Gilles Peskineeb35d782019-01-22 17:56:16 +01002742psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2743 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002744{
2745 if( target_operation->alg != 0 )
2746 return( PSA_ERROR_BAD_STATE );
2747
2748 switch( source_operation->alg )
2749 {
2750 case 0:
2751 return( PSA_ERROR_BAD_STATE );
2752#if defined(MBEDTLS_MD2_C)
2753 case PSA_ALG_MD2:
2754 mbedtls_md2_clone( &target_operation->ctx.md2,
2755 &source_operation->ctx.md2 );
2756 break;
2757#endif
2758#if defined(MBEDTLS_MD4_C)
2759 case PSA_ALG_MD4:
2760 mbedtls_md4_clone( &target_operation->ctx.md4,
2761 &source_operation->ctx.md4 );
2762 break;
2763#endif
2764#if defined(MBEDTLS_MD5_C)
2765 case PSA_ALG_MD5:
2766 mbedtls_md5_clone( &target_operation->ctx.md5,
2767 &source_operation->ctx.md5 );
2768 break;
2769#endif
2770#if defined(MBEDTLS_RIPEMD160_C)
2771 case PSA_ALG_RIPEMD160:
2772 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2773 &source_operation->ctx.ripemd160 );
2774 break;
2775#endif
2776#if defined(MBEDTLS_SHA1_C)
2777 case PSA_ALG_SHA_1:
2778 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2779 &source_operation->ctx.sha1 );
2780 break;
2781#endif
2782#if defined(MBEDTLS_SHA256_C)
2783 case PSA_ALG_SHA_224:
2784 case PSA_ALG_SHA_256:
2785 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2786 &source_operation->ctx.sha256 );
2787 break;
2788#endif
2789#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002790#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002791 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002792#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002793 case PSA_ALG_SHA_512:
2794 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2795 &source_operation->ctx.sha512 );
2796 break;
2797#endif
2798 default:
2799 return( PSA_ERROR_NOT_SUPPORTED );
2800 }
2801
2802 target_operation->alg = source_operation->alg;
2803 return( PSA_SUCCESS );
2804}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002805
2806
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002808/* MAC */
2809/****************************************************************/
2810
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002811static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812 psa_algorithm_t alg,
2813 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002814 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002815 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002818 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002820 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002821 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002822
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2824 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002825 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002827 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002828 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 mode = MBEDTLS_MODE_STREAM;
2830 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831 case PSA_ALG_CTR:
2832 mode = MBEDTLS_MODE_CTR;
2833 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002834 case PSA_ALG_CFB:
2835 mode = MBEDTLS_MODE_CFB;
2836 break;
2837 case PSA_ALG_OFB:
2838 mode = MBEDTLS_MODE_OFB;
2839 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002840 case PSA_ALG_ECB_NO_PADDING:
2841 mode = MBEDTLS_MODE_ECB;
2842 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002843 case PSA_ALG_CBC_NO_PADDING:
2844 mode = MBEDTLS_MODE_CBC;
2845 break;
2846 case PSA_ALG_CBC_PKCS7:
2847 mode = MBEDTLS_MODE_CBC;
2848 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002849 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 mode = MBEDTLS_MODE_CCM;
2851 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002852 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 mode = MBEDTLS_MODE_GCM;
2854 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002855 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2856 mode = MBEDTLS_MODE_CHACHAPOLY;
2857 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858 default:
2859 return( NULL );
2860 }
2861 }
2862 else if( alg == PSA_ALG_CMAC )
2863 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864 else
2865 return( NULL );
2866
2867 switch( key_type )
2868 {
2869 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002870 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871 break;
2872 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002873 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2874 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002876 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002878 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002879 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2880 * but two-key Triple-DES is functionally three-key Triple-DES
2881 * with K1=K3, so that's how we present it to mbedtls. */
2882 if( key_bits == 128 )
2883 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002884 break;
2885 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002886 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002887 break;
2888 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002889 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002891 case PSA_KEY_TYPE_CHACHA20:
2892 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2893 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 default:
2895 return( NULL );
2896 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002897 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002898 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899
Jaeden Amero23bbb752018-06-26 14:16:54 +01002900 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2901 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902}
2903
John Durkop6ba40d12020-11-10 08:50:04 -08002904#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002905static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002906{
Gilles Peskine2d277862018-06-18 15:41:12 +02002907 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002908 {
2909 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002910 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002911 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002912 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002913 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002914 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002915 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002916 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002917 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002918 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002919 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002920 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002921 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002922 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002923 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002924 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002925 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002926 return( 128 );
2927 default:
2928 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002929 }
2930}
John Durkop07cc04a2020-11-16 22:08:34 -08002931#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002932
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002933/* Initialize the MAC operation structure. Once this function has been
2934 * called, psa_mac_abort can run and will do the right thing. */
2935static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2936 psa_algorithm_t alg )
2937{
2938 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2939
2940 operation->alg = alg;
2941 operation->key_set = 0;
2942 operation->iv_set = 0;
2943 operation->iv_required = 0;
2944 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002945 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002946
2947#if defined(MBEDTLS_CMAC_C)
2948 if( alg == PSA_ALG_CMAC )
2949 {
2950 operation->iv_required = 0;
2951 mbedtls_cipher_init( &operation->ctx.cmac );
2952 status = PSA_SUCCESS;
2953 }
2954 else
2955#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002956#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002957 if( PSA_ALG_IS_HMAC( operation->alg ) )
2958 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002959 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2960 operation->ctx.hmac.hash_ctx.alg = 0;
2961 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002962 }
2963 else
John Durkopd0321952020-10-29 21:37:36 -07002964#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002965 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002966 if( ! PSA_ALG_IS_MAC( alg ) )
2967 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968 }
2969
2970 if( status != PSA_SUCCESS )
2971 memset( operation, 0, sizeof( *operation ) );
2972 return( status );
2973}
2974
John Durkop6ba40d12020-11-10 08:50:04 -08002975#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002976static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2977{
Gilles Peskine3f108122018-12-07 18:14:53 +01002978 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002979 return( psa_hash_abort( &hmac->hash_ctx ) );
2980}
John Durkop6ba40d12020-11-10 08:50:04 -08002981#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002982
Gilles Peskine8c9def32018-02-08 10:02:12 +01002983psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2984{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002985 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002986 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002987 /* The object has (apparently) been initialized but it is not
2988 * in use. It's ok to call abort on such an object, and there's
2989 * nothing to do. */
2990 return( PSA_SUCCESS );
2991 }
2992 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002994 if( operation->alg == PSA_ALG_CMAC )
2995 {
2996 mbedtls_cipher_free( &operation->ctx.cmac );
2997 }
2998 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003000#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003001 if( PSA_ALG_IS_HMAC( operation->alg ) )
3002 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003003 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003004 }
3005 else
John Durkopd0321952020-10-29 21:37:36 -07003006#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003007 {
3008 /* Sanity check (shouldn't happen: operation->alg should
3009 * always have been initialized to a valid value). */
3010 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011 }
Moran Peker41deec42018-04-04 15:43:05 +03003012
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013 operation->alg = 0;
3014 operation->key_set = 0;
3015 operation->iv_set = 0;
3016 operation->iv_required = 0;
3017 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003018 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003019
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003021
3022bad_state:
3023 /* If abort is called on an uninitialized object, we can't trust
3024 * anything. Wipe the object in case it contains confidential data.
3025 * This may result in a memory leak if a pointer gets overwritten,
3026 * but it's too late to do anything about this. */
3027 memset( operation, 0, sizeof( *operation ) );
3028 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029}
3030
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003031#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003032static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003033 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003034 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035 const mbedtls_cipher_info_t *cipher_info )
3036{
Janos Follath24eed8d2019-11-22 13:21:35 +00003037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003038
3039 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003040
3041 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3042 if( ret != 0 )
3043 return( ret );
3044
3045 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003046 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003047 key_bits );
3048 return( ret );
3049}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003050#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003051
John Durkop6ba40d12020-11-10 08:50:04 -08003052#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003053static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3054 const uint8_t *key,
3055 size_t key_length,
3056 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003057{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003058 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003059 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003060 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003061 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003062 psa_status_t status;
3063
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003064 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3065 * overflow below. This should never trigger if the hash algorithm
3066 * is implemented correctly. */
3067 /* The size checks against the ipad and opad buffers cannot be written
3068 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3069 * because that triggers -Wlogical-op on GCC 7.3. */
3070 if( block_size > sizeof( ipad ) )
3071 return( PSA_ERROR_NOT_SUPPORTED );
3072 if( block_size > sizeof( hmac->opad ) )
3073 return( PSA_ERROR_NOT_SUPPORTED );
3074 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003075 return( PSA_ERROR_NOT_SUPPORTED );
3076
Gilles Peskined223b522018-06-11 18:12:58 +02003077 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003078 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003079 status = psa_hash_compute( hash_alg, key, key_length,
3080 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003081 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003082 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003083 }
Gilles Peskine96889972018-07-12 17:07:03 +02003084 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3085 * but it is permitted. It is common when HMAC is used in HKDF, for
3086 * example. Don't call `memcpy` in the 0-length because `key` could be
3087 * an invalid pointer which would make the behavior undefined. */
3088 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003089 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003090
Gilles Peskined223b522018-06-11 18:12:58 +02003091 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3092 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003093 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003094 ipad[i] ^= 0x36;
3095 memset( ipad + key_length, 0x36, block_size - key_length );
3096
3097 /* Copy the key material from ipad to opad, flipping the requisite bits,
3098 * and filling the rest of opad with the requisite constant. */
3099 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003100 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3101 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003102
Gilles Peskine01126fa2018-07-12 17:04:55 +02003103 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003104 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003105 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003106
Gilles Peskine01126fa2018-07-12 17:04:55 +02003107 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003108
3109cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003110 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003111
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003112 return( status );
3113}
John Durkop6ba40d12020-11-10 08:50:04 -08003114#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003115
Gilles Peskine89167cb2018-07-08 20:12:23 +02003116static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003117 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003118 psa_algorithm_t alg,
3119 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003120{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003122 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003124 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003125 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003126 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003127 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003128
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003129 /* A context must be freshly initialized before it can be set up. */
3130 if( operation->alg != 0 )
3131 {
3132 return( PSA_ERROR_BAD_STATE );
3133 }
3134
Gilles Peskined911eb72018-08-14 15:18:45 +02003135 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003136 if( status != PSA_SUCCESS )
3137 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003138 if( is_sign )
3139 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140
Gilles Peskine28f8f302019-07-24 13:30:31 +02003141 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003142 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003144 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003145
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003147 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003148 {
3149 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003150 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003151 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003153 if( cipher_info == NULL )
3154 {
3155 status = PSA_ERROR_NOT_SUPPORTED;
3156 goto exit;
3157 }
3158 operation->mac_size = cipher_info->block_size;
3159 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3160 status = mbedtls_to_psa_error( ret );
3161 }
3162 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003164#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003165 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003166 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003167 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003168 if( hash_alg == 0 )
3169 {
3170 status = PSA_ERROR_NOT_SUPPORTED;
3171 goto exit;
3172 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003173
3174 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3175 /* Sanity check. This shouldn't fail on a valid configuration. */
3176 if( operation->mac_size == 0 ||
3177 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3178 {
3179 status = PSA_ERROR_NOT_SUPPORTED;
3180 goto exit;
3181 }
3182
Gilles Peskine8e338702019-07-30 20:06:31 +02003183 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003184 {
3185 status = PSA_ERROR_INVALID_ARGUMENT;
3186 goto exit;
3187 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003188
Gilles Peskine01126fa2018-07-12 17:04:55 +02003189 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003190 slot->data.key.data,
3191 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003192 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003193 }
3194 else
John Durkopd0321952020-10-29 21:37:36 -07003195#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003196 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003197 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003198 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003200
Gilles Peskined911eb72018-08-14 15:18:45 +02003201 if( truncated == 0 )
3202 {
3203 /* The "normal" case: untruncated algorithm. Nothing to do. */
3204 }
3205 else if( truncated < 4 )
3206 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003207 /* A very short MAC is too short for security since it can be
3208 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3209 * so we make this our minimum, even though 32 bits is still
3210 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003211 status = PSA_ERROR_NOT_SUPPORTED;
3212 }
3213 else if( truncated > operation->mac_size )
3214 {
3215 /* It's impossible to "truncate" to a larger length. */
3216 status = PSA_ERROR_INVALID_ARGUMENT;
3217 }
3218 else
3219 operation->mac_size = truncated;
3220
Gilles Peskinefbfac682018-07-08 20:51:54 +02003221exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003222 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003223 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003224 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003226 else
3227 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003228 operation->key_set = 1;
3229 }
3230 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003231}
3232
Gilles Peskine89167cb2018-07-08 20:12:23 +02003233psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003234 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003235 psa_algorithm_t alg )
3236{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003237 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003238}
3239
3240psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003241 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003242 psa_algorithm_t alg )
3243{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003244 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003245}
3246
Gilles Peskine8c9def32018-02-08 10:02:12 +01003247psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3248 const uint8_t *input,
3249 size_t input_length )
3250{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003251 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003252 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003253 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003254 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003255 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003256 operation->has_input = 1;
3257
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003259 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003260 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003261 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3262 input, input_length );
3263 status = mbedtls_to_psa_error( ret );
3264 }
3265 else
3266#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003267#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003268 if( PSA_ALG_IS_HMAC( operation->alg ) )
3269 {
3270 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3271 input_length );
3272 }
3273 else
John Durkopd0321952020-10-29 21:37:36 -07003274#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003275 {
3276 /* This shouldn't happen if `operation` was initialized by
3277 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003278 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003279 }
3280
Gilles Peskinefbfac682018-07-08 20:51:54 +02003281 if( status != PSA_SUCCESS )
3282 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003283 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003284}
3285
John Durkop6ba40d12020-11-10 08:50:04 -08003286#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003287static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3288 uint8_t *mac,
3289 size_t mac_size )
3290{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003291 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003292 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3293 size_t hash_size = 0;
3294 size_t block_size = psa_get_hash_block_size( hash_alg );
3295 psa_status_t status;
3296
Gilles Peskine01126fa2018-07-12 17:04:55 +02003297 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3298 if( status != PSA_SUCCESS )
3299 return( status );
3300 /* From here on, tmp needs to be wiped. */
3301
3302 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3303 if( status != PSA_SUCCESS )
3304 goto exit;
3305
3306 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3307 if( status != PSA_SUCCESS )
3308 goto exit;
3309
3310 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3311 if( status != PSA_SUCCESS )
3312 goto exit;
3313
Gilles Peskined911eb72018-08-14 15:18:45 +02003314 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3315 if( status != PSA_SUCCESS )
3316 goto exit;
3317
3318 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003319
3320exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003321 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003322 return( status );
3323}
John Durkop6ba40d12020-11-10 08:50:04 -08003324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003325
mohammad16036df908f2018-04-02 08:34:15 -07003326static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003327 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003328 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003329{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003330 if( ! operation->key_set )
3331 return( PSA_ERROR_BAD_STATE );
3332 if( operation->iv_required && ! operation->iv_set )
3333 return( PSA_ERROR_BAD_STATE );
3334
Gilles Peskine8c9def32018-02-08 10:02:12 +01003335 if( mac_size < operation->mac_size )
3336 return( PSA_ERROR_BUFFER_TOO_SMALL );
3337
Gilles Peskine8c9def32018-02-08 10:02:12 +01003338#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003339 if( operation->alg == PSA_ALG_CMAC )
3340 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003341 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3342 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3343 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003344 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003345 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003346 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003347 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003348 else
3349#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003350#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003351 if( PSA_ALG_IS_HMAC( operation->alg ) )
3352 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003353 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003354 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003355 }
3356 else
John Durkopd0321952020-10-29 21:37:36 -07003357#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003358 {
3359 /* This shouldn't happen if `operation` was initialized by
3360 * a setup function. */
3361 return( PSA_ERROR_BAD_STATE );
3362 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003363}
3364
Gilles Peskineacd4be32018-07-08 19:56:25 +02003365psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3366 uint8_t *mac,
3367 size_t mac_size,
3368 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003369{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003370 psa_status_t status;
3371
Jaeden Amero252ef282019-02-15 14:05:35 +00003372 if( operation->alg == 0 )
3373 {
3374 return( PSA_ERROR_BAD_STATE );
3375 }
3376
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003377 /* Fill the output buffer with something that isn't a valid mac
3378 * (barring an attack on the mac and deliberately-crafted input),
3379 * in case the caller doesn't check the return status properly. */
3380 *mac_length = mac_size;
3381 /* If mac_size is 0 then mac may be NULL and then the
3382 * call to memset would have undefined behavior. */
3383 if( mac_size != 0 )
3384 memset( mac, '!', mac_size );
3385
Gilles Peskine89167cb2018-07-08 20:12:23 +02003386 if( ! operation->is_sign )
3387 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003388 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003389 }
mohammad16036df908f2018-04-02 08:34:15 -07003390
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003391 status = psa_mac_finish_internal( operation, mac, mac_size );
3392
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003393 if( status == PSA_SUCCESS )
3394 {
3395 status = psa_mac_abort( operation );
3396 if( status == PSA_SUCCESS )
3397 *mac_length = operation->mac_size;
3398 else
3399 memset( mac, '!', mac_size );
3400 }
3401 else
3402 psa_mac_abort( operation );
3403 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003404}
3405
Gilles Peskineacd4be32018-07-08 19:56:25 +02003406psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3407 const uint8_t *mac,
3408 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003409{
Gilles Peskine828ed142018-06-18 23:25:51 +02003410 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003411 psa_status_t status;
3412
Jaeden Amero252ef282019-02-15 14:05:35 +00003413 if( operation->alg == 0 )
3414 {
3415 return( PSA_ERROR_BAD_STATE );
3416 }
3417
Gilles Peskine89167cb2018-07-08 20:12:23 +02003418 if( operation->is_sign )
3419 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003420 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003421 }
3422 if( operation->mac_size != mac_length )
3423 {
3424 status = PSA_ERROR_INVALID_SIGNATURE;
3425 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003426 }
mohammad16036df908f2018-04-02 08:34:15 -07003427
3428 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003429 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003430 if( status != PSA_SUCCESS )
3431 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003432
3433 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3434 status = PSA_ERROR_INVALID_SIGNATURE;
3435
3436cleanup:
3437 if( status == PSA_SUCCESS )
3438 status = psa_mac_abort( operation );
3439 else
3440 psa_mac_abort( operation );
3441
Gilles Peskine3f108122018-12-07 18:14:53 +01003442 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003443
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003444 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003445}
3446
3447
Gilles Peskine20035e32018-02-03 22:44:14 +01003448
Gilles Peskine20035e32018-02-03 22:44:14 +01003449/****************************************************************/
3450/* Asymmetric cryptography */
3451/****************************************************************/
3452
John Durkop6ba40d12020-11-10 08:50:04 -08003453#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3454 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003455/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003456 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003457static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3458 size_t hash_length,
3459 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003460{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003461 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003462 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003463 *md_alg = mbedtls_md_get_type( md_info );
3464
3465 /* The Mbed TLS RSA module uses an unsigned int for hash length
3466 * parameters. Validate that it fits so that we don't risk an
3467 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003468#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003469 if( hash_length > UINT_MAX )
3470 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003471#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003472
John Durkop0e005192020-10-31 22:06:54 -07003473#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003474 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3475 * must be correct. */
3476 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3477 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003478 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003479 if( md_info == NULL )
3480 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003481 if( mbedtls_md_get_size( md_info ) != hash_length )
3482 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003483 }
John Durkop0e005192020-10-31 22:06:54 -07003484#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003485
John Durkop0e005192020-10-31 22:06:54 -07003486#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003487 /* PSS requires a hash internally. */
3488 if( PSA_ALG_IS_RSA_PSS( alg ) )
3489 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003490 if( md_info == NULL )
3491 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003492 }
John Durkop0e005192020-10-31 22:06:54 -07003493#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003494
Gilles Peskine61b91d42018-06-08 16:09:36 +02003495 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003496}
3497
Gilles Peskine2b450e32018-06-27 15:42:46 +02003498static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3499 psa_algorithm_t alg,
3500 const uint8_t *hash,
3501 size_t hash_length,
3502 uint8_t *signature,
3503 size_t signature_size,
3504 size_t *signature_length )
3505{
3506 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003508 mbedtls_md_type_t md_alg;
3509
3510 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3511 if( status != PSA_SUCCESS )
3512 return( status );
3513
Gilles Peskine630a18a2018-06-29 17:49:35 +02003514 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003515 return( PSA_ERROR_BUFFER_TOO_SMALL );
3516
John Durkop0e005192020-10-31 22:06:54 -07003517#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003518 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3519 {
3520 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3521 MBEDTLS_MD_NONE );
3522 ret = mbedtls_rsa_pkcs1_sign( rsa,
3523 mbedtls_ctr_drbg_random,
3524 &global_data.ctr_drbg,
3525 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003526 md_alg,
3527 (unsigned int) hash_length,
3528 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003529 signature );
3530 }
3531 else
John Durkop0e005192020-10-31 22:06:54 -07003532#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3533#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003534 if( PSA_ALG_IS_RSA_PSS( alg ) )
3535 {
3536 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3537 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3538 mbedtls_ctr_drbg_random,
3539 &global_data.ctr_drbg,
3540 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003541 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003542 (unsigned int) hash_length,
3543 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003544 signature );
3545 }
3546 else
John Durkop0e005192020-10-31 22:06:54 -07003547#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003548 {
3549 return( PSA_ERROR_INVALID_ARGUMENT );
3550 }
3551
3552 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003553 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003554 return( mbedtls_to_psa_error( ret ) );
3555}
3556
3557static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3558 psa_algorithm_t alg,
3559 const uint8_t *hash,
3560 size_t hash_length,
3561 const uint8_t *signature,
3562 size_t signature_length )
3563{
3564 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003566 mbedtls_md_type_t md_alg;
3567
3568 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3569 if( status != PSA_SUCCESS )
3570 return( status );
3571
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003572 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3573 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003574
John Durkop0e005192020-10-31 22:06:54 -07003575#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003576 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3577 {
3578 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3579 MBEDTLS_MD_NONE );
3580 ret = mbedtls_rsa_pkcs1_verify( rsa,
3581 mbedtls_ctr_drbg_random,
3582 &global_data.ctr_drbg,
3583 MBEDTLS_RSA_PUBLIC,
3584 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003585 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003586 hash,
3587 signature );
3588 }
3589 else
John Durkop0e005192020-10-31 22:06:54 -07003590#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3591#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003592 if( PSA_ALG_IS_RSA_PSS( alg ) )
3593 {
3594 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3595 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3596 mbedtls_ctr_drbg_random,
3597 &global_data.ctr_drbg,
3598 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003599 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003600 (unsigned int) hash_length,
3601 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003602 signature );
3603 }
3604 else
John Durkop0e005192020-10-31 22:06:54 -07003605#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003606 {
3607 return( PSA_ERROR_INVALID_ARGUMENT );
3608 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003609
3610 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3611 * the rest of the signature is invalid". This has little use in
3612 * practice and PSA doesn't report this distinction. */
3613 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3614 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003615 return( mbedtls_to_psa_error( ret ) );
3616}
John Durkop6ba40d12020-11-10 08:50:04 -08003617#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3618 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003619
John Durkop6ba40d12020-11-10 08:50:04 -08003620#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3621 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003622/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3623 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3624 * (even though these functions don't modify it). */
3625static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3626 psa_algorithm_t alg,
3627 const uint8_t *hash,
3628 size_t hash_length,
3629 uint8_t *signature,
3630 size_t signature_size,
3631 size_t *signature_length )
3632{
Janos Follath24eed8d2019-11-22 13:21:35 +00003633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003634 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003635 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003636 mbedtls_mpi_init( &r );
3637 mbedtls_mpi_init( &s );
3638
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003639 if( signature_size < 2 * curve_bytes )
3640 {
3641 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3642 goto cleanup;
3643 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003644
John Durkop0ea39e02020-10-13 19:58:20 -07003645#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003646 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3647 {
3648 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3649 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3650 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003651 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3652 &ecp->d, hash,
3653 hash_length, md_alg,
3654 mbedtls_ctr_drbg_random,
3655 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003656 }
3657 else
John Durkop0ea39e02020-10-13 19:58:20 -07003658#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003659 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003660 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003661 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3662 hash, hash_length,
3663 mbedtls_ctr_drbg_random,
3664 &global_data.ctr_drbg ) );
3665 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003666
3667 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3668 signature,
3669 curve_bytes ) );
3670 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3671 signature + curve_bytes,
3672 curve_bytes ) );
3673
3674cleanup:
3675 mbedtls_mpi_free( &r );
3676 mbedtls_mpi_free( &s );
3677 if( ret == 0 )
3678 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003679 return( mbedtls_to_psa_error( ret ) );
3680}
3681
3682static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3683 const uint8_t *hash,
3684 size_t hash_length,
3685 const uint8_t *signature,
3686 size_t signature_length )
3687{
Janos Follath24eed8d2019-11-22 13:21:35 +00003688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003689 mbedtls_mpi r, s;
3690 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3691 mbedtls_mpi_init( &r );
3692 mbedtls_mpi_init( &s );
3693
3694 if( signature_length != 2 * curve_bytes )
3695 return( PSA_ERROR_INVALID_SIGNATURE );
3696
3697 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3698 signature,
3699 curve_bytes ) );
3700 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3701 signature + curve_bytes,
3702 curve_bytes ) );
3703
Steven Cooremanacda8342020-07-24 23:09:52 +02003704 /* Check whether the public part is loaded. If not, load it. */
3705 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3706 {
3707 MBEDTLS_MPI_CHK(
3708 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3709 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3710 }
3711
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003712 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3713 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003714
3715cleanup:
3716 mbedtls_mpi_free( &r );
3717 mbedtls_mpi_free( &s );
3718 return( mbedtls_to_psa_error( ret ) );
3719}
John Durkop6ba40d12020-11-10 08:50:04 -08003720#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3721 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003722
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003723psa_status_t psa_sign_hash( psa_key_handle_t handle,
3724 psa_algorithm_t alg,
3725 const uint8_t *hash,
3726 size_t hash_length,
3727 uint8_t *signature,
3728 size_t signature_size,
3729 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003730{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003731 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003732 psa_status_t status;
3733
3734 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003735 /* Immediately reject a zero-length signature buffer. This guarantees
3736 * that signature must be a valid pointer. (On the other hand, the hash
3737 * buffer can in principle be empty since it doesn't actually have
3738 * to be a hash.) */
3739 if( signature_size == 0 )
3740 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003741
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003742 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003743 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003744 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003745 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003746 {
3747 status = PSA_ERROR_INVALID_ARGUMENT;
3748 goto exit;
3749 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003750
Steven Cooremancd84cb42020-07-16 20:28:36 +02003751 /* Try any of the available accelerators first */
3752 status = psa_driver_wrapper_sign_hash( slot,
3753 alg,
3754 hash,
3755 hash_length,
3756 signature,
3757 signature_size,
3758 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003759 if( status != PSA_ERROR_NOT_SUPPORTED ||
3760 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003761 goto exit;
3762
Steven Cooreman7a250572020-07-17 16:43:05 +02003763 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003764#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3765 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003766 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003767 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003768 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003769
Steven Cooreman4fed4552020-08-03 14:46:03 +02003770 status = psa_load_rsa_representation( slot->attr.type,
3771 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003772 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003773 &rsa );
3774 if( status != PSA_SUCCESS )
3775 goto exit;
3776
Steven Cooremana2371e52020-07-28 14:30:39 +02003777 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003778 alg,
3779 hash, hash_length,
3780 signature, signature_size,
3781 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003782
Steven Cooremana2371e52020-07-28 14:30:39 +02003783 mbedtls_rsa_free( rsa );
3784 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003785 }
3786 else
John Durkop6ba40d12020-11-10 08:50:04 -08003787#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3788 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003789 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003790 {
John Durkop9814fa22020-11-04 12:28:15 -08003791#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3792 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003793 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003794#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003795 PSA_ALG_IS_ECDSA( alg )
3796#else
3797 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3798#endif
3799 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003800 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003801 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003802 status = psa_load_ecp_representation( slot->attr.type,
3803 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003804 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003805 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003806 if( status != PSA_SUCCESS )
3807 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003808 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003809 alg,
3810 hash, hash_length,
3811 signature, signature_size,
3812 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003813 mbedtls_ecp_keypair_free( ecp );
3814 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003815 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003816 else
John Durkop9814fa22020-11-04 12:28:15 -08003817#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3818 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003819 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003820 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003821 }
itayzafrir5c753392018-05-08 11:18:38 +03003822 }
3823 else
itayzafrir5c753392018-05-08 11:18:38 +03003824 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003825 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003826 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003827
3828exit:
3829 /* Fill the unused part of the output buffer (the whole buffer on error,
3830 * the trailing part on success) with something that isn't a valid mac
3831 * (barring an attack on the mac and deliberately-crafted input),
3832 * in case the caller doesn't check the return status properly. */
3833 if( status == PSA_SUCCESS )
3834 memset( signature + *signature_length, '!',
3835 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003836 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003837 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003838 /* If signature_size is 0 then we have nothing to do. We must not call
3839 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003840 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003841}
3842
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003843psa_status_t psa_verify_hash( psa_key_handle_t handle,
3844 psa_algorithm_t alg,
3845 const uint8_t *hash,
3846 size_t hash_length,
3847 const uint8_t *signature,
3848 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003849{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003850 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003851 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003852
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003853 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003854 if( status != PSA_SUCCESS )
3855 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003856
Steven Cooreman55ae2172020-07-17 19:46:15 +02003857 /* Try any of the available accelerators first */
3858 status = psa_driver_wrapper_verify_hash( slot,
3859 alg,
3860 hash,
3861 hash_length,
3862 signature,
3863 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003864 if( status != PSA_ERROR_NOT_SUPPORTED ||
3865 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003866 return status;
3867
John Durkop6ba40d12020-11-10 08:50:04 -08003868#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3869 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003870 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003872 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003873
Steven Cooreman4fed4552020-08-03 14:46:03 +02003874 status = psa_load_rsa_representation( slot->attr.type,
3875 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003876 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003877 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003878 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003879 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003880
Steven Cooremana2371e52020-07-28 14:30:39 +02003881 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003882 alg,
3883 hash, hash_length,
3884 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003885 mbedtls_rsa_free( rsa );
3886 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003887 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888 }
3889 else
John Durkop6ba40d12020-11-10 08:50:04 -08003890#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3891 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003892 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003893 {
John Durkop9814fa22020-11-04 12:28:15 -08003894#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3895 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003896 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003897 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003898 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003899 status = psa_load_ecp_representation( slot->attr.type,
3900 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003901 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003902 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003903 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003904 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003905 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003906 hash, hash_length,
3907 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003908 mbedtls_ecp_keypair_free( ecp );
3909 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003910 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003911 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003912 else
John Durkop9814fa22020-11-04 12:28:15 -08003913#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3914 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003915 {
3916 return( PSA_ERROR_INVALID_ARGUMENT );
3917 }
itayzafrir5c753392018-05-08 11:18:38 +03003918 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003919 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003920 {
3921 return( PSA_ERROR_NOT_SUPPORTED );
3922 }
3923}
3924
John Durkop0e005192020-10-31 22:06:54 -07003925#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003926static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3927 mbedtls_rsa_context *rsa )
3928{
3929 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3930 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3931 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3932 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3933}
John Durkop0e005192020-10-31 22:06:54 -07003934#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003935
Gilles Peskinec5487a82018-12-03 18:08:14 +01003936psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003937 psa_algorithm_t alg,
3938 const uint8_t *input,
3939 size_t input_length,
3940 const uint8_t *salt,
3941 size_t salt_length,
3942 uint8_t *output,
3943 size_t output_size,
3944 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003946 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003947 psa_status_t status;
3948
Darryl Green5cc689a2018-07-24 15:34:10 +01003949 (void) input;
3950 (void) input_length;
3951 (void) salt;
3952 (void) output;
3953 (void) output_size;
3954
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003955 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003957 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3958 return( PSA_ERROR_INVALID_ARGUMENT );
3959
Gilles Peskine28f8f302019-07-24 13:30:31 +02003960 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003961 if( status != PSA_SUCCESS )
3962 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003963 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3964 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966
John Durkop6ba40d12020-11-10 08:50:04 -08003967#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3968 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003969 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003971 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003972 status = psa_load_rsa_representation( slot->attr.type,
3973 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003974 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003975 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003976 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003977 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003978
3979 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003980 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003981 status = PSA_ERROR_BUFFER_TOO_SMALL;
3982 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003983 }
John Durkop0e005192020-10-31 22:06:54 -07003984#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003985 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003986 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003987 status = mbedtls_to_psa_error(
3988 mbedtls_rsa_pkcs1_encrypt( rsa,
3989 mbedtls_ctr_drbg_random,
3990 &global_data.ctr_drbg,
3991 MBEDTLS_RSA_PUBLIC,
3992 input_length,
3993 input,
3994 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 }
3996 else
John Durkop0e005192020-10-31 22:06:54 -07003997#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3998#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003999 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004001 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004002 status = mbedtls_to_psa_error(
4003 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4004 mbedtls_ctr_drbg_random,
4005 &global_data.ctr_drbg,
4006 MBEDTLS_RSA_PUBLIC,
4007 salt, salt_length,
4008 input_length,
4009 input,
4010 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011 }
4012 else
John Durkop0e005192020-10-31 22:06:54 -07004013#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004015 status = PSA_ERROR_INVALID_ARGUMENT;
4016 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004018rsa_exit:
4019 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004020 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004021
Steven Cooremana2371e52020-07-28 14:30:39 +02004022 mbedtls_rsa_free( rsa );
4023 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004024 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 else
John Durkop9814fa22020-11-04 12:28:15 -08004027#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004028 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 {
4030 return( PSA_ERROR_NOT_SUPPORTED );
4031 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004032}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033
Gilles Peskinec5487a82018-12-03 18:08:14 +01004034psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004035 psa_algorithm_t alg,
4036 const uint8_t *input,
4037 size_t input_length,
4038 const uint8_t *salt,
4039 size_t salt_length,
4040 uint8_t *output,
4041 size_t output_size,
4042 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004044 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004045 psa_status_t status;
4046
Darryl Green5cc689a2018-07-24 15:34:10 +01004047 (void) input;
4048 (void) input_length;
4049 (void) salt;
4050 (void) output;
4051 (void) output_size;
4052
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004053 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004055 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4056 return( PSA_ERROR_INVALID_ARGUMENT );
4057
Gilles Peskine28f8f302019-07-24 13:30:31 +02004058 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004059 if( status != PSA_SUCCESS )
4060 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004061 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004062 return( PSA_ERROR_INVALID_ARGUMENT );
4063
John Durkop6ba40d12020-11-10 08:50:04 -08004064#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4065 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004066 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004067 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004068 mbedtls_rsa_context *rsa = NULL;
4069 status = psa_load_rsa_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 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004073 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004074 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004075
Steven Cooremana2371e52020-07-28 14:30:39 +02004076 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004077 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004078 status = PSA_ERROR_INVALID_ARGUMENT;
4079 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004080 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081
John Durkop0e005192020-10-31 22:06:54 -07004082#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004083 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004084 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004085 status = mbedtls_to_psa_error(
4086 mbedtls_rsa_pkcs1_decrypt( rsa,
4087 mbedtls_ctr_drbg_random,
4088 &global_data.ctr_drbg,
4089 MBEDTLS_RSA_PRIVATE,
4090 output_length,
4091 input,
4092 output,
4093 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094 }
4095 else
John Durkop0e005192020-10-31 22:06:54 -07004096#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4097#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004098 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004100 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004101 status = mbedtls_to_psa_error(
4102 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4103 mbedtls_ctr_drbg_random,
4104 &global_data.ctr_drbg,
4105 MBEDTLS_RSA_PRIVATE,
4106 salt, salt_length,
4107 output_length,
4108 input,
4109 output,
4110 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004111 }
4112 else
John Durkop0e005192020-10-31 22:06:54 -07004113#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004115 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004117
Steven Cooreman4fed4552020-08-03 14:46:03 +02004118rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004119 mbedtls_rsa_free( rsa );
4120 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004121 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004122 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123 else
John Durkop6ba40d12020-11-10 08:50:04 -08004124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004126 {
4127 return( PSA_ERROR_NOT_SUPPORTED );
4128 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004129}
Gilles Peskine20035e32018-02-03 22:44:14 +01004130
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004131
4132
mohammad1603503973b2018-03-12 15:59:30 +02004133/****************************************************************/
4134/* Symmetric cryptography */
4135/****************************************************************/
4136
Gilles Peskinee553c652018-06-04 16:22:46 +02004137static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004138 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004139 psa_algorithm_t alg,
4140 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004141{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004142 int ret = 0;
4143 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004144 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004145 size_t key_bits;
4146 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004147 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4148 PSA_KEY_USAGE_ENCRYPT :
4149 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004150
Steven Cooremand3feccd2020-09-01 15:56:14 +02004151 /* A context must be freshly initialized before it can be set up. */
4152 if( operation->alg != 0 )
4153 return( PSA_ERROR_BAD_STATE );
4154
Steven Cooremana07b9972020-09-10 14:54:14 +02004155 /* The requested algorithm must be one that can be processed by cipher. */
4156 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004157 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004158
Steven Cooremanef8575e2020-09-11 11:44:50 +02004159 /* Fetch key material from key storage. */
4160 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4161 if( status != PSA_SUCCESS )
4162 goto exit;
4163
4164 /* Initialize the operation struct members, except for alg. The alg member
4165 * is used to indicate to psa_cipher_abort that there are resources to free,
4166 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004167 operation->key_set = 0;
4168 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004169 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004170 operation->iv_size = 0;
4171 operation->block_size = 0;
4172 if( alg == PSA_ALG_ECB_NO_PADDING )
4173 operation->iv_required = 0;
4174 else
4175 operation->iv_required = 1;
4176
Steven Cooremana07b9972020-09-10 14:54:14 +02004177 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004178 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004179 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004180 slot,
4181 alg );
4182 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004183 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004184 slot,
4185 alg );
4186
Steven Cooremanef8575e2020-09-11 11:44:50 +02004187 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004188 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004189 /* Once the driver context is initialised, it needs to be freed using
4190 * psa_cipher_abort. Indicate this through setting alg. */
4191 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004192 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004193
Steven Cooremand3feccd2020-09-01 15:56:14 +02004194 if( status != PSA_ERROR_NOT_SUPPORTED ||
4195 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4196 goto exit;
4197
Steven Cooremana07b9972020-09-10 14:54:14 +02004198 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004199 * available for the given algorithm & key. */
4200 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004201
Steven Cooremana07b9972020-09-10 14:54:14 +02004202 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004203 * psa_cipher_abort. Indicate there is something to be freed through setting
4204 * alg, and indicate the operation is being done using mbedtls crypto through
4205 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004206 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004207 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004208
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004209 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004210 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004211 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004212 {
4213 status = PSA_ERROR_NOT_SUPPORTED;
4214 goto exit;
4215 }
mohammad1603503973b2018-03-12 15:59:30 +02004216
mohammad1603503973b2018-03-12 15:59:30 +02004217 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004218 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004219 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004220
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004221#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004222 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004223 {
4224 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004225 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004226 memcpy( keys, slot->data.key.data, 16 );
4227 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004228 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4229 keys,
4230 192, cipher_operation );
4231 }
4232 else
4233#endif
4234 {
4235 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004236 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004237 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004238 }
Moran Peker41deec42018-04-04 15:43:05 +03004239 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004240 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004241
mohammad16038481e742018-03-18 13:57:31 +02004242#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004243 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004244 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004245 case PSA_ALG_CBC_NO_PADDING:
4246 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4247 MBEDTLS_PADDING_NONE );
4248 break;
4249 case PSA_ALG_CBC_PKCS7:
4250 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4251 MBEDTLS_PADDING_PKCS7 );
4252 break;
4253 default:
4254 /* The algorithm doesn't involve padding. */
4255 ret = 0;
4256 break;
4257 }
4258 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004259 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004260#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4261
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004262 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004263 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004264 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4265 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004266 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004267 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004268 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004269#if defined(MBEDTLS_CHACHA20_C)
4270 else
4271 if( alg == PSA_ALG_CHACHA20 )
4272 operation->iv_size = 12;
4273#endif
mohammad1603503973b2018-03-12 15:59:30 +02004274
Steven Cooreman7df02922020-09-09 15:28:49 +02004275 status = PSA_SUCCESS;
4276
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004277exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004278 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004279 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004280 if( status == PSA_SUCCESS )
4281 {
4282 /* Update operation flags for both driver and software implementations */
4283 operation->key_set = 1;
4284 }
4285 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004286 psa_cipher_abort( operation );
4287 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004288}
4289
Gilles Peskinefe119512018-07-08 21:39:34 +02004290psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004291 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004292 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004293{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004294 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004295}
4296
Gilles Peskinefe119512018-07-08 21:39:34 +02004297psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004298 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004299 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004300{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004301 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004302}
4303
Gilles Peskinefe119512018-07-08 21:39:34 +02004304psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004305 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004306 size_t iv_size,
4307 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004308{
itayzafrir534bd7c2018-08-02 13:56:32 +03004309 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004311 if( operation->iv_set || ! operation->iv_required )
4312 {
4313 return( PSA_ERROR_BAD_STATE );
4314 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004315
Steven Cooremanef8575e2020-09-11 11:44:50 +02004316 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004317 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004318 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004319 iv,
4320 iv_size,
4321 iv_length );
4322 goto exit;
4323 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004324
Moran Peker41deec42018-04-04 15:43:05 +03004325 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004326 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004327 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004328 goto exit;
4329 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004330 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4331 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004332 if( ret != 0 )
4333 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004334 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004335 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004336 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004337
mohammad16038481e742018-03-18 13:57:31 +02004338 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004339 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004340
Moran Peker395db872018-05-31 14:07:14 +03004341exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004342 if( status == PSA_SUCCESS )
4343 operation->iv_set = 1;
4344 else
Moran Peker395db872018-05-31 14:07:14 +03004345 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004346 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004347}
4348
Gilles Peskinefe119512018-07-08 21:39:34 +02004349psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004350 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004351 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004352{
itayzafrir534bd7c2018-08-02 13:56:32 +03004353 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004355 if( operation->iv_set || ! operation->iv_required )
4356 {
4357 return( PSA_ERROR_BAD_STATE );
4358 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004359
Steven Cooremanef8575e2020-09-11 11:44:50 +02004360 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004361 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004362 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004363 iv,
4364 iv_length );
4365 goto exit;
4366 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004367
Moran Pekera28258c2018-05-29 16:25:04 +03004368 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004369 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004370 status = PSA_ERROR_INVALID_ARGUMENT;
4371 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004372 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004373 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4374 status = mbedtls_to_psa_error( ret );
4375exit:
4376 if( status == PSA_SUCCESS )
4377 operation->iv_set = 1;
4378 else
mohammad1603503973b2018-03-12 15:59:30 +02004379 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004380 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004381}
4382
Steven Cooreman177deba2020-09-07 17:14:14 +02004383/* Process input for which the algorithm is set to ECB mode. This requires
4384 * manual processing, since the PSA API is defined as being able to process
4385 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4386 * underlying mbedtls_cipher_update only takes full blocks. */
4387static psa_status_t psa_cipher_update_ecb_internal(
4388 mbedtls_cipher_context_t *ctx,
4389 const uint8_t *input,
4390 size_t input_length,
4391 uint8_t *output,
4392 size_t output_size,
4393 size_t *output_length )
4394{
4395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4396 size_t block_size = ctx->cipher_info->block_size;
4397 size_t internal_output_length = 0;
4398 *output_length = 0;
4399
4400 if( input_length == 0 )
4401 {
4402 status = PSA_SUCCESS;
4403 goto exit;
4404 }
4405
4406 if( ctx->unprocessed_len > 0 )
4407 {
4408 /* Fill up to block size, and run the block if there's a full one. */
4409 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4410
4411 if( input_length < bytes_to_copy )
4412 bytes_to_copy = input_length;
4413
4414 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4415 input, bytes_to_copy );
4416 input_length -= bytes_to_copy;
4417 input += bytes_to_copy;
4418 ctx->unprocessed_len += bytes_to_copy;
4419
4420 if( ctx->unprocessed_len == block_size )
4421 {
4422 status = mbedtls_to_psa_error(
4423 mbedtls_cipher_update( ctx,
4424 ctx->unprocessed_data,
4425 block_size,
4426 output, &internal_output_length ) );
4427
4428 if( status != PSA_SUCCESS )
4429 goto exit;
4430
4431 output += internal_output_length;
4432 output_size -= internal_output_length;
4433 *output_length += internal_output_length;
4434 ctx->unprocessed_len = 0;
4435 }
4436 }
4437
4438 while( input_length >= block_size )
4439 {
4440 /* Run all full blocks we have, one by one */
4441 status = mbedtls_to_psa_error(
4442 mbedtls_cipher_update( ctx, input,
4443 block_size,
4444 output, &internal_output_length ) );
4445
4446 if( status != PSA_SUCCESS )
4447 goto exit;
4448
4449 input_length -= block_size;
4450 input += block_size;
4451
4452 output += internal_output_length;
4453 output_size -= internal_output_length;
4454 *output_length += internal_output_length;
4455 }
4456
4457 if( input_length > 0 )
4458 {
4459 /* Save unprocessed bytes for later processing */
4460 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4461 input, input_length );
4462 ctx->unprocessed_len += input_length;
4463 }
4464
4465 status = PSA_SUCCESS;
4466
4467exit:
4468 return( status );
4469}
4470
Gilles Peskinee553c652018-06-04 16:22:46 +02004471psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4472 const uint8_t *input,
4473 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004474 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004475 size_t output_size,
4476 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004477{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004479 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004480 if( operation->alg == 0 )
4481 {
4482 return( PSA_ERROR_BAD_STATE );
4483 }
4484 if( operation->iv_required && ! operation->iv_set )
4485 {
4486 return( PSA_ERROR_BAD_STATE );
4487 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004488
Steven Cooremanef8575e2020-09-11 11:44:50 +02004489 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004490 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004491 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004492 input,
4493 input_length,
4494 output,
4495 output_size,
4496 output_length );
4497 goto exit;
4498 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004499
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004500 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004501 {
4502 /* Take the unprocessed partial block left over from previous
4503 * update calls, if any, plus the input to this call. Remove
4504 * the last partial block, if any. You get the data that will be
4505 * output in this call. */
4506 expected_output_size =
4507 ( operation->ctx.cipher.unprocessed_len + input_length )
4508 / operation->block_size * operation->block_size;
4509 }
4510 else
4511 {
4512 expected_output_size = input_length;
4513 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004514
Gilles Peskine89d789c2018-06-04 17:17:16 +02004515 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004516 {
4517 status = PSA_ERROR_BUFFER_TOO_SMALL;
4518 goto exit;
4519 }
mohammad160382759612018-03-12 18:16:40 +02004520
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004521 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4522 {
4523 /* mbedtls_cipher_update has an API inconsistency: it will only
4524 * process a single block at a time in ECB mode. Abstract away that
4525 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004526 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4527 input,
4528 input_length,
4529 output,
4530 output_size,
4531 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004532 }
4533 else
4534 {
4535 status = mbedtls_to_psa_error(
4536 mbedtls_cipher_update( &operation->ctx.cipher, input,
4537 input_length, output, output_length ) );
4538 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004539exit:
4540 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004541 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004542 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004543}
4544
Gilles Peskinee553c652018-06-04 16:22:46 +02004545psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4546 uint8_t *output,
4547 size_t output_size,
4548 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004549{
David Saadab4ecc272019-02-14 13:48:10 +02004550 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004551 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004552 if( operation->alg == 0 )
4553 {
4554 return( PSA_ERROR_BAD_STATE );
4555 }
4556 if( operation->iv_required && ! operation->iv_set )
4557 {
4558 return( PSA_ERROR_BAD_STATE );
4559 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004560
Steven Cooremanef8575e2020-09-11 11:44:50 +02004561 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004562 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004563 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004564 output,
4565 output_size,
4566 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004567 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004568 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004569
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004570 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004571 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004572 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004573 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004574 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004575 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004576 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004577 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004578 }
4579
Steven Cooremanef8575e2020-09-11 11:44:50 +02004580 status = mbedtls_to_psa_error(
4581 mbedtls_cipher_finish( &operation->ctx.cipher,
4582 temp_output_buffer,
4583 output_length ) );
4584 if( status != PSA_SUCCESS )
4585 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004586
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004587 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004588 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004589 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004590 memcpy( output, temp_output_buffer, *output_length );
4591 else
Janos Follath315b51c2018-07-09 16:04:51 +01004592 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004593
Steven Cooremanef8575e2020-09-11 11:44:50 +02004594exit:
4595 if( operation->mbedtls_in_use == 1 )
4596 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004597
Steven Cooremanef8575e2020-09-11 11:44:50 +02004598 if( status == PSA_SUCCESS )
4599 return( psa_cipher_abort( operation ) );
4600 else
4601 {
4602 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004603 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004604
Steven Cooremanef8575e2020-09-11 11:44:50 +02004605 return( status );
4606 }
mohammad1603503973b2018-03-12 15:59:30 +02004607}
4608
Gilles Peskinee553c652018-06-04 16:22:46 +02004609psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4610{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004611 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004612 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004613 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004614 * in use. It's ok to call abort on such an object, and there's
4615 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004616 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004617 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004618
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004619 /* Sanity check (shouldn't happen: operation->alg should
4620 * always have been initialized to a valid value). */
4621 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4622 return( PSA_ERROR_BAD_STATE );
4623
Steven Cooremanef8575e2020-09-11 11:44:50 +02004624 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004625 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004626 else
4627 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004628
Moran Peker41deec42018-04-04 15:43:05 +03004629 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004630 operation->key_set = 0;
4631 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004632 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004633 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004634 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004635 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004636
Moran Peker395db872018-05-31 14:07:14 +03004637 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004638}
4639
Gilles Peskinea0655c32018-04-30 17:06:50 +02004640
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004641
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004642
mohammad16035955c982018-04-26 00:53:03 +03004643/****************************************************************/
4644/* AEAD */
4645/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004646
Gilles Peskineedf9a652018-08-17 18:11:56 +02004647typedef struct
4648{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004649 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004650 const mbedtls_cipher_info_t *cipher_info;
4651 union
4652 {
4653#if defined(MBEDTLS_CCM_C)
4654 mbedtls_ccm_context ccm;
4655#endif /* MBEDTLS_CCM_C */
4656#if defined(MBEDTLS_GCM_C)
4657 mbedtls_gcm_context gcm;
4658#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004659#if defined(MBEDTLS_CHACHAPOLY_C)
4660 mbedtls_chachapoly_context chachapoly;
4661#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004662 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004663 psa_algorithm_t core_alg;
4664 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004665 uint8_t tag_length;
4666} aead_operation_t;
4667
Gilles Peskine30a9e412019-01-14 18:36:12 +01004668static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004669{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004670 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004671 {
4672#if defined(MBEDTLS_CCM_C)
4673 case PSA_ALG_CCM:
4674 mbedtls_ccm_free( &operation->ctx.ccm );
4675 break;
4676#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004677#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004678 case PSA_ALG_GCM:
4679 mbedtls_gcm_free( &operation->ctx.gcm );
4680 break;
4681#endif /* MBEDTLS_GCM_C */
4682 }
4683}
4684
4685static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004686 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004687 psa_key_usage_t usage,
4688 psa_algorithm_t alg )
4689{
4690 psa_status_t status;
4691 size_t key_bits;
4692 mbedtls_cipher_id_t cipher_id;
4693
Gilles Peskine28f8f302019-07-24 13:30:31 +02004694 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004695 if( status != PSA_SUCCESS )
4696 return( status );
4697
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004698 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004699
4700 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004701 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004702 &cipher_id );
4703 if( operation->cipher_info == NULL )
4704 return( PSA_ERROR_NOT_SUPPORTED );
4705
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004706 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004707 {
4708#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004709 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4710 operation->core_alg = PSA_ALG_CCM;
4711 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004712 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4713 * The call to mbedtls_ccm_encrypt_and_tag or
4714 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004715 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004716 return( PSA_ERROR_INVALID_ARGUMENT );
4717 mbedtls_ccm_init( &operation->ctx.ccm );
4718 status = mbedtls_to_psa_error(
4719 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004720 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004721 (unsigned int) key_bits ) );
4722 if( status != 0 )
4723 goto cleanup;
4724 break;
4725#endif /* MBEDTLS_CCM_C */
4726
4727#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004728 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4729 operation->core_alg = PSA_ALG_GCM;
4730 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004731 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4732 * The call to mbedtls_gcm_crypt_and_tag or
4733 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004734 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004735 return( PSA_ERROR_INVALID_ARGUMENT );
4736 mbedtls_gcm_init( &operation->ctx.gcm );
4737 status = mbedtls_to_psa_error(
4738 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004739 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004740 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004741 if( status != 0 )
4742 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004743 break;
4744#endif /* MBEDTLS_GCM_C */
4745
Gilles Peskine26869f22019-05-06 15:25:00 +02004746#if defined(MBEDTLS_CHACHAPOLY_C)
4747 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4748 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4749 operation->full_tag_length = 16;
4750 /* We only support the default tag length. */
4751 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4752 return( PSA_ERROR_NOT_SUPPORTED );
4753 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4754 status = mbedtls_to_psa_error(
4755 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004756 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004757 if( status != 0 )
4758 goto cleanup;
4759 break;
4760#endif /* MBEDTLS_CHACHAPOLY_C */
4761
Gilles Peskineedf9a652018-08-17 18:11:56 +02004762 default:
4763 return( PSA_ERROR_NOT_SUPPORTED );
4764 }
4765
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004766 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4767 {
4768 status = PSA_ERROR_INVALID_ARGUMENT;
4769 goto cleanup;
4770 }
4771 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004772
Gilles Peskineedf9a652018-08-17 18:11:56 +02004773 return( PSA_SUCCESS );
4774
4775cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004776 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004777 return( status );
4778}
4779
Gilles Peskinec5487a82018-12-03 18:08:14 +01004780psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004781 psa_algorithm_t alg,
4782 const uint8_t *nonce,
4783 size_t nonce_length,
4784 const uint8_t *additional_data,
4785 size_t additional_data_length,
4786 const uint8_t *plaintext,
4787 size_t plaintext_length,
4788 uint8_t *ciphertext,
4789 size_t ciphertext_size,
4790 size_t *ciphertext_length )
4791{
mohammad16035955c982018-04-26 00:53:03 +03004792 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004793 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004794 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004795
mohammad1603f08a5502018-06-03 15:05:47 +03004796 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004797
Gilles Peskinec5487a82018-12-03 18:08:14 +01004798 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004799 if( status != PSA_SUCCESS )
4800 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004801
Gilles Peskineedf9a652018-08-17 18:11:56 +02004802 /* For all currently supported modes, the tag is at the end of the
4803 * ciphertext. */
4804 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4805 {
4806 status = PSA_ERROR_BUFFER_TOO_SMALL;
4807 goto exit;
4808 }
4809 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004810
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004811#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004812 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004813 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004814 status = mbedtls_to_psa_error(
4815 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4816 MBEDTLS_GCM_ENCRYPT,
4817 plaintext_length,
4818 nonce, nonce_length,
4819 additional_data, additional_data_length,
4820 plaintext, ciphertext,
4821 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004822 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004823 else
4824#endif /* MBEDTLS_GCM_C */
4825#if defined(MBEDTLS_CCM_C)
4826 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004827 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004828 status = mbedtls_to_psa_error(
4829 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4830 plaintext_length,
4831 nonce, nonce_length,
4832 additional_data,
4833 additional_data_length,
4834 plaintext, ciphertext,
4835 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004836 }
mohammad16035c8845f2018-05-09 05:40:09 -07004837 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004838#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004839#if defined(MBEDTLS_CHACHAPOLY_C)
4840 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4841 {
4842 if( nonce_length != 12 || operation.tag_length != 16 )
4843 {
4844 status = PSA_ERROR_NOT_SUPPORTED;
4845 goto exit;
4846 }
4847 status = mbedtls_to_psa_error(
4848 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4849 plaintext_length,
4850 nonce,
4851 additional_data,
4852 additional_data_length,
4853 plaintext,
4854 ciphertext,
4855 tag ) );
4856 }
4857 else
4858#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004859 {
mohammad1603554faad2018-06-03 15:07:38 +03004860 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004861 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004862
Gilles Peskineedf9a652018-08-17 18:11:56 +02004863 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4864 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004865
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004867 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004868 if( status == PSA_SUCCESS )
4869 *ciphertext_length = plaintext_length + operation.tag_length;
4870 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004871}
4872
Gilles Peskineee652a32018-06-01 19:23:52 +02004873/* Locate the tag in a ciphertext buffer containing the encrypted data
4874 * followed by the tag. Return the length of the part preceding the tag in
4875 * *plaintext_length. This is the size of the plaintext in modes where
4876 * the encrypted data has the same size as the plaintext, such as
4877 * CCM and GCM. */
4878static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4879 const uint8_t *ciphertext,
4880 size_t ciphertext_length,
4881 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004882 const uint8_t **p_tag )
4883{
4884 size_t payload_length;
4885 if( tag_length > ciphertext_length )
4886 return( PSA_ERROR_INVALID_ARGUMENT );
4887 payload_length = ciphertext_length - tag_length;
4888 if( payload_length > plaintext_size )
4889 return( PSA_ERROR_BUFFER_TOO_SMALL );
4890 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004891 return( PSA_SUCCESS );
4892}
4893
Gilles Peskinec5487a82018-12-03 18:08:14 +01004894psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004895 psa_algorithm_t alg,
4896 const uint8_t *nonce,
4897 size_t nonce_length,
4898 const uint8_t *additional_data,
4899 size_t additional_data_length,
4900 const uint8_t *ciphertext,
4901 size_t ciphertext_length,
4902 uint8_t *plaintext,
4903 size_t plaintext_size,
4904 size_t *plaintext_length )
4905{
mohammad16035955c982018-04-26 00:53:03 +03004906 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004907 aead_operation_t operation;
4908 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004909
Gilles Peskineee652a32018-06-01 19:23:52 +02004910 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004911
Gilles Peskinec5487a82018-12-03 18:08:14 +01004912 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004913 if( status != PSA_SUCCESS )
4914 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004915
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004916 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4917 ciphertext, ciphertext_length,
4918 plaintext_size, &tag );
4919 if( status != PSA_SUCCESS )
4920 goto exit;
4921
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004922#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004923 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004924 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004925 status = mbedtls_to_psa_error(
4926 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4927 ciphertext_length - operation.tag_length,
4928 nonce, nonce_length,
4929 additional_data,
4930 additional_data_length,
4931 tag, operation.tag_length,
4932 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004933 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004934 else
4935#endif /* MBEDTLS_GCM_C */
4936#if defined(MBEDTLS_CCM_C)
4937 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004938 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939 status = mbedtls_to_psa_error(
4940 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4941 ciphertext_length - operation.tag_length,
4942 nonce, nonce_length,
4943 additional_data,
4944 additional_data_length,
4945 ciphertext, plaintext,
4946 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004947 }
mohammad160339574652018-06-01 04:39:53 -07004948 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004949#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004950#if defined(MBEDTLS_CHACHAPOLY_C)
4951 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4952 {
4953 if( nonce_length != 12 || operation.tag_length != 16 )
4954 {
4955 status = PSA_ERROR_NOT_SUPPORTED;
4956 goto exit;
4957 }
4958 status = mbedtls_to_psa_error(
4959 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4960 ciphertext_length - operation.tag_length,
4961 nonce,
4962 additional_data,
4963 additional_data_length,
4964 tag,
4965 ciphertext,
4966 plaintext ) );
4967 }
4968 else
4969#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004970 {
mohammad1603554faad2018-06-03 15:07:38 +03004971 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004972 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004973
Gilles Peskineedf9a652018-08-17 18:11:56 +02004974 if( status != PSA_SUCCESS && plaintext_size != 0 )
4975 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004976
Gilles Peskineedf9a652018-08-17 18:11:56 +02004977exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004978 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004979 if( status == PSA_SUCCESS )
4980 *plaintext_length = ciphertext_length - operation.tag_length;
4981 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004982}
4983
Gilles Peskinea0655c32018-04-30 17:06:50 +02004984
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004985
Gilles Peskine20035e32018-02-03 22:44:14 +01004986/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004987/* Generators */
4988/****************************************************************/
4989
John Durkop07cc04a2020-11-16 22:08:34 -08004990#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4992 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4993#define AT_LEAST_ONE_BUILTIN_KDF
4994#endif
4995
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004996#define HKDF_STATE_INIT 0 /* no input yet */
4997#define HKDF_STATE_STARTED 1 /* got salt */
4998#define HKDF_STATE_KEYED 2 /* got key */
4999#define HKDF_STATE_OUTPUT 3 /* output started */
5000
Gilles Peskinecbe66502019-05-16 16:59:18 +02005001static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005003{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5005 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005006 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005008}
5009
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005010psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005011{
5012 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005014 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005015 {
5016 /* The object has (apparently) been initialized but it is not
5017 * in use. It's ok to call abort on such an object, and there's
5018 * nothing to do. */
5019 }
5020 else
John Durkopd0321952020-10-29 21:37:36 -07005021#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005022 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005023 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 mbedtls_free( operation->ctx.hkdf.info );
5025 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005026 }
John Durkop07cc04a2020-11-16 22:08:34 -08005027 else
5028#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5029#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5030 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5031 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005033 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005034 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005035 if( operation->ctx.tls12_prf.seed != NULL )
5036 {
5037 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5038 operation->ctx.tls12_prf.seed_length );
5039 mbedtls_free( operation->ctx.tls12_prf.seed );
5040 }
5041
5042 if( operation->ctx.tls12_prf.label != NULL )
5043 {
5044 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5045 operation->ctx.tls12_prf.label_length );
5046 mbedtls_free( operation->ctx.tls12_prf.label );
5047 }
5048
5049 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5050
5051 /* We leave the fields Ai and output_block to be erased safely by the
5052 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005053 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005054 else
John Durkop07cc04a2020-11-16 22:08:34 -08005055#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5056 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005057 {
5058 status = PSA_ERROR_BAD_STATE;
5059 }
Janos Follath083036a2019-06-11 10:22:26 +01005060 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005061 return( status );
5062}
5063
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005065 size_t *capacity)
5066{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005068 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005070 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005071 }
5072
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005074 return( PSA_SUCCESS );
5075}
5076
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005078 size_t capacity )
5079{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005081 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005082 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005083 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005085 return( PSA_SUCCESS );
5086}
5087
John Durkopd0321952020-10-29 21:37:36 -07005088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005089/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005090 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005091static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005092 psa_algorithm_t hash_alg,
5093 uint8_t *output,
5094 size_t output_length )
5095{
5096 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5097 psa_status_t status;
5098
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5100 return( PSA_ERROR_BAD_STATE );
5101 hkdf->state = HKDF_STATE_OUTPUT;
5102
Gilles Peskinebef7f142018-07-12 17:22:21 +02005103 while( output_length != 0 )
5104 {
5105 /* Copy what remains of the current block */
5106 uint8_t n = hash_length - hkdf->offset_in_block;
5107 if( n > output_length )
5108 n = (uint8_t) output_length;
5109 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5110 output += n;
5111 output_length -= n;
5112 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005113 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005114 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005115 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005116 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005117 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005118 * object was corrupted or if this function is called directly
5119 * inside the library. */
5120 if( hkdf->block_number == 0xff )
5121 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005122
5123 /* We need a new block */
5124 ++hkdf->block_number;
5125 hkdf->offset_in_block = 0;
5126 status = psa_hmac_setup_internal( &hkdf->hmac,
5127 hkdf->prk, hash_length,
5128 hash_alg );
5129 if( status != PSA_SUCCESS )
5130 return( status );
5131 if( hkdf->block_number != 1 )
5132 {
5133 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5134 hkdf->output_block,
5135 hash_length );
5136 if( status != PSA_SUCCESS )
5137 return( status );
5138 }
5139 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5140 hkdf->info,
5141 hkdf->info_length );
5142 if( status != PSA_SUCCESS )
5143 return( status );
5144 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5145 &hkdf->block_number, 1 );
5146 if( status != PSA_SUCCESS )
5147 return( status );
5148 status = psa_hmac_finish_internal( &hkdf->hmac,
5149 hkdf->output_block,
5150 sizeof( hkdf->output_block ) );
5151 if( status != PSA_SUCCESS )
5152 return( status );
5153 }
5154
5155 return( PSA_SUCCESS );
5156}
John Durkop07cc04a2020-11-16 22:08:34 -08005157#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005158
John Durkop07cc04a2020-11-16 22:08:34 -08005159#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005161static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5162 psa_tls12_prf_key_derivation_t *tls12_prf,
5163 psa_algorithm_t alg )
5164{
5165 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5166 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005167 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5168 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005169
Janos Follath7742fee2019-06-17 12:58:10 +01005170 /* We can't be wanting more output after block 0xff, otherwise
5171 * the capacity check in psa_key_derivation_output_bytes() would have
5172 * prevented this call. It could happen only if the operation
5173 * object was corrupted or if this function is called directly
5174 * inside the library. */
5175 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005176 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005177
5178 /* We need a new block */
5179 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005180 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005181
5182 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5183 *
5184 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5185 *
5186 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5187 * HMAC_hash(secret, A(2) + seed) +
5188 * HMAC_hash(secret, A(3) + seed) + ...
5189 *
5190 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005191 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005192 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005193 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005194 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005195 * is currently extracted as `output_block` and where i is
5196 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005197 */
5198
Janos Follathea29bfb2019-06-19 12:21:20 +01005199 /* Save the hash context before using it, to preserve the hash state with
5200 * only the inner padding in it. We need this, because inner padding depends
5201 * on the key (secret in the RFC's terminology). */
5202 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5203 if( status != PSA_SUCCESS )
5204 goto cleanup;
5205
5206 /* Calculate A(i) where i = tls12_prf->block_number. */
5207 if( tls12_prf->block_number == 1 )
5208 {
5209 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5210 * the variable seed and in this instance means it in the context of the
5211 * P_hash function, where seed = label + seed.) */
5212 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5213 tls12_prf->label, tls12_prf->label_length );
5214 if( status != PSA_SUCCESS )
5215 goto cleanup;
5216 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5217 tls12_prf->seed, tls12_prf->seed_length );
5218 if( status != PSA_SUCCESS )
5219 goto cleanup;
5220 }
5221 else
5222 {
5223 /* A(i) = HMAC_hash(secret, A(i-1)) */
5224 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5225 tls12_prf->Ai, hash_length );
5226 if( status != PSA_SUCCESS )
5227 goto cleanup;
5228 }
5229
5230 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5231 tls12_prf->Ai, hash_length );
5232 if( status != PSA_SUCCESS )
5233 goto cleanup;
5234 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5235 if( status != PSA_SUCCESS )
5236 goto cleanup;
5237
5238 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5239 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5240 tls12_prf->Ai, hash_length );
5241 if( status != PSA_SUCCESS )
5242 goto cleanup;
5243 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5244 tls12_prf->label, tls12_prf->label_length );
5245 if( status != PSA_SUCCESS )
5246 goto cleanup;
5247 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5248 tls12_prf->seed, tls12_prf->seed_length );
5249 if( status != PSA_SUCCESS )
5250 goto cleanup;
5251 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5252 tls12_prf->output_block, hash_length );
5253 if( status != PSA_SUCCESS )
5254 goto cleanup;
5255 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5256 if( status != PSA_SUCCESS )
5257 goto cleanup;
5258
Janos Follath7742fee2019-06-17 12:58:10 +01005259
5260cleanup:
5261
Janos Follathea29bfb2019-06-19 12:21:20 +01005262 cleanup_status = psa_hash_abort( &backup );
5263 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5264 status = cleanup_status;
5265
5266 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005267}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005268
Janos Follath844eb0e2019-06-19 12:10:49 +01005269static psa_status_t psa_key_derivation_tls12_prf_read(
5270 psa_tls12_prf_key_derivation_t *tls12_prf,
5271 psa_algorithm_t alg,
5272 uint8_t *output,
5273 size_t output_length )
5274{
5275 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5276 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5277 psa_status_t status;
5278 uint8_t offset, length;
5279
5280 while( output_length != 0 )
5281 {
5282 /* Check if we have fully processed the current block. */
5283 if( tls12_prf->left_in_block == 0 )
5284 {
5285 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5286 alg );
5287 if( status != PSA_SUCCESS )
5288 return( status );
5289
5290 continue;
5291 }
5292
5293 if( tls12_prf->left_in_block > output_length )
5294 length = (uint8_t) output_length;
5295 else
5296 length = tls12_prf->left_in_block;
5297
5298 offset = hash_length - tls12_prf->left_in_block;
5299 memcpy( output, tls12_prf->output_block + offset, length );
5300 output += length;
5301 output_length -= length;
5302 tls12_prf->left_in_block -= length;
5303 }
5304
5305 return( PSA_SUCCESS );
5306}
John Durkop07cc04a2020-11-16 22:08:34 -08005307#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5308 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005310psa_status_t psa_key_derivation_output_bytes(
5311 psa_key_derivation_operation_t *operation,
5312 uint8_t *output,
5313 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005314{
5315 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005319 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005321 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005322 }
5323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005325 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005327 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005329 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005330 goto exit;
5331 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005333 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005335 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005336 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5337 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005339 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005340 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005341 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005343
John Durkopd0321952020-10-29 21:37:36 -07005344#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005345 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005346 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005347 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005349 output, output_length );
5350 }
Janos Follathadbec812019-06-14 11:05:39 +01005351 else
John Durkop07cc04a2020-11-16 22:08:34 -08005352#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5353#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5354 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005355 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005356 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005357 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005359 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005360 output_length );
5361 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005362 else
John Durkop07cc04a2020-11-16 22:08:34 -08005363#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5364 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005365 {
5366 return( PSA_ERROR_BAD_STATE );
5367 }
5368
5369exit:
5370 if( status != PSA_SUCCESS )
5371 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005372 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005373 * This allows us to differentiate between exhausted operations and
5374 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5375 * operations. */
5376 psa_algorithm_t alg = operation->alg;
5377 psa_key_derivation_abort( operation );
5378 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005379 memset( output, '!', output_length );
5380 }
5381 return( status );
5382}
5383
Gilles Peskine08542d82018-07-19 17:05:42 +02005384#if defined(MBEDTLS_DES_C)
5385static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5386{
5387 if( data_size >= 8 )
5388 mbedtls_des_key_set_parity( data );
5389 if( data_size >= 16 )
5390 mbedtls_des_key_set_parity( data + 8 );
5391 if( data_size >= 24 )
5392 mbedtls_des_key_set_parity( data + 16 );
5393}
5394#endif /* MBEDTLS_DES_C */
5395
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005396static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005397 psa_key_slot_t *slot,
5398 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005400{
5401 uint8_t *data = NULL;
5402 size_t bytes = PSA_BITS_TO_BYTES( bits );
5403 psa_status_t status;
5404
Gilles Peskine8e338702019-07-30 20:06:31 +02005405 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005406 return( PSA_ERROR_INVALID_ARGUMENT );
5407 if( bits % 8 != 0 )
5408 return( PSA_ERROR_INVALID_ARGUMENT );
5409 data = mbedtls_calloc( 1, bytes );
5410 if( data == NULL )
5411 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5412
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005413 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005414 if( status != PSA_SUCCESS )
5415 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005416#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005417 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005418 psa_des_set_key_parity( data, bytes );
5419#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005420 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005421
5422exit:
5423 mbedtls_free( data );
5424 return( status );
5425}
5426
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005427psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005429 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005430{
5431 psa_status_t status;
5432 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005433 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005434
5435 /* Reject any attempt to create a zero-length key so that we don't
5436 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5437 if( psa_get_key_bits( attributes ) == 0 )
5438 return( PSA_ERROR_INVALID_ARGUMENT );
5439
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005440 if( ! operation->can_output_key )
5441 return( PSA_ERROR_NOT_PERMITTED );
5442
Gilles Peskinedf179142019-07-15 22:02:14 +02005443 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5444 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005445#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5446 if( driver != NULL )
5447 {
5448 /* Deriving a key in a secure element is not implemented yet. */
5449 status = PSA_ERROR_NOT_SUPPORTED;
5450 }
5451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005452 if( status == PSA_SUCCESS )
5453 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005454 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005455 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005457 }
5458 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005459 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005460 if( status != PSA_SUCCESS )
5461 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005462 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005463 *handle = 0;
5464 }
5465 return( status );
5466}
5467
Gilles Peskineeab56e42018-07-12 17:12:33 +02005468
5469
5470/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005471/* Key derivation */
5472/****************************************************************/
5473
John Durkop07cc04a2020-11-16 22:08:34 -08005474#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005475static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005477 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005478{
John Durkop07cc04a2020-11-16 22:08:34 -08005479 int is_kdf_alg_supported;
5480
Janos Follath5fe19732019-06-20 15:09:30 +01005481 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5482 * initialisers for this union leave some bytes unspecified.) */
5483 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5484
Gilles Peskine969c5d62019-01-16 15:53:06 +01005485 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005486#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005487 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5488 is_kdf_alg_supported = 1;
5489 else
5490#endif
5491#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5492 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5493 is_kdf_alg_supported = 1;
5494 else
5495#endif
5496#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5497 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5498 is_kdf_alg_supported = 1;
5499 else
5500#endif
5501 is_kdf_alg_supported = 0;
5502
5503 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005504 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005505 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005506 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5507 if( hash_size == 0 )
5508 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005509 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5510 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005511 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005512 {
5513 return( PSA_ERROR_NOT_SUPPORTED );
5514 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005516 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005517 }
John Durkop07cc04a2020-11-16 22:08:34 -08005518
5519 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005520}
John Durkop07cc04a2020-11-16 22:08:34 -08005521#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005524 psa_algorithm_t alg )
5525{
5526 psa_status_t status;
5527
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005529 return( PSA_ERROR_BAD_STATE );
5530
Gilles Peskine6843c292019-01-18 16:44:49 +01005531 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5532 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005533#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005534 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005535 {
5536 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005538 }
5539 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5540 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005542 }
John Durkop07cc04a2020-11-16 22:08:34 -08005543#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005544 else
5545 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005546
5547 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005549 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005550}
5551
John Durkopd0321952020-10-29 21:37:36 -07005552#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005553static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005554 psa_algorithm_t hash_alg,
5555 psa_key_derivation_step_t step,
5556 const uint8_t *data,
5557 size_t data_length )
5558{
5559 psa_status_t status;
5560 switch( step )
5561 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005562 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005563 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005564 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005565 status = psa_hmac_setup_internal( &hkdf->hmac,
5566 data, data_length,
5567 hash_alg );
5568 if( status != PSA_SUCCESS )
5569 return( status );
5570 hkdf->state = HKDF_STATE_STARTED;
5571 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005572 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573 /* If no salt was provided, use an empty salt. */
5574 if( hkdf->state == HKDF_STATE_INIT )
5575 {
5576 status = psa_hmac_setup_internal( &hkdf->hmac,
5577 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005578 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005579 if( status != PSA_SUCCESS )
5580 return( status );
5581 hkdf->state = HKDF_STATE_STARTED;
5582 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005583 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005584 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005585 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5586 data, data_length );
5587 if( status != PSA_SUCCESS )
5588 return( status );
5589 status = psa_hmac_finish_internal( &hkdf->hmac,
5590 hkdf->prk,
5591 sizeof( hkdf->prk ) );
5592 if( status != PSA_SUCCESS )
5593 return( status );
5594 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5595 hkdf->block_number = 0;
5596 hkdf->state = HKDF_STATE_KEYED;
5597 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005598 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005599 if( hkdf->state == HKDF_STATE_OUTPUT )
5600 return( PSA_ERROR_BAD_STATE );
5601 if( hkdf->info_set )
5602 return( PSA_ERROR_BAD_STATE );
5603 hkdf->info_length = data_length;
5604 if( data_length != 0 )
5605 {
5606 hkdf->info = mbedtls_calloc( 1, data_length );
5607 if( hkdf->info == NULL )
5608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5609 memcpy( hkdf->info, data, data_length );
5610 }
5611 hkdf->info_set = 1;
5612 return( PSA_SUCCESS );
5613 default:
5614 return( PSA_ERROR_INVALID_ARGUMENT );
5615 }
5616}
John Durkop07cc04a2020-11-16 22:08:34 -08005617#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005618
John Durkop07cc04a2020-11-16 22:08:34 -08005619#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5620 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005621static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5622 const uint8_t *data,
5623 size_t data_length )
5624{
5625 if( prf->state != TLS12_PRF_STATE_INIT )
5626 return( PSA_ERROR_BAD_STATE );
5627
Janos Follathd6dce9f2019-07-04 09:11:38 +01005628 if( data_length != 0 )
5629 {
5630 prf->seed = mbedtls_calloc( 1, data_length );
5631 if( prf->seed == NULL )
5632 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005633
Janos Follathd6dce9f2019-07-04 09:11:38 +01005634 memcpy( prf->seed, data, data_length );
5635 prf->seed_length = data_length;
5636 }
Janos Follathf08e2652019-06-13 09:05:41 +01005637
5638 prf->state = TLS12_PRF_STATE_SEED_SET;
5639
5640 return( PSA_SUCCESS );
5641}
5642
Janos Follath81550542019-06-13 14:26:34 +01005643static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5644 psa_algorithm_t hash_alg,
5645 const uint8_t *data,
5646 size_t data_length )
5647{
5648 psa_status_t status;
5649 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5650 return( PSA_ERROR_BAD_STATE );
5651
5652 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5653 if( status != PSA_SUCCESS )
5654 return( status );
5655
5656 prf->state = TLS12_PRF_STATE_KEY_SET;
5657
5658 return( PSA_SUCCESS );
5659}
5660
Janos Follath63028dd2019-06-13 09:15:47 +01005661static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5662 const uint8_t *data,
5663 size_t data_length )
5664{
5665 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5666 return( PSA_ERROR_BAD_STATE );
5667
Janos Follathd6dce9f2019-07-04 09:11:38 +01005668 if( data_length != 0 )
5669 {
5670 prf->label = mbedtls_calloc( 1, data_length );
5671 if( prf->label == NULL )
5672 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005673
Janos Follathd6dce9f2019-07-04 09:11:38 +01005674 memcpy( prf->label, data, data_length );
5675 prf->label_length = data_length;
5676 }
Janos Follath63028dd2019-06-13 09:15:47 +01005677
5678 prf->state = TLS12_PRF_STATE_LABEL_SET;
5679
5680 return( PSA_SUCCESS );
5681}
5682
Janos Follathb03233e2019-06-11 15:30:30 +01005683static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5684 psa_algorithm_t hash_alg,
5685 psa_key_derivation_step_t step,
5686 const uint8_t *data,
5687 size_t data_length )
5688{
Janos Follathb03233e2019-06-11 15:30:30 +01005689 switch( step )
5690 {
Janos Follathf08e2652019-06-13 09:05:41 +01005691 case PSA_KEY_DERIVATION_INPUT_SEED:
5692 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005693 case PSA_KEY_DERIVATION_INPUT_SECRET:
5694 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005695 case PSA_KEY_DERIVATION_INPUT_LABEL:
5696 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005697 default:
5698 return( PSA_ERROR_INVALID_ARGUMENT );
5699 }
5700}
John Durkop07cc04a2020-11-16 22:08:34 -08005701#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5702 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5703
5704#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5705static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5706 psa_tls12_prf_key_derivation_t *prf,
5707 psa_algorithm_t hash_alg,
5708 const uint8_t *data,
5709 size_t data_length )
5710{
5711 psa_status_t status;
5712 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5713 uint8_t *cur = pms;
5714
5715 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5716 return( PSA_ERROR_INVALID_ARGUMENT );
5717
5718 /* Quoting RFC 4279, Section 2:
5719 *
5720 * The premaster secret is formed as follows: if the PSK is N octets
5721 * long, concatenate a uint16 with the value N, N zero octets, a second
5722 * uint16 with the value N, and the PSK itself.
5723 */
5724
5725 *cur++ = ( data_length >> 8 ) & 0xff;
5726 *cur++ = ( data_length >> 0 ) & 0xff;
5727 memset( cur, 0, data_length );
5728 cur += data_length;
5729 *cur++ = pms[0];
5730 *cur++ = pms[1];
5731 memcpy( cur, data, data_length );
5732 cur += data_length;
5733
5734 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5735
5736 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5737 return( status );
5738}
Janos Follath6660f0e2019-06-17 08:44:03 +01005739
5740static psa_status_t psa_tls12_prf_psk_to_ms_input(
5741 psa_tls12_prf_key_derivation_t *prf,
5742 psa_algorithm_t hash_alg,
5743 psa_key_derivation_step_t step,
5744 const uint8_t *data,
5745 size_t data_length )
5746{
5747 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005748 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005749 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5750 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005751 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005752
5753 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5754}
John Durkop07cc04a2020-11-16 22:08:34 -08005755#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005756
Gilles Peskineb8965192019-09-24 16:21:10 +02005757/** Check whether the given key type is acceptable for the given
5758 * input step of a key derivation.
5759 *
5760 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5761 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5762 * Both secret and non-secret inputs can alternatively have the type
5763 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5764 * that the input was passed as a buffer rather than via a key object.
5765 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005766static int psa_key_derivation_check_input_type(
5767 psa_key_derivation_step_t step,
5768 psa_key_type_t key_type )
5769{
5770 switch( step )
5771 {
5772 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005773 if( key_type == PSA_KEY_TYPE_DERIVE )
5774 return( PSA_SUCCESS );
5775 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005776 return( PSA_SUCCESS );
5777 break;
5778 case PSA_KEY_DERIVATION_INPUT_LABEL:
5779 case PSA_KEY_DERIVATION_INPUT_SALT:
5780 case PSA_KEY_DERIVATION_INPUT_INFO:
5781 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005782 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5783 return( PSA_SUCCESS );
5784 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005785 return( PSA_SUCCESS );
5786 break;
5787 }
5788 return( PSA_ERROR_INVALID_ARGUMENT );
5789}
5790
Janos Follathb80a94e2019-06-12 15:54:46 +01005791static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005792 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005793 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005794 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005795 const uint8_t *data,
5796 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005797{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005798 psa_status_t status;
5799 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5800
5801 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005802 if( status != PSA_SUCCESS )
5803 goto exit;
5804
John Durkopd0321952020-10-29 21:37:36 -07005805#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005806 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005807 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005808 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005809 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005810 step, data, data_length );
5811 }
John Durkop07cc04a2020-11-16 22:08:34 -08005812 else
5813#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5814#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5815 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005816 {
Janos Follathb03233e2019-06-11 15:30:30 +01005817 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5818 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5819 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005820 }
John Durkop07cc04a2020-11-16 22:08:34 -08005821 else
5822#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5823#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5824 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005825 {
5826 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5827 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5828 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005829 }
5830 else
John Durkop07cc04a2020-11-16 22:08:34 -08005831#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005832 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005833 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005834 return( PSA_ERROR_BAD_STATE );
5835 }
5836
Gilles Peskine224b0d62019-09-23 18:13:17 +02005837exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005838 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005839 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005840 return( status );
5841}
5842
Janos Follath51f4a0f2019-06-14 11:35:55 +01005843psa_status_t psa_key_derivation_input_bytes(
5844 psa_key_derivation_operation_t *operation,
5845 psa_key_derivation_step_t step,
5846 const uint8_t *data,
5847 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005848{
Gilles Peskineb8965192019-09-24 16:21:10 +02005849 return( psa_key_derivation_input_internal( operation, step,
5850 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005851 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005852}
5853
Janos Follath51f4a0f2019-06-14 11:35:55 +01005854psa_status_t psa_key_derivation_input_key(
5855 psa_key_derivation_operation_t *operation,
5856 psa_key_derivation_step_t step,
5857 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005858{
5859 psa_key_slot_t *slot;
5860 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005861
Gilles Peskine28f8f302019-07-24 13:30:31 +02005862 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005863 PSA_KEY_USAGE_DERIVE,
5864 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005865 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005866 {
5867 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005868 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005869 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005870
5871 /* Passing a key object as a SECRET input unlocks the permission
5872 * to output to a key object. */
5873 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5874 operation->can_output_key = 1;
5875
Janos Follathb80a94e2019-06-12 15:54:46 +01005876 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005877 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005878 slot->data.key.data,
5879 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005880}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005881
5882
5883
5884/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005885/* Key agreement */
5886/****************************************************************/
5887
John Durkop6ba40d12020-11-10 08:50:04 -08005888#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005889static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5890 size_t peer_key_length,
5891 const mbedtls_ecp_keypair *our_key,
5892 uint8_t *shared_secret,
5893 size_t shared_secret_size,
5894 size_t *shared_secret_length )
5895{
Steven Cooremand4867872020-08-05 16:31:39 +02005896 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005897 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005898 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005899 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005900 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005901 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005902
Steven Cooreman4fed4552020-08-03 14:46:03 +02005903 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5904 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005905 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005906 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005907 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005908 goto exit;
5909
Jaeden Amero97271b32019-01-10 19:38:51 +00005910 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005911 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005912 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005913 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005914 status = mbedtls_to_psa_error(
5915 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5916 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005917 goto exit;
5918
Jaeden Amero97271b32019-01-10 19:38:51 +00005919 status = mbedtls_to_psa_error(
5920 mbedtls_ecdh_calc_secret( &ecdh,
5921 shared_secret_length,
5922 shared_secret, shared_secret_size,
5923 mbedtls_ctr_drbg_random,
5924 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005925 if( status != PSA_SUCCESS )
5926 goto exit;
5927 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5928 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005929
5930exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005931 if( status != PSA_SUCCESS )
5932 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005933 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005934 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005935 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005936
Jaeden Amero97271b32019-01-10 19:38:51 +00005937 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005938}
John Durkop6ba40d12020-11-10 08:50:04 -08005939#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005940
Gilles Peskine01d718c2018-09-18 12:01:02 +02005941#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5942
Gilles Peskine0216fe12019-04-11 21:23:21 +02005943static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5944 psa_key_slot_t *private_key,
5945 const uint8_t *peer_key,
5946 size_t peer_key_length,
5947 uint8_t *shared_secret,
5948 size_t shared_secret_size,
5949 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005950{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005951 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005952 {
John Durkop6ba40d12020-11-10 08:50:04 -08005953#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005954 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005955 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005956 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005957 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005958 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005959 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005960 private_key->data.key.data,
5961 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005962 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005963 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005964 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005965 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005966 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005967 shared_secret, shared_secret_size,
5968 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005969 mbedtls_ecp_keypair_free( ecp );
5970 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005971 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005972#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005973 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005974 (void) private_key;
5975 (void) peer_key;
5976 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005977 (void) shared_secret;
5978 (void) shared_secret_size;
5979 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005980 return( PSA_ERROR_NOT_SUPPORTED );
5981 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005982}
5983
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005984/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005985 * to potentially free embedded data structures and wipe confidential data.
5986 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005987static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005988 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005989 psa_key_slot_t *private_key,
5990 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005991 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005992{
5993 psa_status_t status;
5994 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5995 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005996 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005997
5998 /* Step 1: run the secret agreement algorithm to generate the shared
5999 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006000 status = psa_key_agreement_raw_internal( ka_alg,
6001 private_key,
6002 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006003 shared_secret,
6004 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006005 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006006 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006007 goto exit;
6008
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006009 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006010 * the shared secret. A shared secret is permitted wherever a key
6011 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006012 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006013 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006014 shared_secret,
6015 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006016exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006017 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006018 return( status );
6019}
6020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006021psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006022 psa_key_derivation_step_t step,
6023 psa_key_handle_t private_key,
6024 const uint8_t *peer_key,
6025 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006026{
Gilles Peskine2f060a82018-12-04 17:12:32 +01006027 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02006028 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006029 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006030 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006031 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006032 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006033 if( status != PSA_SUCCESS )
6034 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006035 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006036 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006037 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006038 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006039 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006040 else
6041 {
6042 /* If a private key has been added as SECRET, we allow the derived
6043 * key material to be used as a key in PSA Crypto. */
6044 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6045 operation->can_output_key = 1;
6046 }
Gilles Peskine346797d2018-11-16 16:05:06 +01006047 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006048}
6049
Gilles Peskinebe697d82019-05-16 18:00:41 +02006050psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
6051 psa_key_handle_t private_key,
6052 const uint8_t *peer_key,
6053 size_t peer_key_length,
6054 uint8_t *output,
6055 size_t output_size,
6056 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006057{
6058 psa_key_slot_t *slot;
6059 psa_status_t status;
6060
6061 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6062 {
6063 status = PSA_ERROR_INVALID_ARGUMENT;
6064 goto exit;
6065 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006066 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006067 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006068 if( status != PSA_SUCCESS )
6069 goto exit;
6070
6071 status = psa_key_agreement_raw_internal( alg, slot,
6072 peer_key, peer_key_length,
6073 output, output_size,
6074 output_length );
6075
6076exit:
6077 if( status != PSA_SUCCESS )
6078 {
6079 /* If an error happens and is not handled properly, the output
6080 * may be used as a key to protect sensitive data. Arrange for such
6081 * a key to be random, which is likely to result in decryption or
6082 * verification errors. This is better than filling the buffer with
6083 * some constant data such as zeros, which would result in the data
6084 * being protected with a reproducible, easily knowable key.
6085 */
6086 psa_generate_random( output, output_size );
6087 *output_length = output_size;
6088 }
6089 return( status );
6090}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006091
6092
6093/****************************************************************/
6094/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006095/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006096
Gilles Peskine4c317f42018-07-12 01:24:09 +02006097psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006098 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006099{
Janos Follath24eed8d2019-11-22 13:21:35 +00006100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101 GUARD_MODULE_INITIALIZED;
6102
Gilles Peskinef181eca2019-08-07 13:49:00 +02006103 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6104 {
6105 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6106 output,
6107 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6108 if( ret != 0 )
6109 return( mbedtls_to_psa_error( ret ) );
6110 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6111 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6112 }
6113
Gilles Peskinee59236f2018-01-27 23:32:46 +01006114 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6115 return( mbedtls_to_psa_error( ret ) );
6116}
6117
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006118#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6119#include "mbedtls/entropy_poll.h"
6120
Gilles Peskine7228da22019-07-15 11:06:15 +02006121psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006122 size_t seed_size )
6123{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006124 if( global_data.initialized )
6125 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006126
6127 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6128 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6129 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6130 return( PSA_ERROR_INVALID_ARGUMENT );
6131
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006132 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006133}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006134#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006135
John Durkop07cc04a2020-11-16 22:08:34 -08006136#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006137static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6138 size_t domain_parameters_size,
6139 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006140{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006141 size_t i;
6142 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006143
Gilles Peskinee56e8782019-04-26 17:34:02 +02006144 if( domain_parameters_size == 0 )
6145 {
6146 *exponent = 65537;
6147 return( PSA_SUCCESS );
6148 }
6149
6150 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6151 * support values that fit in a 32-bit integer, which is larger than
6152 * int on just about every platform anyway. */
6153 if( domain_parameters_size > sizeof( acc ) )
6154 return( PSA_ERROR_NOT_SUPPORTED );
6155 for( i = 0; i < domain_parameters_size; i++ )
6156 acc = ( acc << 8 ) | domain_parameters[i];
6157 if( acc > INT_MAX )
6158 return( PSA_ERROR_NOT_SUPPORTED );
6159 *exponent = acc;
6160 return( PSA_SUCCESS );
6161}
John Durkop07cc04a2020-11-16 22:08:34 -08006162#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006163
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006164static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006165 psa_key_slot_t *slot, size_t bits,
6166 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006167{
Gilles Peskine8e338702019-07-30 20:06:31 +02006168 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006169
Gilles Peskinee56e8782019-04-26 17:34:02 +02006170 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171 return( PSA_ERROR_INVALID_ARGUMENT );
6172
Gilles Peskinee59236f2018-01-27 23:32:46 +01006173 if( key_type_is_raw_bytes( type ) )
6174 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006175 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006176
6177 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006178 if( status != PSA_SUCCESS )
6179 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006180
6181 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006182 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6183 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006184 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006185
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006186 status = psa_generate_random( slot->data.key.data,
6187 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006188 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006189 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006190
6191 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192#if defined(MBEDTLS_DES_C)
6193 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006194 psa_des_set_key_parity( slot->data.key.data,
6195 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006196#endif /* MBEDTLS_DES_C */
6197 }
6198 else
6199
John Durkop07cc04a2020-11-16 22:08:34 -08006200#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006201 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006202 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006203 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006205 int exponent;
6206 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006207 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6208 return( PSA_ERROR_NOT_SUPPORTED );
6209 /* Accept only byte-aligned keys, for the same reasons as
6210 * in psa_import_rsa_key(). */
6211 if( bits % 8 != 0 )
6212 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006213 status = psa_read_rsa_exponent( domain_parameters,
6214 domain_parameters_size,
6215 &exponent );
6216 if( status != PSA_SUCCESS )
6217 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006218 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6219 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220 mbedtls_ctr_drbg_random,
6221 &global_data.ctr_drbg,
6222 (unsigned int) bits,
6223 exponent );
6224 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006225 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006226
6227 /* Make sure to always have an export representation available */
6228 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6229
Steven Cooreman75b74362020-07-28 14:30:13 +02006230 status = psa_allocate_buffer_to_slot( slot, bytes );
6231 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006232 {
6233 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006234 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006235 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006236
6237 status = psa_export_rsa_key( type,
6238 &rsa,
6239 slot->data.key.data,
6240 bytes,
6241 &slot->data.key.bytes );
6242 mbedtls_rsa_free( &rsa );
6243 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006244 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006245 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006246 }
6247 else
John Durkop07cc04a2020-11-16 22:08:34 -08006248#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006249
John Durkop9814fa22020-11-04 12:28:15 -08006250#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006251 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006252 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006253 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006254 mbedtls_ecp_group_id grp_id =
6255 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006256 const mbedtls_ecp_curve_info *curve_info =
6257 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006258 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006260 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006261 return( PSA_ERROR_NOT_SUPPORTED );
6262 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6263 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006264 mbedtls_ecp_keypair_init( &ecp );
6265 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006266 mbedtls_ctr_drbg_random,
6267 &global_data.ctr_drbg );
6268 if( ret != 0 )
6269 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006270 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006271 return( mbedtls_to_psa_error( ret ) );
6272 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006273
6274
6275 /* Make sure to always have an export representation available */
6276 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006277 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6278 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006279 {
6280 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006281 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006282 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006283
6284 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006285 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6286
6287 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006288 if( status != PSA_SUCCESS ) {
6289 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006290 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006291 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006292 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006293 }
6294 else
John Durkop9814fa22020-11-04 12:28:15 -08006295#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006296 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006297 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006298 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006299
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006300 return( PSA_SUCCESS );
6301}
Darryl Green0c6575a2018-11-07 16:05:30 +00006302
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006303psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006304 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006305{
6306 psa_status_t status;
6307 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006308 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006309
Gilles Peskine0f84d622019-09-12 19:03:13 +02006310 /* Reject any attempt to create a zero-length key so that we don't
6311 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6312 if( psa_get_key_bits( attributes ) == 0 )
6313 return( PSA_ERROR_INVALID_ARGUMENT );
6314
Gilles Peskinedf179142019-07-15 22:02:14 +02006315 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6316 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006317 if( status != PSA_SUCCESS )
6318 goto exit;
6319
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006320 status = psa_driver_wrapper_generate_key( attributes,
6321 slot );
6322 if( status != PSA_ERROR_NOT_SUPPORTED ||
6323 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6324 goto exit;
6325
6326 status = psa_generate_key_internal(
6327 slot, attributes->core.bits,
6328 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006329
6330exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006331 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006332 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006333 if( status != PSA_SUCCESS )
6334 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006335 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006336 *handle = 0;
6337 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006338 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006339}
6340
6341
Gilles Peskinee59236f2018-01-27 23:32:46 +01006342
6343/****************************************************************/
6344/* Module setup */
6345/****************************************************************/
6346
Gilles Peskine5e769522018-11-20 21:59:56 +01006347psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6348 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6349 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6350{
6351 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6352 return( PSA_ERROR_BAD_STATE );
6353 global_data.entropy_init = entropy_init;
6354 global_data.entropy_free = entropy_free;
6355 return( PSA_SUCCESS );
6356}
6357
Gilles Peskinee59236f2018-01-27 23:32:46 +01006358void mbedtls_psa_crypto_free( void )
6359{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006360 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006361 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6362 {
6363 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006364 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006365 }
6366 /* Wipe all remaining data, including configuration.
6367 * In particular, this sets all state indicator to the value
6368 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006369 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006371 /* Unregister all secure element drivers, so that we restart from
6372 * a pristine state. */
6373 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006374#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006375}
6376
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006377#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6378/** Recover a transaction that was interrupted by a power failure.
6379 *
6380 * This function is called during initialization, before psa_crypto_init()
6381 * returns. If this function returns a failure status, the initialization
6382 * fails.
6383 */
6384static psa_status_t psa_crypto_recover_transaction(
6385 const psa_crypto_transaction_t *transaction )
6386{
6387 switch( transaction->unknown.type )
6388 {
6389 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6390 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006391 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006392 * is implemented.
6393 * https://github.com/ARMmbed/mbed-crypto/issues/218
6394 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006395 default:
6396 /* We found an unsupported transaction in the storage.
6397 * We don't know what state the storage is in. Give up. */
6398 return( PSA_ERROR_STORAGE_FAILURE );
6399 }
6400}
6401#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6402
Gilles Peskinee59236f2018-01-27 23:32:46 +01006403psa_status_t psa_crypto_init( void )
6404{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006405 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006406 const unsigned char drbg_seed[] = "PSA";
6407
Gilles Peskinec6b69072018-11-20 21:42:52 +01006408 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409 if( global_data.initialized != 0 )
6410 return( PSA_SUCCESS );
6411
Gilles Peskine5e769522018-11-20 21:59:56 +01006412 /* Set default configuration if
6413 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6414 if( global_data.entropy_init == NULL )
6415 global_data.entropy_init = mbedtls_entropy_init;
6416 if( global_data.entropy_free == NULL )
6417 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006418
Gilles Peskinec6b69072018-11-20 21:42:52 +01006419 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006420 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006421#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6422 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6423 /* The PSA entropy injection feature depends on using NV seed as an entropy
6424 * source. Add NV seed as an entropy source for PSA entropy injection. */
6425 mbedtls_entropy_add_source( &global_data.entropy,
6426 mbedtls_nv_seed_poll, NULL,
6427 MBEDTLS_ENTROPY_BLOCK_SIZE,
6428 MBEDTLS_ENTROPY_SOURCE_STRONG );
6429#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006431 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006432 status = mbedtls_to_psa_error(
6433 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6434 mbedtls_entropy_func,
6435 &global_data.entropy,
6436 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6437 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006438 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006439 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440
Gilles Peskine66fb1262018-12-10 16:29:04 +01006441 status = psa_initialize_key_slots( );
6442 if( status != PSA_SUCCESS )
6443 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006444
Gilles Peskined9348f22019-10-01 15:22:29 +02006445#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6446 status = psa_init_all_se_drivers( );
6447 if( status != PSA_SUCCESS )
6448 goto exit;
6449#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6450
Gilles Peskine4b734222019-07-24 15:56:31 +02006451#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006452 status = psa_crypto_load_transaction( );
6453 if( status == PSA_SUCCESS )
6454 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006455 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6456 if( status != PSA_SUCCESS )
6457 goto exit;
6458 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006459 }
6460 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6461 {
6462 /* There's no transaction to complete. It's all good. */
6463 status = PSA_SUCCESS;
6464 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006465#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006466
Gilles Peskinec6b69072018-11-20 21:42:52 +01006467 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468 global_data.initialized = 1;
6469
6470exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006471 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006472 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006473 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006474}
6475
6476#endif /* MBEDTLS_PSA_CRYPTO_C */