blob: 8a2e4138322585b6ea9fde989c4b47abf7c7d946 [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 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001659
Steven Cooreman560c28a2020-07-24 23:20:24 +02001660 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001661 * so conversion is needed. Try the accelerators first. */
1662 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1663 data,
1664 data_size,
1665 data_length );
1666
1667 if( status != PSA_ERROR_NOT_SUPPORTED ||
1668 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1669 return( status );
1670
Steven Cooreman560c28a2020-07-24 23:20:24 +02001671 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1672 {
John Durkop0e005192020-10-31 22:06:54 -07001673#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1674 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001675 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001676 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001677 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001678 slot->data.key.data,
1679 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001680 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001681 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001682 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001683
Steven Cooreman560c28a2020-07-24 23:20:24 +02001684 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001685 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001686 data,
1687 data_size,
1688 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001689
Steven Cooremana2371e52020-07-28 14:30:39 +02001690 mbedtls_rsa_free( rsa );
1691 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001692
Steven Cooreman560c28a2020-07-24 23:20:24 +02001693 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001694#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001695 /* We don't know how to convert a private RSA key to public. */
1696 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001697#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1698 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001699 }
1700 else
Moran Pekera998bc62018-04-16 18:16:20 +03001701 {
John Durkop6ba40d12020-11-10 08:50:04 -08001702#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1703 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001704 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001705 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001706 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001707 slot->data.key.data,
1708 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001709 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001710 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001711 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001712
1713 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1714 PSA_KEY_TYPE_ECC_GET_FAMILY(
1715 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001716 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001717 data,
1718 data_size,
1719 data_length );
1720
Steven Cooremana2371e52020-07-28 14:30:39 +02001721 mbedtls_ecp_keypair_free( ecp );
1722 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001723 return( status );
1724#else
1725 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001726 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001727#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1728 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001729 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001730 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001731 else
1732 {
1733 /* This shouldn't happen in the reference implementation, but
1734 it is valid for a special-purpose implementation to omit
1735 support for exporting certain key types. */
1736 return( PSA_ERROR_NOT_SUPPORTED );
1737 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001738}
1739
Gilles Peskinec5487a82018-12-03 18:08:14 +01001740psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001741 uint8_t *data,
1742 size_t data_size,
1743 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001744{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001745 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001746 psa_status_t status;
1747
1748 /* Set the key to empty now, so that even when there are errors, we always
1749 * set data_length to a value between 0 and data_size. On error, setting
1750 * the key to empty is a good choice because an empty key representation is
1751 * unlikely to be accepted anywhere. */
1752 *data_length = 0;
1753
1754 /* Export requires the EXPORT flag. There is an exception for public keys,
1755 * which don't require any flag, but psa_get_key_from_slot takes
1756 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001757 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001758 if( status != PSA_SUCCESS )
1759 return( status );
1760 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001761 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001762}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001763
Gilles Peskinec5487a82018-12-03 18:08:14 +01001764psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001765 uint8_t *data,
1766 size_t data_size,
1767 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001768{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001769 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001770 psa_status_t status;
1771
1772 /* Set the key to empty now, so that even when there are errors, we always
1773 * set data_length to a value between 0 and data_size. On error, setting
1774 * the key to empty is a good choice because an empty key representation is
1775 * unlikely to be accepted anywhere. */
1776 *data_length = 0;
1777
1778 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001779 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001780 if( status != PSA_SUCCESS )
1781 return( status );
1782 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001783 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001784}
1785
Gilles Peskine91e8c332019-08-02 19:19:39 +02001786#if defined(static_assert)
1787static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1788 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001789static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001790 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001791static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001792 "One or more key attribute flag is listed as both internal-only and external-only" );
1793#endif
1794
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001795/** Validate that a key policy is internally well-formed.
1796 *
1797 * This function only rejects invalid policies. It does not validate the
1798 * consistency of the policy with respect to other attributes of the key
1799 * such as the key type.
1800 */
1801static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001802{
1803 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001804 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 PSA_KEY_USAGE_ENCRYPT |
1806 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001807 PSA_KEY_USAGE_SIGN_HASH |
1808 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001809 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1810 return( PSA_ERROR_INVALID_ARGUMENT );
1811
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 return( PSA_SUCCESS );
1813}
1814
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001815/** Validate the internal consistency of key attributes.
1816 *
1817 * This function only rejects invalid attribute values. If does not
1818 * validate the consistency of the attributes with any key data that may
1819 * be involved in the creation of the key.
1820 *
1821 * Call this function early in the key creation process.
1822 *
1823 * \param[in] attributes Key attributes for the new key.
1824 * \param[out] p_drv On any return, the driver for the key, if any.
1825 * NULL for a transparent key.
1826 *
1827 */
1828static psa_status_t psa_validate_key_attributes(
1829 const psa_key_attributes_t *attributes,
1830 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001831{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001832 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001833
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001834 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1835 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001836 if( status != PSA_SUCCESS )
1837 return( status );
1838
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001839 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1840 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001841 if( status != PSA_SUCCESS )
1842 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001843
1844 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001845 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001846 return( status );
1847
1848 /* Refuse to create overly large keys.
1849 * Note that this doesn't trigger on import if the attributes don't
1850 * explicitly specify a size (so psa_get_key_bits returns 0), so
1851 * psa_import_key() needs its own checks. */
1852 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1853 return( PSA_ERROR_NOT_SUPPORTED );
1854
Gilles Peskine91e8c332019-08-02 19:19:39 +02001855 /* Reject invalid flags. These should not be reachable through the API. */
1856 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1857 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1858 return( PSA_ERROR_INVALID_ARGUMENT );
1859
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001860 return( PSA_SUCCESS );
1861}
1862
Gilles Peskine4747d192019-04-17 15:05:45 +02001863/** Prepare a key slot to receive key material.
1864 *
1865 * This function allocates a key slot and sets its metadata.
1866 *
1867 * If this function fails, call psa_fail_key_creation().
1868 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001869 * This function is intended to be used as follows:
1870 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1871 * it with the specified attributes, and assign it a handle.
1872 * -# Populate the slot with the key material.
1873 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1874 * In case of failure at any step, stop the sequence and call
1875 * psa_fail_key_creation().
1876 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001877 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 * \param[in] attributes Key attributes for the new key.
1879 * \param[out] handle On success, a handle for the allocated slot.
1880 * \param[out] p_slot On success, a pointer to the prepared slot.
1881 * \param[out] p_drv On any return, the driver for the key, if any.
1882 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001883 *
1884 * \retval #PSA_SUCCESS
1885 * The key slot is ready to receive key material.
1886 * \return If this function fails, the key slot is an invalid state.
1887 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001888 */
1889static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001890 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 const psa_key_attributes_t *attributes,
1892 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001893 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001894 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001895{
1896 psa_status_t status;
1897 psa_key_slot_t *slot;
1898
Gilles Peskinedf179142019-07-15 22:02:14 +02001899 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001900 *p_drv = NULL;
1901
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001902 status = psa_validate_key_attributes( attributes, p_drv );
1903 if( status != PSA_SUCCESS )
1904 return( status );
1905
Gilles Peskineedbed562019-08-07 18:19:59 +02001906 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 if( status != PSA_SUCCESS )
1908 return( status );
1909 slot = *p_slot;
1910
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001911 /* We're storing the declared bit-size of the key. It's up to each
1912 * creation mechanism to verify that this information is correct.
1913 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001914 * an input (generate, device) but not for those where the bit-size
1915 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001916
1917 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001918
Gilles Peskine91e8c332019-08-02 19:19:39 +02001919 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001920 * external-only flags, query `attributes`. Thanks to the check
1921 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001922 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001923 * may have set. */
1924 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001925
Gilles Peskinecbaff462019-07-12 23:46:04 +02001926#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001927 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001928 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001929 * create the key file in internal storage, create the
1930 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001931 * persistent data. This is done by starting a transaction that will
1932 * encompass these three actions.
1933 * For registering a volatile key, we just need to find an appropriate
1934 * slot number inside the SE. Since the key is designated volatile, creating
1935 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001936 /* The first thing to do is to find a slot number for the new key.
1937 * We save the slot number in persistent storage as part of the
1938 * transaction data. It will be needed to recover if the power
1939 * fails during the key creation process, to clean up on the secure
1940 * element side after restarting. Obtaining a slot number from the
1941 * secure element driver updates its persistent state, but we do not yet
1942 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001943 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001944 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001945 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001946 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001947 &slot->data.se.slot_number );
1948 if( status != PSA_SUCCESS )
1949 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001950
1951 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001952 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001953 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1954 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1955 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1956 psa_crypto_transaction.key.id = slot->attr.id;
1957 status = psa_crypto_save_transaction( );
1958 if( status != PSA_SUCCESS )
1959 {
1960 (void) psa_crypto_stop_transaction( );
1961 return( status );
1962 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001963 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001964 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001965
1966 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1967 {
1968 /* Key registration only makes sense with a secure element. */
1969 return( PSA_ERROR_INVALID_ARGUMENT );
1970 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001971#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1972
Darryl Green0c6575a2018-11-07 16:05:30 +00001973 return( status );
1974}
Gilles Peskine4747d192019-04-17 15:05:45 +02001975
1976/** Finalize the creation of a key once its key material has been set.
1977 *
1978 * This entails writing the key to persistent storage.
1979 *
1980 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001981 * See the documentation of psa_start_key_creation() for the intended use
1982 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001983 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001984 * \param[in,out] slot Pointer to the slot with key material.
1985 * \param[in] driver The secure element driver for the key,
1986 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001987 *
1988 * \retval #PSA_SUCCESS
1989 * The key was successfully created. The handle is now valid.
1990 * \return If this function fails, the key slot is an invalid state.
1991 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001992 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001993static psa_status_t psa_finish_key_creation(
1994 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001995 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001996{
1997 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001998 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001999 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002000
2001#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002002 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002003 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002004#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2005 if( driver != NULL )
2006 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002007 psa_se_key_data_storage_t data;
2008#if defined(static_assert)
2009 static_assert( sizeof( slot->data.se.slot_number ) ==
2010 sizeof( data.slot_number ),
2011 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002012#endif
2013 memcpy( &data.slot_number, &slot->data.se.slot_number,
2014 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002015 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002016 (uint8_t*) &data,
2017 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002018 }
2019 else
2020#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2021 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002022 /* Key material is saved in export representation in the slot, so
2023 * just pass the slot buffer for storage. */
2024 status = psa_save_persistent_key( &slot->attr,
2025 slot->data.key.data,
2026 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002027 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002028 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002029#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2030
Gilles Peskinecbaff462019-07-12 23:46:04 +02002031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002032 /* Finish the transaction for a key creation. This does not
2033 * happen when registering an existing key. Detect this case
2034 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002035 * creation of a persistent key in a secure element requires a transaction,
2036 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002037 if( driver != NULL &&
2038 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002039 {
2040 status = psa_save_se_persistent_data( driver );
2041 if( status != PSA_SUCCESS )
2042 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002043 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002044 return( status );
2045 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002046 status = psa_crypto_stop_transaction( );
2047 if( status != PSA_SUCCESS )
2048 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002049 }
2050#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2051
Gilles Peskine4747d192019-04-17 15:05:45 +02002052 return( status );
2053}
2054
2055/** Abort the creation of a key.
2056 *
2057 * You may call this function after calling psa_start_key_creation(),
2058 * or after psa_finish_key_creation() fails. In other circumstances, this
2059 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002060 * See the documentation of psa_start_key_creation() for the intended use
2061 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002062 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002063 * \param[in,out] slot Pointer to the slot with key material.
2064 * \param[in] driver The secure element driver for the key,
2065 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002066 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002067static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002068 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002069{
Gilles Peskine011e4282019-06-26 18:34:38 +02002070 (void) driver;
2071
Gilles Peskine4747d192019-04-17 15:05:45 +02002072 if( slot == NULL )
2073 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002074
2075#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002076 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002077 * element, and the failure happened later (when saving metadata
2078 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002079 * element.
2080 * https://github.com/ARMmbed/mbed-crypto/issues/217
2081 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002082
Gilles Peskined7729582019-08-05 15:55:54 +02002083 /* Abort the ongoing transaction if any (there may not be one if
2084 * the creation process failed before starting one, or if the
2085 * key creation is a registration of a key in a secure element).
2086 * Earlier functions must already have done what it takes to undo any
2087 * partial creation. All that's left is to update the transaction data
2088 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002089 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002090#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2091
Gilles Peskine4747d192019-04-17 15:05:45 +02002092 psa_wipe_key_slot( slot );
2093}
2094
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002095/** Validate optional attributes during key creation.
2096 *
2097 * Some key attributes are optional during key creation. If they are
2098 * specified in the attributes structure, check that they are consistent
2099 * with the data in the slot.
2100 *
2101 * This function should be called near the end of key creation, after
2102 * the slot in memory is fully populated but before saving persistent data.
2103 */
2104static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105 const psa_key_slot_t *slot,
2106 const psa_key_attributes_t *attributes )
2107{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002108 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002110 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111 return( PSA_ERROR_INVALID_ARGUMENT );
2112 }
2113
2114 if( attributes->domain_parameters_size != 0 )
2115 {
John Durkop0e005192020-10-31 22:06:54 -07002116#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2117 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002118 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002119 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002120 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002121 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002123
Steven Cooreman7f391872020-07-30 14:57:44 +02002124 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002125 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002126 slot->data.key.data,
2127 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002128 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002129 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002130 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002131
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002132 mbedtls_mpi_init( &actual );
2133 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002134 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002135 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002136 mbedtls_rsa_free( rsa );
2137 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002138 if( ret != 0 )
2139 goto rsa_exit;
2140 ret = mbedtls_mpi_read_binary( &required,
2141 attributes->domain_parameters,
2142 attributes->domain_parameters_size );
2143 if( ret != 0 )
2144 goto rsa_exit;
2145 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2146 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2147 rsa_exit:
2148 mbedtls_mpi_free( &actual );
2149 mbedtls_mpi_free( &required );
2150 if( ret != 0)
2151 return( mbedtls_to_psa_error( ret ) );
2152 }
2153 else
John Durkop9814fa22020-11-04 12:28:15 -08002154#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2155 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002156 {
2157 return( PSA_ERROR_INVALID_ARGUMENT );
2158 }
2159 }
2160
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002161 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002163 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002164 return( PSA_ERROR_INVALID_ARGUMENT );
2165 }
2166
2167 return( PSA_SUCCESS );
2168}
2169
Gilles Peskine4747d192019-04-17 15:05:45 +02002170psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002171 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002172 size_t data_length,
2173 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002174{
2175 psa_status_t status;
2176 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002177 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002178
Gilles Peskine0f84d622019-09-12 19:03:13 +02002179 /* Reject zero-length symmetric keys (including raw data key objects).
2180 * This also rejects any key which might be encoded as an empty string,
2181 * which is never valid. */
2182 if( data_length == 0 )
2183 return( PSA_ERROR_INVALID_ARGUMENT );
2184
Gilles Peskinedf179142019-07-15 22:02:14 +02002185 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2186 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002187 if( status != PSA_SUCCESS )
2188 goto exit;
2189
Gilles Peskine5d309672019-07-12 23:47:28 +02002190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2191 if( driver != NULL )
2192 {
2193 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002194 /* The driver should set the number of key bits, however in
2195 * case it doesn't, we initialize bits to an invalid value. */
2196 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002197 if( drv->key_management == NULL ||
2198 drv->key_management->p_import == NULL )
2199 {
2200 status = PSA_ERROR_NOT_SUPPORTED;
2201 goto exit;
2202 }
2203 status = drv->key_management->p_import(
2204 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002205 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002206 &bits );
2207 if( status != PSA_SUCCESS )
2208 goto exit;
2209 if( bits > PSA_MAX_KEY_BITS )
2210 {
2211 status = PSA_ERROR_NOT_SUPPORTED;
2212 goto exit;
2213 }
2214 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002215 }
2216 else
2217#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2218 {
2219 status = psa_import_key_into_slot( slot, data, data_length );
2220 if( status != PSA_SUCCESS )
2221 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002222 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002223 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002224 if( status != PSA_SUCCESS )
2225 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002226
Gilles Peskine011e4282019-06-26 18:34:38 +02002227 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002228exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002229 if( status != PSA_SUCCESS )
2230 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002231 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002232 *handle = 0;
2233 }
2234 return( status );
2235}
2236
Gilles Peskined7729582019-08-05 15:55:54 +02002237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2238psa_status_t mbedtls_psa_register_se_key(
2239 const psa_key_attributes_t *attributes )
2240{
2241 psa_status_t status;
2242 psa_key_slot_t *slot = NULL;
2243 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002244 psa_key_handle_t handle = 0;
2245
2246 /* Leaving attributes unspecified is not currently supported.
2247 * It could make sense to query the key type and size from the
2248 * secure element, but not all secure elements support this
2249 * and the driver HAL doesn't currently support it. */
2250 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2251 return( PSA_ERROR_NOT_SUPPORTED );
2252 if( psa_get_key_bits( attributes ) == 0 )
2253 return( PSA_ERROR_NOT_SUPPORTED );
2254
2255 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2256 &handle, &slot, &driver );
2257 if( status != PSA_SUCCESS )
2258 goto exit;
2259
Gilles Peskined7729582019-08-05 15:55:54 +02002260 status = psa_finish_key_creation( slot, driver );
2261
2262exit:
2263 if( status != PSA_SUCCESS )
2264 {
2265 psa_fail_key_creation( slot, driver );
2266 }
2267 /* Registration doesn't keep the key in RAM. */
2268 psa_close_key( handle );
2269 return( status );
2270}
2271#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2272
Gilles Peskinef603c712019-01-19 13:40:11 +01002273static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002274 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002275{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002276 psa_status_t status = psa_copy_key_material_into_slot( target,
2277 source->data.key.data,
2278 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002279 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002280 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002281
Steven Cooreman398aee52020-10-13 14:35:45 +02002282 target->attr.type = source->attr.type;
2283 target->attr.bits = source->attr.bits;
2284
2285 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002286}
2287
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002288psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2289 const psa_key_attributes_t *specified_attributes,
2290 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002291{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002292 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002293 psa_key_slot_t *source_slot = NULL;
2294 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002295 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002296 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002297
Gilles Peskine28f8f302019-07-24 13:30:31 +02002298 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002299 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002300 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002301 goto exit;
2302
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002303 status = psa_validate_optional_attributes( source_slot,
2304 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002305 if( status != PSA_SUCCESS )
2306 goto exit;
2307
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002308 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002309 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002310 if( status != PSA_SUCCESS )
2311 goto exit;
2312
Gilles Peskinedf179142019-07-15 22:02:14 +02002313 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2314 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002315 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002316 if( status != PSA_SUCCESS )
2317 goto exit;
2318
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2320 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002321 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002322 /* Copying to a secure element is not implemented yet. */
2323 status = PSA_ERROR_NOT_SUPPORTED;
2324 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002325 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002327
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002328 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002329 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002330 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002331
Gilles Peskine011e4282019-06-26 18:34:38 +02002332 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002333exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002334 if( status != PSA_SUCCESS )
2335 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002336 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002337 *target_handle = 0;
2338 }
2339 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002340}
2341
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002342
2343
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002344/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002345/* Message digests */
2346/****************************************************************/
2347
John Durkop07cc04a2020-11-16 22:08:34 -08002348#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002349 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2350 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002351 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002352static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002353{
2354 switch( alg )
2355 {
2356#if defined(MBEDTLS_MD2_C)
2357 case PSA_ALG_MD2:
2358 return( &mbedtls_md2_info );
2359#endif
2360#if defined(MBEDTLS_MD4_C)
2361 case PSA_ALG_MD4:
2362 return( &mbedtls_md4_info );
2363#endif
2364#if defined(MBEDTLS_MD5_C)
2365 case PSA_ALG_MD5:
2366 return( &mbedtls_md5_info );
2367#endif
2368#if defined(MBEDTLS_RIPEMD160_C)
2369 case PSA_ALG_RIPEMD160:
2370 return( &mbedtls_ripemd160_info );
2371#endif
2372#if defined(MBEDTLS_SHA1_C)
2373 case PSA_ALG_SHA_1:
2374 return( &mbedtls_sha1_info );
2375#endif
2376#if defined(MBEDTLS_SHA256_C)
2377 case PSA_ALG_SHA_224:
2378 return( &mbedtls_sha224_info );
2379 case PSA_ALG_SHA_256:
2380 return( &mbedtls_sha256_info );
2381#endif
2382#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002383#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002384 case PSA_ALG_SHA_384:
2385 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002386#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002387 case PSA_ALG_SHA_512:
2388 return( &mbedtls_sha512_info );
2389#endif
2390 default:
2391 return( NULL );
2392 }
2393}
John Durkop07cc04a2020-11-16 22:08:34 -08002394#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002395 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2396 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2397 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002398
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2400{
2401 switch( operation->alg )
2402 {
Gilles Peskine81736312018-06-26 15:04:31 +02002403 case 0:
2404 /* The object has (apparently) been initialized but it is not
2405 * in use. It's ok to call abort on such an object, and there's
2406 * nothing to do. */
2407 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408#if defined(MBEDTLS_MD2_C)
2409 case PSA_ALG_MD2:
2410 mbedtls_md2_free( &operation->ctx.md2 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_MD4_C)
2414 case PSA_ALG_MD4:
2415 mbedtls_md4_free( &operation->ctx.md4 );
2416 break;
2417#endif
2418#if defined(MBEDTLS_MD5_C)
2419 case PSA_ALG_MD5:
2420 mbedtls_md5_free( &operation->ctx.md5 );
2421 break;
2422#endif
2423#if defined(MBEDTLS_RIPEMD160_C)
2424 case PSA_ALG_RIPEMD160:
2425 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2426 break;
2427#endif
2428#if defined(MBEDTLS_SHA1_C)
2429 case PSA_ALG_SHA_1:
2430 mbedtls_sha1_free( &operation->ctx.sha1 );
2431 break;
2432#endif
2433#if defined(MBEDTLS_SHA256_C)
2434 case PSA_ALG_SHA_224:
2435 case PSA_ALG_SHA_256:
2436 mbedtls_sha256_free( &operation->ctx.sha256 );
2437 break;
2438#endif
2439#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002440#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002441 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002442#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002443 case PSA_ALG_SHA_512:
2444 mbedtls_sha512_free( &operation->ctx.sha512 );
2445 break;
2446#endif
2447 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002448 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002449 }
2450 operation->alg = 0;
2451 return( PSA_SUCCESS );
2452}
2453
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002454psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002455 psa_algorithm_t alg )
2456{
Janos Follath24eed8d2019-11-22 13:21:35 +00002457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002458
2459 /* A context must be freshly initialized before it can be set up. */
2460 if( operation->alg != 0 )
2461 {
2462 return( PSA_ERROR_BAD_STATE );
2463 }
2464
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002465 switch( alg )
2466 {
2467#if defined(MBEDTLS_MD2_C)
2468 case PSA_ALG_MD2:
2469 mbedtls_md2_init( &operation->ctx.md2 );
2470 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2471 break;
2472#endif
2473#if defined(MBEDTLS_MD4_C)
2474 case PSA_ALG_MD4:
2475 mbedtls_md4_init( &operation->ctx.md4 );
2476 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2477 break;
2478#endif
2479#if defined(MBEDTLS_MD5_C)
2480 case PSA_ALG_MD5:
2481 mbedtls_md5_init( &operation->ctx.md5 );
2482 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2483 break;
2484#endif
2485#if defined(MBEDTLS_RIPEMD160_C)
2486 case PSA_ALG_RIPEMD160:
2487 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2488 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2489 break;
2490#endif
2491#if defined(MBEDTLS_SHA1_C)
2492 case PSA_ALG_SHA_1:
2493 mbedtls_sha1_init( &operation->ctx.sha1 );
2494 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2495 break;
2496#endif
2497#if defined(MBEDTLS_SHA256_C)
2498 case PSA_ALG_SHA_224:
2499 mbedtls_sha256_init( &operation->ctx.sha256 );
2500 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2501 break;
2502 case PSA_ALG_SHA_256:
2503 mbedtls_sha256_init( &operation->ctx.sha256 );
2504 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2505 break;
2506#endif
2507#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002508#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509 case PSA_ALG_SHA_384:
2510 mbedtls_sha512_init( &operation->ctx.sha512 );
2511 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2512 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002513#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514 case PSA_ALG_SHA_512:
2515 mbedtls_sha512_init( &operation->ctx.sha512 );
2516 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2517 break;
2518#endif
2519 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002520 return( PSA_ALG_IS_HASH( alg ) ?
2521 PSA_ERROR_NOT_SUPPORTED :
2522 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002523 }
2524 if( ret == 0 )
2525 operation->alg = alg;
2526 else
2527 psa_hash_abort( operation );
2528 return( mbedtls_to_psa_error( ret ) );
2529}
2530
2531psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2532 const uint8_t *input,
2533 size_t input_length )
2534{
Janos Follath24eed8d2019-11-22 13:21:35 +00002535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002536
2537 /* Don't require hash implementations to behave correctly on a
2538 * zero-length input, which may have an invalid pointer. */
2539 if( input_length == 0 )
2540 return( PSA_SUCCESS );
2541
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542 switch( operation->alg )
2543 {
2544#if defined(MBEDTLS_MD2_C)
2545 case PSA_ALG_MD2:
2546 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2547 input, input_length );
2548 break;
2549#endif
2550#if defined(MBEDTLS_MD4_C)
2551 case PSA_ALG_MD4:
2552 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2553 input, input_length );
2554 break;
2555#endif
2556#if defined(MBEDTLS_MD5_C)
2557 case PSA_ALG_MD5:
2558 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2559 input, input_length );
2560 break;
2561#endif
2562#if defined(MBEDTLS_RIPEMD160_C)
2563 case PSA_ALG_RIPEMD160:
2564 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2565 input, input_length );
2566 break;
2567#endif
2568#if defined(MBEDTLS_SHA1_C)
2569 case PSA_ALG_SHA_1:
2570 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2571 input, input_length );
2572 break;
2573#endif
2574#if defined(MBEDTLS_SHA256_C)
2575 case PSA_ALG_SHA_224:
2576 case PSA_ALG_SHA_256:
2577 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2578 input, input_length );
2579 break;
2580#endif
2581#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002582#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002584#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002585 case PSA_ALG_SHA_512:
2586 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2587 input, input_length );
2588 break;
2589#endif
2590 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002591 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002592 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002593
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 if( ret != 0 )
2595 psa_hash_abort( operation );
2596 return( mbedtls_to_psa_error( ret ) );
2597}
2598
2599psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2600 uint8_t *hash,
2601 size_t hash_size,
2602 size_t *hash_length )
2603{
itayzafrir40835d42018-08-02 13:14:17 +03002604 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002606 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002607
2608 /* Fill the output buffer with something that isn't a valid hash
2609 * (barring an attack on the hash and deliberately-crafted input),
2610 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002611 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002612 /* If hash_size is 0 then hash may be NULL and then the
2613 * call to memset would have undefined behavior. */
2614 if( hash_size != 0 )
2615 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002616
2617 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002618 {
2619 status = PSA_ERROR_BUFFER_TOO_SMALL;
2620 goto exit;
2621 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002622
2623 switch( operation->alg )
2624 {
2625#if defined(MBEDTLS_MD2_C)
2626 case PSA_ALG_MD2:
2627 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2628 break;
2629#endif
2630#if defined(MBEDTLS_MD4_C)
2631 case PSA_ALG_MD4:
2632 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2633 break;
2634#endif
2635#if defined(MBEDTLS_MD5_C)
2636 case PSA_ALG_MD5:
2637 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2638 break;
2639#endif
2640#if defined(MBEDTLS_RIPEMD160_C)
2641 case PSA_ALG_RIPEMD160:
2642 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2643 break;
2644#endif
2645#if defined(MBEDTLS_SHA1_C)
2646 case PSA_ALG_SHA_1:
2647 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2648 break;
2649#endif
2650#if defined(MBEDTLS_SHA256_C)
2651 case PSA_ALG_SHA_224:
2652 case PSA_ALG_SHA_256:
2653 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2654 break;
2655#endif
2656#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002657#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002658 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002659#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002660 case PSA_ALG_SHA_512:
2661 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2662 break;
2663#endif
2664 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002665 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002666 }
itayzafrir40835d42018-08-02 13:14:17 +03002667 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002668
itayzafrir40835d42018-08-02 13:14:17 +03002669exit:
2670 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002671 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002672 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002673 return( psa_hash_abort( operation ) );
2674 }
2675 else
2676 {
2677 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002678 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002679 }
2680}
2681
Gilles Peskine2d277862018-06-18 15:41:12 +02002682psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2683 const uint8_t *hash,
2684 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002685{
2686 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2687 size_t actual_hash_length;
2688 psa_status_t status = psa_hash_finish( operation,
2689 actual_hash, sizeof( actual_hash ),
2690 &actual_hash_length );
2691 if( status != PSA_SUCCESS )
2692 return( status );
2693 if( actual_hash_length != hash_length )
2694 return( PSA_ERROR_INVALID_SIGNATURE );
2695 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2696 return( PSA_ERROR_INVALID_SIGNATURE );
2697 return( PSA_SUCCESS );
2698}
2699
Gilles Peskine0a749c82019-11-28 19:33:58 +01002700psa_status_t psa_hash_compute( psa_algorithm_t alg,
2701 const uint8_t *input, size_t input_length,
2702 uint8_t *hash, size_t hash_size,
2703 size_t *hash_length )
2704{
2705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2707
2708 *hash_length = hash_size;
2709 status = psa_hash_setup( &operation, alg );
2710 if( status != PSA_SUCCESS )
2711 goto exit;
2712 status = psa_hash_update( &operation, input, input_length );
2713 if( status != PSA_SUCCESS )
2714 goto exit;
2715 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2716 if( status != PSA_SUCCESS )
2717 goto exit;
2718
2719exit:
2720 if( status == PSA_SUCCESS )
2721 status = psa_hash_abort( &operation );
2722 else
2723 psa_hash_abort( &operation );
2724 return( status );
2725}
2726
2727psa_status_t psa_hash_compare( psa_algorithm_t alg,
2728 const uint8_t *input, size_t input_length,
2729 const uint8_t *hash, size_t hash_length )
2730{
2731 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2733
2734 status = psa_hash_setup( &operation, alg );
2735 if( status != PSA_SUCCESS )
2736 goto exit;
2737 status = psa_hash_update( &operation, input, input_length );
2738 if( status != PSA_SUCCESS )
2739 goto exit;
2740 status = psa_hash_verify( &operation, hash, hash_length );
2741 if( status != PSA_SUCCESS )
2742 goto exit;
2743
2744exit:
2745 if( status == PSA_SUCCESS )
2746 status = psa_hash_abort( &operation );
2747 else
2748 psa_hash_abort( &operation );
2749 return( status );
2750}
2751
Gilles Peskineeb35d782019-01-22 17:56:16 +01002752psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2753 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002754{
2755 if( target_operation->alg != 0 )
2756 return( PSA_ERROR_BAD_STATE );
2757
2758 switch( source_operation->alg )
2759 {
2760 case 0:
2761 return( PSA_ERROR_BAD_STATE );
2762#if defined(MBEDTLS_MD2_C)
2763 case PSA_ALG_MD2:
2764 mbedtls_md2_clone( &target_operation->ctx.md2,
2765 &source_operation->ctx.md2 );
2766 break;
2767#endif
2768#if defined(MBEDTLS_MD4_C)
2769 case PSA_ALG_MD4:
2770 mbedtls_md4_clone( &target_operation->ctx.md4,
2771 &source_operation->ctx.md4 );
2772 break;
2773#endif
2774#if defined(MBEDTLS_MD5_C)
2775 case PSA_ALG_MD5:
2776 mbedtls_md5_clone( &target_operation->ctx.md5,
2777 &source_operation->ctx.md5 );
2778 break;
2779#endif
2780#if defined(MBEDTLS_RIPEMD160_C)
2781 case PSA_ALG_RIPEMD160:
2782 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2783 &source_operation->ctx.ripemd160 );
2784 break;
2785#endif
2786#if defined(MBEDTLS_SHA1_C)
2787 case PSA_ALG_SHA_1:
2788 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2789 &source_operation->ctx.sha1 );
2790 break;
2791#endif
2792#if defined(MBEDTLS_SHA256_C)
2793 case PSA_ALG_SHA_224:
2794 case PSA_ALG_SHA_256:
2795 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2796 &source_operation->ctx.sha256 );
2797 break;
2798#endif
2799#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002800#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002801 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002802#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002803 case PSA_ALG_SHA_512:
2804 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2805 &source_operation->ctx.sha512 );
2806 break;
2807#endif
2808 default:
2809 return( PSA_ERROR_NOT_SUPPORTED );
2810 }
2811
2812 target_operation->alg = source_operation->alg;
2813 return( PSA_SUCCESS );
2814}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002815
2816
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002817/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818/* MAC */
2819/****************************************************************/
2820
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002821static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822 psa_algorithm_t alg,
2823 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002824 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002825 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002828 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002830 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002831 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002832
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2834 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002835 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002836 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002837 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002838 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839 mode = MBEDTLS_MODE_STREAM;
2840 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841 case PSA_ALG_CTR:
2842 mode = MBEDTLS_MODE_CTR;
2843 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002844 case PSA_ALG_CFB:
2845 mode = MBEDTLS_MODE_CFB;
2846 break;
2847 case PSA_ALG_OFB:
2848 mode = MBEDTLS_MODE_OFB;
2849 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002850 case PSA_ALG_ECB_NO_PADDING:
2851 mode = MBEDTLS_MODE_ECB;
2852 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002853 case PSA_ALG_CBC_NO_PADDING:
2854 mode = MBEDTLS_MODE_CBC;
2855 break;
2856 case PSA_ALG_CBC_PKCS7:
2857 mode = MBEDTLS_MODE_CBC;
2858 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002859 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 mode = MBEDTLS_MODE_CCM;
2861 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002862 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002863 mode = MBEDTLS_MODE_GCM;
2864 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002865 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2866 mode = MBEDTLS_MODE_CHACHAPOLY;
2867 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868 default:
2869 return( NULL );
2870 }
2871 }
2872 else if( alg == PSA_ALG_CMAC )
2873 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 else
2875 return( NULL );
2876
2877 switch( key_type )
2878 {
2879 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002880 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881 break;
2882 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002883 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2884 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002886 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002887 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002888 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002889 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2890 * but two-key Triple-DES is functionally three-key Triple-DES
2891 * with K1=K3, so that's how we present it to mbedtls. */
2892 if( key_bits == 128 )
2893 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 break;
2895 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002896 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897 break;
2898 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002899 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002901 case PSA_KEY_TYPE_CHACHA20:
2902 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2903 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904 default:
2905 return( NULL );
2906 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002907 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002908 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909
Jaeden Amero23bbb752018-06-26 14:16:54 +01002910 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2911 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912}
2913
John Durkop6ba40d12020-11-10 08:50:04 -08002914#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002915static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002916{
Gilles Peskine2d277862018-06-18 15:41:12 +02002917 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002918 {
2919 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002920 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002921 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002922 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002923 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002924 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002925 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002926 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002927 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002928 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002929 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002930 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002931 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002932 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002933 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002934 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002935 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002936 return( 128 );
2937 default:
2938 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002939 }
2940}
John Durkop07cc04a2020-11-16 22:08:34 -08002941#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002942
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002943/* Initialize the MAC operation structure. Once this function has been
2944 * called, psa_mac_abort can run and will do the right thing. */
2945static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2946 psa_algorithm_t alg )
2947{
2948 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2949
2950 operation->alg = alg;
2951 operation->key_set = 0;
2952 operation->iv_set = 0;
2953 operation->iv_required = 0;
2954 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002955 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002956
2957#if defined(MBEDTLS_CMAC_C)
2958 if( alg == PSA_ALG_CMAC )
2959 {
2960 operation->iv_required = 0;
2961 mbedtls_cipher_init( &operation->ctx.cmac );
2962 status = PSA_SUCCESS;
2963 }
2964 else
2965#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002966#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002967 if( PSA_ALG_IS_HMAC( operation->alg ) )
2968 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002969 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2970 operation->ctx.hmac.hash_ctx.alg = 0;
2971 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002972 }
2973 else
John Durkopd0321952020-10-29 21:37:36 -07002974#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002975 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002976 if( ! PSA_ALG_IS_MAC( alg ) )
2977 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978 }
2979
2980 if( status != PSA_SUCCESS )
2981 memset( operation, 0, sizeof( *operation ) );
2982 return( status );
2983}
2984
John Durkop6ba40d12020-11-10 08:50:04 -08002985#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002986static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2987{
Gilles Peskine3f108122018-12-07 18:14:53 +01002988 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002989 return( psa_hash_abort( &hmac->hash_ctx ) );
2990}
John Durkop6ba40d12020-11-10 08:50:04 -08002991#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002992
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2994{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002995 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002997 /* The object has (apparently) been initialized but it is not
2998 * in use. It's ok to call abort on such an object, and there's
2999 * nothing to do. */
3000 return( PSA_SUCCESS );
3001 }
3002 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003004 if( operation->alg == PSA_ALG_CMAC )
3005 {
3006 mbedtls_cipher_free( &operation->ctx.cmac );
3007 }
3008 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003010#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003011 if( PSA_ALG_IS_HMAC( operation->alg ) )
3012 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003013 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003014 }
3015 else
John Durkopd0321952020-10-29 21:37:36 -07003016#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003017 {
3018 /* Sanity check (shouldn't happen: operation->alg should
3019 * always have been initialized to a valid value). */
3020 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 }
Moran Peker41deec42018-04-04 15:43:05 +03003022
Gilles Peskine8c9def32018-02-08 10:02:12 +01003023 operation->alg = 0;
3024 operation->key_set = 0;
3025 operation->iv_set = 0;
3026 operation->iv_required = 0;
3027 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003028 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003029
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031
3032bad_state:
3033 /* If abort is called on an uninitialized object, we can't trust
3034 * anything. Wipe the object in case it contains confidential data.
3035 * This may result in a memory leak if a pointer gets overwritten,
3036 * but it's too late to do anything about this. */
3037 memset( operation, 0, sizeof( *operation ) );
3038 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039}
3040
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003041#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003042static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003043 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003044 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003045 const mbedtls_cipher_info_t *cipher_info )
3046{
Janos Follath24eed8d2019-11-22 13:21:35 +00003047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003048
3049 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003050
3051 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3052 if( ret != 0 )
3053 return( ret );
3054
3055 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003056 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003057 key_bits );
3058 return( ret );
3059}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003060#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003061
John Durkop6ba40d12020-11-10 08:50:04 -08003062#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003063static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3064 const uint8_t *key,
3065 size_t key_length,
3066 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003067{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003068 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003069 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003070 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003071 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003072 psa_status_t status;
3073
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003074 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3075 * overflow below. This should never trigger if the hash algorithm
3076 * is implemented correctly. */
3077 /* The size checks against the ipad and opad buffers cannot be written
3078 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3079 * because that triggers -Wlogical-op on GCC 7.3. */
3080 if( block_size > sizeof( ipad ) )
3081 return( PSA_ERROR_NOT_SUPPORTED );
3082 if( block_size > sizeof( hmac->opad ) )
3083 return( PSA_ERROR_NOT_SUPPORTED );
3084 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003085 return( PSA_ERROR_NOT_SUPPORTED );
3086
Gilles Peskined223b522018-06-11 18:12:58 +02003087 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003088 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003089 status = psa_hash_compute( hash_alg, key, key_length,
3090 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003091 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003092 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003093 }
Gilles Peskine96889972018-07-12 17:07:03 +02003094 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3095 * but it is permitted. It is common when HMAC is used in HKDF, for
3096 * example. Don't call `memcpy` in the 0-length because `key` could be
3097 * an invalid pointer which would make the behavior undefined. */
3098 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003099 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003100
Gilles Peskined223b522018-06-11 18:12:58 +02003101 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3102 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003103 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003104 ipad[i] ^= 0x36;
3105 memset( ipad + key_length, 0x36, block_size - key_length );
3106
3107 /* Copy the key material from ipad to opad, flipping the requisite bits,
3108 * and filling the rest of opad with the requisite constant. */
3109 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003110 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3111 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003112
Gilles Peskine01126fa2018-07-12 17:04:55 +02003113 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003114 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003115 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003116
Gilles Peskine01126fa2018-07-12 17:04:55 +02003117 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003118
3119cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003120 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003121
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122 return( status );
3123}
John Durkop6ba40d12020-11-10 08:50:04 -08003124#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003125
Gilles Peskine89167cb2018-07-08 20:12:23 +02003126static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003127 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003128 psa_algorithm_t alg,
3129 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003131 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003132 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003133 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003134 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003135 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003136 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003137 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003138
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003139 /* A context must be freshly initialized before it can be set up. */
3140 if( operation->alg != 0 )
3141 {
3142 return( PSA_ERROR_BAD_STATE );
3143 }
3144
Gilles Peskined911eb72018-08-14 15:18:45 +02003145 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146 if( status != PSA_SUCCESS )
3147 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003148 if( is_sign )
3149 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003150
Gilles Peskine28f8f302019-07-24 13:30:31 +02003151 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003152 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003153 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003154 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003155
Gilles Peskine8c9def32018-02-08 10:02:12 +01003156#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003157 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003158 {
3159 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003160 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003161 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003163 if( cipher_info == NULL )
3164 {
3165 status = PSA_ERROR_NOT_SUPPORTED;
3166 goto exit;
3167 }
3168 operation->mac_size = cipher_info->block_size;
3169 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3170 status = mbedtls_to_psa_error( ret );
3171 }
3172 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003174#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003175 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003176 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003177 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003178 if( hash_alg == 0 )
3179 {
3180 status = PSA_ERROR_NOT_SUPPORTED;
3181 goto exit;
3182 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003183
3184 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3185 /* Sanity check. This shouldn't fail on a valid configuration. */
3186 if( operation->mac_size == 0 ||
3187 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3188 {
3189 status = PSA_ERROR_NOT_SUPPORTED;
3190 goto exit;
3191 }
3192
Gilles Peskine8e338702019-07-30 20:06:31 +02003193 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003194 {
3195 status = PSA_ERROR_INVALID_ARGUMENT;
3196 goto exit;
3197 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003198
Gilles Peskine01126fa2018-07-12 17:04:55 +02003199 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003200 slot->data.key.data,
3201 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003202 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003203 }
3204 else
John Durkopd0321952020-10-29 21:37:36 -07003205#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003206 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003207 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003208 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003209 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003210
Gilles Peskined911eb72018-08-14 15:18:45 +02003211 if( truncated == 0 )
3212 {
3213 /* The "normal" case: untruncated algorithm. Nothing to do. */
3214 }
3215 else if( truncated < 4 )
3216 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003217 /* A very short MAC is too short for security since it can be
3218 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3219 * so we make this our minimum, even though 32 bits is still
3220 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003221 status = PSA_ERROR_NOT_SUPPORTED;
3222 }
3223 else if( truncated > operation->mac_size )
3224 {
3225 /* It's impossible to "truncate" to a larger length. */
3226 status = PSA_ERROR_INVALID_ARGUMENT;
3227 }
3228 else
3229 operation->mac_size = truncated;
3230
Gilles Peskinefbfac682018-07-08 20:51:54 +02003231exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003232 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003233 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003234 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003235 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003236 else
3237 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003238 operation->key_set = 1;
3239 }
3240 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003241}
3242
Gilles Peskine89167cb2018-07-08 20:12:23 +02003243psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003244 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003245 psa_algorithm_t alg )
3246{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003247 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003248}
3249
3250psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003251 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003252 psa_algorithm_t alg )
3253{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003254 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003255}
3256
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3258 const uint8_t *input,
3259 size_t input_length )
3260{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003261 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003262 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003263 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003264 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003265 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003266 operation->has_input = 1;
3267
Gilles Peskine8c9def32018-02-08 10:02:12 +01003268#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003269 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003270 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003271 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3272 input, input_length );
3273 status = mbedtls_to_psa_error( ret );
3274 }
3275 else
3276#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003277#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003278 if( PSA_ALG_IS_HMAC( operation->alg ) )
3279 {
3280 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3281 input_length );
3282 }
3283 else
John Durkopd0321952020-10-29 21:37:36 -07003284#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003285 {
3286 /* This shouldn't happen if `operation` was initialized by
3287 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003288 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003289 }
3290
Gilles Peskinefbfac682018-07-08 20:51:54 +02003291 if( status != PSA_SUCCESS )
3292 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003293 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003294}
3295
John Durkop6ba40d12020-11-10 08:50:04 -08003296#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003297static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3298 uint8_t *mac,
3299 size_t mac_size )
3300{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003301 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003302 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3303 size_t hash_size = 0;
3304 size_t block_size = psa_get_hash_block_size( hash_alg );
3305 psa_status_t status;
3306
Gilles Peskine01126fa2018-07-12 17:04:55 +02003307 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3308 if( status != PSA_SUCCESS )
3309 return( status );
3310 /* From here on, tmp needs to be wiped. */
3311
3312 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3313 if( status != PSA_SUCCESS )
3314 goto exit;
3315
3316 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3317 if( status != PSA_SUCCESS )
3318 goto exit;
3319
3320 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3321 if( status != PSA_SUCCESS )
3322 goto exit;
3323
Gilles Peskined911eb72018-08-14 15:18:45 +02003324 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3325 if( status != PSA_SUCCESS )
3326 goto exit;
3327
3328 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003329
3330exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003331 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003332 return( status );
3333}
John Durkop6ba40d12020-11-10 08:50:04 -08003334#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003335
mohammad16036df908f2018-04-02 08:34:15 -07003336static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003337 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003338 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003339{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003340 if( ! operation->key_set )
3341 return( PSA_ERROR_BAD_STATE );
3342 if( operation->iv_required && ! operation->iv_set )
3343 return( PSA_ERROR_BAD_STATE );
3344
Gilles Peskine8c9def32018-02-08 10:02:12 +01003345 if( mac_size < operation->mac_size )
3346 return( PSA_ERROR_BUFFER_TOO_SMALL );
3347
Gilles Peskine8c9def32018-02-08 10:02:12 +01003348#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003349 if( operation->alg == PSA_ALG_CMAC )
3350 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003351 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3352 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3353 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003354 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003355 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003356 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003357 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003358 else
3359#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003360#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003361 if( PSA_ALG_IS_HMAC( operation->alg ) )
3362 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003363 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003364 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003365 }
3366 else
John Durkopd0321952020-10-29 21:37:36 -07003367#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003368 {
3369 /* This shouldn't happen if `operation` was initialized by
3370 * a setup function. */
3371 return( PSA_ERROR_BAD_STATE );
3372 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003373}
3374
Gilles Peskineacd4be32018-07-08 19:56:25 +02003375psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3376 uint8_t *mac,
3377 size_t mac_size,
3378 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003379{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003380 psa_status_t status;
3381
Jaeden Amero252ef282019-02-15 14:05:35 +00003382 if( operation->alg == 0 )
3383 {
3384 return( PSA_ERROR_BAD_STATE );
3385 }
3386
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003387 /* Fill the output buffer with something that isn't a valid mac
3388 * (barring an attack on the mac and deliberately-crafted input),
3389 * in case the caller doesn't check the return status properly. */
3390 *mac_length = mac_size;
3391 /* If mac_size is 0 then mac may be NULL and then the
3392 * call to memset would have undefined behavior. */
3393 if( mac_size != 0 )
3394 memset( mac, '!', mac_size );
3395
Gilles Peskine89167cb2018-07-08 20:12:23 +02003396 if( ! operation->is_sign )
3397 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003398 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003399 }
mohammad16036df908f2018-04-02 08:34:15 -07003400
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003401 status = psa_mac_finish_internal( operation, mac, mac_size );
3402
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003403 if( status == PSA_SUCCESS )
3404 {
3405 status = psa_mac_abort( operation );
3406 if( status == PSA_SUCCESS )
3407 *mac_length = operation->mac_size;
3408 else
3409 memset( mac, '!', mac_size );
3410 }
3411 else
3412 psa_mac_abort( operation );
3413 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003414}
3415
Gilles Peskineacd4be32018-07-08 19:56:25 +02003416psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3417 const uint8_t *mac,
3418 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003419{
Gilles Peskine828ed142018-06-18 23:25:51 +02003420 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003421 psa_status_t status;
3422
Jaeden Amero252ef282019-02-15 14:05:35 +00003423 if( operation->alg == 0 )
3424 {
3425 return( PSA_ERROR_BAD_STATE );
3426 }
3427
Gilles Peskine89167cb2018-07-08 20:12:23 +02003428 if( operation->is_sign )
3429 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003430 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003431 }
3432 if( operation->mac_size != mac_length )
3433 {
3434 status = PSA_ERROR_INVALID_SIGNATURE;
3435 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003436 }
mohammad16036df908f2018-04-02 08:34:15 -07003437
3438 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003439 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003440 if( status != PSA_SUCCESS )
3441 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003442
3443 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3444 status = PSA_ERROR_INVALID_SIGNATURE;
3445
3446cleanup:
3447 if( status == PSA_SUCCESS )
3448 status = psa_mac_abort( operation );
3449 else
3450 psa_mac_abort( operation );
3451
Gilles Peskine3f108122018-12-07 18:14:53 +01003452 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003453
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003454 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003455}
3456
3457
Gilles Peskine20035e32018-02-03 22:44:14 +01003458
Gilles Peskine20035e32018-02-03 22:44:14 +01003459/****************************************************************/
3460/* Asymmetric cryptography */
3461/****************************************************************/
3462
John Durkop6ba40d12020-11-10 08:50:04 -08003463#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3464 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003465/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003466 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003467static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3468 size_t hash_length,
3469 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003470{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003471 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003472 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003473 *md_alg = mbedtls_md_get_type( md_info );
3474
3475 /* The Mbed TLS RSA module uses an unsigned int for hash length
3476 * parameters. Validate that it fits so that we don't risk an
3477 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003478#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003479 if( hash_length > UINT_MAX )
3480 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003481#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003482
John Durkop0e005192020-10-31 22:06:54 -07003483#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003484 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3485 * must be correct. */
3486 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3487 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003488 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003489 if( md_info == NULL )
3490 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003491 if( mbedtls_md_get_size( md_info ) != hash_length )
3492 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003493 }
John Durkop0e005192020-10-31 22:06:54 -07003494#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003495
John Durkop0e005192020-10-31 22:06:54 -07003496#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003497 /* PSS requires a hash internally. */
3498 if( PSA_ALG_IS_RSA_PSS( alg ) )
3499 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003500 if( md_info == NULL )
3501 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003502 }
John Durkop0e005192020-10-31 22:06:54 -07003503#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003504
Gilles Peskine61b91d42018-06-08 16:09:36 +02003505 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003506}
3507
Gilles Peskine2b450e32018-06-27 15:42:46 +02003508static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3509 psa_algorithm_t alg,
3510 const uint8_t *hash,
3511 size_t hash_length,
3512 uint8_t *signature,
3513 size_t signature_size,
3514 size_t *signature_length )
3515{
3516 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003518 mbedtls_md_type_t md_alg;
3519
3520 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3521 if( status != PSA_SUCCESS )
3522 return( status );
3523
Gilles Peskine630a18a2018-06-29 17:49:35 +02003524 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003525 return( PSA_ERROR_BUFFER_TOO_SMALL );
3526
John Durkop0e005192020-10-31 22:06:54 -07003527#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003528 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3529 {
3530 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3531 MBEDTLS_MD_NONE );
3532 ret = mbedtls_rsa_pkcs1_sign( rsa,
3533 mbedtls_ctr_drbg_random,
3534 &global_data.ctr_drbg,
3535 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003536 md_alg,
3537 (unsigned int) hash_length,
3538 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003539 signature );
3540 }
3541 else
John Durkop0e005192020-10-31 22:06:54 -07003542#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3543#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003544 if( PSA_ALG_IS_RSA_PSS( alg ) )
3545 {
3546 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3547 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3548 mbedtls_ctr_drbg_random,
3549 &global_data.ctr_drbg,
3550 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003551 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003552 (unsigned int) hash_length,
3553 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003554 signature );
3555 }
3556 else
John Durkop0e005192020-10-31 22:06:54 -07003557#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003558 {
3559 return( PSA_ERROR_INVALID_ARGUMENT );
3560 }
3561
3562 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003563 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003564 return( mbedtls_to_psa_error( ret ) );
3565}
3566
3567static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3568 psa_algorithm_t alg,
3569 const uint8_t *hash,
3570 size_t hash_length,
3571 const uint8_t *signature,
3572 size_t signature_length )
3573{
3574 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003576 mbedtls_md_type_t md_alg;
3577
3578 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3579 if( status != PSA_SUCCESS )
3580 return( status );
3581
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003582 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3583 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003584
John Durkop0e005192020-10-31 22:06:54 -07003585#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003586 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3587 {
3588 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3589 MBEDTLS_MD_NONE );
3590 ret = mbedtls_rsa_pkcs1_verify( rsa,
3591 mbedtls_ctr_drbg_random,
3592 &global_data.ctr_drbg,
3593 MBEDTLS_RSA_PUBLIC,
3594 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003595 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003596 hash,
3597 signature );
3598 }
3599 else
John Durkop0e005192020-10-31 22:06:54 -07003600#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3601#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003602 if( PSA_ALG_IS_RSA_PSS( alg ) )
3603 {
3604 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3605 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3606 mbedtls_ctr_drbg_random,
3607 &global_data.ctr_drbg,
3608 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003609 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003610 (unsigned int) hash_length,
3611 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003612 signature );
3613 }
3614 else
John Durkop0e005192020-10-31 22:06:54 -07003615#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003616 {
3617 return( PSA_ERROR_INVALID_ARGUMENT );
3618 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003619
3620 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3621 * the rest of the signature is invalid". This has little use in
3622 * practice and PSA doesn't report this distinction. */
3623 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3624 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003625 return( mbedtls_to_psa_error( ret ) );
3626}
John Durkop6ba40d12020-11-10 08:50:04 -08003627#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3628 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003629
John Durkop6ba40d12020-11-10 08:50:04 -08003630#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3631 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003632/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3633 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3634 * (even though these functions don't modify it). */
3635static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3636 psa_algorithm_t alg,
3637 const uint8_t *hash,
3638 size_t hash_length,
3639 uint8_t *signature,
3640 size_t signature_size,
3641 size_t *signature_length )
3642{
Janos Follath24eed8d2019-11-22 13:21:35 +00003643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003644 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003645 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003646 mbedtls_mpi_init( &r );
3647 mbedtls_mpi_init( &s );
3648
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003649 if( signature_size < 2 * curve_bytes )
3650 {
3651 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3652 goto cleanup;
3653 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003654
John Durkop0ea39e02020-10-13 19:58:20 -07003655#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003656 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3657 {
3658 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3659 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3660 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003661 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3662 &ecp->d, hash,
3663 hash_length, md_alg,
3664 mbedtls_ctr_drbg_random,
3665 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003666 }
3667 else
John Durkop0ea39e02020-10-13 19:58:20 -07003668#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003669 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003670 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003671 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3672 hash, hash_length,
3673 mbedtls_ctr_drbg_random,
3674 &global_data.ctr_drbg ) );
3675 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003676
3677 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3678 signature,
3679 curve_bytes ) );
3680 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3681 signature + curve_bytes,
3682 curve_bytes ) );
3683
3684cleanup:
3685 mbedtls_mpi_free( &r );
3686 mbedtls_mpi_free( &s );
3687 if( ret == 0 )
3688 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003689 return( mbedtls_to_psa_error( ret ) );
3690}
3691
3692static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3693 const uint8_t *hash,
3694 size_t hash_length,
3695 const uint8_t *signature,
3696 size_t signature_length )
3697{
Janos Follath24eed8d2019-11-22 13:21:35 +00003698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003699 mbedtls_mpi r, s;
3700 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3701 mbedtls_mpi_init( &r );
3702 mbedtls_mpi_init( &s );
3703
3704 if( signature_length != 2 * curve_bytes )
3705 return( PSA_ERROR_INVALID_SIGNATURE );
3706
3707 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3708 signature,
3709 curve_bytes ) );
3710 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3711 signature + curve_bytes,
3712 curve_bytes ) );
3713
Steven Cooremanacda8342020-07-24 23:09:52 +02003714 /* Check whether the public part is loaded. If not, load it. */
3715 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3716 {
3717 MBEDTLS_MPI_CHK(
3718 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3719 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3720 }
3721
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003722 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3723 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003724
3725cleanup:
3726 mbedtls_mpi_free( &r );
3727 mbedtls_mpi_free( &s );
3728 return( mbedtls_to_psa_error( ret ) );
3729}
John Durkop6ba40d12020-11-10 08:50:04 -08003730#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3731 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003732
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003733psa_status_t psa_sign_hash( psa_key_handle_t handle,
3734 psa_algorithm_t alg,
3735 const uint8_t *hash,
3736 size_t hash_length,
3737 uint8_t *signature,
3738 size_t signature_size,
3739 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003740{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003741 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003742 psa_status_t status;
3743
3744 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003745 /* Immediately reject a zero-length signature buffer. This guarantees
3746 * that signature must be a valid pointer. (On the other hand, the hash
3747 * buffer can in principle be empty since it doesn't actually have
3748 * to be a hash.) */
3749 if( signature_size == 0 )
3750 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003751
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003752 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003753 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003754 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003755 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003756 {
3757 status = PSA_ERROR_INVALID_ARGUMENT;
3758 goto exit;
3759 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003760
Steven Cooremancd84cb42020-07-16 20:28:36 +02003761 /* Try any of the available accelerators first */
3762 status = psa_driver_wrapper_sign_hash( slot,
3763 alg,
3764 hash,
3765 hash_length,
3766 signature,
3767 signature_size,
3768 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003769 if( status != PSA_ERROR_NOT_SUPPORTED ||
3770 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003771 goto exit;
3772
Steven Cooreman7a250572020-07-17 16:43:05 +02003773 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003774#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3775 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003776 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003777 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003778 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003779
Steven Cooreman4fed4552020-08-03 14:46:03 +02003780 status = psa_load_rsa_representation( slot->attr.type,
3781 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003782 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003783 &rsa );
3784 if( status != PSA_SUCCESS )
3785 goto exit;
3786
Steven Cooremana2371e52020-07-28 14:30:39 +02003787 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003788 alg,
3789 hash, hash_length,
3790 signature, signature_size,
3791 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003792
Steven Cooremana2371e52020-07-28 14:30:39 +02003793 mbedtls_rsa_free( rsa );
3794 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003795 }
3796 else
John Durkop6ba40d12020-11-10 08:50:04 -08003797#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3798 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003799 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003800 {
John Durkop9814fa22020-11-04 12:28:15 -08003801#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3802 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003803 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003804#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003805 PSA_ALG_IS_ECDSA( alg )
3806#else
3807 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3808#endif
3809 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003810 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003811 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003812 status = psa_load_ecp_representation( slot->attr.type,
3813 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003814 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003815 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003816 if( status != PSA_SUCCESS )
3817 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003818 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003819 alg,
3820 hash, hash_length,
3821 signature, signature_size,
3822 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003823 mbedtls_ecp_keypair_free( ecp );
3824 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003825 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003826 else
John Durkop9814fa22020-11-04 12:28:15 -08003827#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3828 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003829 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003830 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003831 }
itayzafrir5c753392018-05-08 11:18:38 +03003832 }
3833 else
itayzafrir5c753392018-05-08 11:18:38 +03003834 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003835 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003836 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003837
3838exit:
3839 /* Fill the unused part of the output buffer (the whole buffer on error,
3840 * the trailing part on success) with something that isn't a valid mac
3841 * (barring an attack on the mac and deliberately-crafted input),
3842 * in case the caller doesn't check the return status properly. */
3843 if( status == PSA_SUCCESS )
3844 memset( signature + *signature_length, '!',
3845 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003846 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003847 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003848 /* If signature_size is 0 then we have nothing to do. We must not call
3849 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003850 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003851}
3852
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003853psa_status_t psa_verify_hash( psa_key_handle_t handle,
3854 psa_algorithm_t alg,
3855 const uint8_t *hash,
3856 size_t hash_length,
3857 const uint8_t *signature,
3858 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003859{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003860 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003861 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003862
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003863 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003864 if( status != PSA_SUCCESS )
3865 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003866
Steven Cooreman55ae2172020-07-17 19:46:15 +02003867 /* Try any of the available accelerators first */
3868 status = psa_driver_wrapper_verify_hash( slot,
3869 alg,
3870 hash,
3871 hash_length,
3872 signature,
3873 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003874 if( status != PSA_ERROR_NOT_SUPPORTED ||
3875 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003876 return status;
3877
John Durkop6ba40d12020-11-10 08:50:04 -08003878#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3879 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003880 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003882 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003883
Steven Cooreman4fed4552020-08-03 14:46:03 +02003884 status = psa_load_rsa_representation( slot->attr.type,
3885 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003886 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003887 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003888 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003889 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003890
Steven Cooremana2371e52020-07-28 14:30:39 +02003891 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003892 alg,
3893 hash, hash_length,
3894 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003895 mbedtls_rsa_free( rsa );
3896 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003897 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898 }
3899 else
John Durkop6ba40d12020-11-10 08:50:04 -08003900#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3901 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003902 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003903 {
John Durkop9814fa22020-11-04 12:28:15 -08003904#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3905 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003906 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003907 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003908 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003909 status = psa_load_ecp_representation( slot->attr.type,
3910 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003911 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003912 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003913 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003914 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003915 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003916 hash, hash_length,
3917 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003918 mbedtls_ecp_keypair_free( ecp );
3919 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003920 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003921 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003922 else
John Durkop9814fa22020-11-04 12:28:15 -08003923#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3924 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003925 {
3926 return( PSA_ERROR_INVALID_ARGUMENT );
3927 }
itayzafrir5c753392018-05-08 11:18:38 +03003928 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003929 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003930 {
3931 return( PSA_ERROR_NOT_SUPPORTED );
3932 }
3933}
3934
John Durkop0e005192020-10-31 22:06:54 -07003935#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003936static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3937 mbedtls_rsa_context *rsa )
3938{
3939 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3940 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3941 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3942 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3943}
John Durkop0e005192020-10-31 22:06:54 -07003944#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003945
Gilles Peskinec5487a82018-12-03 18:08:14 +01003946psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003947 psa_algorithm_t alg,
3948 const uint8_t *input,
3949 size_t input_length,
3950 const uint8_t *salt,
3951 size_t salt_length,
3952 uint8_t *output,
3953 size_t output_size,
3954 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003956 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003957 psa_status_t status;
3958
Darryl Green5cc689a2018-07-24 15:34:10 +01003959 (void) input;
3960 (void) input_length;
3961 (void) salt;
3962 (void) output;
3963 (void) output_size;
3964
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003965 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003967 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3968 return( PSA_ERROR_INVALID_ARGUMENT );
3969
Gilles Peskine28f8f302019-07-24 13:30:31 +02003970 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003971 if( status != PSA_SUCCESS )
3972 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003973 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3974 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976
John Durkop6ba40d12020-11-10 08:50:04 -08003977#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3978 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003979 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003981 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003982 status = psa_load_rsa_representation( slot->attr.type,
3983 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003984 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003985 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003986 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003987 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003988
3989 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003990 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003991 status = PSA_ERROR_BUFFER_TOO_SMALL;
3992 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003993 }
John Durkop0e005192020-10-31 22:06:54 -07003994#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003995 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003997 status = mbedtls_to_psa_error(
3998 mbedtls_rsa_pkcs1_encrypt( rsa,
3999 mbedtls_ctr_drbg_random,
4000 &global_data.ctr_drbg,
4001 MBEDTLS_RSA_PUBLIC,
4002 input_length,
4003 input,
4004 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005 }
4006 else
John Durkop0e005192020-10-31 22:06:54 -07004007#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4008#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004009 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004011 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004012 status = mbedtls_to_psa_error(
4013 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4014 mbedtls_ctr_drbg_random,
4015 &global_data.ctr_drbg,
4016 MBEDTLS_RSA_PUBLIC,
4017 salt, salt_length,
4018 input_length,
4019 input,
4020 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021 }
4022 else
John Durkop0e005192020-10-31 22:06:54 -07004023#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004025 status = PSA_ERROR_INVALID_ARGUMENT;
4026 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004028rsa_exit:
4029 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004030 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004031
Steven Cooremana2371e52020-07-28 14:30:39 +02004032 mbedtls_rsa_free( rsa );
4033 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004034 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036 else
John Durkop9814fa22020-11-04 12:28:15 -08004037#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004038 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 {
4040 return( PSA_ERROR_NOT_SUPPORTED );
4041 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004042}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043
Gilles Peskinec5487a82018-12-03 18:08:14 +01004044psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004045 psa_algorithm_t alg,
4046 const uint8_t *input,
4047 size_t input_length,
4048 const uint8_t *salt,
4049 size_t salt_length,
4050 uint8_t *output,
4051 size_t output_size,
4052 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004053{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004054 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004055 psa_status_t status;
4056
Darryl Green5cc689a2018-07-24 15:34:10 +01004057 (void) input;
4058 (void) input_length;
4059 (void) salt;
4060 (void) output;
4061 (void) output_size;
4062
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004063 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004065 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4066 return( PSA_ERROR_INVALID_ARGUMENT );
4067
Gilles Peskine28f8f302019-07-24 13:30:31 +02004068 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004069 if( status != PSA_SUCCESS )
4070 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004071 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004072 return( PSA_ERROR_INVALID_ARGUMENT );
4073
John Durkop6ba40d12020-11-10 08:50:04 -08004074#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4075 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004076 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004078 mbedtls_rsa_context *rsa = NULL;
4079 status = psa_load_rsa_representation( slot->attr.type,
4080 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004081 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004082 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004083 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004084 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085
Steven Cooremana2371e52020-07-28 14:30:39 +02004086 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004087 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004088 status = PSA_ERROR_INVALID_ARGUMENT;
4089 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004090 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091
John Durkop0e005192020-10-31 22:06:54 -07004092#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004093 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004095 status = mbedtls_to_psa_error(
4096 mbedtls_rsa_pkcs1_decrypt( rsa,
4097 mbedtls_ctr_drbg_random,
4098 &global_data.ctr_drbg,
4099 MBEDTLS_RSA_PRIVATE,
4100 output_length,
4101 input,
4102 output,
4103 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004104 }
4105 else
John Durkop0e005192020-10-31 22:06:54 -07004106#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4107#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004108 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004110 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004111 status = mbedtls_to_psa_error(
4112 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4113 mbedtls_ctr_drbg_random,
4114 &global_data.ctr_drbg,
4115 MBEDTLS_RSA_PRIVATE,
4116 salt, salt_length,
4117 output_length,
4118 input,
4119 output,
4120 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121 }
4122 else
John Durkop0e005192020-10-31 22:06:54 -07004123#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004125 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004126 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004127
Steven Cooreman4fed4552020-08-03 14:46:03 +02004128rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004129 mbedtls_rsa_free( rsa );
4130 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004131 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133 else
John Durkop6ba40d12020-11-10 08:50:04 -08004134#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4135 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136 {
4137 return( PSA_ERROR_NOT_SUPPORTED );
4138 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004139}
Gilles Peskine20035e32018-02-03 22:44:14 +01004140
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004141
4142
mohammad1603503973b2018-03-12 15:59:30 +02004143/****************************************************************/
4144/* Symmetric cryptography */
4145/****************************************************************/
4146
Gilles Peskinee553c652018-06-04 16:22:46 +02004147static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004149 psa_algorithm_t alg,
4150 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004151{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004152 int ret = 0;
4153 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004154 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004155 size_t key_bits;
4156 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004157 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4158 PSA_KEY_USAGE_ENCRYPT :
4159 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004160
Steven Cooremand3feccd2020-09-01 15:56:14 +02004161 /* A context must be freshly initialized before it can be set up. */
4162 if( operation->alg != 0 )
4163 return( PSA_ERROR_BAD_STATE );
4164
Steven Cooremana07b9972020-09-10 14:54:14 +02004165 /* The requested algorithm must be one that can be processed by cipher. */
4166 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004167 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004168
Steven Cooremanef8575e2020-09-11 11:44:50 +02004169 /* Fetch key material from key storage. */
4170 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4171 if( status != PSA_SUCCESS )
4172 goto exit;
4173
4174 /* Initialize the operation struct members, except for alg. The alg member
4175 * is used to indicate to psa_cipher_abort that there are resources to free,
4176 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004177 operation->key_set = 0;
4178 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004179 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004180 operation->iv_size = 0;
4181 operation->block_size = 0;
4182 if( alg == PSA_ALG_ECB_NO_PADDING )
4183 operation->iv_required = 0;
4184 else
4185 operation->iv_required = 1;
4186
Steven Cooremana07b9972020-09-10 14:54:14 +02004187 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004188 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004189 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004190 slot,
4191 alg );
4192 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004193 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004194 slot,
4195 alg );
4196
Steven Cooremanef8575e2020-09-11 11:44:50 +02004197 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004198 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004199 /* Once the driver context is initialised, it needs to be freed using
4200 * psa_cipher_abort. Indicate this through setting alg. */
4201 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004202 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004203
Steven Cooremand3feccd2020-09-01 15:56:14 +02004204 if( status != PSA_ERROR_NOT_SUPPORTED ||
4205 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4206 goto exit;
4207
Steven Cooremana07b9972020-09-10 14:54:14 +02004208 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004209 * available for the given algorithm & key. */
4210 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004211
Steven Cooremana07b9972020-09-10 14:54:14 +02004212 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004213 * psa_cipher_abort. Indicate there is something to be freed through setting
4214 * alg, and indicate the operation is being done using mbedtls crypto through
4215 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004216 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004217 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004218
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004219 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004220 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004221 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004222 {
4223 status = PSA_ERROR_NOT_SUPPORTED;
4224 goto exit;
4225 }
mohammad1603503973b2018-03-12 15:59:30 +02004226
mohammad1603503973b2018-03-12 15:59:30 +02004227 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004228 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004229 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004230
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004231#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004232 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004233 {
4234 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004235 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004236 memcpy( keys, slot->data.key.data, 16 );
4237 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004238 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4239 keys,
4240 192, cipher_operation );
4241 }
4242 else
4243#endif
4244 {
4245 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004246 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004247 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004248 }
Moran Peker41deec42018-04-04 15:43:05 +03004249 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004250 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004251
mohammad16038481e742018-03-18 13:57:31 +02004252#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004253 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004254 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004255 case PSA_ALG_CBC_NO_PADDING:
4256 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4257 MBEDTLS_PADDING_NONE );
4258 break;
4259 case PSA_ALG_CBC_PKCS7:
4260 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4261 MBEDTLS_PADDING_PKCS7 );
4262 break;
4263 default:
4264 /* The algorithm doesn't involve padding. */
4265 ret = 0;
4266 break;
4267 }
4268 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004269 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004270#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4271
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004272 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004273 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004274 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4275 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004276 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004277 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004278 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004279#if defined(MBEDTLS_CHACHA20_C)
4280 else
4281 if( alg == PSA_ALG_CHACHA20 )
4282 operation->iv_size = 12;
4283#endif
mohammad1603503973b2018-03-12 15:59:30 +02004284
Steven Cooreman7df02922020-09-09 15:28:49 +02004285 status = PSA_SUCCESS;
4286
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004287exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004288 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004289 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004290 if( status == PSA_SUCCESS )
4291 {
4292 /* Update operation flags for both driver and software implementations */
4293 operation->key_set = 1;
4294 }
4295 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004296 psa_cipher_abort( operation );
4297 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004298}
4299
Gilles Peskinefe119512018-07-08 21:39:34 +02004300psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004301 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004302 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004303{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004304 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004305}
4306
Gilles Peskinefe119512018-07-08 21:39:34 +02004307psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004308 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004309 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004310{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004311 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004312}
4313
Gilles Peskinefe119512018-07-08 21:39:34 +02004314psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004315 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004316 size_t iv_size,
4317 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004318{
itayzafrir534bd7c2018-08-02 13:56:32 +03004319 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004321 if( operation->iv_set || ! operation->iv_required )
4322 {
4323 return( PSA_ERROR_BAD_STATE );
4324 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004325
Steven Cooremanef8575e2020-09-11 11:44:50 +02004326 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004327 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004328 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004329 iv,
4330 iv_size,
4331 iv_length );
4332 goto exit;
4333 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004334
Moran Peker41deec42018-04-04 15:43:05 +03004335 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004336 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004337 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004338 goto exit;
4339 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004340 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4341 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004342 if( ret != 0 )
4343 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004344 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004345 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004346 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004347
mohammad16038481e742018-03-18 13:57:31 +02004348 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004349 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004350
Moran Peker395db872018-05-31 14:07:14 +03004351exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004352 if( status == PSA_SUCCESS )
4353 operation->iv_set = 1;
4354 else
Moran Peker395db872018-05-31 14:07:14 +03004355 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004356 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004357}
4358
Gilles Peskinefe119512018-07-08 21:39:34 +02004359psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004360 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004361 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004362{
itayzafrir534bd7c2018-08-02 13:56:32 +03004363 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004365 if( operation->iv_set || ! operation->iv_required )
4366 {
4367 return( PSA_ERROR_BAD_STATE );
4368 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004369
Steven Cooremanef8575e2020-09-11 11:44:50 +02004370 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004371 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004372 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004373 iv,
4374 iv_length );
4375 goto exit;
4376 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004377
Moran Pekera28258c2018-05-29 16:25:04 +03004378 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004379 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004380 status = PSA_ERROR_INVALID_ARGUMENT;
4381 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004382 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004383 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4384 status = mbedtls_to_psa_error( ret );
4385exit:
4386 if( status == PSA_SUCCESS )
4387 operation->iv_set = 1;
4388 else
mohammad1603503973b2018-03-12 15:59:30 +02004389 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004390 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004391}
4392
Steven Cooreman177deba2020-09-07 17:14:14 +02004393/* Process input for which the algorithm is set to ECB mode. This requires
4394 * manual processing, since the PSA API is defined as being able to process
4395 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4396 * underlying mbedtls_cipher_update only takes full blocks. */
4397static psa_status_t psa_cipher_update_ecb_internal(
4398 mbedtls_cipher_context_t *ctx,
4399 const uint8_t *input,
4400 size_t input_length,
4401 uint8_t *output,
4402 size_t output_size,
4403 size_t *output_length )
4404{
4405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4406 size_t block_size = ctx->cipher_info->block_size;
4407 size_t internal_output_length = 0;
4408 *output_length = 0;
4409
4410 if( input_length == 0 )
4411 {
4412 status = PSA_SUCCESS;
4413 goto exit;
4414 }
4415
4416 if( ctx->unprocessed_len > 0 )
4417 {
4418 /* Fill up to block size, and run the block if there's a full one. */
4419 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4420
4421 if( input_length < bytes_to_copy )
4422 bytes_to_copy = input_length;
4423
4424 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4425 input, bytes_to_copy );
4426 input_length -= bytes_to_copy;
4427 input += bytes_to_copy;
4428 ctx->unprocessed_len += bytes_to_copy;
4429
4430 if( ctx->unprocessed_len == block_size )
4431 {
4432 status = mbedtls_to_psa_error(
4433 mbedtls_cipher_update( ctx,
4434 ctx->unprocessed_data,
4435 block_size,
4436 output, &internal_output_length ) );
4437
4438 if( status != PSA_SUCCESS )
4439 goto exit;
4440
4441 output += internal_output_length;
4442 output_size -= internal_output_length;
4443 *output_length += internal_output_length;
4444 ctx->unprocessed_len = 0;
4445 }
4446 }
4447
4448 while( input_length >= block_size )
4449 {
4450 /* Run all full blocks we have, one by one */
4451 status = mbedtls_to_psa_error(
4452 mbedtls_cipher_update( ctx, input,
4453 block_size,
4454 output, &internal_output_length ) );
4455
4456 if( status != PSA_SUCCESS )
4457 goto exit;
4458
4459 input_length -= block_size;
4460 input += block_size;
4461
4462 output += internal_output_length;
4463 output_size -= internal_output_length;
4464 *output_length += internal_output_length;
4465 }
4466
4467 if( input_length > 0 )
4468 {
4469 /* Save unprocessed bytes for later processing */
4470 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4471 input, input_length );
4472 ctx->unprocessed_len += input_length;
4473 }
4474
4475 status = PSA_SUCCESS;
4476
4477exit:
4478 return( status );
4479}
4480
Gilles Peskinee553c652018-06-04 16:22:46 +02004481psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4482 const uint8_t *input,
4483 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004484 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004485 size_t output_size,
4486 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004487{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004489 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004490 if( operation->alg == 0 )
4491 {
4492 return( PSA_ERROR_BAD_STATE );
4493 }
4494 if( operation->iv_required && ! operation->iv_set )
4495 {
4496 return( PSA_ERROR_BAD_STATE );
4497 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004498
Steven Cooremanef8575e2020-09-11 11:44:50 +02004499 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004500 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004501 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004502 input,
4503 input_length,
4504 output,
4505 output_size,
4506 output_length );
4507 goto exit;
4508 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004509
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004510 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004511 {
4512 /* Take the unprocessed partial block left over from previous
4513 * update calls, if any, plus the input to this call. Remove
4514 * the last partial block, if any. You get the data that will be
4515 * output in this call. */
4516 expected_output_size =
4517 ( operation->ctx.cipher.unprocessed_len + input_length )
4518 / operation->block_size * operation->block_size;
4519 }
4520 else
4521 {
4522 expected_output_size = input_length;
4523 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004524
Gilles Peskine89d789c2018-06-04 17:17:16 +02004525 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004526 {
4527 status = PSA_ERROR_BUFFER_TOO_SMALL;
4528 goto exit;
4529 }
mohammad160382759612018-03-12 18:16:40 +02004530
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004531 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4532 {
4533 /* mbedtls_cipher_update has an API inconsistency: it will only
4534 * process a single block at a time in ECB mode. Abstract away that
4535 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004536 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4537 input,
4538 input_length,
4539 output,
4540 output_size,
4541 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004542 }
4543 else
4544 {
4545 status = mbedtls_to_psa_error(
4546 mbedtls_cipher_update( &operation->ctx.cipher, input,
4547 input_length, output, output_length ) );
4548 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004549exit:
4550 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004551 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004552 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004553}
4554
Gilles Peskinee553c652018-06-04 16:22:46 +02004555psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4556 uint8_t *output,
4557 size_t output_size,
4558 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004559{
David Saadab4ecc272019-02-14 13:48:10 +02004560 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004561 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004562 if( operation->alg == 0 )
4563 {
4564 return( PSA_ERROR_BAD_STATE );
4565 }
4566 if( operation->iv_required && ! operation->iv_set )
4567 {
4568 return( PSA_ERROR_BAD_STATE );
4569 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004570
Steven Cooremanef8575e2020-09-11 11:44:50 +02004571 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004572 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004573 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004574 output,
4575 output_size,
4576 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004577 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004578 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004579
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004580 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004581 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004582 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004583 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004584 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004585 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004586 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004587 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004588 }
4589
Steven Cooremanef8575e2020-09-11 11:44:50 +02004590 status = mbedtls_to_psa_error(
4591 mbedtls_cipher_finish( &operation->ctx.cipher,
4592 temp_output_buffer,
4593 output_length ) );
4594 if( status != PSA_SUCCESS )
4595 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004596
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004597 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004598 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004599 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004600 memcpy( output, temp_output_buffer, *output_length );
4601 else
Janos Follath315b51c2018-07-09 16:04:51 +01004602 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004603
Steven Cooremanef8575e2020-09-11 11:44:50 +02004604exit:
4605 if( operation->mbedtls_in_use == 1 )
4606 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004607
Steven Cooremanef8575e2020-09-11 11:44:50 +02004608 if( status == PSA_SUCCESS )
4609 return( psa_cipher_abort( operation ) );
4610 else
4611 {
4612 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004613 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004614
Steven Cooremanef8575e2020-09-11 11:44:50 +02004615 return( status );
4616 }
mohammad1603503973b2018-03-12 15:59:30 +02004617}
4618
Gilles Peskinee553c652018-06-04 16:22:46 +02004619psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4620{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004621 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004622 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004623 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004624 * in use. It's ok to call abort on such an object, and there's
4625 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004626 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004627 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004628
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004629 /* Sanity check (shouldn't happen: operation->alg should
4630 * always have been initialized to a valid value). */
4631 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4632 return( PSA_ERROR_BAD_STATE );
4633
Steven Cooremanef8575e2020-09-11 11:44:50 +02004634 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004635 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004636 else
4637 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004638
Moran Peker41deec42018-04-04 15:43:05 +03004639 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004640 operation->key_set = 0;
4641 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004642 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004643 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004644 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004645 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004646
Moran Peker395db872018-05-31 14:07:14 +03004647 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004648}
4649
Gilles Peskinea0655c32018-04-30 17:06:50 +02004650
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004651
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004652
mohammad16035955c982018-04-26 00:53:03 +03004653/****************************************************************/
4654/* AEAD */
4655/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004656
Gilles Peskineedf9a652018-08-17 18:11:56 +02004657typedef struct
4658{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004659 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 const mbedtls_cipher_info_t *cipher_info;
4661 union
4662 {
4663#if defined(MBEDTLS_CCM_C)
4664 mbedtls_ccm_context ccm;
4665#endif /* MBEDTLS_CCM_C */
4666#if defined(MBEDTLS_GCM_C)
4667 mbedtls_gcm_context gcm;
4668#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004669#if defined(MBEDTLS_CHACHAPOLY_C)
4670 mbedtls_chachapoly_context chachapoly;
4671#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004672 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004673 psa_algorithm_t core_alg;
4674 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004675 uint8_t tag_length;
4676} aead_operation_t;
4677
Gilles Peskine30a9e412019-01-14 18:36:12 +01004678static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004680 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004681 {
4682#if defined(MBEDTLS_CCM_C)
4683 case PSA_ALG_CCM:
4684 mbedtls_ccm_free( &operation->ctx.ccm );
4685 break;
4686#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004687#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004688 case PSA_ALG_GCM:
4689 mbedtls_gcm_free( &operation->ctx.gcm );
4690 break;
4691#endif /* MBEDTLS_GCM_C */
4692 }
4693}
4694
4695static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004696 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004697 psa_key_usage_t usage,
4698 psa_algorithm_t alg )
4699{
4700 psa_status_t status;
4701 size_t key_bits;
4702 mbedtls_cipher_id_t cipher_id;
4703
Gilles Peskine28f8f302019-07-24 13:30:31 +02004704 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004705 if( status != PSA_SUCCESS )
4706 return( status );
4707
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004708 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004709
4710 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004711 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004712 &cipher_id );
4713 if( operation->cipher_info == NULL )
4714 return( PSA_ERROR_NOT_SUPPORTED );
4715
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004716 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004717 {
4718#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004719 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4720 operation->core_alg = PSA_ALG_CCM;
4721 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004722 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4723 * The call to mbedtls_ccm_encrypt_and_tag or
4724 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004725 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004726 return( PSA_ERROR_INVALID_ARGUMENT );
4727 mbedtls_ccm_init( &operation->ctx.ccm );
4728 status = mbedtls_to_psa_error(
4729 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004730 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731 (unsigned int) key_bits ) );
4732 if( status != 0 )
4733 goto cleanup;
4734 break;
4735#endif /* MBEDTLS_CCM_C */
4736
4737#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004738 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4739 operation->core_alg = PSA_ALG_GCM;
4740 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004741 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4742 * The call to mbedtls_gcm_crypt_and_tag or
4743 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004744 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004745 return( PSA_ERROR_INVALID_ARGUMENT );
4746 mbedtls_gcm_init( &operation->ctx.gcm );
4747 status = mbedtls_to_psa_error(
4748 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004749 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004751 if( status != 0 )
4752 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004753 break;
4754#endif /* MBEDTLS_GCM_C */
4755
Gilles Peskine26869f22019-05-06 15:25:00 +02004756#if defined(MBEDTLS_CHACHAPOLY_C)
4757 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4758 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4759 operation->full_tag_length = 16;
4760 /* We only support the default tag length. */
4761 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4762 return( PSA_ERROR_NOT_SUPPORTED );
4763 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4764 status = mbedtls_to_psa_error(
4765 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004766 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004767 if( status != 0 )
4768 goto cleanup;
4769 break;
4770#endif /* MBEDTLS_CHACHAPOLY_C */
4771
Gilles Peskineedf9a652018-08-17 18:11:56 +02004772 default:
4773 return( PSA_ERROR_NOT_SUPPORTED );
4774 }
4775
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004776 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4777 {
4778 status = PSA_ERROR_INVALID_ARGUMENT;
4779 goto cleanup;
4780 }
4781 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004782
Gilles Peskineedf9a652018-08-17 18:11:56 +02004783 return( PSA_SUCCESS );
4784
4785cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004786 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004787 return( status );
4788}
4789
Gilles Peskinec5487a82018-12-03 18:08:14 +01004790psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004791 psa_algorithm_t alg,
4792 const uint8_t *nonce,
4793 size_t nonce_length,
4794 const uint8_t *additional_data,
4795 size_t additional_data_length,
4796 const uint8_t *plaintext,
4797 size_t plaintext_length,
4798 uint8_t *ciphertext,
4799 size_t ciphertext_size,
4800 size_t *ciphertext_length )
4801{
mohammad16035955c982018-04-26 00:53:03 +03004802 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004803 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004804 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004805
mohammad1603f08a5502018-06-03 15:05:47 +03004806 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004807
Gilles Peskinec5487a82018-12-03 18:08:14 +01004808 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004809 if( status != PSA_SUCCESS )
4810 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004811
Gilles Peskineedf9a652018-08-17 18:11:56 +02004812 /* For all currently supported modes, the tag is at the end of the
4813 * ciphertext. */
4814 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4815 {
4816 status = PSA_ERROR_BUFFER_TOO_SMALL;
4817 goto exit;
4818 }
4819 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004820
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004821#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004822 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004823 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004824 status = mbedtls_to_psa_error(
4825 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4826 MBEDTLS_GCM_ENCRYPT,
4827 plaintext_length,
4828 nonce, nonce_length,
4829 additional_data, additional_data_length,
4830 plaintext, ciphertext,
4831 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004832 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004833 else
4834#endif /* MBEDTLS_GCM_C */
4835#if defined(MBEDTLS_CCM_C)
4836 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004837 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004838 status = mbedtls_to_psa_error(
4839 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4840 plaintext_length,
4841 nonce, nonce_length,
4842 additional_data,
4843 additional_data_length,
4844 plaintext, ciphertext,
4845 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004846 }
mohammad16035c8845f2018-05-09 05:40:09 -07004847 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004848#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004849#if defined(MBEDTLS_CHACHAPOLY_C)
4850 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4851 {
4852 if( nonce_length != 12 || operation.tag_length != 16 )
4853 {
4854 status = PSA_ERROR_NOT_SUPPORTED;
4855 goto exit;
4856 }
4857 status = mbedtls_to_psa_error(
4858 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4859 plaintext_length,
4860 nonce,
4861 additional_data,
4862 additional_data_length,
4863 plaintext,
4864 ciphertext,
4865 tag ) );
4866 }
4867 else
4868#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004869 {
mohammad1603554faad2018-06-03 15:07:38 +03004870 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004871 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004872
Gilles Peskineedf9a652018-08-17 18:11:56 +02004873 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4874 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004875
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004877 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878 if( status == PSA_SUCCESS )
4879 *ciphertext_length = plaintext_length + operation.tag_length;
4880 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004881}
4882
Gilles Peskineee652a32018-06-01 19:23:52 +02004883/* Locate the tag in a ciphertext buffer containing the encrypted data
4884 * followed by the tag. Return the length of the part preceding the tag in
4885 * *plaintext_length. This is the size of the plaintext in modes where
4886 * the encrypted data has the same size as the plaintext, such as
4887 * CCM and GCM. */
4888static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4889 const uint8_t *ciphertext,
4890 size_t ciphertext_length,
4891 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004892 const uint8_t **p_tag )
4893{
4894 size_t payload_length;
4895 if( tag_length > ciphertext_length )
4896 return( PSA_ERROR_INVALID_ARGUMENT );
4897 payload_length = ciphertext_length - tag_length;
4898 if( payload_length > plaintext_size )
4899 return( PSA_ERROR_BUFFER_TOO_SMALL );
4900 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004901 return( PSA_SUCCESS );
4902}
4903
Gilles Peskinec5487a82018-12-03 18:08:14 +01004904psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004905 psa_algorithm_t alg,
4906 const uint8_t *nonce,
4907 size_t nonce_length,
4908 const uint8_t *additional_data,
4909 size_t additional_data_length,
4910 const uint8_t *ciphertext,
4911 size_t ciphertext_length,
4912 uint8_t *plaintext,
4913 size_t plaintext_size,
4914 size_t *plaintext_length )
4915{
mohammad16035955c982018-04-26 00:53:03 +03004916 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004917 aead_operation_t operation;
4918 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004919
Gilles Peskineee652a32018-06-01 19:23:52 +02004920 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004921
Gilles Peskinec5487a82018-12-03 18:08:14 +01004922 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004923 if( status != PSA_SUCCESS )
4924 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004925
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004926 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4927 ciphertext, ciphertext_length,
4928 plaintext_size, &tag );
4929 if( status != PSA_SUCCESS )
4930 goto exit;
4931
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004932#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004933 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004934 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004935 status = mbedtls_to_psa_error(
4936 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4937 ciphertext_length - operation.tag_length,
4938 nonce, nonce_length,
4939 additional_data,
4940 additional_data_length,
4941 tag, operation.tag_length,
4942 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004943 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004944 else
4945#endif /* MBEDTLS_GCM_C */
4946#if defined(MBEDTLS_CCM_C)
4947 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004948 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004949 status = mbedtls_to_psa_error(
4950 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4951 ciphertext_length - operation.tag_length,
4952 nonce, nonce_length,
4953 additional_data,
4954 additional_data_length,
4955 ciphertext, plaintext,
4956 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004957 }
mohammad160339574652018-06-01 04:39:53 -07004958 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004959#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004960#if defined(MBEDTLS_CHACHAPOLY_C)
4961 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4962 {
4963 if( nonce_length != 12 || operation.tag_length != 16 )
4964 {
4965 status = PSA_ERROR_NOT_SUPPORTED;
4966 goto exit;
4967 }
4968 status = mbedtls_to_psa_error(
4969 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4970 ciphertext_length - operation.tag_length,
4971 nonce,
4972 additional_data,
4973 additional_data_length,
4974 tag,
4975 ciphertext,
4976 plaintext ) );
4977 }
4978 else
4979#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004980 {
mohammad1603554faad2018-06-03 15:07:38 +03004981 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004982 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004983
Gilles Peskineedf9a652018-08-17 18:11:56 +02004984 if( status != PSA_SUCCESS && plaintext_size != 0 )
4985 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004986
Gilles Peskineedf9a652018-08-17 18:11:56 +02004987exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004988 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004989 if( status == PSA_SUCCESS )
4990 *plaintext_length = ciphertext_length - operation.tag_length;
4991 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004992}
4993
Gilles Peskinea0655c32018-04-30 17:06:50 +02004994
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004995
Gilles Peskine20035e32018-02-03 22:44:14 +01004996/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004997/* Generators */
4998/****************************************************************/
4999
John Durkop07cc04a2020-11-16 22:08:34 -08005000#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5001 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5002 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5003#define AT_LEAST_ONE_BUILTIN_KDF
5004#endif
5005
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005006#define HKDF_STATE_INIT 0 /* no input yet */
5007#define HKDF_STATE_STARTED 1 /* got salt */
5008#define HKDF_STATE_KEYED 2 /* got key */
5009#define HKDF_STATE_OUTPUT 3 /* output started */
5010
Gilles Peskinecbe66502019-05-16 16:59:18 +02005011static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005013{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5015 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005016 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005017 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018}
5019
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005021{
5022 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005025 {
5026 /* The object has (apparently) been initialized but it is not
5027 * in use. It's ok to call abort on such an object, and there's
5028 * nothing to do. */
5029 }
5030 else
John Durkopd0321952020-10-29 21:37:36 -07005031#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005032 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005033 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 mbedtls_free( operation->ctx.hkdf.info );
5035 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005036 }
John Durkop07cc04a2020-11-16 22:08:34 -08005037 else
5038#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5039#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5040 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5041 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005043 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005044 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005045 if( operation->ctx.tls12_prf.seed != NULL )
5046 {
5047 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5048 operation->ctx.tls12_prf.seed_length );
5049 mbedtls_free( operation->ctx.tls12_prf.seed );
5050 }
5051
5052 if( operation->ctx.tls12_prf.label != NULL )
5053 {
5054 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5055 operation->ctx.tls12_prf.label_length );
5056 mbedtls_free( operation->ctx.tls12_prf.label );
5057 }
5058
5059 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5060
5061 /* We leave the fields Ai and output_block to be erased safely by the
5062 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005063 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005064 else
John Durkop07cc04a2020-11-16 22:08:34 -08005065#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5066 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005067 {
5068 status = PSA_ERROR_BAD_STATE;
5069 }
Janos Follath083036a2019-06-11 10:22:26 +01005070 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005071 return( status );
5072}
5073
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005075 size_t *capacity)
5076{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005078 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005079 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005080 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005081 }
5082
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084 return( PSA_SUCCESS );
5085}
5086
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005088 size_t capacity )
5089{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005091 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005093 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005095 return( PSA_SUCCESS );
5096}
5097
John Durkopd0321952020-10-29 21:37:36 -07005098#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005100 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005101static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005102 psa_algorithm_t hash_alg,
5103 uint8_t *output,
5104 size_t output_length )
5105{
5106 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5107 psa_status_t status;
5108
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5110 return( PSA_ERROR_BAD_STATE );
5111 hkdf->state = HKDF_STATE_OUTPUT;
5112
Gilles Peskinebef7f142018-07-12 17:22:21 +02005113 while( output_length != 0 )
5114 {
5115 /* Copy what remains of the current block */
5116 uint8_t n = hash_length - hkdf->offset_in_block;
5117 if( n > output_length )
5118 n = (uint8_t) output_length;
5119 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5120 output += n;
5121 output_length -= n;
5122 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005123 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005124 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005125 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005126 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005128 * object was corrupted or if this function is called directly
5129 * inside the library. */
5130 if( hkdf->block_number == 0xff )
5131 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005132
5133 /* We need a new block */
5134 ++hkdf->block_number;
5135 hkdf->offset_in_block = 0;
5136 status = psa_hmac_setup_internal( &hkdf->hmac,
5137 hkdf->prk, hash_length,
5138 hash_alg );
5139 if( status != PSA_SUCCESS )
5140 return( status );
5141 if( hkdf->block_number != 1 )
5142 {
5143 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5144 hkdf->output_block,
5145 hash_length );
5146 if( status != PSA_SUCCESS )
5147 return( status );
5148 }
5149 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5150 hkdf->info,
5151 hkdf->info_length );
5152 if( status != PSA_SUCCESS )
5153 return( status );
5154 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5155 &hkdf->block_number, 1 );
5156 if( status != PSA_SUCCESS )
5157 return( status );
5158 status = psa_hmac_finish_internal( &hkdf->hmac,
5159 hkdf->output_block,
5160 sizeof( hkdf->output_block ) );
5161 if( status != PSA_SUCCESS )
5162 return( status );
5163 }
5164
5165 return( PSA_SUCCESS );
5166}
John Durkop07cc04a2020-11-16 22:08:34 -08005167#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005168
John Durkop07cc04a2020-11-16 22:08:34 -08005169#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5170 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005171static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5172 psa_tls12_prf_key_derivation_t *tls12_prf,
5173 psa_algorithm_t alg )
5174{
5175 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5176 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005177 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5178 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005179
Janos Follath7742fee2019-06-17 12:58:10 +01005180 /* We can't be wanting more output after block 0xff, otherwise
5181 * the capacity check in psa_key_derivation_output_bytes() would have
5182 * prevented this call. It could happen only if the operation
5183 * object was corrupted or if this function is called directly
5184 * inside the library. */
5185 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005186 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005187
5188 /* We need a new block */
5189 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005190 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005191
5192 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5193 *
5194 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5195 *
5196 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5197 * HMAC_hash(secret, A(2) + seed) +
5198 * HMAC_hash(secret, A(3) + seed) + ...
5199 *
5200 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005201 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005202 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005203 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005204 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005205 * is currently extracted as `output_block` and where i is
5206 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005207 */
5208
Janos Follathea29bfb2019-06-19 12:21:20 +01005209 /* Save the hash context before using it, to preserve the hash state with
5210 * only the inner padding in it. We need this, because inner padding depends
5211 * on the key (secret in the RFC's terminology). */
5212 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5213 if( status != PSA_SUCCESS )
5214 goto cleanup;
5215
5216 /* Calculate A(i) where i = tls12_prf->block_number. */
5217 if( tls12_prf->block_number == 1 )
5218 {
5219 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5220 * the variable seed and in this instance means it in the context of the
5221 * P_hash function, where seed = label + seed.) */
5222 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5223 tls12_prf->label, tls12_prf->label_length );
5224 if( status != PSA_SUCCESS )
5225 goto cleanup;
5226 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5227 tls12_prf->seed, tls12_prf->seed_length );
5228 if( status != PSA_SUCCESS )
5229 goto cleanup;
5230 }
5231 else
5232 {
5233 /* A(i) = HMAC_hash(secret, A(i-1)) */
5234 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5235 tls12_prf->Ai, hash_length );
5236 if( status != PSA_SUCCESS )
5237 goto cleanup;
5238 }
5239
5240 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5241 tls12_prf->Ai, hash_length );
5242 if( status != PSA_SUCCESS )
5243 goto cleanup;
5244 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5245 if( status != PSA_SUCCESS )
5246 goto cleanup;
5247
5248 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5249 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5250 tls12_prf->Ai, hash_length );
5251 if( status != PSA_SUCCESS )
5252 goto cleanup;
5253 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5254 tls12_prf->label, tls12_prf->label_length );
5255 if( status != PSA_SUCCESS )
5256 goto cleanup;
5257 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5258 tls12_prf->seed, tls12_prf->seed_length );
5259 if( status != PSA_SUCCESS )
5260 goto cleanup;
5261 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5262 tls12_prf->output_block, hash_length );
5263 if( status != PSA_SUCCESS )
5264 goto cleanup;
5265 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5266 if( status != PSA_SUCCESS )
5267 goto cleanup;
5268
Janos Follath7742fee2019-06-17 12:58:10 +01005269
5270cleanup:
5271
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 cleanup_status = psa_hash_abort( &backup );
5273 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5274 status = cleanup_status;
5275
5276 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005277}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005278
Janos Follath844eb0e2019-06-19 12:10:49 +01005279static psa_status_t psa_key_derivation_tls12_prf_read(
5280 psa_tls12_prf_key_derivation_t *tls12_prf,
5281 psa_algorithm_t alg,
5282 uint8_t *output,
5283 size_t output_length )
5284{
5285 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5286 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5287 psa_status_t status;
5288 uint8_t offset, length;
5289
5290 while( output_length != 0 )
5291 {
5292 /* Check if we have fully processed the current block. */
5293 if( tls12_prf->left_in_block == 0 )
5294 {
5295 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5296 alg );
5297 if( status != PSA_SUCCESS )
5298 return( status );
5299
5300 continue;
5301 }
5302
5303 if( tls12_prf->left_in_block > output_length )
5304 length = (uint8_t) output_length;
5305 else
5306 length = tls12_prf->left_in_block;
5307
5308 offset = hash_length - tls12_prf->left_in_block;
5309 memcpy( output, tls12_prf->output_block + offset, length );
5310 output += length;
5311 output_length -= length;
5312 tls12_prf->left_in_block -= length;
5313 }
5314
5315 return( PSA_SUCCESS );
5316}
John Durkop07cc04a2020-11-16 22:08:34 -08005317#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5318 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005319
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005320psa_status_t psa_key_derivation_output_bytes(
5321 psa_key_derivation_operation_t *operation,
5322 uint8_t *output,
5323 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005324{
5325 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005329 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005331 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005332 }
5333
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005335 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005337 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005339 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005340 goto exit;
5341 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005343 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005345 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005346 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5347 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005349 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005350 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005351 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005353
John Durkopd0321952020-10-29 21:37:36 -07005354#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005355 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005356 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005357 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005359 output, output_length );
5360 }
Janos Follathadbec812019-06-14 11:05:39 +01005361 else
John Durkop07cc04a2020-11-16 22:08:34 -08005362#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5363#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5364 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005365 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005366 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005367 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005369 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005370 output_length );
5371 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005372 else
John Durkop07cc04a2020-11-16 22:08:34 -08005373#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5374 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005375 {
5376 return( PSA_ERROR_BAD_STATE );
5377 }
5378
5379exit:
5380 if( status != PSA_SUCCESS )
5381 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005382 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 * This allows us to differentiate between exhausted operations and
5384 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5385 * operations. */
5386 psa_algorithm_t alg = operation->alg;
5387 psa_key_derivation_abort( operation );
5388 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005389 memset( output, '!', output_length );
5390 }
5391 return( status );
5392}
5393
Gilles Peskine08542d82018-07-19 17:05:42 +02005394#if defined(MBEDTLS_DES_C)
5395static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5396{
5397 if( data_size >= 8 )
5398 mbedtls_des_key_set_parity( data );
5399 if( data_size >= 16 )
5400 mbedtls_des_key_set_parity( data + 8 );
5401 if( data_size >= 24 )
5402 mbedtls_des_key_set_parity( data + 16 );
5403}
5404#endif /* MBEDTLS_DES_C */
5405
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005406static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005407 psa_key_slot_t *slot,
5408 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005410{
5411 uint8_t *data = NULL;
5412 size_t bytes = PSA_BITS_TO_BYTES( bits );
5413 psa_status_t status;
5414
Gilles Peskine8e338702019-07-30 20:06:31 +02005415 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005416 return( PSA_ERROR_INVALID_ARGUMENT );
5417 if( bits % 8 != 0 )
5418 return( PSA_ERROR_INVALID_ARGUMENT );
5419 data = mbedtls_calloc( 1, bytes );
5420 if( data == NULL )
5421 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5422
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005423 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005424 if( status != PSA_SUCCESS )
5425 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005426#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005427 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005428 psa_des_set_key_parity( data, bytes );
5429#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005430 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005431
5432exit:
5433 mbedtls_free( data );
5434 return( status );
5435}
5436
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005437psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005438 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005439 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005440{
5441 psa_status_t status;
5442 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005443 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005444
5445 /* Reject any attempt to create a zero-length key so that we don't
5446 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5447 if( psa_get_key_bits( attributes ) == 0 )
5448 return( PSA_ERROR_INVALID_ARGUMENT );
5449
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005450 if( ! operation->can_output_key )
5451 return( PSA_ERROR_NOT_PERMITTED );
5452
Gilles Peskinedf179142019-07-15 22:02:14 +02005453 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5454 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5456 if( driver != NULL )
5457 {
5458 /* Deriving a key in a secure element is not implemented yet. */
5459 status = PSA_ERROR_NOT_SUPPORTED;
5460 }
5461#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005462 if( status == PSA_SUCCESS )
5463 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005464 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005465 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005467 }
5468 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005469 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005470 if( status != PSA_SUCCESS )
5471 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005472 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005473 *handle = 0;
5474 }
5475 return( status );
5476}
5477
Gilles Peskineeab56e42018-07-12 17:12:33 +02005478
5479
5480/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005481/* Key derivation */
5482/****************************************************************/
5483
John Durkop07cc04a2020-11-16 22:08:34 -08005484#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005485static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005487 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005488{
John Durkop07cc04a2020-11-16 22:08:34 -08005489 int is_kdf_alg_supported;
5490
Janos Follath5fe19732019-06-20 15:09:30 +01005491 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5492 * initialisers for this union leave some bytes unspecified.) */
5493 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5494
Gilles Peskine969c5d62019-01-16 15:53:06 +01005495 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005496#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005497 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5498 is_kdf_alg_supported = 1;
5499 else
5500#endif
5501#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5502 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5503 is_kdf_alg_supported = 1;
5504 else
5505#endif
5506#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5507 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5508 is_kdf_alg_supported = 1;
5509 else
5510#endif
5511 is_kdf_alg_supported = 0;
5512
5513 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005514 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005515 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005516 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5517 if( hash_size == 0 )
5518 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005519 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5520 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005521 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005522 {
5523 return( PSA_ERROR_NOT_SUPPORTED );
5524 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005526 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005527 }
John Durkop07cc04a2020-11-16 22:08:34 -08005528
5529 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005530}
John Durkop07cc04a2020-11-16 22:08:34 -08005531#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005532
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005534 psa_algorithm_t alg )
5535{
5536 psa_status_t status;
5537
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005538 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005539 return( PSA_ERROR_BAD_STATE );
5540
Gilles Peskine6843c292019-01-18 16:44:49 +01005541 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5542 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005543#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005544 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005545 {
5546 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005547 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005548 }
5549 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5550 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005552 }
John Durkop07cc04a2020-11-16 22:08:34 -08005553#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005554 else
5555 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005556
5557 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005559 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005560}
5561
John Durkopd0321952020-10-29 21:37:36 -07005562#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005563static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005564 psa_algorithm_t hash_alg,
5565 psa_key_derivation_step_t step,
5566 const uint8_t *data,
5567 size_t data_length )
5568{
5569 psa_status_t status;
5570 switch( step )
5571 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005572 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005573 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005575 status = psa_hmac_setup_internal( &hkdf->hmac,
5576 data, data_length,
5577 hash_alg );
5578 if( status != PSA_SUCCESS )
5579 return( status );
5580 hkdf->state = HKDF_STATE_STARTED;
5581 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005582 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005583 /* If no salt was provided, use an empty salt. */
5584 if( hkdf->state == HKDF_STATE_INIT )
5585 {
5586 status = psa_hmac_setup_internal( &hkdf->hmac,
5587 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005588 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005589 if( status != PSA_SUCCESS )
5590 return( status );
5591 hkdf->state = HKDF_STATE_STARTED;
5592 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005593 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005594 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005595 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5596 data, data_length );
5597 if( status != PSA_SUCCESS )
5598 return( status );
5599 status = psa_hmac_finish_internal( &hkdf->hmac,
5600 hkdf->prk,
5601 sizeof( hkdf->prk ) );
5602 if( status != PSA_SUCCESS )
5603 return( status );
5604 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5605 hkdf->block_number = 0;
5606 hkdf->state = HKDF_STATE_KEYED;
5607 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005608 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005609 if( hkdf->state == HKDF_STATE_OUTPUT )
5610 return( PSA_ERROR_BAD_STATE );
5611 if( hkdf->info_set )
5612 return( PSA_ERROR_BAD_STATE );
5613 hkdf->info_length = data_length;
5614 if( data_length != 0 )
5615 {
5616 hkdf->info = mbedtls_calloc( 1, data_length );
5617 if( hkdf->info == NULL )
5618 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5619 memcpy( hkdf->info, data, data_length );
5620 }
5621 hkdf->info_set = 1;
5622 return( PSA_SUCCESS );
5623 default:
5624 return( PSA_ERROR_INVALID_ARGUMENT );
5625 }
5626}
John Durkop07cc04a2020-11-16 22:08:34 -08005627#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005628
John Durkop07cc04a2020-11-16 22:08:34 -08005629#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5630 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005631static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5632 const uint8_t *data,
5633 size_t data_length )
5634{
5635 if( prf->state != TLS12_PRF_STATE_INIT )
5636 return( PSA_ERROR_BAD_STATE );
5637
Janos Follathd6dce9f2019-07-04 09:11:38 +01005638 if( data_length != 0 )
5639 {
5640 prf->seed = mbedtls_calloc( 1, data_length );
5641 if( prf->seed == NULL )
5642 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005643
Janos Follathd6dce9f2019-07-04 09:11:38 +01005644 memcpy( prf->seed, data, data_length );
5645 prf->seed_length = data_length;
5646 }
Janos Follathf08e2652019-06-13 09:05:41 +01005647
5648 prf->state = TLS12_PRF_STATE_SEED_SET;
5649
5650 return( PSA_SUCCESS );
5651}
5652
Janos Follath81550542019-06-13 14:26:34 +01005653static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5654 psa_algorithm_t hash_alg,
5655 const uint8_t *data,
5656 size_t data_length )
5657{
5658 psa_status_t status;
5659 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5660 return( PSA_ERROR_BAD_STATE );
5661
5662 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5663 if( status != PSA_SUCCESS )
5664 return( status );
5665
5666 prf->state = TLS12_PRF_STATE_KEY_SET;
5667
5668 return( PSA_SUCCESS );
5669}
5670
Janos Follath63028dd2019-06-13 09:15:47 +01005671static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5672 const uint8_t *data,
5673 size_t data_length )
5674{
5675 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5676 return( PSA_ERROR_BAD_STATE );
5677
Janos Follathd6dce9f2019-07-04 09:11:38 +01005678 if( data_length != 0 )
5679 {
5680 prf->label = mbedtls_calloc( 1, data_length );
5681 if( prf->label == NULL )
5682 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005683
Janos Follathd6dce9f2019-07-04 09:11:38 +01005684 memcpy( prf->label, data, data_length );
5685 prf->label_length = data_length;
5686 }
Janos Follath63028dd2019-06-13 09:15:47 +01005687
5688 prf->state = TLS12_PRF_STATE_LABEL_SET;
5689
5690 return( PSA_SUCCESS );
5691}
5692
Janos Follathb03233e2019-06-11 15:30:30 +01005693static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5694 psa_algorithm_t hash_alg,
5695 psa_key_derivation_step_t step,
5696 const uint8_t *data,
5697 size_t data_length )
5698{
Janos Follathb03233e2019-06-11 15:30:30 +01005699 switch( step )
5700 {
Janos Follathf08e2652019-06-13 09:05:41 +01005701 case PSA_KEY_DERIVATION_INPUT_SEED:
5702 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005703 case PSA_KEY_DERIVATION_INPUT_SECRET:
5704 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005705 case PSA_KEY_DERIVATION_INPUT_LABEL:
5706 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005707 default:
5708 return( PSA_ERROR_INVALID_ARGUMENT );
5709 }
5710}
John Durkop07cc04a2020-11-16 22:08:34 -08005711#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5712 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5713
5714#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5715static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5716 psa_tls12_prf_key_derivation_t *prf,
5717 psa_algorithm_t hash_alg,
5718 const uint8_t *data,
5719 size_t data_length )
5720{
5721 psa_status_t status;
5722 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5723 uint8_t *cur = pms;
5724
5725 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5726 return( PSA_ERROR_INVALID_ARGUMENT );
5727
5728 /* Quoting RFC 4279, Section 2:
5729 *
5730 * The premaster secret is formed as follows: if the PSK is N octets
5731 * long, concatenate a uint16 with the value N, N zero octets, a second
5732 * uint16 with the value N, and the PSK itself.
5733 */
5734
5735 *cur++ = ( data_length >> 8 ) & 0xff;
5736 *cur++ = ( data_length >> 0 ) & 0xff;
5737 memset( cur, 0, data_length );
5738 cur += data_length;
5739 *cur++ = pms[0];
5740 *cur++ = pms[1];
5741 memcpy( cur, data, data_length );
5742 cur += data_length;
5743
5744 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5745
5746 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5747 return( status );
5748}
Janos Follath6660f0e2019-06-17 08:44:03 +01005749
5750static psa_status_t psa_tls12_prf_psk_to_ms_input(
5751 psa_tls12_prf_key_derivation_t *prf,
5752 psa_algorithm_t hash_alg,
5753 psa_key_derivation_step_t step,
5754 const uint8_t *data,
5755 size_t data_length )
5756{
5757 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005758 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005759 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5760 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005761 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005762
5763 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5764}
John Durkop07cc04a2020-11-16 22:08:34 -08005765#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005766
Gilles Peskineb8965192019-09-24 16:21:10 +02005767/** Check whether the given key type is acceptable for the given
5768 * input step of a key derivation.
5769 *
5770 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5771 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5772 * Both secret and non-secret inputs can alternatively have the type
5773 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5774 * that the input was passed as a buffer rather than via a key object.
5775 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005776static int psa_key_derivation_check_input_type(
5777 psa_key_derivation_step_t step,
5778 psa_key_type_t key_type )
5779{
5780 switch( step )
5781 {
5782 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005783 if( key_type == PSA_KEY_TYPE_DERIVE )
5784 return( PSA_SUCCESS );
5785 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005786 return( PSA_SUCCESS );
5787 break;
5788 case PSA_KEY_DERIVATION_INPUT_LABEL:
5789 case PSA_KEY_DERIVATION_INPUT_SALT:
5790 case PSA_KEY_DERIVATION_INPUT_INFO:
5791 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005792 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5793 return( PSA_SUCCESS );
5794 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005795 return( PSA_SUCCESS );
5796 break;
5797 }
5798 return( PSA_ERROR_INVALID_ARGUMENT );
5799}
5800
Janos Follathb80a94e2019-06-12 15:54:46 +01005801static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005803 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005804 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005805 const uint8_t *data,
5806 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005807{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005808 psa_status_t status;
5809 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5810
5811 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005812 if( status != PSA_SUCCESS )
5813 goto exit;
5814
John Durkopd0321952020-10-29 21:37:36 -07005815#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005816 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005817 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005818 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005819 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005820 step, data, data_length );
5821 }
John Durkop07cc04a2020-11-16 22:08:34 -08005822 else
5823#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5824#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5825 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005826 {
Janos Follathb03233e2019-06-11 15:30:30 +01005827 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5828 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5829 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005830 }
John Durkop07cc04a2020-11-16 22:08:34 -08005831 else
5832#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5833#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5834 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005835 {
5836 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5837 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5838 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005839 }
5840 else
John Durkop07cc04a2020-11-16 22:08:34 -08005841#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005842 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005843 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005844 return( PSA_ERROR_BAD_STATE );
5845 }
5846
Gilles Peskine224b0d62019-09-23 18:13:17 +02005847exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005848 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005849 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005850 return( status );
5851}
5852
Janos Follath51f4a0f2019-06-14 11:35:55 +01005853psa_status_t psa_key_derivation_input_bytes(
5854 psa_key_derivation_operation_t *operation,
5855 psa_key_derivation_step_t step,
5856 const uint8_t *data,
5857 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005858{
Gilles Peskineb8965192019-09-24 16:21:10 +02005859 return( psa_key_derivation_input_internal( operation, step,
5860 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005861 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005862}
5863
Janos Follath51f4a0f2019-06-14 11:35:55 +01005864psa_status_t psa_key_derivation_input_key(
5865 psa_key_derivation_operation_t *operation,
5866 psa_key_derivation_step_t step,
5867 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005868{
5869 psa_key_slot_t *slot;
5870 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005871
Gilles Peskine28f8f302019-07-24 13:30:31 +02005872 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005873 PSA_KEY_USAGE_DERIVE,
5874 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005875 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005876 {
5877 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005878 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005879 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005880
5881 /* Passing a key object as a SECRET input unlocks the permission
5882 * to output to a key object. */
5883 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5884 operation->can_output_key = 1;
5885
Janos Follathb80a94e2019-06-12 15:54:46 +01005886 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005887 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005888 slot->data.key.data,
5889 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005890}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005891
5892
5893
5894/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005895/* Key agreement */
5896/****************************************************************/
5897
John Durkop6ba40d12020-11-10 08:50:04 -08005898#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005899static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5900 size_t peer_key_length,
5901 const mbedtls_ecp_keypair *our_key,
5902 uint8_t *shared_secret,
5903 size_t shared_secret_size,
5904 size_t *shared_secret_length )
5905{
Steven Cooremand4867872020-08-05 16:31:39 +02005906 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005907 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005908 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005909 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005910 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005911 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005912
Steven Cooreman4fed4552020-08-03 14:46:03 +02005913 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5914 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005915 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005916 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005917 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005918 goto exit;
5919
Jaeden Amero97271b32019-01-10 19:38:51 +00005920 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005921 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005922 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005923 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005924 status = mbedtls_to_psa_error(
5925 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5926 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005927 goto exit;
5928
Jaeden Amero97271b32019-01-10 19:38:51 +00005929 status = mbedtls_to_psa_error(
5930 mbedtls_ecdh_calc_secret( &ecdh,
5931 shared_secret_length,
5932 shared_secret, shared_secret_size,
5933 mbedtls_ctr_drbg_random,
5934 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005935 if( status != PSA_SUCCESS )
5936 goto exit;
5937 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5938 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005939
5940exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005941 if( status != PSA_SUCCESS )
5942 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005943 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005944 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005945 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005946
Jaeden Amero97271b32019-01-10 19:38:51 +00005947 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005948}
John Durkop6ba40d12020-11-10 08:50:04 -08005949#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005950
Gilles Peskine01d718c2018-09-18 12:01:02 +02005951#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5952
Gilles Peskine0216fe12019-04-11 21:23:21 +02005953static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5954 psa_key_slot_t *private_key,
5955 const uint8_t *peer_key,
5956 size_t peer_key_length,
5957 uint8_t *shared_secret,
5958 size_t shared_secret_size,
5959 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005960{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005961 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005962 {
John Durkop6ba40d12020-11-10 08:50:04 -08005963#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005964 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005965 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005966 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005967 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005968 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005969 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005970 private_key->data.key.data,
5971 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005972 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005973 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005974 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005975 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005976 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005977 shared_secret, shared_secret_size,
5978 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005979 mbedtls_ecp_keypair_free( ecp );
5980 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005981 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005982#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005983 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005984 (void) private_key;
5985 (void) peer_key;
5986 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005987 (void) shared_secret;
5988 (void) shared_secret_size;
5989 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005990 return( PSA_ERROR_NOT_SUPPORTED );
5991 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005992}
5993
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005994/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005995 * to potentially free embedded data structures and wipe confidential data.
5996 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005997static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005998 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005999 psa_key_slot_t *private_key,
6000 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006001 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006002{
6003 psa_status_t status;
6004 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6005 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006006 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006007
6008 /* Step 1: run the secret agreement algorithm to generate the shared
6009 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006010 status = psa_key_agreement_raw_internal( ka_alg,
6011 private_key,
6012 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006013 shared_secret,
6014 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006015 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006016 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006017 goto exit;
6018
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006019 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006020 * the shared secret. A shared secret is permitted wherever a key
6021 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006022 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006023 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006024 shared_secret,
6025 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006026exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006027 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006028 return( status );
6029}
6030
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006031psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006032 psa_key_derivation_step_t step,
6033 psa_key_handle_t private_key,
6034 const uint8_t *peer_key,
6035 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006036{
Gilles Peskine2f060a82018-12-04 17:12:32 +01006037 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02006038 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006039 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006040 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006041 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006042 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006043 if( status != PSA_SUCCESS )
6044 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006045 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006046 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006047 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006048 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006049 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006050 else
6051 {
6052 /* If a private key has been added as SECRET, we allow the derived
6053 * key material to be used as a key in PSA Crypto. */
6054 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6055 operation->can_output_key = 1;
6056 }
Gilles Peskine346797d2018-11-16 16:05:06 +01006057 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006058}
6059
Gilles Peskinebe697d82019-05-16 18:00:41 +02006060psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
6061 psa_key_handle_t private_key,
6062 const uint8_t *peer_key,
6063 size_t peer_key_length,
6064 uint8_t *output,
6065 size_t output_size,
6066 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006067{
6068 psa_key_slot_t *slot;
6069 psa_status_t status;
6070
6071 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6072 {
6073 status = PSA_ERROR_INVALID_ARGUMENT;
6074 goto exit;
6075 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006076 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006077 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006078 if( status != PSA_SUCCESS )
6079 goto exit;
6080
6081 status = psa_key_agreement_raw_internal( alg, slot,
6082 peer_key, peer_key_length,
6083 output, output_size,
6084 output_length );
6085
6086exit:
6087 if( status != PSA_SUCCESS )
6088 {
6089 /* If an error happens and is not handled properly, the output
6090 * may be used as a key to protect sensitive data. Arrange for such
6091 * a key to be random, which is likely to result in decryption or
6092 * verification errors. This is better than filling the buffer with
6093 * some constant data such as zeros, which would result in the data
6094 * being protected with a reproducible, easily knowable key.
6095 */
6096 psa_generate_random( output, output_size );
6097 *output_length = output_size;
6098 }
6099 return( status );
6100}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006101
6102
6103/****************************************************************/
6104/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006105/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006106
Gilles Peskine4c317f42018-07-12 01:24:09 +02006107psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006108 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006109{
Janos Follath24eed8d2019-11-22 13:21:35 +00006110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006111 GUARD_MODULE_INITIALIZED;
6112
Gilles Peskinef181eca2019-08-07 13:49:00 +02006113 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6114 {
6115 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6116 output,
6117 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6118 if( ret != 0 )
6119 return( mbedtls_to_psa_error( ret ) );
6120 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6121 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6122 }
6123
Gilles Peskinee59236f2018-01-27 23:32:46 +01006124 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6125 return( mbedtls_to_psa_error( ret ) );
6126}
6127
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006128#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6129#include "mbedtls/entropy_poll.h"
6130
Gilles Peskine7228da22019-07-15 11:06:15 +02006131psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006132 size_t seed_size )
6133{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006134 if( global_data.initialized )
6135 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006136
6137 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6138 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6139 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6140 return( PSA_ERROR_INVALID_ARGUMENT );
6141
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006142 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006143}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006144#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006145
John Durkop07cc04a2020-11-16 22:08:34 -08006146#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006147static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6148 size_t domain_parameters_size,
6149 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006151 size_t i;
6152 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006153
Gilles Peskinee56e8782019-04-26 17:34:02 +02006154 if( domain_parameters_size == 0 )
6155 {
6156 *exponent = 65537;
6157 return( PSA_SUCCESS );
6158 }
6159
6160 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6161 * support values that fit in a 32-bit integer, which is larger than
6162 * int on just about every platform anyway. */
6163 if( domain_parameters_size > sizeof( acc ) )
6164 return( PSA_ERROR_NOT_SUPPORTED );
6165 for( i = 0; i < domain_parameters_size; i++ )
6166 acc = ( acc << 8 ) | domain_parameters[i];
6167 if( acc > INT_MAX )
6168 return( PSA_ERROR_NOT_SUPPORTED );
6169 *exponent = acc;
6170 return( PSA_SUCCESS );
6171}
John Durkop07cc04a2020-11-16 22:08:34 -08006172#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006173
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006174static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006175 psa_key_slot_t *slot, size_t bits,
6176 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006177{
Gilles Peskine8e338702019-07-30 20:06:31 +02006178 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006179
Gilles Peskinee56e8782019-04-26 17:34:02 +02006180 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 return( PSA_ERROR_INVALID_ARGUMENT );
6182
Gilles Peskinee59236f2018-01-27 23:32:46 +01006183 if( key_type_is_raw_bytes( type ) )
6184 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006185 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006186
6187 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006188 if( status != PSA_SUCCESS )
6189 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006190
6191 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006192 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6193 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006194 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006195
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006196 status = psa_generate_random( slot->data.key.data,
6197 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006199 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006200
6201 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006202#if defined(MBEDTLS_DES_C)
6203 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006204 psa_des_set_key_parity( slot->data.key.data,
6205 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006206#endif /* MBEDTLS_DES_C */
6207 }
6208 else
6209
John Durkop07cc04a2020-11-16 22:08:34 -08006210#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006211 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006212 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006213 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006215 int exponent;
6216 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006217 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6218 return( PSA_ERROR_NOT_SUPPORTED );
6219 /* Accept only byte-aligned keys, for the same reasons as
6220 * in psa_import_rsa_key(). */
6221 if( bits % 8 != 0 )
6222 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006223 status = psa_read_rsa_exponent( domain_parameters,
6224 domain_parameters_size,
6225 &exponent );
6226 if( status != PSA_SUCCESS )
6227 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006228 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6229 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006230 mbedtls_ctr_drbg_random,
6231 &global_data.ctr_drbg,
6232 (unsigned int) bits,
6233 exponent );
6234 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006235 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006236
6237 /* Make sure to always have an export representation available */
6238 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6239
Steven Cooreman75b74362020-07-28 14:30:13 +02006240 status = psa_allocate_buffer_to_slot( slot, bytes );
6241 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006242 {
6243 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006244 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006245 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006246
6247 status = psa_export_rsa_key( type,
6248 &rsa,
6249 slot->data.key.data,
6250 bytes,
6251 &slot->data.key.bytes );
6252 mbedtls_rsa_free( &rsa );
6253 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006254 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006255 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006256 }
6257 else
John Durkop07cc04a2020-11-16 22:08:34 -08006258#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006259
John Durkop9814fa22020-11-04 12:28:15 -08006260#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006261 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006262 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006263 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006264 mbedtls_ecp_group_id grp_id =
6265 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006266 const mbedtls_ecp_curve_info *curve_info =
6267 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006268 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006270 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006271 return( PSA_ERROR_NOT_SUPPORTED );
6272 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6273 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006274 mbedtls_ecp_keypair_init( &ecp );
6275 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006276 mbedtls_ctr_drbg_random,
6277 &global_data.ctr_drbg );
6278 if( ret != 0 )
6279 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006280 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006281 return( mbedtls_to_psa_error( ret ) );
6282 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006283
6284
6285 /* Make sure to always have an export representation available */
6286 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006287 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6288 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006289 {
6290 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006291 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006292 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006293
6294 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006295 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6296
6297 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006298 if( status != PSA_SUCCESS ) {
6299 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006300 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006301 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006302 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006303 }
6304 else
John Durkop9814fa22020-11-04 12:28:15 -08006305#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006306 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006307 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006308 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006309
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006310 return( PSA_SUCCESS );
6311}
Darryl Green0c6575a2018-11-07 16:05:30 +00006312
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006313psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006314 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006315{
6316 psa_status_t status;
6317 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006318 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006319
Gilles Peskine0f84d622019-09-12 19:03:13 +02006320 /* Reject any attempt to create a zero-length key so that we don't
6321 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6322 if( psa_get_key_bits( attributes ) == 0 )
6323 return( PSA_ERROR_INVALID_ARGUMENT );
6324
Gilles Peskinedf179142019-07-15 22:02:14 +02006325 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6326 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006327 if( status != PSA_SUCCESS )
6328 goto exit;
6329
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006330 status = psa_driver_wrapper_generate_key( attributes,
6331 slot );
6332 if( status != PSA_ERROR_NOT_SUPPORTED ||
6333 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6334 goto exit;
6335
6336 status = psa_generate_key_internal(
6337 slot, attributes->core.bits,
6338 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006339
6340exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006341 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006342 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006343 if( status != PSA_SUCCESS )
6344 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006345 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006346 *handle = 0;
6347 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006348 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006349}
6350
6351
Gilles Peskinee59236f2018-01-27 23:32:46 +01006352
6353/****************************************************************/
6354/* Module setup */
6355/****************************************************************/
6356
Gilles Peskine5e769522018-11-20 21:59:56 +01006357psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6358 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6359 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6360{
6361 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6362 return( PSA_ERROR_BAD_STATE );
6363 global_data.entropy_init = entropy_init;
6364 global_data.entropy_free = entropy_free;
6365 return( PSA_SUCCESS );
6366}
6367
Gilles Peskinee59236f2018-01-27 23:32:46 +01006368void mbedtls_psa_crypto_free( void )
6369{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006370 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006371 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6372 {
6373 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006374 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006375 }
6376 /* Wipe all remaining data, including configuration.
6377 * In particular, this sets all state indicator to the value
6378 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006379 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006380#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006381 /* Unregister all secure element drivers, so that we restart from
6382 * a pristine state. */
6383 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006385}
6386
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006387#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6388/** Recover a transaction that was interrupted by a power failure.
6389 *
6390 * This function is called during initialization, before psa_crypto_init()
6391 * returns. If this function returns a failure status, the initialization
6392 * fails.
6393 */
6394static psa_status_t psa_crypto_recover_transaction(
6395 const psa_crypto_transaction_t *transaction )
6396{
6397 switch( transaction->unknown.type )
6398 {
6399 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6400 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006401 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006402 * is implemented.
6403 * https://github.com/ARMmbed/mbed-crypto/issues/218
6404 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006405 default:
6406 /* We found an unsupported transaction in the storage.
6407 * We don't know what state the storage is in. Give up. */
6408 return( PSA_ERROR_STORAGE_FAILURE );
6409 }
6410}
6411#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6412
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413psa_status_t psa_crypto_init( void )
6414{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006415 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006416 const unsigned char drbg_seed[] = "PSA";
6417
Gilles Peskinec6b69072018-11-20 21:42:52 +01006418 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419 if( global_data.initialized != 0 )
6420 return( PSA_SUCCESS );
6421
Gilles Peskine5e769522018-11-20 21:59:56 +01006422 /* Set default configuration if
6423 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6424 if( global_data.entropy_init == NULL )
6425 global_data.entropy_init = mbedtls_entropy_init;
6426 if( global_data.entropy_free == NULL )
6427 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428
Gilles Peskinec6b69072018-11-20 21:42:52 +01006429 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006430 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006431#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6432 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6433 /* The PSA entropy injection feature depends on using NV seed as an entropy
6434 * source. Add NV seed as an entropy source for PSA entropy injection. */
6435 mbedtls_entropy_add_source( &global_data.entropy,
6436 mbedtls_nv_seed_poll, NULL,
6437 MBEDTLS_ENTROPY_BLOCK_SIZE,
6438 MBEDTLS_ENTROPY_SOURCE_STRONG );
6439#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006441 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006442 status = mbedtls_to_psa_error(
6443 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6444 mbedtls_entropy_func,
6445 &global_data.entropy,
6446 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6447 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006448 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006449 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006450
Gilles Peskine66fb1262018-12-10 16:29:04 +01006451 status = psa_initialize_key_slots( );
6452 if( status != PSA_SUCCESS )
6453 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006454
Gilles Peskined9348f22019-10-01 15:22:29 +02006455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6456 status = psa_init_all_se_drivers( );
6457 if( status != PSA_SUCCESS )
6458 goto exit;
6459#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6460
Gilles Peskine4b734222019-07-24 15:56:31 +02006461#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006462 status = psa_crypto_load_transaction( );
6463 if( status == PSA_SUCCESS )
6464 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006465 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6466 if( status != PSA_SUCCESS )
6467 goto exit;
6468 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006469 }
6470 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6471 {
6472 /* There's no transaction to complete. It's all good. */
6473 status = PSA_SUCCESS;
6474 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006475#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006476
Gilles Peskinec6b69072018-11-20 21:42:52 +01006477 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006478 global_data.initialized = 1;
6479
6480exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006481 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006482 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006483 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006484}
6485
6486#endif /* MBEDTLS_PSA_CRYPTO_C */