blob: 235ab3181beb6c45bf01dcd02ab51747156a0b08 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Steven Cooreman01164162020-07-20 15:31:37 +0200132psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200206 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
John Durkop07cc04a2020-11-16 22:08:34 -0800377/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
378 * current test driver in key_management.c is using this function
379 * when accelerators are used for ECC key pair and public key.
380 * Once that dependency is resolved these guards can be removed.
381 */
John Durkop9814fa22020-11-04 12:28:15 -0800382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800383 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
384 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
385 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100386mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100387 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200388{
389 switch( curve )
390 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100391 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100392 switch( byte_length )
393 {
394 case PSA_BITS_TO_BYTES( 192 ):
395 return( MBEDTLS_ECP_DP_SECP192R1 );
396 case PSA_BITS_TO_BYTES( 224 ):
397 return( MBEDTLS_ECP_DP_SECP224R1 );
398 case PSA_BITS_TO_BYTES( 256 ):
399 return( MBEDTLS_ECP_DP_SECP256R1 );
400 case PSA_BITS_TO_BYTES( 384 ):
401 return( MBEDTLS_ECP_DP_SECP384R1 );
402 case PSA_BITS_TO_BYTES( 521 ):
403 return( MBEDTLS_ECP_DP_SECP521R1 );
404 default:
405 return( MBEDTLS_ECP_DP_NONE );
406 }
407 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100408
Paul Elliott8ff510a2020-06-02 17:19:28 +0100409 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100410 switch( byte_length )
411 {
412 case PSA_BITS_TO_BYTES( 256 ):
413 return( MBEDTLS_ECP_DP_BP256R1 );
414 case PSA_BITS_TO_BYTES( 384 ):
415 return( MBEDTLS_ECP_DP_BP384R1 );
416 case PSA_BITS_TO_BYTES( 512 ):
417 return( MBEDTLS_ECP_DP_BP512R1 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 255 ):
427 return( MBEDTLS_ECP_DP_CURVE25519 );
428 case PSA_BITS_TO_BYTES( 448 ):
429 return( MBEDTLS_ECP_DP_CURVE448 );
430 default:
431 return( MBEDTLS_ECP_DP_NONE );
432 }
433 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100434
Paul Elliott8ff510a2020-06-02 17:19:28 +0100435 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100436 switch( byte_length )
437 {
438 case PSA_BITS_TO_BYTES( 192 ):
439 return( MBEDTLS_ECP_DP_SECP192K1 );
440 case PSA_BITS_TO_BYTES( 224 ):
441 return( MBEDTLS_ECP_DP_SECP224K1 );
442 case PSA_BITS_TO_BYTES( 256 ):
443 return( MBEDTLS_ECP_DP_SECP256K1 );
444 default:
445 return( MBEDTLS_ECP_DP_NONE );
446 }
447 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100448
Gilles Peskine12313cd2018-06-20 00:20:32 +0200449 default:
450 return( MBEDTLS_ECP_DP_NONE );
451 }
452}
John Durkop9814fa22020-11-04 12:28:15 -0800453#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800454 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
455 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
456 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200457
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200458static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
459 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460{
461 /* Check that the bit size is acceptable for the key type */
462 switch( type )
463 {
464 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200466 case PSA_KEY_TYPE_DERIVE:
467 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468#if defined(MBEDTLS_AES_C)
469 case PSA_KEY_TYPE_AES:
470 if( bits != 128 && bits != 192 && bits != 256 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_CAMELLIA_C)
475 case PSA_KEY_TYPE_CAMELLIA:
476 if( bits != 128 && bits != 192 && bits != 256 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
480#if defined(MBEDTLS_DES_C)
481 case PSA_KEY_TYPE_DES:
482 if( bits != 64 && bits != 128 && bits != 192 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
486#if defined(MBEDTLS_ARC4_C)
487 case PSA_KEY_TYPE_ARC4:
488 if( bits < 8 || bits > 2048 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
490 break;
491#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200492#if defined(MBEDTLS_CHACHA20_C)
493 case PSA_KEY_TYPE_CHACHA20:
494 if( bits != 256 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
496 break;
497#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 default:
499 return( PSA_ERROR_NOT_SUPPORTED );
500 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200501 if( bits % 8 != 0 )
502 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 return( PSA_SUCCESS );
505}
506
John Durkop0e005192020-10-31 22:06:54 -0700507#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
508 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
509 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800510 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
511 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
512 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200513
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Steven Cooreman7f391872020-07-30 14:57:44 +0200536/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200538 * \param[in] type The type of key contained in \p data.
539 * \param[in] data The buffer from which to load the representation.
540 * \param[in] data_length The size in bytes of \p data.
541 * \param[out] p_rsa Returns a pointer to an RSA context on success.
542 * The caller is responsible for freeing both the
543 * contents of the context and the context itself
544 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200545 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200546static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
547 const uint8_t *data,
548 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200549 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000551 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200552 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200554 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555
556 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200557 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200559 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000561 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200562 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( status != PSA_SUCCESS )
564 goto exit;
565
566 /* We have something that the pkparse module recognizes. If it is a
567 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200569 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000570 status = PSA_ERROR_INVALID_ARGUMENT;
571 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200572 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000573
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000574 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
575 * supports non-byte-aligned key sizes, but not well. For example,
576 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200577 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
579 {
580 status = PSA_ERROR_NOT_SUPPORTED;
581 goto exit;
582 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200583 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 if( status != PSA_SUCCESS )
585 goto exit;
586
Steven Cooremana2371e52020-07-28 14:30:39 +0200587 /* Copy out the pointer to the RSA context, and reset the PK context
588 * such that pk_free doesn't free the RSA context we just grabbed. */
589 *p_rsa = mbedtls_pk_rsa( ctx );
590 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000591
592exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200593 mbedtls_pk_free( &ctx );
594 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200595}
596
John Durkop6ba40d12020-11-10 08:50:04 -0800597#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
598 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
599 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
600 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
601 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
602 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
603
604#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
605 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
606
Steven Cooremana01795d2020-07-24 22:48:15 +0200607/** Export an RSA key to export representation
608 *
609 * \param[in] type The type of key (public/private) to export
610 * \param[in] rsa The internal RSA representation from which to export
611 * \param[out] data The buffer to export to
612 * \param[in] data_size The length of the buffer to export to
613 * \param[out] data_length The amount of bytes written to \p data
614 */
615static psa_status_t psa_export_rsa_key( psa_key_type_t type,
616 mbedtls_rsa_context *rsa,
617 uint8_t *data,
618 size_t data_size,
619 size_t *data_length )
620{
621#if defined(MBEDTLS_PK_WRITE_C)
622 int ret;
623 mbedtls_pk_context pk;
624 uint8_t *pos = data + data_size;
625
626 mbedtls_pk_init( &pk );
627 pk.pk_info = &mbedtls_rsa_info;
628 pk.pk_ctx = rsa;
629
630 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200631 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
632 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200633 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
634 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
635 else
636 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
637
638 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200639 {
640 /* Clean up in case pk_write failed halfway through. */
641 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200642 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200643 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200644
645 /* The mbedtls_pk_xxx functions write to the end of the buffer.
646 * Move the data to the beginning and erase remaining data
647 * at the original location. */
648 if( 2 * (size_t) ret <= data_size )
649 {
650 memcpy( data, data + data_size - ret, ret );
651 memset( data + data_size - ret, 0, ret );
652 }
653 else if( (size_t) ret < data_size )
654 {
655 memmove( data, data + data_size - ret, ret );
656 memset( data + ret, 0, data_size - ret );
657 }
658
659 *data_length = ret;
660 return( PSA_SUCCESS );
661#else
662 (void) type;
663 (void) rsa;
664 (void) data;
665 (void) data_size;
666 (void) data_length;
667 return( PSA_ERROR_NOT_SUPPORTED );
668#endif /* MBEDTLS_PK_WRITE_C */
669}
670
671/** Import an RSA key from import representation to a slot
672 *
673 * \param[in,out] slot The slot where to store the export representation to
674 * \param[in] data The buffer containing the import representation
675 * \param[in] data_length The amount of bytes in \p data
676 */
677static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
678 const uint8_t *data,
679 size_t data_length )
680{
681 psa_status_t status;
682 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200683 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200684
Steven Cooremana01795d2020-07-24 22:48:15 +0200685 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200686 status = psa_load_rsa_representation( slot->attr.type,
687 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200688 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200689 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200690 if( status != PSA_SUCCESS )
691 goto exit;
692
693 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200694 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200695
Steven Cooreman75b74362020-07-28 14:30:13 +0200696 /* Re-export the data to PSA export format, such that we can store export
697 * representation in the key slot. Export representation in case of RSA is
698 * the smallest representation that's allowed as input, so a straight-up
699 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200700 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200701 if( output == NULL )
702 {
703 status = PSA_ERROR_INSUFFICIENT_MEMORY;
704 goto exit;
705 }
706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200708 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200709 output,
710 data_length,
711 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200712exit:
713 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200714 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200715 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200716
717 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000718 if( status != PSA_SUCCESS )
719 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200720 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000721 return( status );
722 }
723
Steven Cooremana01795d2020-07-24 22:48:15 +0200724 /* On success, store the allocated export-formatted key. */
725 slot->data.key.data = output;
726 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000727
728 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200729}
John Durkop6ba40d12020-11-10 08:50:04 -0800730
731#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800732 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200733
John Durkop9814fa22020-11-04 12:28:15 -0800734#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800735 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
736 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
737 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
738 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200739/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200740 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200741 * \param[in] type The type of key contained in \p data.
742 * \param[in] data The buffer from which to load the representation.
743 * \param[in] data_length The size in bytes of \p data.
744 * \param[out] p_ecp Returns a pointer to an ECP context on success.
745 * The caller is responsible for freeing both the
746 * contents of the context and the context itself
747 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200748 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200749static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
750 const uint8_t *data,
751 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200752 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100753{
754 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200755 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200756 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200757 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100758
Steven Cooreman4fed4552020-08-03 14:46:03 +0200759 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
760 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100761 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200762 /* A Weierstrass public key is represented as:
763 * - The byte 0x04;
764 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
765 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200766 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200767 */
768 if( ( data_length & 1 ) == 0 )
769 return( PSA_ERROR_INVALID_ARGUMENT );
770 curve_size = data_length / 2;
771
772 /* Montgomery public keys are represented in compressed format, meaning
773 * their curve_size is equal to the amount of input. */
774
775 /* Private keys are represented in uncompressed private random integer
776 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100777 }
778
Steven Cooreman6d839f02020-07-30 11:36:45 +0200779 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200780 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200781 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200782 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200783 mbedtls_ecp_keypair_init( ecp );
784
Gilles Peskine4cd32772019-12-02 20:49:42 +0100785 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200786 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
787 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100788 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200789 {
790 status = PSA_ERROR_INVALID_ARGUMENT;
791 goto exit;
792 }
793
Steven Cooremanacda8342020-07-24 23:09:52 +0200794 status = mbedtls_to_psa_error(
795 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
796 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200797 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200798
Steven Cooreman6d839f02020-07-30 11:36:45 +0200799 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200800 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200801 {
802 /* Load the public value. */
803 status = mbedtls_to_psa_error(
804 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200805 data,
806 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200807 if( status != PSA_SUCCESS )
808 goto exit;
809
810 /* Check that the point is on the curve. */
811 status = mbedtls_to_psa_error(
812 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
813 if( status != PSA_SUCCESS )
814 goto exit;
815 }
816 else
817 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200818 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200819 status = mbedtls_to_psa_error(
820 mbedtls_ecp_read_key( ecp->grp.id,
821 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200822 data,
823 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200824 if( status != PSA_SUCCESS )
825 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200826 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200827
828 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200829exit:
830 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200831 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200833 mbedtls_free( ecp );
834 }
835
Steven Cooreman29149862020-08-05 15:43:42 +0200836 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100837}
John Durkop6ba40d12020-11-10 08:50:04 -0800838#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
839 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
840 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
841 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
842 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000843
John Durkop6ba40d12020-11-10 08:50:04 -0800844#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
845 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200846/** Export an ECP key to export representation
847 *
848 * \param[in] type The type of key (public/private) to export
849 * \param[in] ecp The internal ECP representation from which to export
850 * \param[out] data The buffer to export to
851 * \param[in] data_size The length of the buffer to export to
852 * \param[out] data_length The amount of bytes written to \p data
853 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200854static psa_status_t psa_export_ecp_key( psa_key_type_t type,
855 mbedtls_ecp_keypair *ecp,
856 uint8_t *data,
857 size_t data_size,
858 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200859{
Steven Cooremanacda8342020-07-24 23:09:52 +0200860 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000861
Steven Cooremanacda8342020-07-24 23:09:52 +0200862 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200863 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 /* Check whether the public part is loaded */
865 if( mbedtls_ecp_is_zero( &ecp->Q ) )
866 {
867 /* Calculate the public key */
868 status = mbedtls_to_psa_error(
869 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
870 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
871 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200872 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200873 }
874
Steven Cooreman4fed4552020-08-03 14:46:03 +0200875 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200876 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
877 MBEDTLS_ECP_PF_UNCOMPRESSED,
878 data_length,
879 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200880 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200881 if( status != PSA_SUCCESS )
882 memset( data, 0, data_size );
883
Steven Cooreman29149862020-08-05 15:43:42 +0200884 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200885 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 else
887 {
Steven Cooreman29149862020-08-05 15:43:42 +0200888 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 return( PSA_ERROR_BUFFER_TOO_SMALL );
890
891 status = mbedtls_to_psa_error(
892 mbedtls_ecp_write_key( ecp,
893 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200894 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200896 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200897 else
898 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200899
900 return( status );
901 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200902}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200903
Steven Cooreman4fed4552020-08-03 14:46:03 +0200904/** Import an ECP key from import representation to a slot
905 *
906 * \param[in,out] slot The slot where to store the export representation to
907 * \param[in] data The buffer containing the import representation
908 * \param[in] data_length The amount of bytes in \p data
909 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200910static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
911 const uint8_t *data,
912 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100913{
Steven Cooremanacda8342020-07-24 23:09:52 +0200914 psa_status_t status;
915 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100917
Steven Cooremanacda8342020-07-24 23:09:52 +0200918 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200919 status = psa_load_ecp_representation( slot->attr.type,
920 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200921 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200922 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100923 if( status != PSA_SUCCESS )
924 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100925
Steven Cooremanacda8342020-07-24 23:09:52 +0200926 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200927 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200928 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200929 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200930
Steven Cooremanacda8342020-07-24 23:09:52 +0200931 /* Re-export the data to PSA export format. There is currently no support
932 * for other input formats then the export format, so this is a 1-1
933 * copy operation. */
934 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200935 if( output == NULL )
936 {
937 status = PSA_ERROR_INSUFFICIENT_MEMORY;
938 goto exit;
939 }
940
941 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200942 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200943 output,
944 data_length,
945 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100946exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200947 /* Always free the PK object (will also free contained ECP context) */
948 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200949 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200950
951 /* Free the allocated buffer only on error. */
952 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100953 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200954 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200955 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100956 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200957
958 /* On success, store the allocated export-formatted key. */
959 slot->data.key.data = output;
960 slot->data.key.bytes = data_length;
961
962 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100963}
John Durkop9814fa22020-11-04 12:28:15 -0800964#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
965 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100966
Gilles Peskineb46bef22019-07-30 21:32:04 +0200967/** Return the size of the key in the given slot, in bits.
968 *
969 * \param[in] slot A key slot.
970 *
971 * \return The key size in bits, read from the metadata in the slot.
972 */
973static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
974{
975 return( slot->attr.bits );
976}
977
Steven Cooreman75b74362020-07-28 14:30:13 +0200978/** Try to allocate a buffer to an empty key slot.
979 *
980 * \param[in,out] slot Key slot to attach buffer to.
981 * \param[in] buffer_length Requested size of the buffer.
982 *
983 * \retval #PSA_SUCCESS
984 * The buffer has been successfully allocated.
985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
986 * Not enough memory was available for allocation.
987 * \retval #PSA_ERROR_ALREADY_EXISTS
988 * Trying to allocate a buffer to a non-empty key slot.
989 */
990static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
991 size_t buffer_length )
992{
993 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200994 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200995
996 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
997 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200998 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200999
1000 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001001 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001002}
1003
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001004psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1005 const uint8_t* data,
1006 size_t data_length )
1007{
1008 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1009 data_length );
1010 if( status != PSA_SUCCESS )
1011 return( status );
1012
1013 memcpy( slot->data.key.data, data, data_length );
1014 return( PSA_SUCCESS );
1015}
1016
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001017/** Import key data into a slot.
1018 *
1019 * `slot->type` must have been set previously.
1020 * This function assumes that the slot does not contain any key material yet.
1021 * On failure, the slot content is unchanged.
1022 *
1023 * Persistent storage is not affected.
1024 *
1025 * \param[in,out] slot The key slot to import data into.
1026 * Its `type` field must have previously been set to
1027 * the desired key type.
1028 * It must not contain any key material yet.
1029 * \param[in] data Buffer containing the key material to parse and import.
1030 * \param data_length Size of \p data in bytes.
1031 *
1032 * \retval #PSA_SUCCESS
1033 * \retval #PSA_ERROR_INVALID_ARGUMENT
1034 * \retval #PSA_ERROR_NOT_SUPPORTED
1035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1036 */
1037static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1038 const uint8_t *data,
1039 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001041 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001042 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001044 /* zero-length keys are never supported. */
1045 if( data_length == 0 )
1046 return( PSA_ERROR_NOT_SUPPORTED );
1047
Gilles Peskine8e338702019-07-30 20:06:31 +02001048 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 {
Steven Cooreman04524762020-10-13 17:43:44 +02001050 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001051
Steven Cooreman75b74362020-07-28 14:30:13 +02001052 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1053 if( data_length > SIZE_MAX / 8 )
1054 return( PSA_ERROR_NOT_SUPPORTED );
1055
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001056 /* Enforce a size limit, and in particular ensure that the bit
1057 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001058 if( bit_size > PSA_MAX_KEY_BITS )
1059 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001060
1061 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001062 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001063 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001064
1065 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001066 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001067 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001068 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001069
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001070 /* Write the actual key size to the slot.
1071 * psa_start_key_creation() wrote the size declared by the
1072 * caller, which may be 0 (meaning unspecified) or wrong. */
1073 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001074
1075 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001077 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001078 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001079 /* Try validation through accelerators first. */
1080 bit_size = slot->attr.bits;
1081 psa_key_attributes_t attributes = {
1082 .core = slot->attr
1083 };
1084 status = psa_driver_wrapper_validate_key( &attributes,
1085 data,
1086 data_length,
1087 &bit_size );
1088 if( status == PSA_SUCCESS )
1089 {
1090 /* Key has been validated successfully by an accelerator.
1091 * Copy key material into slot. */
1092 status = psa_copy_key_material_into_slot( slot, data, data_length );
1093 if( status != PSA_SUCCESS )
1094 return( status );
1095
1096 slot->attr.bits = (psa_key_bits_t) bit_size;
1097 return( PSA_SUCCESS );
1098 }
1099 else if( status != PSA_ERROR_NOT_SUPPORTED )
1100 return( status );
1101
1102 /* Key format is not supported by any accelerator, try software fallback
1103 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001104#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1105 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001106 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1107 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001108 return( psa_import_ecp_key( slot, data, data_length ) );
1109 }
John Durkop9814fa22020-11-04 12:28:15 -08001110#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1111 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001112#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1113 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001114 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001115 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001116 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001117 }
John Durkop9814fa22020-11-04 12:28:15 -08001118#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1119 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001120
1121 /* Fell through the fallback as well, so have nothing else to try. */
1122 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 }
1124 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001125 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001126 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001127 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001128 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001129}
1130
Gilles Peskinef603c712019-01-19 13:40:11 +01001131/** Calculate the intersection of two algorithm usage policies.
1132 *
1133 * Return 0 (which allows no operation) on incompatibility.
1134 */
1135static psa_algorithm_t psa_key_policy_algorithm_intersection(
1136 psa_algorithm_t alg1,
1137 psa_algorithm_t alg2 )
1138{
Gilles Peskine549ea862019-05-22 11:45:59 +02001139 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001140 if( alg1 == alg2 )
1141 return( alg1 );
1142 /* If the policies are from the same hash-and-sign family, check
1143 * if one is a wildcard. If so the other has the specific algorithm. */
1144 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1145 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1146 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1147 {
1148 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1149 return( alg2 );
1150 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1151 return( alg1 );
1152 }
1153 /* If the policies are incompatible, allow nothing. */
1154 return( 0 );
1155}
1156
Gilles Peskined6f371b2019-05-10 19:33:38 +02001157static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1158 psa_algorithm_t requested_alg )
1159{
Gilles Peskine549ea862019-05-22 11:45:59 +02001160 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001161 if( requested_alg == policy_alg )
1162 return( 1 );
1163 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001164 * and requested_alg is the same hash-and-sign family with any hash,
1165 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001166 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1167 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1168 {
1169 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1170 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1171 }
Steven Cooremance48e852020-10-05 16:02:45 +02001172 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001173 * a key derivation with that key agreement should also be allowed. This
1174 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001175 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1176 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1177 {
1178 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1179 policy_alg );
1180 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001181 /* If it isn't permitted, it's forbidden. */
1182 return( 0 );
1183}
1184
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001185/** Test whether a policy permits an algorithm.
1186 *
1187 * The caller must test usage flags separately.
1188 */
1189static int psa_key_policy_permits( const psa_key_policy_t *policy,
1190 psa_algorithm_t alg )
1191{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001192 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1193 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001194}
1195
Gilles Peskinef603c712019-01-19 13:40:11 +01001196/** Restrict a key policy based on a constraint.
1197 *
1198 * \param[in,out] policy The policy to restrict.
1199 * \param[in] constraint The policy constraint to apply.
1200 *
1201 * \retval #PSA_SUCCESS
1202 * \c *policy contains the intersection of the original value of
1203 * \c *policy and \c *constraint.
1204 * \retval #PSA_ERROR_INVALID_ARGUMENT
1205 * \c *policy and \c *constraint are incompatible.
1206 * \c *policy is unchanged.
1207 */
1208static psa_status_t psa_restrict_key_policy(
1209 psa_key_policy_t *policy,
1210 const psa_key_policy_t *constraint )
1211{
1212 psa_algorithm_t intersection_alg =
1213 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001214 psa_algorithm_t intersection_alg2 =
1215 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001216 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1217 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001218 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1219 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001220 policy->usage &= constraint->usage;
1221 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001222 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001223 return( PSA_SUCCESS );
1224}
1225
Ronald Cron5c522922020-11-14 16:35:34 +01001226/** Get the description of a key given its identifier and policy constraints
1227 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228 *
Ronald Cron5c522922020-11-14 16:35:34 +01001229 * The key must have allow all the usage flags set in \p usage. If \p alg is
1230 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001231 *
Ronald Cron5c522922020-11-14 16:35:34 +01001232 * In case of a persistent key, the function loads the description of the key
1233 * into a key slot if not already done.
1234 *
1235 * On success, the returned key slot is locked. It is the responsibility of
1236 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001237 */
Ronald Cron5c522922020-11-14 16:35:34 +01001238static psa_status_t psa_get_and_lock_key_slot_with_policy(
1239 mbedtls_svc_key_id_t key,
1240 psa_key_slot_t **p_slot,
1241 psa_key_usage_t usage,
1242 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001243{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1245 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001246
Ronald Cron5c522922020-11-14 16:35:34 +01001247 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001248 if( status != PSA_SUCCESS )
1249 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001250 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001251
1252 /* Enforce that usage policy for the key slot contains all the flags
1253 * required by the usage parameter. There is one exception: public
1254 * keys can always be exported, so we treat public key objects as
1255 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001256 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001257 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001258
1259 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001260 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001262
1263 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001265 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001266
Darryl Green06fd18d2018-07-16 11:21:11 +01001267 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001268
1269error:
1270 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001271 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001272
1273 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001274}
Darryl Green940d72c2018-07-13 13:18:51 +01001275
Ronald Cron5c522922020-11-14 16:35:34 +01001276/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001277 *
1278 * A transparent key is a key for which the key material is directly
1279 * available, as opposed to a key in a secure element.
1280 *
Ronald Cron5c522922020-11-14 16:35:34 +01001281 * This is a temporary function to use instead of
1282 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1283 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001284 *
Ronald Cron5c522922020-11-14 16:35:34 +01001285 * On success, the returned key slot is locked. It is the responsibility of the
1286 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001287 */
1288#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001289static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1290 mbedtls_svc_key_id_t key,
1291 psa_key_slot_t **p_slot,
1292 psa_key_usage_t usage,
1293 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001294{
Ronald Cron5c522922020-11-14 16:35:34 +01001295 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1296 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001297 if( status != PSA_SUCCESS )
1298 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299
Gilles Peskineadad8132019-07-25 11:31:23 +02001300 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001301 {
Ronald Cron5c522922020-11-14 16:35:34 +01001302 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001303 *p_slot = NULL;
1304 return( PSA_ERROR_NOT_SUPPORTED );
1305 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306
Gilles Peskine28f8f302019-07-24 13:30:31 +02001307 return( PSA_SUCCESS );
1308}
1309#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1310/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001311#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1312 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001313#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1314
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001315/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001316static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001317{
Gilles Peskine73167e12019-07-12 23:44:37 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1319 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001320 {
1321 /* No key material to clean. */
1322 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001323 else
1324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001325 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001326 /* Data pointer will always be either a valid pointer or NULL in an
1327 * initialized slot, so we can just free it. */
1328 if( slot->data.key.data != NULL )
1329 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001330 mbedtls_free( slot->data.key.data );
1331 slot->data.key.data = NULL;
1332 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001333 }
Darryl Green40225ba2018-11-15 14:48:15 +00001334
1335 return( PSA_SUCCESS );
1336}
1337
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001338/** Completely wipe a slot in memory, including its policy.
1339 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001340psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001341{
1342 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001343
1344 /*
1345 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001346 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001347 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1348 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001349 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001350 */
Ronald Cron5c522922020-11-14 16:35:34 +01001351 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001352 {
1353#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001354 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001355#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001356 status = PSA_ERROR_CORRUPTION_DETECTED;
1357 }
1358
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001359 /* Multipart operations may still be using the key. This is safe
1360 * because all multipart operation objects are independent from
1361 * the key slot: if they need to access the key after the setup
1362 * phase, they have a copy of the key. Note that this means that
1363 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001364 /* At this point, key material and other type-specific content has
1365 * been wiped. Clear remaining metadata. We can call memset and not
1366 * zeroize because the metadata is not particularly sensitive. */
1367 memset( slot, 0, sizeof( *slot ) );
1368 return( status );
1369}
1370
Ronald Croncf56a0a2020-08-04 09:51:30 +02001371psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001372{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001373 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 psa_status_t status; /* status of the last operation */
1375 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001376#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1377 psa_se_drv_table_entry_t *driver;
1378#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379
Ronald Croncf56a0a2020-08-04 09:51:30 +02001380 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001381 return( PSA_SUCCESS );
1382
Ronald Cronf2911112020-10-29 17:51:10 +01001383 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001384 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001385 * key, this will load the key description from persistent memory if not
1386 * done yet. We cannot avoid this loading as without it we don't know if
1387 * the key is operated by an SE or not and this information is needed by
1388 * the current implementation.
1389 */
Ronald Cron5c522922020-11-14 16:35:34 +01001390 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001391 if( status != PSA_SUCCESS )
1392 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001393
Ronald Cronf2911112020-10-29 17:51:10 +01001394 /*
1395 * If the key slot containing the key description is under access by the
1396 * library (apart from the present access), the key cannot be destroyed
1397 * yet. For the time being, just return in error. Eventually (to be
1398 * implemented), the key should be destroyed when all accesses have
1399 * stopped.
1400 */
Ronald Cron5c522922020-11-14 16:35:34 +01001401 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001402 {
Ronald Cron5c522922020-11-14 16:35:34 +01001403 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001404 return( PSA_ERROR_GENERIC_ERROR );
1405 }
1406
Gilles Peskine354f7672019-07-12 23:46:38 +02001407#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001408 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001409 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001410 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001411 /* For a key in a secure element, we need to do three things:
1412 * remove the key file in internal storage, destroy the
1413 * key inside the secure element, and update the driver's
1414 * persistent data. Start a transaction that will encompass these
1415 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001416 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001417 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001418 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001419 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001420 status = psa_crypto_save_transaction( );
1421 if( status != PSA_SUCCESS )
1422 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001423 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001424 /* We should still try to destroy the key in the secure
1425 * element and the key metadata in storage. This is especially
1426 * important if the error is that the storage is full.
1427 * But how to do it exactly without risking an inconsistent
1428 * state after a reset?
1429 * https://github.com/ARMmbed/mbed-crypto/issues/215
1430 */
1431 overall_status = status;
1432 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001433 }
1434
Gilles Peskine354f7672019-07-12 23:46:38 +02001435 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001436 if( overall_status == PSA_SUCCESS )
1437 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001438 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001439#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1440
Darryl Greend49a4992018-06-18 17:27:26 +01001441#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001442 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001443 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001444 status = psa_destroy_persistent_key( slot->attr.id );
1445 if( overall_status == PSA_SUCCESS )
1446 overall_status = status;
1447
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001448 /* TODO: other slots may have a copy of the same key. We should
1449 * invalidate them.
1450 * https://github.com/ARMmbed/mbed-crypto/issues/214
1451 */
Darryl Greend49a4992018-06-18 17:27:26 +01001452 }
1453#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001454
Gilles Peskinefc762652019-07-22 19:30:34 +02001455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1456 if( driver != NULL )
1457 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001458 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001459 if( overall_status == PSA_SUCCESS )
1460 overall_status = status;
1461 status = psa_crypto_stop_transaction( );
1462 if( overall_status == PSA_SUCCESS )
1463 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001464 }
1465#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1466
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001467#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1468exit:
1469#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001470 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001471 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1472 if( overall_status == PSA_SUCCESS )
1473 overall_status = status;
1474 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475}
1476
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001477void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001478{
Gilles Peskineb699f072019-04-26 16:06:02 +02001479 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001480 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001481}
1482
Gilles Peskineb699f072019-04-26 16:06:02 +02001483psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1484 psa_key_type_t type,
1485 const uint8_t *data,
1486 size_t data_length )
1487{
1488 uint8_t *copy = NULL;
1489
1490 if( data_length != 0 )
1491 {
1492 copy = mbedtls_calloc( 1, data_length );
1493 if( copy == NULL )
1494 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1495 memcpy( copy, data, data_length );
1496 }
1497 /* After this point, this function is guaranteed to succeed, so it
1498 * can start modifying `*attributes`. */
1499
1500 if( attributes->domain_parameters != NULL )
1501 {
1502 mbedtls_free( attributes->domain_parameters );
1503 attributes->domain_parameters = NULL;
1504 attributes->domain_parameters_size = 0;
1505 }
1506
1507 attributes->domain_parameters = copy;
1508 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001509 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001510 return( PSA_SUCCESS );
1511}
1512
1513psa_status_t psa_get_key_domain_parameters(
1514 const psa_key_attributes_t *attributes,
1515 uint8_t *data, size_t data_size, size_t *data_length )
1516{
1517 if( attributes->domain_parameters_size > data_size )
1518 return( PSA_ERROR_BUFFER_TOO_SMALL );
1519 *data_length = attributes->domain_parameters_size;
1520 if( attributes->domain_parameters_size != 0 )
1521 memcpy( data, attributes->domain_parameters,
1522 attributes->domain_parameters_size );
1523 return( PSA_SUCCESS );
1524}
1525
John Durkop07cc04a2020-11-16 22:08:34 -08001526#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001527 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001528static psa_status_t psa_get_rsa_public_exponent(
1529 const mbedtls_rsa_context *rsa,
1530 psa_key_attributes_t *attributes )
1531{
1532 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001534 uint8_t *buffer = NULL;
1535 size_t buflen;
1536 mbedtls_mpi_init( &mpi );
1537
1538 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1539 if( ret != 0 )
1540 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001541 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1542 {
1543 /* It's the default value, which is reported as an empty string,
1544 * so there's nothing to do. */
1545 goto exit;
1546 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001547
1548 buflen = mbedtls_mpi_size( &mpi );
1549 buffer = mbedtls_calloc( 1, buflen );
1550 if( buffer == NULL )
1551 {
1552 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1553 goto exit;
1554 }
1555 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1556 if( ret != 0 )
1557 goto exit;
1558 attributes->domain_parameters = buffer;
1559 attributes->domain_parameters_size = buflen;
1560
1561exit:
1562 mbedtls_mpi_free( &mpi );
1563 if( ret != 0 )
1564 mbedtls_free( buffer );
1565 return( mbedtls_to_psa_error( ret ) );
1566}
John Durkop07cc04a2020-11-16 22:08:34 -08001567#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001568 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001569
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001570/** Retrieve all the publicly-accessible attributes of a key.
1571 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001572psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001573 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001576 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001577 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001578
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001579 psa_reset_key_attributes( attributes );
1580
Ronald Cron5c522922020-11-14 16:35:34 +01001581 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001582 if( status != PSA_SUCCESS )
1583 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001584
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001586 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1587 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1588
1589#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1590 if( psa_key_slot_is_external( slot ) )
1591 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1592#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001593
Gilles Peskine8e338702019-07-30 20:06:31 +02001594 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001595 {
John Durkop07cc04a2020-11-16 22:08:34 -08001596#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001597 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001598 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001599 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001600#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001601 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001602 * is not yet implemented.
1603 * https://github.com/ARMmbed/mbed-crypto/issues/216
1604 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001605 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001606 break;
1607#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001608 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001609 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001610
Steven Cooreman4fed4552020-08-03 14:46:03 +02001611 status = psa_load_rsa_representation( slot->attr.type,
1612 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001613 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001614 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001615 if( status != PSA_SUCCESS )
1616 break;
1617
Steven Cooremana2371e52020-07-28 14:30:39 +02001618 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001619 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001620 mbedtls_rsa_free( rsa );
1621 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001622 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001623 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001624#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001625 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001626 default:
1627 /* Nothing else to do. */
1628 break;
1629 }
1630
1631 if( status != PSA_SUCCESS )
1632 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001633
Ronald Cron5c522922020-11-14 16:35:34 +01001634 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001635
Ronald Cron5c522922020-11-14 16:35:34 +01001636 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637}
1638
Gilles Peskinec8000c02019-08-02 20:15:51 +02001639#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1640psa_status_t psa_get_key_slot_number(
1641 const psa_key_attributes_t *attributes,
1642 psa_key_slot_number_t *slot_number )
1643{
1644 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1645 {
1646 *slot_number = attributes->slot_number;
1647 return( PSA_SUCCESS );
1648 }
1649 else
1650 return( PSA_ERROR_INVALID_ARGUMENT );
1651}
1652#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1653
Steven Cooremana01795d2020-07-24 22:48:15 +02001654static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1655 uint8_t *data,
1656 size_t data_size,
1657 size_t *data_length )
1658{
1659 if( slot->data.key.bytes > data_size )
1660 return( PSA_ERROR_BUFFER_TOO_SMALL );
1661 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1662 memset( data + slot->data.key.bytes, 0,
1663 data_size - slot->data.key.bytes );
1664 *data_length = slot->data.key.bytes;
1665 return( PSA_SUCCESS );
1666}
1667
Gilles Peskinef603c712019-01-19 13:40:11 +01001668static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1669 uint8_t *data,
1670 size_t data_size,
1671 size_t *data_length,
1672 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673{
Gilles Peskine5d309672019-07-12 23:47:28 +02001674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1675 const psa_drv_se_t *drv;
1676 psa_drv_se_context_t *drv_context;
1677#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1678
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001679 *data_length = 0;
1680
Gilles Peskine8e338702019-07-30 20:06:31 +02001681 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001682 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001683
Gilles Peskinef9168942019-09-12 19:20:29 +02001684 /* Reject a zero-length output buffer now, since this can never be a
1685 * valid key representation. This way we know that data must be a valid
1686 * pointer and we can do things like memset(data, ..., data_size). */
1687 if( data_size == 0 )
1688 return( PSA_ERROR_BUFFER_TOO_SMALL );
1689
Gilles Peskine5d309672019-07-12 23:47:28 +02001690#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001691 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001692 {
1693 psa_drv_se_export_key_t method;
1694 if( drv->key_management == NULL )
1695 return( PSA_ERROR_NOT_SUPPORTED );
1696 method = ( export_public_key ?
1697 drv->key_management->p_export_public :
1698 drv->key_management->p_export );
1699 if( method == NULL )
1700 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001701 return( method( drv_context,
1702 slot->data.se.slot_number,
1703 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001704 }
1705#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1706
Gilles Peskine8e338702019-07-30 20:06:31 +02001707 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001708 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001709 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001710 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001711 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1712 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001713 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001714 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001715 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001716 /* Exporting public -> public */
1717 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1718 }
1719 else if( !export_public_key )
1720 {
1721 /* Exporting private -> private */
1722 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1723 }
1724 /* Need to export the public part of a private key,
1725 * so conversion is needed */
1726 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1727 {
John Durkop0e005192020-10-31 22:06:54 -07001728#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1729 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001730 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001731 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001732 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001733 slot->data.key.data,
1734 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001735 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001736 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001737 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001738
Steven Cooreman560c28a2020-07-24 23:20:24 +02001739 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001740 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001741 data,
1742 data_size,
1743 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001744
Steven Cooremana2371e52020-07-28 14:30:39 +02001745 mbedtls_rsa_free( rsa );
1746 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001747
Steven Cooreman560c28a2020-07-24 23:20:24 +02001748 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001749#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001750 /* We don't know how to convert a private RSA key to public. */
1751 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001752#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1753 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001754 }
1755 else
Moran Pekera998bc62018-04-16 18:16:20 +03001756 {
John Durkop6ba40d12020-11-10 08:50:04 -08001757#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1758 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001759 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001760 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001761 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001762 slot->data.key.data,
1763 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001764 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001765 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001766 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001767
1768 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1769 PSA_KEY_TYPE_ECC_GET_FAMILY(
1770 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001771 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001772 data,
1773 data_size,
1774 data_length );
1775
Steven Cooremana2371e52020-07-28 14:30:39 +02001776 mbedtls_ecp_keypair_free( ecp );
1777 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001778 return( status );
1779#else
1780 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001781 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001782#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1783 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001784 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001785 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001786 else
1787 {
1788 /* This shouldn't happen in the reference implementation, but
1789 it is valid for a special-purpose implementation to omit
1790 support for exporting certain key types. */
1791 return( PSA_ERROR_NOT_SUPPORTED );
1792 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001793}
1794
Ronald Croncf56a0a2020-08-04 09:51:30 +02001795psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001796 uint8_t *data,
1797 size_t data_size,
1798 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001799{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001800 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001801 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001802 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001803
1804 /* Set the key to empty now, so that even when there are errors, we always
1805 * set data_length to a value between 0 and data_size. On error, setting
1806 * the key to empty is a good choice because an empty key representation is
1807 * unlikely to be accepted anywhere. */
1808 *data_length = 0;
1809
1810 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001811 * which don't require any flag, but
1812 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1813 */
1814 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1815 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001816 if( status != PSA_SUCCESS )
1817 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001818
1819 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001820 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001821
Ronald Cron5c522922020-11-14 16:35:34 +01001822 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001823}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001824
Ronald Croncf56a0a2020-08-04 09:51:30 +02001825psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001826 uint8_t *data,
1827 size_t data_size,
1828 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001829{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001831 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001832 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001833
1834 /* Set the key to empty now, so that even when there are errors, we always
1835 * set data_length to a value between 0 and data_size. On error, setting
1836 * the key to empty is a good choice because an empty key representation is
1837 * unlikely to be accepted anywhere. */
1838 *data_length = 0;
1839
1840 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001841 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001842 if( status != PSA_SUCCESS )
1843 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001844
1845 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001846 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001847
Ronald Cron5c522922020-11-14 16:35:34 +01001848 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001849}
1850
Gilles Peskine91e8c332019-08-02 19:19:39 +02001851#if defined(static_assert)
1852static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1853 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001854static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001855 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001856static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001857 "One or more key attribute flag is listed as both internal-only and external-only" );
1858#endif
1859
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001860/** Validate that a key policy is internally well-formed.
1861 *
1862 * This function only rejects invalid policies. It does not validate the
1863 * consistency of the policy with respect to other attributes of the key
1864 * such as the key type.
1865 */
1866static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001867{
1868 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001869 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 PSA_KEY_USAGE_ENCRYPT |
1871 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001872 PSA_KEY_USAGE_SIGN_HASH |
1873 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1875 return( PSA_ERROR_INVALID_ARGUMENT );
1876
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 return( PSA_SUCCESS );
1878}
1879
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001880/** Validate the internal consistency of key attributes.
1881 *
1882 * This function only rejects invalid attribute values. If does not
1883 * validate the consistency of the attributes with any key data that may
1884 * be involved in the creation of the key.
1885 *
1886 * Call this function early in the key creation process.
1887 *
1888 * \param[in] attributes Key attributes for the new key.
1889 * \param[out] p_drv On any return, the driver for the key, if any.
1890 * NULL for a transparent key.
1891 *
1892 */
1893static psa_status_t psa_validate_key_attributes(
1894 const psa_key_attributes_t *attributes,
1895 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001896{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001897 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001898 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001899 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001900
Ronald Cron54b90082020-10-29 15:26:43 +01001901 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001902 if( status != PSA_SUCCESS )
1903 return( status );
1904
Ronald Crond2ed4812020-07-17 16:11:30 +02001905 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001906 if( status != PSA_SUCCESS )
1907 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001908
Ronald Cron65f38a32020-10-23 17:11:13 +02001909 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1910 {
1911 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1912 return( PSA_ERROR_INVALID_ARGUMENT );
1913 }
1914 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001915 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001916 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001917 if( status != PSA_SUCCESS )
1918 return( status );
1919 }
1920
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001921 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001922 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001923 return( status );
1924
1925 /* Refuse to create overly large keys.
1926 * Note that this doesn't trigger on import if the attributes don't
1927 * explicitly specify a size (so psa_get_key_bits returns 0), so
1928 * psa_import_key() needs its own checks. */
1929 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1930 return( PSA_ERROR_NOT_SUPPORTED );
1931
Gilles Peskine91e8c332019-08-02 19:19:39 +02001932 /* Reject invalid flags. These should not be reachable through the API. */
1933 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1934 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1935 return( PSA_ERROR_INVALID_ARGUMENT );
1936
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001937 return( PSA_SUCCESS );
1938}
1939
Gilles Peskine4747d192019-04-17 15:05:45 +02001940/** Prepare a key slot to receive key material.
1941 *
1942 * This function allocates a key slot and sets its metadata.
1943 *
1944 * If this function fails, call psa_fail_key_creation().
1945 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001946 * This function is intended to be used as follows:
1947 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001948 * it with the specified attributes, and in case of a volatile key assign it
1949 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001950 * -# Populate the slot with the key material.
1951 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1952 * In case of failure at any step, stop the sequence and call
1953 * psa_fail_key_creation().
1954 *
Ronald Cron5c522922020-11-14 16:35:34 +01001955 * On success, the key slot is locked. It is the responsibility of the caller
1956 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001957 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001958 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001959 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001960 * \param[out] p_slot On success, a pointer to the prepared slot.
1961 * \param[out] p_drv On any return, the driver for the key, if any.
1962 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001963 *
1964 * \retval #PSA_SUCCESS
1965 * The key slot is ready to receive key material.
1966 * \return If this function fails, the key slot is an invalid state.
1967 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 */
1969static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001970 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001972 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001973 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001974{
1975 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001976 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001977 psa_key_slot_t *slot;
1978
Gilles Peskinedf179142019-07-15 22:02:14 +02001979 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001980 *p_drv = NULL;
1981
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001982 status = psa_validate_key_attributes( attributes, p_drv );
1983 if( status != PSA_SUCCESS )
1984 return( status );
1985
Ronald Cronc4d1b512020-07-31 11:26:37 +02001986 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001987 if( status != PSA_SUCCESS )
1988 return( status );
1989 slot = *p_slot;
1990
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001991 /* We're storing the declared bit-size of the key. It's up to each
1992 * creation mechanism to verify that this information is correct.
1993 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001994 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001995 * is optional (import, copy). In case of a volatile key, assign it the
1996 * volatile key identifier associated to the slot returned to contain its
1997 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001998
1999 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002000 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2001 {
2002#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2003 slot->attr.id = volatile_key_id;
2004#else
2005 slot->attr.id.key_id = volatile_key_id;
2006#endif
2007 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002008
Gilles Peskine91e8c332019-08-02 19:19:39 +02002009 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002010 * external-only flags, query `attributes`. Thanks to the check
2011 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002012 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002013 * may have set. */
2014 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002015
Gilles Peskinecbaff462019-07-12 23:46:04 +02002016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002017 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002018 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002019 * create the key file in internal storage, create the
2020 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002021 * persistent data. This is done by starting a transaction that will
2022 * encompass these three actions.
2023 * For registering a volatile key, we just need to find an appropriate
2024 * slot number inside the SE. Since the key is designated volatile, creating
2025 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002026 /* The first thing to do is to find a slot number for the new key.
2027 * We save the slot number in persistent storage as part of the
2028 * transaction data. It will be needed to recover if the power
2029 * fails during the key creation process, to clean up on the secure
2030 * element side after restarting. Obtaining a slot number from the
2031 * secure element driver updates its persistent state, but we do not yet
2032 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002033 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002034 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002035 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002036 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002037 &slot->data.se.slot_number );
2038 if( status != PSA_SUCCESS )
2039 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002040
2041 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002042 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002043 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2044 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2045 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2046 psa_crypto_transaction.key.id = slot->attr.id;
2047 status = psa_crypto_save_transaction( );
2048 if( status != PSA_SUCCESS )
2049 {
2050 (void) psa_crypto_stop_transaction( );
2051 return( status );
2052 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002053 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002054 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002055
2056 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2057 {
2058 /* Key registration only makes sense with a secure element. */
2059 return( PSA_ERROR_INVALID_ARGUMENT );
2060 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002061#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2062
Ronald Cronc4d1b512020-07-31 11:26:37 +02002063 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002064}
Gilles Peskine4747d192019-04-17 15:05:45 +02002065
2066/** Finalize the creation of a key once its key material has been set.
2067 *
2068 * This entails writing the key to persistent storage.
2069 *
2070 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002071 * See the documentation of psa_start_key_creation() for the intended use
2072 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002073 *
Ronald Cron5c522922020-11-14 16:35:34 +01002074 * If the finalization succeeds, the function unlocks the key slot (it was
2075 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2076 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002077 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002078 * \param[in,out] slot Pointer to the slot with key material.
2079 * \param[in] driver The secure element driver for the key,
2080 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 * \param[out] key On success, identifier of the key. Note that the
2082 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002083 *
2084 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002085 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002086 * \return If this function fails, the key slot is an invalid state.
2087 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002088 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002089static psa_status_t psa_finish_key_creation(
2090 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002091 psa_se_drv_table_entry_t *driver,
2092 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002093{
2094 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002095 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002096 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002097
2098#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002099 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002100 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002101#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2102 if( driver != NULL )
2103 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002104 psa_se_key_data_storage_t data;
2105#if defined(static_assert)
2106 static_assert( sizeof( slot->data.se.slot_number ) ==
2107 sizeof( data.slot_number ),
2108 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002109#endif
2110 memcpy( &data.slot_number, &slot->data.se.slot_number,
2111 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002112 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002113 (uint8_t*) &data,
2114 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002115 }
2116 else
2117#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2118 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002119 /* Key material is saved in export representation in the slot, so
2120 * just pass the slot buffer for storage. */
2121 status = psa_save_persistent_key( &slot->attr,
2122 slot->data.key.data,
2123 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002124 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002125 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002126#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2127
Gilles Peskinecbaff462019-07-12 23:46:04 +02002128#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002129 /* Finish the transaction for a key creation. This does not
2130 * happen when registering an existing key. Detect this case
2131 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002132 * creation of a persistent key in a secure element requires a transaction,
2133 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002134 if( driver != NULL &&
2135 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002136 {
2137 status = psa_save_se_persistent_data( driver );
2138 if( status != PSA_SUCCESS )
2139 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002140 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002141 return( status );
2142 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002143 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002144 }
2145#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2146
Ronald Cron50972942020-11-14 11:28:25 +01002147 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002148 {
2149 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002150 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002151 if( status != PSA_SUCCESS )
2152 *key = MBEDTLS_SVC_KEY_ID_INIT;
2153 }
Ronald Cron50972942020-11-14 11:28:25 +01002154
Gilles Peskine4747d192019-04-17 15:05:45 +02002155 return( status );
2156}
2157
2158/** Abort the creation of a key.
2159 *
2160 * You may call this function after calling psa_start_key_creation(),
2161 * or after psa_finish_key_creation() fails. In other circumstances, this
2162 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002163 * See the documentation of psa_start_key_creation() for the intended use
2164 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002165 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002166 * \param[in,out] slot Pointer to the slot with key material.
2167 * \param[in] driver The secure element driver for the key,
2168 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002169 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002170static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002171 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002172{
Gilles Peskine011e4282019-06-26 18:34:38 +02002173 (void) driver;
2174
Gilles Peskine4747d192019-04-17 15:05:45 +02002175 if( slot == NULL )
2176 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002177
2178#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002179 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002180 * element, and the failure happened later (when saving metadata
2181 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002182 * element.
2183 * https://github.com/ARMmbed/mbed-crypto/issues/217
2184 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002185
Gilles Peskined7729582019-08-05 15:55:54 +02002186 /* Abort the ongoing transaction if any (there may not be one if
2187 * the creation process failed before starting one, or if the
2188 * key creation is a registration of a key in a secure element).
2189 * Earlier functions must already have done what it takes to undo any
2190 * partial creation. All that's left is to update the transaction data
2191 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002192 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002193#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2194
Gilles Peskine4747d192019-04-17 15:05:45 +02002195 psa_wipe_key_slot( slot );
2196}
2197
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002198/** Validate optional attributes during key creation.
2199 *
2200 * Some key attributes are optional during key creation. If they are
2201 * specified in the attributes structure, check that they are consistent
2202 * with the data in the slot.
2203 *
2204 * This function should be called near the end of key creation, after
2205 * the slot in memory is fully populated but before saving persistent data.
2206 */
2207static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002208 const psa_key_slot_t *slot,
2209 const psa_key_attributes_t *attributes )
2210{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002211 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002212 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002213 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002214 return( PSA_ERROR_INVALID_ARGUMENT );
2215 }
2216
2217 if( attributes->domain_parameters_size != 0 )
2218 {
John Durkop0e005192020-10-31 22:06:54 -07002219#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2220 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002221 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002222 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002223 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002224 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002226
Steven Cooreman7f391872020-07-30 14:57:44 +02002227 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002228 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002229 slot->data.key.data,
2230 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002231 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002232 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002233 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002234
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235 mbedtls_mpi_init( &actual );
2236 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002237 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002238 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002239 mbedtls_rsa_free( rsa );
2240 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002241 if( ret != 0 )
2242 goto rsa_exit;
2243 ret = mbedtls_mpi_read_binary( &required,
2244 attributes->domain_parameters,
2245 attributes->domain_parameters_size );
2246 if( ret != 0 )
2247 goto rsa_exit;
2248 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2249 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2250 rsa_exit:
2251 mbedtls_mpi_free( &actual );
2252 mbedtls_mpi_free( &required );
2253 if( ret != 0)
2254 return( mbedtls_to_psa_error( ret ) );
2255 }
2256 else
John Durkop9814fa22020-11-04 12:28:15 -08002257#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2258 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002259 {
2260 return( PSA_ERROR_INVALID_ARGUMENT );
2261 }
2262 }
2263
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002264 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002265 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002266 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002267 return( PSA_ERROR_INVALID_ARGUMENT );
2268 }
2269
2270 return( PSA_SUCCESS );
2271}
2272
Gilles Peskine4747d192019-04-17 15:05:45 +02002273psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002274 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002275 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002276 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002277{
2278 psa_status_t status;
2279 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002280 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002281
Ronald Cron81709fc2020-11-14 12:10:32 +01002282 *key = MBEDTLS_SVC_KEY_ID_INIT;
2283
Gilles Peskine0f84d622019-09-12 19:03:13 +02002284 /* Reject zero-length symmetric keys (including raw data key objects).
2285 * This also rejects any key which might be encoded as an empty string,
2286 * which is never valid. */
2287 if( data_length == 0 )
2288 return( PSA_ERROR_INVALID_ARGUMENT );
2289
Gilles Peskinedf179142019-07-15 22:02:14 +02002290 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002291 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002292 if( status != PSA_SUCCESS )
2293 goto exit;
2294
Gilles Peskine5d309672019-07-12 23:47:28 +02002295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2296 if( driver != NULL )
2297 {
2298 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002299 /* The driver should set the number of key bits, however in
2300 * case it doesn't, we initialize bits to an invalid value. */
2301 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002302 if( drv->key_management == NULL ||
2303 drv->key_management->p_import == NULL )
2304 {
2305 status = PSA_ERROR_NOT_SUPPORTED;
2306 goto exit;
2307 }
2308 status = drv->key_management->p_import(
2309 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002310 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002311 &bits );
2312 if( status != PSA_SUCCESS )
2313 goto exit;
2314 if( bits > PSA_MAX_KEY_BITS )
2315 {
2316 status = PSA_ERROR_NOT_SUPPORTED;
2317 goto exit;
2318 }
2319 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002320 }
2321 else
2322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2323 {
2324 status = psa_import_key_into_slot( slot, data, data_length );
2325 if( status != PSA_SUCCESS )
2326 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002327 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002328 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002329 if( status != PSA_SUCCESS )
2330 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002331
Ronald Cron81709fc2020-11-14 12:10:32 +01002332 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002333exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002334 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002335 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002336
Gilles Peskine4747d192019-04-17 15:05:45 +02002337 return( status );
2338}
2339
Gilles Peskined7729582019-08-05 15:55:54 +02002340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2341psa_status_t mbedtls_psa_register_se_key(
2342 const psa_key_attributes_t *attributes )
2343{
2344 psa_status_t status;
2345 psa_key_slot_t *slot = NULL;
2346 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002348
2349 /* Leaving attributes unspecified is not currently supported.
2350 * It could make sense to query the key type and size from the
2351 * secure element, but not all secure elements support this
2352 * and the driver HAL doesn't currently support it. */
2353 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2354 return( PSA_ERROR_NOT_SUPPORTED );
2355 if( psa_get_key_bits( attributes ) == 0 )
2356 return( PSA_ERROR_NOT_SUPPORTED );
2357
2358 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002359 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002360 if( status != PSA_SUCCESS )
2361 goto exit;
2362
Ronald Cron81709fc2020-11-14 12:10:32 +01002363 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002364
2365exit:
2366 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002367 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002368
Gilles Peskined7729582019-08-05 15:55:54 +02002369 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002370 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002371 return( status );
2372}
2373#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2374
Gilles Peskinef603c712019-01-19 13:40:11 +01002375static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002376 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002377{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002378 psa_status_t status = psa_copy_key_material_into_slot( target,
2379 source->data.key.data,
2380 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002381 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002382 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002383
Steven Cooreman398aee52020-10-13 14:35:45 +02002384 target->attr.type = source->attr.type;
2385 target->attr.bits = source->attr.bits;
2386
2387 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002388}
2389
Ronald Croncf56a0a2020-08-04 09:51:30 +02002390psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002391 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002392 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002393{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002395 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002396 psa_key_slot_t *source_slot = NULL;
2397 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002398 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002399 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002400
Ronald Cron81709fc2020-11-14 12:10:32 +01002401 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2402
Ronald Cron5c522922020-11-14 16:35:34 +01002403 status = psa_get_and_lock_transparent_key_slot_with_policy(
2404 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002405 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002406 goto exit;
2407
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002408 status = psa_validate_optional_attributes( source_slot,
2409 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002410 if( status != PSA_SUCCESS )
2411 goto exit;
2412
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002413 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002414 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002415 if( status != PSA_SUCCESS )
2416 goto exit;
2417
Ronald Cron81709fc2020-11-14 12:10:32 +01002418 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2419 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002420 if( status != PSA_SUCCESS )
2421 goto exit;
2422
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002423#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2424 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002425 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002426 /* Copying to a secure element is not implemented yet. */
2427 status = PSA_ERROR_NOT_SUPPORTED;
2428 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002429 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002430#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002431
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002432 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002433 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002434 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002435
Ronald Cron81709fc2020-11-14 12:10:32 +01002436 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002437exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002438 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002439 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002440
Ronald Cron5c522922020-11-14 16:35:34 +01002441 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002442
Ronald Cron5c522922020-11-14 16:35:34 +01002443 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002444}
2445
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002446
2447
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002448/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002449/* Message digests */
2450/****************************************************************/
2451
John Durkop07cc04a2020-11-16 22:08:34 -08002452#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002453 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2454 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002455 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002456static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002457{
2458 switch( alg )
2459 {
2460#if defined(MBEDTLS_MD2_C)
2461 case PSA_ALG_MD2:
2462 return( &mbedtls_md2_info );
2463#endif
2464#if defined(MBEDTLS_MD4_C)
2465 case PSA_ALG_MD4:
2466 return( &mbedtls_md4_info );
2467#endif
2468#if defined(MBEDTLS_MD5_C)
2469 case PSA_ALG_MD5:
2470 return( &mbedtls_md5_info );
2471#endif
2472#if defined(MBEDTLS_RIPEMD160_C)
2473 case PSA_ALG_RIPEMD160:
2474 return( &mbedtls_ripemd160_info );
2475#endif
2476#if defined(MBEDTLS_SHA1_C)
2477 case PSA_ALG_SHA_1:
2478 return( &mbedtls_sha1_info );
2479#endif
2480#if defined(MBEDTLS_SHA256_C)
2481 case PSA_ALG_SHA_224:
2482 return( &mbedtls_sha224_info );
2483 case PSA_ALG_SHA_256:
2484 return( &mbedtls_sha256_info );
2485#endif
2486#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002487#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002488 case PSA_ALG_SHA_384:
2489 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002490#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 case PSA_ALG_SHA_512:
2492 return( &mbedtls_sha512_info );
2493#endif
2494 default:
2495 return( NULL );
2496 }
2497}
John Durkop07cc04a2020-11-16 22:08:34 -08002498#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002499 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2500 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2501 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2504{
2505 switch( operation->alg )
2506 {
Gilles Peskine81736312018-06-26 15:04:31 +02002507 case 0:
2508 /* The object has (apparently) been initialized but it is not
2509 * in use. It's ok to call abort on such an object, and there's
2510 * nothing to do. */
2511 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002512#if defined(MBEDTLS_MD2_C)
2513 case PSA_ALG_MD2:
2514 mbedtls_md2_free( &operation->ctx.md2 );
2515 break;
2516#endif
2517#if defined(MBEDTLS_MD4_C)
2518 case PSA_ALG_MD4:
2519 mbedtls_md4_free( &operation->ctx.md4 );
2520 break;
2521#endif
2522#if defined(MBEDTLS_MD5_C)
2523 case PSA_ALG_MD5:
2524 mbedtls_md5_free( &operation->ctx.md5 );
2525 break;
2526#endif
2527#if defined(MBEDTLS_RIPEMD160_C)
2528 case PSA_ALG_RIPEMD160:
2529 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2530 break;
2531#endif
2532#if defined(MBEDTLS_SHA1_C)
2533 case PSA_ALG_SHA_1:
2534 mbedtls_sha1_free( &operation->ctx.sha1 );
2535 break;
2536#endif
2537#if defined(MBEDTLS_SHA256_C)
2538 case PSA_ALG_SHA_224:
2539 case PSA_ALG_SHA_256:
2540 mbedtls_sha256_free( &operation->ctx.sha256 );
2541 break;
2542#endif
2543#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002544#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002545 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002546#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002547 case PSA_ALG_SHA_512:
2548 mbedtls_sha512_free( &operation->ctx.sha512 );
2549 break;
2550#endif
2551 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002552 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 }
2554 operation->alg = 0;
2555 return( PSA_SUCCESS );
2556}
2557
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002558psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002559 psa_algorithm_t alg )
2560{
Janos Follath24eed8d2019-11-22 13:21:35 +00002561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002562
2563 /* A context must be freshly initialized before it can be set up. */
2564 if( operation->alg != 0 )
2565 {
2566 return( PSA_ERROR_BAD_STATE );
2567 }
2568
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002569 switch( alg )
2570 {
2571#if defined(MBEDTLS_MD2_C)
2572 case PSA_ALG_MD2:
2573 mbedtls_md2_init( &operation->ctx.md2 );
2574 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2575 break;
2576#endif
2577#if defined(MBEDTLS_MD4_C)
2578 case PSA_ALG_MD4:
2579 mbedtls_md4_init( &operation->ctx.md4 );
2580 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2581 break;
2582#endif
2583#if defined(MBEDTLS_MD5_C)
2584 case PSA_ALG_MD5:
2585 mbedtls_md5_init( &operation->ctx.md5 );
2586 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2587 break;
2588#endif
2589#if defined(MBEDTLS_RIPEMD160_C)
2590 case PSA_ALG_RIPEMD160:
2591 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2592 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2593 break;
2594#endif
2595#if defined(MBEDTLS_SHA1_C)
2596 case PSA_ALG_SHA_1:
2597 mbedtls_sha1_init( &operation->ctx.sha1 );
2598 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2599 break;
2600#endif
2601#if defined(MBEDTLS_SHA256_C)
2602 case PSA_ALG_SHA_224:
2603 mbedtls_sha256_init( &operation->ctx.sha256 );
2604 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2605 break;
2606 case PSA_ALG_SHA_256:
2607 mbedtls_sha256_init( &operation->ctx.sha256 );
2608 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2609 break;
2610#endif
2611#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002612#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002613 case PSA_ALG_SHA_384:
2614 mbedtls_sha512_init( &operation->ctx.sha512 );
2615 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2616 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002617#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002618 case PSA_ALG_SHA_512:
2619 mbedtls_sha512_init( &operation->ctx.sha512 );
2620 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2621 break;
2622#endif
2623 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002624 return( PSA_ALG_IS_HASH( alg ) ?
2625 PSA_ERROR_NOT_SUPPORTED :
2626 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002627 }
2628 if( ret == 0 )
2629 operation->alg = alg;
2630 else
2631 psa_hash_abort( operation );
2632 return( mbedtls_to_psa_error( ret ) );
2633}
2634
2635psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2636 const uint8_t *input,
2637 size_t input_length )
2638{
Janos Follath24eed8d2019-11-22 13:21:35 +00002639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002640
2641 /* Don't require hash implementations to behave correctly on a
2642 * zero-length input, which may have an invalid pointer. */
2643 if( input_length == 0 )
2644 return( PSA_SUCCESS );
2645
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002646 switch( operation->alg )
2647 {
2648#if defined(MBEDTLS_MD2_C)
2649 case PSA_ALG_MD2:
2650 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2651 input, input_length );
2652 break;
2653#endif
2654#if defined(MBEDTLS_MD4_C)
2655 case PSA_ALG_MD4:
2656 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2657 input, input_length );
2658 break;
2659#endif
2660#if defined(MBEDTLS_MD5_C)
2661 case PSA_ALG_MD5:
2662 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2663 input, input_length );
2664 break;
2665#endif
2666#if defined(MBEDTLS_RIPEMD160_C)
2667 case PSA_ALG_RIPEMD160:
2668 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2669 input, input_length );
2670 break;
2671#endif
2672#if defined(MBEDTLS_SHA1_C)
2673 case PSA_ALG_SHA_1:
2674 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2675 input, input_length );
2676 break;
2677#endif
2678#if defined(MBEDTLS_SHA256_C)
2679 case PSA_ALG_SHA_224:
2680 case PSA_ALG_SHA_256:
2681 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2682 input, input_length );
2683 break;
2684#endif
2685#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002686#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002687 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002688#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002689 case PSA_ALG_SHA_512:
2690 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2691 input, input_length );
2692 break;
2693#endif
2694 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002695 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002696 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002697
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002698 if( ret != 0 )
2699 psa_hash_abort( operation );
2700 return( mbedtls_to_psa_error( ret ) );
2701}
2702
2703psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2704 uint8_t *hash,
2705 size_t hash_size,
2706 size_t *hash_length )
2707{
itayzafrir40835d42018-08-02 13:14:17 +03002708 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002710 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002711
2712 /* Fill the output buffer with something that isn't a valid hash
2713 * (barring an attack on the hash and deliberately-crafted input),
2714 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002715 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002716 /* If hash_size is 0 then hash may be NULL and then the
2717 * call to memset would have undefined behavior. */
2718 if( hash_size != 0 )
2719 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002720
2721 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002722 {
2723 status = PSA_ERROR_BUFFER_TOO_SMALL;
2724 goto exit;
2725 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002726
2727 switch( operation->alg )
2728 {
2729#if defined(MBEDTLS_MD2_C)
2730 case PSA_ALG_MD2:
2731 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2732 break;
2733#endif
2734#if defined(MBEDTLS_MD4_C)
2735 case PSA_ALG_MD4:
2736 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2737 break;
2738#endif
2739#if defined(MBEDTLS_MD5_C)
2740 case PSA_ALG_MD5:
2741 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2742 break;
2743#endif
2744#if defined(MBEDTLS_RIPEMD160_C)
2745 case PSA_ALG_RIPEMD160:
2746 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2747 break;
2748#endif
2749#if defined(MBEDTLS_SHA1_C)
2750 case PSA_ALG_SHA_1:
2751 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2752 break;
2753#endif
2754#if defined(MBEDTLS_SHA256_C)
2755 case PSA_ALG_SHA_224:
2756 case PSA_ALG_SHA_256:
2757 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2758 break;
2759#endif
2760#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002761#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002762 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002763#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002764 case PSA_ALG_SHA_512:
2765 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2766 break;
2767#endif
2768 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002769 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002770 }
itayzafrir40835d42018-08-02 13:14:17 +03002771 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002772
itayzafrir40835d42018-08-02 13:14:17 +03002773exit:
2774 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002775 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002776 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002777 return( psa_hash_abort( operation ) );
2778 }
2779 else
2780 {
2781 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002782 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002783 }
2784}
2785
Gilles Peskine2d277862018-06-18 15:41:12 +02002786psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2787 const uint8_t *hash,
2788 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789{
2790 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2791 size_t actual_hash_length;
2792 psa_status_t status = psa_hash_finish( operation,
2793 actual_hash, sizeof( actual_hash ),
2794 &actual_hash_length );
2795 if( status != PSA_SUCCESS )
2796 return( status );
2797 if( actual_hash_length != hash_length )
2798 return( PSA_ERROR_INVALID_SIGNATURE );
2799 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2800 return( PSA_ERROR_INVALID_SIGNATURE );
2801 return( PSA_SUCCESS );
2802}
2803
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804psa_status_t psa_hash_compute( psa_algorithm_t alg,
2805 const uint8_t *input, size_t input_length,
2806 uint8_t *hash, size_t hash_size,
2807 size_t *hash_length )
2808{
2809 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2811
2812 *hash_length = hash_size;
2813 status = psa_hash_setup( &operation, alg );
2814 if( status != PSA_SUCCESS )
2815 goto exit;
2816 status = psa_hash_update( &operation, input, input_length );
2817 if( status != PSA_SUCCESS )
2818 goto exit;
2819 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2820 if( status != PSA_SUCCESS )
2821 goto exit;
2822
2823exit:
2824 if( status == PSA_SUCCESS )
2825 status = psa_hash_abort( &operation );
2826 else
2827 psa_hash_abort( &operation );
2828 return( status );
2829}
2830
2831psa_status_t psa_hash_compare( psa_algorithm_t alg,
2832 const uint8_t *input, size_t input_length,
2833 const uint8_t *hash, size_t hash_length )
2834{
2835 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2837
2838 status = psa_hash_setup( &operation, alg );
2839 if( status != PSA_SUCCESS )
2840 goto exit;
2841 status = psa_hash_update( &operation, input, input_length );
2842 if( status != PSA_SUCCESS )
2843 goto exit;
2844 status = psa_hash_verify( &operation, hash, hash_length );
2845 if( status != PSA_SUCCESS )
2846 goto exit;
2847
2848exit:
2849 if( status == PSA_SUCCESS )
2850 status = psa_hash_abort( &operation );
2851 else
2852 psa_hash_abort( &operation );
2853 return( status );
2854}
2855
Gilles Peskineeb35d782019-01-22 17:56:16 +01002856psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2857 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002858{
2859 if( target_operation->alg != 0 )
2860 return( PSA_ERROR_BAD_STATE );
2861
2862 switch( source_operation->alg )
2863 {
2864 case 0:
2865 return( PSA_ERROR_BAD_STATE );
2866#if defined(MBEDTLS_MD2_C)
2867 case PSA_ALG_MD2:
2868 mbedtls_md2_clone( &target_operation->ctx.md2,
2869 &source_operation->ctx.md2 );
2870 break;
2871#endif
2872#if defined(MBEDTLS_MD4_C)
2873 case PSA_ALG_MD4:
2874 mbedtls_md4_clone( &target_operation->ctx.md4,
2875 &source_operation->ctx.md4 );
2876 break;
2877#endif
2878#if defined(MBEDTLS_MD5_C)
2879 case PSA_ALG_MD5:
2880 mbedtls_md5_clone( &target_operation->ctx.md5,
2881 &source_operation->ctx.md5 );
2882 break;
2883#endif
2884#if defined(MBEDTLS_RIPEMD160_C)
2885 case PSA_ALG_RIPEMD160:
2886 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2887 &source_operation->ctx.ripemd160 );
2888 break;
2889#endif
2890#if defined(MBEDTLS_SHA1_C)
2891 case PSA_ALG_SHA_1:
2892 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2893 &source_operation->ctx.sha1 );
2894 break;
2895#endif
2896#if defined(MBEDTLS_SHA256_C)
2897 case PSA_ALG_SHA_224:
2898 case PSA_ALG_SHA_256:
2899 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2900 &source_operation->ctx.sha256 );
2901 break;
2902#endif
2903#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002904#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002905 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002906#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002907 case PSA_ALG_SHA_512:
2908 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2909 &source_operation->ctx.sha512 );
2910 break;
2911#endif
2912 default:
2913 return( PSA_ERROR_NOT_SUPPORTED );
2914 }
2915
2916 target_operation->alg = source_operation->alg;
2917 return( PSA_SUCCESS );
2918}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002919
2920
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002921/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922/* MAC */
2923/****************************************************************/
2924
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002925static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926 psa_algorithm_t alg,
2927 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002928 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002929 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002932 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002934 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002935 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002936
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2938 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002939 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002941 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002942 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943 mode = MBEDTLS_MODE_STREAM;
2944 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945 case PSA_ALG_CTR:
2946 mode = MBEDTLS_MODE_CTR;
2947 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002948 case PSA_ALG_CFB:
2949 mode = MBEDTLS_MODE_CFB;
2950 break;
2951 case PSA_ALG_OFB:
2952 mode = MBEDTLS_MODE_OFB;
2953 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002954 case PSA_ALG_ECB_NO_PADDING:
2955 mode = MBEDTLS_MODE_ECB;
2956 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002957 case PSA_ALG_CBC_NO_PADDING:
2958 mode = MBEDTLS_MODE_CBC;
2959 break;
2960 case PSA_ALG_CBC_PKCS7:
2961 mode = MBEDTLS_MODE_CBC;
2962 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002963 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964 mode = MBEDTLS_MODE_CCM;
2965 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002966 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967 mode = MBEDTLS_MODE_GCM;
2968 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002969 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2970 mode = MBEDTLS_MODE_CHACHAPOLY;
2971 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 default:
2973 return( NULL );
2974 }
2975 }
2976 else if( alg == PSA_ALG_CMAC )
2977 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978 else
2979 return( NULL );
2980
2981 switch( key_type )
2982 {
2983 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002984 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002985 break;
2986 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002987 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2988 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002990 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002992 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002993 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2994 * but two-key Triple-DES is functionally three-key Triple-DES
2995 * with K1=K3, so that's how we present it to mbedtls. */
2996 if( key_bits == 128 )
2997 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002998 break;
2999 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003000 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003001 break;
3002 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003003 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003005 case PSA_KEY_TYPE_CHACHA20:
3006 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3007 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 default:
3009 return( NULL );
3010 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003011 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003012 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013
Jaeden Amero23bbb752018-06-26 14:16:54 +01003014 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3015 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016}
3017
John Durkop6ba40d12020-11-10 08:50:04 -08003018#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003019static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003020{
Gilles Peskine2d277862018-06-18 15:41:12 +02003021 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003022 {
3023 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003024 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003025 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003026 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003027 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003028 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003029 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003030 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003031 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003032 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003033 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003034 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003035 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003036 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003037 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003038 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003039 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003040 return( 128 );
3041 default:
3042 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003043 }
3044}
John Durkop07cc04a2020-11-16 22:08:34 -08003045#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003046
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003047/* Initialize the MAC operation structure. Once this function has been
3048 * called, psa_mac_abort can run and will do the right thing. */
3049static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3050 psa_algorithm_t alg )
3051{
3052 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3053
3054 operation->alg = alg;
3055 operation->key_set = 0;
3056 operation->iv_set = 0;
3057 operation->iv_required = 0;
3058 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003059 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003060
3061#if defined(MBEDTLS_CMAC_C)
3062 if( alg == PSA_ALG_CMAC )
3063 {
3064 operation->iv_required = 0;
3065 mbedtls_cipher_init( &operation->ctx.cmac );
3066 status = PSA_SUCCESS;
3067 }
3068 else
3069#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003070#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003071 if( PSA_ALG_IS_HMAC( operation->alg ) )
3072 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003073 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3074 operation->ctx.hmac.hash_ctx.alg = 0;
3075 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003076 }
3077 else
John Durkopd0321952020-10-29 21:37:36 -07003078#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003079 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003080 if( ! PSA_ALG_IS_MAC( alg ) )
3081 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003082 }
3083
3084 if( status != PSA_SUCCESS )
3085 memset( operation, 0, sizeof( *operation ) );
3086 return( status );
3087}
3088
John Durkop6ba40d12020-11-10 08:50:04 -08003089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003090static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3091{
Gilles Peskine3f108122018-12-07 18:14:53 +01003092 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003093 return( psa_hash_abort( &hmac->hash_ctx ) );
3094}
John Durkop6ba40d12020-11-10 08:50:04 -08003095#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003096
Gilles Peskine8c9def32018-02-08 10:02:12 +01003097psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3098{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003099 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003101 /* The object has (apparently) been initialized but it is not
3102 * in use. It's ok to call abort on such an object, and there's
3103 * nothing to do. */
3104 return( PSA_SUCCESS );
3105 }
3106 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003107#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 if( operation->alg == PSA_ALG_CMAC )
3109 {
3110 mbedtls_cipher_free( &operation->ctx.cmac );
3111 }
3112 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003113#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003115 if( PSA_ALG_IS_HMAC( operation->alg ) )
3116 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003117 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003118 }
3119 else
John Durkopd0321952020-10-29 21:37:36 -07003120#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003121 {
3122 /* Sanity check (shouldn't happen: operation->alg should
3123 * always have been initialized to a valid value). */
3124 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125 }
Moran Peker41deec42018-04-04 15:43:05 +03003126
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127 operation->alg = 0;
3128 operation->key_set = 0;
3129 operation->iv_set = 0;
3130 operation->iv_required = 0;
3131 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003132 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003133
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003135
3136bad_state:
3137 /* If abort is called on an uninitialized object, we can't trust
3138 * anything. Wipe the object in case it contains confidential data.
3139 * This may result in a memory leak if a pointer gets overwritten,
3140 * but it's too late to do anything about this. */
3141 memset( operation, 0, sizeof( *operation ) );
3142 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003143}
3144
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003145#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003146static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003147 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003148 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003149 const mbedtls_cipher_info_t *cipher_info )
3150{
Janos Follath24eed8d2019-11-22 13:21:35 +00003151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003152
3153 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003154
3155 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3156 if( ret != 0 )
3157 return( ret );
3158
3159 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003160 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003161 key_bits );
3162 return( ret );
3163}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003164#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003165
John Durkop6ba40d12020-11-10 08:50:04 -08003166#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003167static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3168 const uint8_t *key,
3169 size_t key_length,
3170 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003171{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003172 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003173 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003174 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003175 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003176 psa_status_t status;
3177
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003178 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3179 * overflow below. This should never trigger if the hash algorithm
3180 * is implemented correctly. */
3181 /* The size checks against the ipad and opad buffers cannot be written
3182 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3183 * because that triggers -Wlogical-op on GCC 7.3. */
3184 if( block_size > sizeof( ipad ) )
3185 return( PSA_ERROR_NOT_SUPPORTED );
3186 if( block_size > sizeof( hmac->opad ) )
3187 return( PSA_ERROR_NOT_SUPPORTED );
3188 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003189 return( PSA_ERROR_NOT_SUPPORTED );
3190
Gilles Peskined223b522018-06-11 18:12:58 +02003191 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003192 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003193 status = psa_hash_compute( hash_alg, key, key_length,
3194 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003195 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003196 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003197 }
Gilles Peskine96889972018-07-12 17:07:03 +02003198 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3199 * but it is permitted. It is common when HMAC is used in HKDF, for
3200 * example. Don't call `memcpy` in the 0-length because `key` could be
3201 * an invalid pointer which would make the behavior undefined. */
3202 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003203 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003204
Gilles Peskined223b522018-06-11 18:12:58 +02003205 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3206 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003207 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003208 ipad[i] ^= 0x36;
3209 memset( ipad + key_length, 0x36, block_size - key_length );
3210
3211 /* Copy the key material from ipad to opad, flipping the requisite bits,
3212 * and filling the rest of opad with the requisite constant. */
3213 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003214 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3215 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003216
Gilles Peskine01126fa2018-07-12 17:04:55 +02003217 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003218 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003219 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003220
Gilles Peskine01126fa2018-07-12 17:04:55 +02003221 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003222
3223cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003224 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003225
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003226 return( status );
3227}
John Durkop6ba40d12020-11-10 08:50:04 -08003228#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003229
Gilles Peskine89167cb2018-07-08 20:12:23 +02003230static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003231 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003232 psa_algorithm_t alg,
3233 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003234{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003236 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003237 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003238 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003239 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003240 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003241 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003242 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003243
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003244 /* A context must be freshly initialized before it can be set up. */
3245 if( operation->alg != 0 )
3246 {
3247 return( PSA_ERROR_BAD_STATE );
3248 }
3249
Gilles Peskined911eb72018-08-14 15:18:45 +02003250 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003251 if( status != PSA_SUCCESS )
3252 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003253 if( is_sign )
3254 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003255
Ronald Cron5c522922020-11-14 16:35:34 +01003256 status = psa_get_and_lock_transparent_key_slot_with_policy(
3257 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003258 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003259 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003260 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003261
Gilles Peskine8c9def32018-02-08 10:02:12 +01003262#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003263 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003264 {
3265 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003266 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003267 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003269 if( cipher_info == NULL )
3270 {
3271 status = PSA_ERROR_NOT_SUPPORTED;
3272 goto exit;
3273 }
3274 operation->mac_size = cipher_info->block_size;
3275 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3276 status = mbedtls_to_psa_error( ret );
3277 }
3278 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003279#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003281 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003282 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003283 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003284 if( hash_alg == 0 )
3285 {
3286 status = PSA_ERROR_NOT_SUPPORTED;
3287 goto exit;
3288 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003289
3290 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3291 /* Sanity check. This shouldn't fail on a valid configuration. */
3292 if( operation->mac_size == 0 ||
3293 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3294 {
3295 status = PSA_ERROR_NOT_SUPPORTED;
3296 goto exit;
3297 }
3298
Gilles Peskine8e338702019-07-30 20:06:31 +02003299 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003300 {
3301 status = PSA_ERROR_INVALID_ARGUMENT;
3302 goto exit;
3303 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003304
Gilles Peskine01126fa2018-07-12 17:04:55 +02003305 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003306 slot->data.key.data,
3307 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003308 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003309 }
3310 else
John Durkopd0321952020-10-29 21:37:36 -07003311#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003312 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003313 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003314 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003315 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003316
Gilles Peskined911eb72018-08-14 15:18:45 +02003317 if( truncated == 0 )
3318 {
3319 /* The "normal" case: untruncated algorithm. Nothing to do. */
3320 }
3321 else if( truncated < 4 )
3322 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003323 /* A very short MAC is too short for security since it can be
3324 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3325 * so we make this our minimum, even though 32 bits is still
3326 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003327 status = PSA_ERROR_NOT_SUPPORTED;
3328 }
3329 else if( truncated > operation->mac_size )
3330 {
3331 /* It's impossible to "truncate" to a larger length. */
3332 status = PSA_ERROR_INVALID_ARGUMENT;
3333 }
3334 else
3335 operation->mac_size = truncated;
3336
Gilles Peskinefbfac682018-07-08 20:51:54 +02003337exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003338 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003339 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003340 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003341 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003342 else
3343 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003344 operation->key_set = 1;
3345 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003346
Ronald Cron5c522922020-11-14 16:35:34 +01003347 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003348
Ronald Cron5c522922020-11-14 16:35:34 +01003349 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003350}
3351
Gilles Peskine89167cb2018-07-08 20:12:23 +02003352psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003353 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003354 psa_algorithm_t alg )
3355{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003356 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003357}
3358
3359psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003360 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003361 psa_algorithm_t alg )
3362{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003363 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003364}
3365
Gilles Peskine8c9def32018-02-08 10:02:12 +01003366psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3367 const uint8_t *input,
3368 size_t input_length )
3369{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003370 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003371 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003372 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003373 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003374 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003375 operation->has_input = 1;
3376
Gilles Peskine8c9def32018-02-08 10:02:12 +01003377#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003378 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003379 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003380 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3381 input, input_length );
3382 status = mbedtls_to_psa_error( ret );
3383 }
3384 else
3385#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003386#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003387 if( PSA_ALG_IS_HMAC( operation->alg ) )
3388 {
3389 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3390 input_length );
3391 }
3392 else
John Durkopd0321952020-10-29 21:37:36 -07003393#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003394 {
3395 /* This shouldn't happen if `operation` was initialized by
3396 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003397 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003398 }
3399
Gilles Peskinefbfac682018-07-08 20:51:54 +02003400 if( status != PSA_SUCCESS )
3401 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003402 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003403}
3404
John Durkop6ba40d12020-11-10 08:50:04 -08003405#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003406static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3407 uint8_t *mac,
3408 size_t mac_size )
3409{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003410 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003411 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3412 size_t hash_size = 0;
3413 size_t block_size = psa_get_hash_block_size( hash_alg );
3414 psa_status_t status;
3415
Gilles Peskine01126fa2018-07-12 17:04:55 +02003416 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3417 if( status != PSA_SUCCESS )
3418 return( status );
3419 /* From here on, tmp needs to be wiped. */
3420
3421 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3422 if( status != PSA_SUCCESS )
3423 goto exit;
3424
3425 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3426 if( status != PSA_SUCCESS )
3427 goto exit;
3428
3429 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3430 if( status != PSA_SUCCESS )
3431 goto exit;
3432
Gilles Peskined911eb72018-08-14 15:18:45 +02003433 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3434 if( status != PSA_SUCCESS )
3435 goto exit;
3436
3437 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003438
3439exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003440 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003441 return( status );
3442}
John Durkop6ba40d12020-11-10 08:50:04 -08003443#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003444
mohammad16036df908f2018-04-02 08:34:15 -07003445static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003446 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003447 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003448{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003449 if( ! operation->key_set )
3450 return( PSA_ERROR_BAD_STATE );
3451 if( operation->iv_required && ! operation->iv_set )
3452 return( PSA_ERROR_BAD_STATE );
3453
Gilles Peskine8c9def32018-02-08 10:02:12 +01003454 if( mac_size < operation->mac_size )
3455 return( PSA_ERROR_BUFFER_TOO_SMALL );
3456
Gilles Peskine8c9def32018-02-08 10:02:12 +01003457#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003458 if( operation->alg == PSA_ALG_CMAC )
3459 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003460 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3461 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3462 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003463 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003464 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003465 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003466 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003467 else
3468#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003469#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003470 if( PSA_ALG_IS_HMAC( operation->alg ) )
3471 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003472 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003473 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003474 }
3475 else
John Durkopd0321952020-10-29 21:37:36 -07003476#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003477 {
3478 /* This shouldn't happen if `operation` was initialized by
3479 * a setup function. */
3480 return( PSA_ERROR_BAD_STATE );
3481 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003482}
3483
Gilles Peskineacd4be32018-07-08 19:56:25 +02003484psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3485 uint8_t *mac,
3486 size_t mac_size,
3487 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003488{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003489 psa_status_t status;
3490
Jaeden Amero252ef282019-02-15 14:05:35 +00003491 if( operation->alg == 0 )
3492 {
3493 return( PSA_ERROR_BAD_STATE );
3494 }
3495
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003496 /* Fill the output buffer with something that isn't a valid mac
3497 * (barring an attack on the mac and deliberately-crafted input),
3498 * in case the caller doesn't check the return status properly. */
3499 *mac_length = mac_size;
3500 /* If mac_size is 0 then mac may be NULL and then the
3501 * call to memset would have undefined behavior. */
3502 if( mac_size != 0 )
3503 memset( mac, '!', mac_size );
3504
Gilles Peskine89167cb2018-07-08 20:12:23 +02003505 if( ! operation->is_sign )
3506 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003507 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003508 }
mohammad16036df908f2018-04-02 08:34:15 -07003509
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003510 status = psa_mac_finish_internal( operation, mac, mac_size );
3511
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003512 if( status == PSA_SUCCESS )
3513 {
3514 status = psa_mac_abort( operation );
3515 if( status == PSA_SUCCESS )
3516 *mac_length = operation->mac_size;
3517 else
3518 memset( mac, '!', mac_size );
3519 }
3520 else
3521 psa_mac_abort( operation );
3522 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003523}
3524
Gilles Peskineacd4be32018-07-08 19:56:25 +02003525psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3526 const uint8_t *mac,
3527 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003528{
Gilles Peskine828ed142018-06-18 23:25:51 +02003529 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003530 psa_status_t status;
3531
Jaeden Amero252ef282019-02-15 14:05:35 +00003532 if( operation->alg == 0 )
3533 {
3534 return( PSA_ERROR_BAD_STATE );
3535 }
3536
Gilles Peskine89167cb2018-07-08 20:12:23 +02003537 if( operation->is_sign )
3538 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003539 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003540 }
3541 if( operation->mac_size != mac_length )
3542 {
3543 status = PSA_ERROR_INVALID_SIGNATURE;
3544 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003545 }
mohammad16036df908f2018-04-02 08:34:15 -07003546
3547 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003548 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003549 if( status != PSA_SUCCESS )
3550 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003551
3552 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3553 status = PSA_ERROR_INVALID_SIGNATURE;
3554
3555cleanup:
3556 if( status == PSA_SUCCESS )
3557 status = psa_mac_abort( operation );
3558 else
3559 psa_mac_abort( operation );
3560
Gilles Peskine3f108122018-12-07 18:14:53 +01003561 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003562
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003563 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003564}
3565
3566
Gilles Peskine20035e32018-02-03 22:44:14 +01003567
Gilles Peskine20035e32018-02-03 22:44:14 +01003568/****************************************************************/
3569/* Asymmetric cryptography */
3570/****************************************************************/
3571
John Durkop6ba40d12020-11-10 08:50:04 -08003572#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3573 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003574/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003575 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003576static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3577 size_t hash_length,
3578 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003579{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003580 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003581 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003582 *md_alg = mbedtls_md_get_type( md_info );
3583
3584 /* The Mbed TLS RSA module uses an unsigned int for hash length
3585 * parameters. Validate that it fits so that we don't risk an
3586 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003587#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003588 if( hash_length > UINT_MAX )
3589 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003590#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003591
John Durkop0e005192020-10-31 22:06:54 -07003592#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003593 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3594 * must be correct. */
3595 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3596 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003597 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003598 if( md_info == NULL )
3599 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003600 if( mbedtls_md_get_size( md_info ) != hash_length )
3601 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003602 }
John Durkop0e005192020-10-31 22:06:54 -07003603#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003604
John Durkop0e005192020-10-31 22:06:54 -07003605#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003606 /* PSS requires a hash internally. */
3607 if( PSA_ALG_IS_RSA_PSS( alg ) )
3608 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003609 if( md_info == NULL )
3610 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003611 }
John Durkop0e005192020-10-31 22:06:54 -07003612#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003613
Gilles Peskine61b91d42018-06-08 16:09:36 +02003614 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003615}
3616
Gilles Peskine2b450e32018-06-27 15:42:46 +02003617static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3618 psa_algorithm_t alg,
3619 const uint8_t *hash,
3620 size_t hash_length,
3621 uint8_t *signature,
3622 size_t signature_size,
3623 size_t *signature_length )
3624{
3625 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003627 mbedtls_md_type_t md_alg;
3628
3629 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3630 if( status != PSA_SUCCESS )
3631 return( status );
3632
Gilles Peskine630a18a2018-06-29 17:49:35 +02003633 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003634 return( PSA_ERROR_BUFFER_TOO_SMALL );
3635
John Durkop0e005192020-10-31 22:06:54 -07003636#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003637 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3638 {
3639 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3640 MBEDTLS_MD_NONE );
3641 ret = mbedtls_rsa_pkcs1_sign( rsa,
3642 mbedtls_ctr_drbg_random,
3643 &global_data.ctr_drbg,
3644 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003645 md_alg,
3646 (unsigned int) hash_length,
3647 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003648 signature );
3649 }
3650 else
John Durkop0e005192020-10-31 22:06:54 -07003651#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3652#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003653 if( PSA_ALG_IS_RSA_PSS( alg ) )
3654 {
3655 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3656 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3657 mbedtls_ctr_drbg_random,
3658 &global_data.ctr_drbg,
3659 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003660 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003661 (unsigned int) hash_length,
3662 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003663 signature );
3664 }
3665 else
John Durkop0e005192020-10-31 22:06:54 -07003666#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003667 {
3668 return( PSA_ERROR_INVALID_ARGUMENT );
3669 }
3670
3671 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003672 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003673 return( mbedtls_to_psa_error( ret ) );
3674}
3675
3676static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3677 psa_algorithm_t alg,
3678 const uint8_t *hash,
3679 size_t hash_length,
3680 const uint8_t *signature,
3681 size_t signature_length )
3682{
3683 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003685 mbedtls_md_type_t md_alg;
3686
3687 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3688 if( status != PSA_SUCCESS )
3689 return( status );
3690
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003691 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3692 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003693
John Durkop0e005192020-10-31 22:06:54 -07003694#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003695 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3696 {
3697 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3698 MBEDTLS_MD_NONE );
3699 ret = mbedtls_rsa_pkcs1_verify( rsa,
3700 mbedtls_ctr_drbg_random,
3701 &global_data.ctr_drbg,
3702 MBEDTLS_RSA_PUBLIC,
3703 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003704 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003705 hash,
3706 signature );
3707 }
3708 else
John Durkop0e005192020-10-31 22:06:54 -07003709#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3710#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003711 if( PSA_ALG_IS_RSA_PSS( alg ) )
3712 {
3713 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3714 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3715 mbedtls_ctr_drbg_random,
3716 &global_data.ctr_drbg,
3717 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003718 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003719 (unsigned int) hash_length,
3720 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003721 signature );
3722 }
3723 else
John Durkop0e005192020-10-31 22:06:54 -07003724#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003725 {
3726 return( PSA_ERROR_INVALID_ARGUMENT );
3727 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003728
3729 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3730 * the rest of the signature is invalid". This has little use in
3731 * practice and PSA doesn't report this distinction. */
3732 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3733 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003734 return( mbedtls_to_psa_error( ret ) );
3735}
John Durkop6ba40d12020-11-10 08:50:04 -08003736#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3737 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003738
John Durkop6ba40d12020-11-10 08:50:04 -08003739#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3740 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003741/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3742 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3743 * (even though these functions don't modify it). */
3744static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3745 psa_algorithm_t alg,
3746 const uint8_t *hash,
3747 size_t hash_length,
3748 uint8_t *signature,
3749 size_t signature_size,
3750 size_t *signature_length )
3751{
Janos Follath24eed8d2019-11-22 13:21:35 +00003752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003753 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003754 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003755 mbedtls_mpi_init( &r );
3756 mbedtls_mpi_init( &s );
3757
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003758 if( signature_size < 2 * curve_bytes )
3759 {
3760 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3761 goto cleanup;
3762 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003763
John Durkop0ea39e02020-10-13 19:58:20 -07003764#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003765 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3766 {
3767 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3768 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3769 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003770 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3771 &ecp->d, hash,
3772 hash_length, md_alg,
3773 mbedtls_ctr_drbg_random,
3774 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003775 }
3776 else
John Durkop0ea39e02020-10-13 19:58:20 -07003777#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003778 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003779 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003780 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3781 hash, hash_length,
3782 mbedtls_ctr_drbg_random,
3783 &global_data.ctr_drbg ) );
3784 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003785
3786 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3787 signature,
3788 curve_bytes ) );
3789 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3790 signature + curve_bytes,
3791 curve_bytes ) );
3792
3793cleanup:
3794 mbedtls_mpi_free( &r );
3795 mbedtls_mpi_free( &s );
3796 if( ret == 0 )
3797 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003798 return( mbedtls_to_psa_error( ret ) );
3799}
3800
3801static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3802 const uint8_t *hash,
3803 size_t hash_length,
3804 const uint8_t *signature,
3805 size_t signature_length )
3806{
Janos Follath24eed8d2019-11-22 13:21:35 +00003807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003808 mbedtls_mpi r, s;
3809 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3810 mbedtls_mpi_init( &r );
3811 mbedtls_mpi_init( &s );
3812
3813 if( signature_length != 2 * curve_bytes )
3814 return( PSA_ERROR_INVALID_SIGNATURE );
3815
3816 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3817 signature,
3818 curve_bytes ) );
3819 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3820 signature + curve_bytes,
3821 curve_bytes ) );
3822
Steven Cooremanacda8342020-07-24 23:09:52 +02003823 /* Check whether the public part is loaded. If not, load it. */
3824 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3825 {
3826 MBEDTLS_MPI_CHK(
3827 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3828 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3829 }
3830
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003831 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3832 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003833
3834cleanup:
3835 mbedtls_mpi_free( &r );
3836 mbedtls_mpi_free( &s );
3837 return( mbedtls_to_psa_error( ret ) );
3838}
John Durkop6ba40d12020-11-10 08:50:04 -08003839#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3840 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003841
Ronald Croncf56a0a2020-08-04 09:51:30 +02003842psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003843 psa_algorithm_t alg,
3844 const uint8_t *hash,
3845 size_t hash_length,
3846 uint8_t *signature,
3847 size_t signature_size,
3848 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003849{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003851 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003852 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003853
3854 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003855 /* Immediately reject a zero-length signature buffer. This guarantees
3856 * that signature must be a valid pointer. (On the other hand, the hash
3857 * buffer can in principle be empty since it doesn't actually have
3858 * to be a hash.) */
3859 if( signature_size == 0 )
3860 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003861
Ronald Cron5c522922020-11-14 16:35:34 +01003862 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3863 PSA_KEY_USAGE_SIGN_HASH,
3864 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003865 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003866 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003867 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003868 {
3869 status = PSA_ERROR_INVALID_ARGUMENT;
3870 goto exit;
3871 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003872
Steven Cooremancd84cb42020-07-16 20:28:36 +02003873 /* Try any of the available accelerators first */
3874 status = psa_driver_wrapper_sign_hash( slot,
3875 alg,
3876 hash,
3877 hash_length,
3878 signature,
3879 signature_size,
3880 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003881 if( status != PSA_ERROR_NOT_SUPPORTED ||
3882 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003883 goto exit;
3884
Steven Cooreman7a250572020-07-17 16:43:05 +02003885 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003886#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3887 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003888 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003889 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003890 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003891
Steven Cooreman4fed4552020-08-03 14:46:03 +02003892 status = psa_load_rsa_representation( slot->attr.type,
3893 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003894 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003895 &rsa );
3896 if( status != PSA_SUCCESS )
3897 goto exit;
3898
Steven Cooremana2371e52020-07-28 14:30:39 +02003899 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003900 alg,
3901 hash, hash_length,
3902 signature, signature_size,
3903 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003904
Steven Cooremana2371e52020-07-28 14:30:39 +02003905 mbedtls_rsa_free( rsa );
3906 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003907 }
3908 else
John Durkop6ba40d12020-11-10 08:50:04 -08003909#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3910 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003911 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003912 {
John Durkop9814fa22020-11-04 12:28:15 -08003913#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3914 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003915 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003916#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003917 PSA_ALG_IS_ECDSA( alg )
3918#else
3919 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3920#endif
3921 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003922 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003923 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003924 status = psa_load_ecp_representation( slot->attr.type,
3925 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003926 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003927 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003928 if( status != PSA_SUCCESS )
3929 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003930 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003931 alg,
3932 hash, hash_length,
3933 signature, signature_size,
3934 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003935 mbedtls_ecp_keypair_free( ecp );
3936 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003937 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003938 else
John Durkop9814fa22020-11-04 12:28:15 -08003939#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3940 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003941 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003942 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003943 }
itayzafrir5c753392018-05-08 11:18:38 +03003944 }
3945 else
itayzafrir5c753392018-05-08 11:18:38 +03003946 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003947 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003948 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003949
3950exit:
3951 /* Fill the unused part of the output buffer (the whole buffer on error,
3952 * the trailing part on success) with something that isn't a valid mac
3953 * (barring an attack on the mac and deliberately-crafted input),
3954 * in case the caller doesn't check the return status properly. */
3955 if( status == PSA_SUCCESS )
3956 memset( signature + *signature_length, '!',
3957 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003958 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003959 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003960 /* If signature_size is 0 then we have nothing to do. We must not call
3961 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003962
Ronald Cron5c522922020-11-14 16:35:34 +01003963 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003964
Ronald Cron5c522922020-11-14 16:35:34 +01003965 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003966}
3967
Ronald Croncf56a0a2020-08-04 09:51:30 +02003968psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003969 psa_algorithm_t alg,
3970 const uint8_t *hash,
3971 size_t hash_length,
3972 const uint8_t *signature,
3973 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003974{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003976 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003977 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003978
Ronald Cron5c522922020-11-14 16:35:34 +01003979 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3980 PSA_KEY_USAGE_VERIFY_HASH,
3981 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003982 if( status != PSA_SUCCESS )
3983 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003984
Steven Cooreman55ae2172020-07-17 19:46:15 +02003985 /* Try any of the available accelerators first */
3986 status = psa_driver_wrapper_verify_hash( slot,
3987 alg,
3988 hash,
3989 hash_length,
3990 signature,
3991 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003992 if( status != PSA_ERROR_NOT_SUPPORTED ||
3993 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003994 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003995
John Durkop6ba40d12020-11-10 08:50:04 -08003996#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3997 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003998 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003999 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004000 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004001
Steven Cooreman4fed4552020-08-03 14:46:03 +02004002 status = psa_load_rsa_representation( slot->attr.type,
4003 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004004 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004005 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004006 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004007 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004008
Steven Cooremana2371e52020-07-28 14:30:39 +02004009 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004010 alg,
4011 hash, hash_length,
4012 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004013 mbedtls_rsa_free( rsa );
4014 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004015 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 }
4017 else
John Durkop6ba40d12020-11-10 08:50:04 -08004018#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4019 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004020 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004021 {
John Durkop9814fa22020-11-04 12:28:15 -08004022#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4023 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004024 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004025 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004026 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004027 status = psa_load_ecp_representation( slot->attr.type,
4028 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004029 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004030 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004031 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004032 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004033 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004034 hash, hash_length,
4035 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004036 mbedtls_ecp_keypair_free( ecp );
4037 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004038 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004039 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004040 else
John Durkop9814fa22020-11-04 12:28:15 -08004041#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4042 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004043 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004044 status = PSA_ERROR_INVALID_ARGUMENT;
4045 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004046 }
itayzafrir5c753392018-05-08 11:18:38 +03004047 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004048 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004049 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004050 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004051 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004052
4053exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004054 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004055
Ronald Cron5c522922020-11-14 16:35:34 +01004056 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004057}
4058
John Durkop0e005192020-10-31 22:06:54 -07004059#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004060static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4061 mbedtls_rsa_context *rsa )
4062{
4063 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4064 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4065 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4066 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4067}
John Durkop0e005192020-10-31 22:06:54 -07004068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004069
Ronald Croncf56a0a2020-08-04 09:51:30 +02004070psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004071 psa_algorithm_t alg,
4072 const uint8_t *input,
4073 size_t input_length,
4074 const uint8_t *salt,
4075 size_t salt_length,
4076 uint8_t *output,
4077 size_t output_size,
4078 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004081 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004082 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004083
Darryl Green5cc689a2018-07-24 15:34:10 +01004084 (void) input;
4085 (void) input_length;
4086 (void) salt;
4087 (void) output;
4088 (void) output_size;
4089
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004090 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004092 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4093 return( PSA_ERROR_INVALID_ARGUMENT );
4094
Ronald Cron5c522922020-11-14 16:35:34 +01004095 status = psa_get_and_lock_transparent_key_slot_with_policy(
4096 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004097 if( status != PSA_SUCCESS )
4098 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004099 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4100 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004101 {
4102 status = PSA_ERROR_INVALID_ARGUMENT;
4103 goto exit;
4104 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105
John Durkop6ba40d12020-11-10 08:50:04 -08004106#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4107 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004108 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004110 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004111 status = psa_load_rsa_representation( slot->attr.type,
4112 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004113 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004114 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004115 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004116 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004117
4118 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004119 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004120 status = PSA_ERROR_BUFFER_TOO_SMALL;
4121 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004122 }
John Durkop0e005192020-10-31 22:06:54 -07004123#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004124 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004126 status = mbedtls_to_psa_error(
4127 mbedtls_rsa_pkcs1_encrypt( rsa,
4128 mbedtls_ctr_drbg_random,
4129 &global_data.ctr_drbg,
4130 MBEDTLS_RSA_PUBLIC,
4131 input_length,
4132 input,
4133 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004134 }
4135 else
John Durkop0e005192020-10-31 22:06:54 -07004136#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4137#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004138 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004139 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004140 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004141 status = mbedtls_to_psa_error(
4142 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4143 mbedtls_ctr_drbg_random,
4144 &global_data.ctr_drbg,
4145 MBEDTLS_RSA_PUBLIC,
4146 salt, salt_length,
4147 input_length,
4148 input,
4149 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150 }
4151 else
John Durkop0e005192020-10-31 22:06:54 -07004152#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004154 status = PSA_ERROR_INVALID_ARGUMENT;
4155 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004157rsa_exit:
4158 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004159 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004160
Steven Cooremana2371e52020-07-28 14:30:39 +02004161 mbedtls_rsa_free( rsa );
4162 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164 else
John Durkop9814fa22020-11-04 12:28:15 -08004165#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004166 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004168 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004170
4171exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004172 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004173
Ronald Cron5c522922020-11-14 16:35:34 +01004174 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004175}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176
Ronald Croncf56a0a2020-08-04 09:51:30 +02004177psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004178 psa_algorithm_t alg,
4179 const uint8_t *input,
4180 size_t input_length,
4181 const uint8_t *salt,
4182 size_t salt_length,
4183 uint8_t *output,
4184 size_t output_size,
4185 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004188 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004189 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004190
Darryl Green5cc689a2018-07-24 15:34:10 +01004191 (void) input;
4192 (void) input_length;
4193 (void) salt;
4194 (void) output;
4195 (void) output_size;
4196
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004197 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004199 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4200 return( PSA_ERROR_INVALID_ARGUMENT );
4201
Ronald Cron5c522922020-11-14 16:35:34 +01004202 status = psa_get_and_lock_transparent_key_slot_with_policy(
4203 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004204 if( status != PSA_SUCCESS )
4205 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004206 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004207 {
4208 status = PSA_ERROR_INVALID_ARGUMENT;
4209 goto exit;
4210 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004211
John Durkop6ba40d12020-11-10 08:50:04 -08004212#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4213 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004214 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004216 mbedtls_rsa_context *rsa = NULL;
4217 status = psa_load_rsa_representation( slot->attr.type,
4218 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004219 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004220 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004221 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004222 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223
Steven Cooremana2371e52020-07-28 14:30:39 +02004224 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004225 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004226 status = PSA_ERROR_INVALID_ARGUMENT;
4227 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004228 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229
John Durkop0e005192020-10-31 22:06:54 -07004230#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004231 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004232 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004233 status = mbedtls_to_psa_error(
4234 mbedtls_rsa_pkcs1_decrypt( rsa,
4235 mbedtls_ctr_drbg_random,
4236 &global_data.ctr_drbg,
4237 MBEDTLS_RSA_PRIVATE,
4238 output_length,
4239 input,
4240 output,
4241 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004242 }
4243 else
John Durkop0e005192020-10-31 22:06:54 -07004244#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4245#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004246 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004247 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004248 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004249 status = mbedtls_to_psa_error(
4250 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4251 mbedtls_ctr_drbg_random,
4252 &global_data.ctr_drbg,
4253 MBEDTLS_RSA_PRIVATE,
4254 salt, salt_length,
4255 output_length,
4256 input,
4257 output,
4258 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 }
4260 else
John Durkop0e005192020-10-31 22:06:54 -07004261#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004263 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004265
Steven Cooreman4fed4552020-08-03 14:46:03 +02004266rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004267 mbedtls_rsa_free( rsa );
4268 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004269 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004270 else
John Durkop6ba40d12020-11-10 08:50:04 -08004271#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4272 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004273 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004274 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004275 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004276
4277exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004278 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004279
Ronald Cron5c522922020-11-14 16:35:34 +01004280 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004281}
Gilles Peskine20035e32018-02-03 22:44:14 +01004282
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004283
4284
mohammad1603503973b2018-03-12 15:59:30 +02004285/****************************************************************/
4286/* Symmetric cryptography */
4287/****************************************************************/
4288
Gilles Peskinee553c652018-06-04 16:22:46 +02004289static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004290 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004291 psa_algorithm_t alg,
4292 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004293{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004295 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004296 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004297 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004298 size_t key_bits;
4299 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004300 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4301 PSA_KEY_USAGE_ENCRYPT :
4302 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004303
Steven Cooremand3feccd2020-09-01 15:56:14 +02004304 /* A context must be freshly initialized before it can be set up. */
4305 if( operation->alg != 0 )
4306 return( PSA_ERROR_BAD_STATE );
4307
Steven Cooremana07b9972020-09-10 14:54:14 +02004308 /* The requested algorithm must be one that can be processed by cipher. */
4309 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004310 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004311
Steven Cooremanef8575e2020-09-11 11:44:50 +02004312 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004313 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004314 if( status != PSA_SUCCESS )
4315 goto exit;
4316
4317 /* Initialize the operation struct members, except for alg. The alg member
4318 * is used to indicate to psa_cipher_abort that there are resources to free,
4319 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004320 operation->key_set = 0;
4321 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004322 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004323 operation->iv_size = 0;
4324 operation->block_size = 0;
4325 if( alg == PSA_ALG_ECB_NO_PADDING )
4326 operation->iv_required = 0;
4327 else
4328 operation->iv_required = 1;
4329
Steven Cooremana07b9972020-09-10 14:54:14 +02004330 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004331 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004332 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004333 slot,
4334 alg );
4335 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004336 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004337 slot,
4338 alg );
4339
Steven Cooremanef8575e2020-09-11 11:44:50 +02004340 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004341 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004342 /* Once the driver context is initialised, it needs to be freed using
4343 * psa_cipher_abort. Indicate this through setting alg. */
4344 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004345 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004346
Steven Cooremand3feccd2020-09-01 15:56:14 +02004347 if( status != PSA_ERROR_NOT_SUPPORTED ||
4348 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4349 goto exit;
4350
Steven Cooremana07b9972020-09-10 14:54:14 +02004351 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004352 * available for the given algorithm & key. */
4353 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004354
Steven Cooremana07b9972020-09-10 14:54:14 +02004355 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004356 * psa_cipher_abort. Indicate there is something to be freed through setting
4357 * alg, and indicate the operation is being done using mbedtls crypto through
4358 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004359 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004360 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004361
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004362 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004363 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004364 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004365 {
4366 status = PSA_ERROR_NOT_SUPPORTED;
4367 goto exit;
4368 }
mohammad1603503973b2018-03-12 15:59:30 +02004369
mohammad1603503973b2018-03-12 15:59:30 +02004370 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004371 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004372 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004373
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004374#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004375 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004376 {
4377 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004378 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004379 memcpy( keys, slot->data.key.data, 16 );
4380 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004381 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4382 keys,
4383 192, cipher_operation );
4384 }
4385 else
4386#endif
4387 {
4388 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004389 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004390 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004391 }
Moran Peker41deec42018-04-04 15:43:05 +03004392 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004393 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004394
mohammad16038481e742018-03-18 13:57:31 +02004395#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004396 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004397 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004398 case PSA_ALG_CBC_NO_PADDING:
4399 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4400 MBEDTLS_PADDING_NONE );
4401 break;
4402 case PSA_ALG_CBC_PKCS7:
4403 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4404 MBEDTLS_PADDING_PKCS7 );
4405 break;
4406 default:
4407 /* The algorithm doesn't involve padding. */
4408 ret = 0;
4409 break;
4410 }
4411 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004412 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004413#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4414
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004415 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004416 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004417 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4418 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004419 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004420 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004421 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004422#if defined(MBEDTLS_CHACHA20_C)
4423 else
4424 if( alg == PSA_ALG_CHACHA20 )
4425 operation->iv_size = 12;
4426#endif
mohammad1603503973b2018-03-12 15:59:30 +02004427
Steven Cooreman7df02922020-09-09 15:28:49 +02004428 status = PSA_SUCCESS;
4429
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004430exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004431 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004432 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004433 if( status == PSA_SUCCESS )
4434 {
4435 /* Update operation flags for both driver and software implementations */
4436 operation->key_set = 1;
4437 }
4438 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004439 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004440
Ronald Cron5c522922020-11-14 16:35:34 +01004441 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004442
Ronald Cron5c522922020-11-14 16:35:34 +01004443 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004444}
4445
Gilles Peskinefe119512018-07-08 21:39:34 +02004446psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004447 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004448 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004449{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004450 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004451}
4452
Gilles Peskinefe119512018-07-08 21:39:34 +02004453psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004454 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004455 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004456{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004457 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004458}
4459
Gilles Peskinefe119512018-07-08 21:39:34 +02004460psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004461 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004462 size_t iv_size,
4463 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004464{
itayzafrir534bd7c2018-08-02 13:56:32 +03004465 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004467 if( operation->iv_set || ! operation->iv_required )
4468 {
4469 return( PSA_ERROR_BAD_STATE );
4470 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004471
Steven Cooremanef8575e2020-09-11 11:44:50 +02004472 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004473 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004474 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004475 iv,
4476 iv_size,
4477 iv_length );
4478 goto exit;
4479 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004480
Moran Peker41deec42018-04-04 15:43:05 +03004481 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004482 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004483 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004484 goto exit;
4485 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004486 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4487 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004488 if( ret != 0 )
4489 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004490 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004491 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004492 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004493
mohammad16038481e742018-03-18 13:57:31 +02004494 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004495 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004496
Moran Peker395db872018-05-31 14:07:14 +03004497exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004498 if( status == PSA_SUCCESS )
4499 operation->iv_set = 1;
4500 else
Moran Peker395db872018-05-31 14:07:14 +03004501 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004502 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004503}
4504
Gilles Peskinefe119512018-07-08 21:39:34 +02004505psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004506 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004507 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004508{
itayzafrir534bd7c2018-08-02 13:56:32 +03004509 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004511 if( operation->iv_set || ! operation->iv_required )
4512 {
4513 return( PSA_ERROR_BAD_STATE );
4514 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004515
Steven Cooremanef8575e2020-09-11 11:44:50 +02004516 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004517 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004518 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004519 iv,
4520 iv_length );
4521 goto exit;
4522 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004523
Moran Pekera28258c2018-05-29 16:25:04 +03004524 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004525 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004526 status = PSA_ERROR_INVALID_ARGUMENT;
4527 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004528 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004529 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4530 status = mbedtls_to_psa_error( ret );
4531exit:
4532 if( status == PSA_SUCCESS )
4533 operation->iv_set = 1;
4534 else
mohammad1603503973b2018-03-12 15:59:30 +02004535 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004536 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004537}
4538
Steven Cooreman177deba2020-09-07 17:14:14 +02004539/* Process input for which the algorithm is set to ECB mode. This requires
4540 * manual processing, since the PSA API is defined as being able to process
4541 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4542 * underlying mbedtls_cipher_update only takes full blocks. */
4543static psa_status_t psa_cipher_update_ecb_internal(
4544 mbedtls_cipher_context_t *ctx,
4545 const uint8_t *input,
4546 size_t input_length,
4547 uint8_t *output,
4548 size_t output_size,
4549 size_t *output_length )
4550{
4551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4552 size_t block_size = ctx->cipher_info->block_size;
4553 size_t internal_output_length = 0;
4554 *output_length = 0;
4555
4556 if( input_length == 0 )
4557 {
4558 status = PSA_SUCCESS;
4559 goto exit;
4560 }
4561
4562 if( ctx->unprocessed_len > 0 )
4563 {
4564 /* Fill up to block size, and run the block if there's a full one. */
4565 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4566
4567 if( input_length < bytes_to_copy )
4568 bytes_to_copy = input_length;
4569
4570 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4571 input, bytes_to_copy );
4572 input_length -= bytes_to_copy;
4573 input += bytes_to_copy;
4574 ctx->unprocessed_len += bytes_to_copy;
4575
4576 if( ctx->unprocessed_len == block_size )
4577 {
4578 status = mbedtls_to_psa_error(
4579 mbedtls_cipher_update( ctx,
4580 ctx->unprocessed_data,
4581 block_size,
4582 output, &internal_output_length ) );
4583
4584 if( status != PSA_SUCCESS )
4585 goto exit;
4586
4587 output += internal_output_length;
4588 output_size -= internal_output_length;
4589 *output_length += internal_output_length;
4590 ctx->unprocessed_len = 0;
4591 }
4592 }
4593
4594 while( input_length >= block_size )
4595 {
4596 /* Run all full blocks we have, one by one */
4597 status = mbedtls_to_psa_error(
4598 mbedtls_cipher_update( ctx, input,
4599 block_size,
4600 output, &internal_output_length ) );
4601
4602 if( status != PSA_SUCCESS )
4603 goto exit;
4604
4605 input_length -= block_size;
4606 input += block_size;
4607
4608 output += internal_output_length;
4609 output_size -= internal_output_length;
4610 *output_length += internal_output_length;
4611 }
4612
4613 if( input_length > 0 )
4614 {
4615 /* Save unprocessed bytes for later processing */
4616 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4617 input, input_length );
4618 ctx->unprocessed_len += input_length;
4619 }
4620
4621 status = PSA_SUCCESS;
4622
4623exit:
4624 return( status );
4625}
4626
Gilles Peskinee553c652018-06-04 16:22:46 +02004627psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4628 const uint8_t *input,
4629 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004630 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004631 size_t output_size,
4632 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004633{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004635 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004636 if( operation->alg == 0 )
4637 {
4638 return( PSA_ERROR_BAD_STATE );
4639 }
4640 if( operation->iv_required && ! operation->iv_set )
4641 {
4642 return( PSA_ERROR_BAD_STATE );
4643 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004644
Steven Cooremanef8575e2020-09-11 11:44:50 +02004645 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004646 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004647 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004648 input,
4649 input_length,
4650 output,
4651 output_size,
4652 output_length );
4653 goto exit;
4654 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004655
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004656 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004657 {
4658 /* Take the unprocessed partial block left over from previous
4659 * update calls, if any, plus the input to this call. Remove
4660 * the last partial block, if any. You get the data that will be
4661 * output in this call. */
4662 expected_output_size =
4663 ( operation->ctx.cipher.unprocessed_len + input_length )
4664 / operation->block_size * operation->block_size;
4665 }
4666 else
4667 {
4668 expected_output_size = input_length;
4669 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004670
Gilles Peskine89d789c2018-06-04 17:17:16 +02004671 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004672 {
4673 status = PSA_ERROR_BUFFER_TOO_SMALL;
4674 goto exit;
4675 }
mohammad160382759612018-03-12 18:16:40 +02004676
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004677 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4678 {
4679 /* mbedtls_cipher_update has an API inconsistency: it will only
4680 * process a single block at a time in ECB mode. Abstract away that
4681 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004682 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4683 input,
4684 input_length,
4685 output,
4686 output_size,
4687 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004688 }
4689 else
4690 {
4691 status = mbedtls_to_psa_error(
4692 mbedtls_cipher_update( &operation->ctx.cipher, input,
4693 input_length, output, output_length ) );
4694 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004695exit:
4696 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004697 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004698 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004699}
4700
Gilles Peskinee553c652018-06-04 16:22:46 +02004701psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4702 uint8_t *output,
4703 size_t output_size,
4704 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004705{
David Saadab4ecc272019-02-14 13:48:10 +02004706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004707 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004708 if( operation->alg == 0 )
4709 {
4710 return( PSA_ERROR_BAD_STATE );
4711 }
4712 if( operation->iv_required && ! operation->iv_set )
4713 {
4714 return( PSA_ERROR_BAD_STATE );
4715 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004716
Steven Cooremanef8575e2020-09-11 11:44:50 +02004717 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004718 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004719 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004720 output,
4721 output_size,
4722 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004723 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004724 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004725
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004726 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004727 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004728 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004729 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004730 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004731 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004732 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004733 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004734 }
4735
Steven Cooremanef8575e2020-09-11 11:44:50 +02004736 status = mbedtls_to_psa_error(
4737 mbedtls_cipher_finish( &operation->ctx.cipher,
4738 temp_output_buffer,
4739 output_length ) );
4740 if( status != PSA_SUCCESS )
4741 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004742
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004743 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004744 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004745 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004746 memcpy( output, temp_output_buffer, *output_length );
4747 else
Janos Follath315b51c2018-07-09 16:04:51 +01004748 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004749
Steven Cooremanef8575e2020-09-11 11:44:50 +02004750exit:
4751 if( operation->mbedtls_in_use == 1 )
4752 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004753
Steven Cooremanef8575e2020-09-11 11:44:50 +02004754 if( status == PSA_SUCCESS )
4755 return( psa_cipher_abort( operation ) );
4756 else
4757 {
4758 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004759 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004760
Steven Cooremanef8575e2020-09-11 11:44:50 +02004761 return( status );
4762 }
mohammad1603503973b2018-03-12 15:59:30 +02004763}
4764
Gilles Peskinee553c652018-06-04 16:22:46 +02004765psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4766{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004767 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004768 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004769 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004770 * in use. It's ok to call abort on such an object, and there's
4771 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004772 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004773 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004774
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004775 /* Sanity check (shouldn't happen: operation->alg should
4776 * always have been initialized to a valid value). */
4777 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4778 return( PSA_ERROR_BAD_STATE );
4779
Steven Cooremanef8575e2020-09-11 11:44:50 +02004780 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004781 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004782 else
4783 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004784
Moran Peker41deec42018-04-04 15:43:05 +03004785 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004786 operation->key_set = 0;
4787 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004788 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004789 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004790 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004791 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004792
Moran Peker395db872018-05-31 14:07:14 +03004793 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004794}
4795
Gilles Peskinea0655c32018-04-30 17:06:50 +02004796
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004797
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004798
mohammad16035955c982018-04-26 00:53:03 +03004799/****************************************************************/
4800/* AEAD */
4801/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004802
Gilles Peskineedf9a652018-08-17 18:11:56 +02004803typedef struct
4804{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004805 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004806 const mbedtls_cipher_info_t *cipher_info;
4807 union
4808 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004809 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004810#if defined(MBEDTLS_CCM_C)
4811 mbedtls_ccm_context ccm;
4812#endif /* MBEDTLS_CCM_C */
4813#if defined(MBEDTLS_GCM_C)
4814 mbedtls_gcm_context gcm;
4815#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004816#if defined(MBEDTLS_CHACHAPOLY_C)
4817 mbedtls_chachapoly_context chachapoly;
4818#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004819 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004820 psa_algorithm_t core_alg;
4821 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004822 uint8_t tag_length;
4823} aead_operation_t;
4824
Ronald Cronf95a2b12020-10-22 15:24:49 +02004825#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4826
Gilles Peskine30a9e412019-01-14 18:36:12 +01004827static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004828{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004829 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004830 {
4831#if defined(MBEDTLS_CCM_C)
4832 case PSA_ALG_CCM:
4833 mbedtls_ccm_free( &operation->ctx.ccm );
4834 break;
4835#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004836#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 case PSA_ALG_GCM:
4838 mbedtls_gcm_free( &operation->ctx.gcm );
4839 break;
4840#endif /* MBEDTLS_GCM_C */
4841 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004842
Ronald Cron5c522922020-11-14 16:35:34 +01004843 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004844}
4845
4846static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004847 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004848 psa_key_usage_t usage,
4849 psa_algorithm_t alg )
4850{
4851 psa_status_t status;
4852 size_t key_bits;
4853 mbedtls_cipher_id_t cipher_id;
4854
Ronald Cron5c522922020-11-14 16:35:34 +01004855 status = psa_get_and_lock_transparent_key_slot_with_policy(
4856 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004857 if( status != PSA_SUCCESS )
4858 return( status );
4859
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004860 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004861
4862 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004863 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004864 &cipher_id );
4865 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004866 {
4867 status = PSA_ERROR_NOT_SUPPORTED;
4868 goto cleanup;
4869 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004870
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004871 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 {
4873#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004874 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4875 operation->core_alg = PSA_ALG_CCM;
4876 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004877 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4878 * The call to mbedtls_ccm_encrypt_and_tag or
4879 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004880 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004881 {
4882 status = PSA_ERROR_INVALID_ARGUMENT;
4883 goto cleanup;
4884 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004885 mbedtls_ccm_init( &operation->ctx.ccm );
4886 status = mbedtls_to_psa_error(
4887 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004888 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889 (unsigned int) key_bits ) );
4890 if( status != 0 )
4891 goto cleanup;
4892 break;
4893#endif /* MBEDTLS_CCM_C */
4894
4895#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004896 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4897 operation->core_alg = PSA_ALG_GCM;
4898 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004899 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4900 * The call to mbedtls_gcm_crypt_and_tag or
4901 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004902 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004903 {
4904 status = PSA_ERROR_INVALID_ARGUMENT;
4905 goto cleanup;
4906 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004907 mbedtls_gcm_init( &operation->ctx.gcm );
4908 status = mbedtls_to_psa_error(
4909 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004910 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004911 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004912 if( status != 0 )
4913 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004914 break;
4915#endif /* MBEDTLS_GCM_C */
4916
Gilles Peskine26869f22019-05-06 15:25:00 +02004917#if defined(MBEDTLS_CHACHAPOLY_C)
4918 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4919 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4920 operation->full_tag_length = 16;
4921 /* We only support the default tag length. */
4922 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004923 {
4924 status = PSA_ERROR_NOT_SUPPORTED;
4925 goto cleanup;
4926 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004927 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4928 status = mbedtls_to_psa_error(
4929 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004930 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004931 if( status != 0 )
4932 goto cleanup;
4933 break;
4934#endif /* MBEDTLS_CHACHAPOLY_C */
4935
Gilles Peskineedf9a652018-08-17 18:11:56 +02004936 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004937 status = PSA_ERROR_NOT_SUPPORTED;
4938 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939 }
4940
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004941 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4942 {
4943 status = PSA_ERROR_INVALID_ARGUMENT;
4944 goto cleanup;
4945 }
4946 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004947
Gilles Peskineedf9a652018-08-17 18:11:56 +02004948 return( PSA_SUCCESS );
4949
4950cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004951 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004952 return( status );
4953}
4954
Ronald Croncf56a0a2020-08-04 09:51:30 +02004955psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004956 psa_algorithm_t alg,
4957 const uint8_t *nonce,
4958 size_t nonce_length,
4959 const uint8_t *additional_data,
4960 size_t additional_data_length,
4961 const uint8_t *plaintext,
4962 size_t plaintext_length,
4963 uint8_t *ciphertext,
4964 size_t ciphertext_size,
4965 size_t *ciphertext_length )
4966{
mohammad16035955c982018-04-26 00:53:03 +03004967 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004968 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004969 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004970
mohammad1603f08a5502018-06-03 15:05:47 +03004971 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004972
Ronald Croncf56a0a2020-08-04 09:51:30 +02004973 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004974 if( status != PSA_SUCCESS )
4975 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004976
Gilles Peskineedf9a652018-08-17 18:11:56 +02004977 /* For all currently supported modes, the tag is at the end of the
4978 * ciphertext. */
4979 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4980 {
4981 status = PSA_ERROR_BUFFER_TOO_SMALL;
4982 goto exit;
4983 }
4984 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004985
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004986#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004987 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004988 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004989 status = mbedtls_to_psa_error(
4990 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4991 MBEDTLS_GCM_ENCRYPT,
4992 plaintext_length,
4993 nonce, nonce_length,
4994 additional_data, additional_data_length,
4995 plaintext, ciphertext,
4996 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004997 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004998 else
4999#endif /* MBEDTLS_GCM_C */
5000#if defined(MBEDTLS_CCM_C)
5001 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005002 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005003 status = mbedtls_to_psa_error(
5004 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5005 plaintext_length,
5006 nonce, nonce_length,
5007 additional_data,
5008 additional_data_length,
5009 plaintext, ciphertext,
5010 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005011 }
mohammad16035c8845f2018-05-09 05:40:09 -07005012 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005013#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005014#if defined(MBEDTLS_CHACHAPOLY_C)
5015 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5016 {
5017 if( nonce_length != 12 || operation.tag_length != 16 )
5018 {
5019 status = PSA_ERROR_NOT_SUPPORTED;
5020 goto exit;
5021 }
5022 status = mbedtls_to_psa_error(
5023 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5024 plaintext_length,
5025 nonce,
5026 additional_data,
5027 additional_data_length,
5028 plaintext,
5029 ciphertext,
5030 tag ) );
5031 }
5032 else
5033#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005034 {
mohammad1603554faad2018-06-03 15:07:38 +03005035 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005036 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005037
Gilles Peskineedf9a652018-08-17 18:11:56 +02005038 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5039 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005040
Gilles Peskineedf9a652018-08-17 18:11:56 +02005041exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005042 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005043 if( status == PSA_SUCCESS )
5044 *ciphertext_length = plaintext_length + operation.tag_length;
5045 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005046}
5047
Gilles Peskineee652a32018-06-01 19:23:52 +02005048/* Locate the tag in a ciphertext buffer containing the encrypted data
5049 * followed by the tag. Return the length of the part preceding the tag in
5050 * *plaintext_length. This is the size of the plaintext in modes where
5051 * the encrypted data has the same size as the plaintext, such as
5052 * CCM and GCM. */
5053static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5054 const uint8_t *ciphertext,
5055 size_t ciphertext_length,
5056 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005057 const uint8_t **p_tag )
5058{
5059 size_t payload_length;
5060 if( tag_length > ciphertext_length )
5061 return( PSA_ERROR_INVALID_ARGUMENT );
5062 payload_length = ciphertext_length - tag_length;
5063 if( payload_length > plaintext_size )
5064 return( PSA_ERROR_BUFFER_TOO_SMALL );
5065 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005066 return( PSA_SUCCESS );
5067}
5068
Ronald Croncf56a0a2020-08-04 09:51:30 +02005069psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005070 psa_algorithm_t alg,
5071 const uint8_t *nonce,
5072 size_t nonce_length,
5073 const uint8_t *additional_data,
5074 size_t additional_data_length,
5075 const uint8_t *ciphertext,
5076 size_t ciphertext_length,
5077 uint8_t *plaintext,
5078 size_t plaintext_size,
5079 size_t *plaintext_length )
5080{
mohammad16035955c982018-04-26 00:53:03 +03005081 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005082 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005083 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005084
Gilles Peskineee652a32018-06-01 19:23:52 +02005085 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005086
Ronald Croncf56a0a2020-08-04 09:51:30 +02005087 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005088 if( status != PSA_SUCCESS )
5089 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005090
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005091 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5092 ciphertext, ciphertext_length,
5093 plaintext_size, &tag );
5094 if( status != PSA_SUCCESS )
5095 goto exit;
5096
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005097#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005098 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005099 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005100 status = mbedtls_to_psa_error(
5101 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5102 ciphertext_length - operation.tag_length,
5103 nonce, nonce_length,
5104 additional_data,
5105 additional_data_length,
5106 tag, operation.tag_length,
5107 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005108 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005109 else
5110#endif /* MBEDTLS_GCM_C */
5111#if defined(MBEDTLS_CCM_C)
5112 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005113 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005114 status = mbedtls_to_psa_error(
5115 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5116 ciphertext_length - operation.tag_length,
5117 nonce, nonce_length,
5118 additional_data,
5119 additional_data_length,
5120 ciphertext, plaintext,
5121 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005122 }
mohammad160339574652018-06-01 04:39:53 -07005123 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005124#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005125#if defined(MBEDTLS_CHACHAPOLY_C)
5126 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5127 {
5128 if( nonce_length != 12 || operation.tag_length != 16 )
5129 {
5130 status = PSA_ERROR_NOT_SUPPORTED;
5131 goto exit;
5132 }
5133 status = mbedtls_to_psa_error(
5134 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5135 ciphertext_length - operation.tag_length,
5136 nonce,
5137 additional_data,
5138 additional_data_length,
5139 tag,
5140 ciphertext,
5141 plaintext ) );
5142 }
5143 else
5144#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005145 {
mohammad1603554faad2018-06-03 15:07:38 +03005146 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005147 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005148
Gilles Peskineedf9a652018-08-17 18:11:56 +02005149 if( status != PSA_SUCCESS && plaintext_size != 0 )
5150 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005151
Gilles Peskineedf9a652018-08-17 18:11:56 +02005152exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005153 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005154 if( status == PSA_SUCCESS )
5155 *plaintext_length = ciphertext_length - operation.tag_length;
5156 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005157}
5158
Gilles Peskinea0655c32018-04-30 17:06:50 +02005159
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005160
Gilles Peskine20035e32018-02-03 22:44:14 +01005161/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005162/* Generators */
5163/****************************************************************/
5164
John Durkop07cc04a2020-11-16 22:08:34 -08005165#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5166 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5167 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5168#define AT_LEAST_ONE_BUILTIN_KDF
5169#endif
5170
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005171#define HKDF_STATE_INIT 0 /* no input yet */
5172#define HKDF_STATE_STARTED 1 /* got salt */
5173#define HKDF_STATE_KEYED 2 /* got key */
5174#define HKDF_STATE_OUTPUT 3 /* output started */
5175
Gilles Peskinecbe66502019-05-16 16:59:18 +02005176static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005178{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5180 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005181 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005183}
5184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005186{
5187 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005190 {
5191 /* The object has (apparently) been initialized but it is not
5192 * in use. It's ok to call abort on such an object, and there's
5193 * nothing to do. */
5194 }
5195 else
John Durkopd0321952020-10-29 21:37:36 -07005196#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005197 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005198 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 mbedtls_free( operation->ctx.hkdf.info );
5200 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005201 }
John Durkop07cc04a2020-11-16 22:08:34 -08005202 else
5203#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5204#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5205 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5206 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005209 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005210 if( operation->ctx.tls12_prf.seed != NULL )
5211 {
5212 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5213 operation->ctx.tls12_prf.seed_length );
5214 mbedtls_free( operation->ctx.tls12_prf.seed );
5215 }
5216
5217 if( operation->ctx.tls12_prf.label != NULL )
5218 {
5219 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5220 operation->ctx.tls12_prf.label_length );
5221 mbedtls_free( operation->ctx.tls12_prf.label );
5222 }
5223
5224 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5225
5226 /* We leave the fields Ai and output_block to be erased safely by the
5227 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005228 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005229 else
John Durkop07cc04a2020-11-16 22:08:34 -08005230#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5231 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 {
5233 status = PSA_ERROR_BAD_STATE;
5234 }
Janos Follath083036a2019-06-11 10:22:26 +01005235 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005236 return( status );
5237}
5238
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005240 size_t *capacity)
5241{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005243 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005245 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005246 }
5247
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005249 return( PSA_SUCCESS );
5250}
5251
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253 size_t capacity )
5254{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005258 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005260 return( PSA_SUCCESS );
5261}
5262
John Durkopd0321952020-10-29 21:37:36 -07005263#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005265 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005266static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005267 psa_algorithm_t hash_alg,
5268 uint8_t *output,
5269 size_t output_length )
5270{
5271 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5272 psa_status_t status;
5273
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005274 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5275 return( PSA_ERROR_BAD_STATE );
5276 hkdf->state = HKDF_STATE_OUTPUT;
5277
Gilles Peskinebef7f142018-07-12 17:22:21 +02005278 while( output_length != 0 )
5279 {
5280 /* Copy what remains of the current block */
5281 uint8_t n = hash_length - hkdf->offset_in_block;
5282 if( n > output_length )
5283 n = (uint8_t) output_length;
5284 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5285 output += n;
5286 output_length -= n;
5287 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005288 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005289 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005290 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005291 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005293 * object was corrupted or if this function is called directly
5294 * inside the library. */
5295 if( hkdf->block_number == 0xff )
5296 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297
5298 /* We need a new block */
5299 ++hkdf->block_number;
5300 hkdf->offset_in_block = 0;
5301 status = psa_hmac_setup_internal( &hkdf->hmac,
5302 hkdf->prk, hash_length,
5303 hash_alg );
5304 if( status != PSA_SUCCESS )
5305 return( status );
5306 if( hkdf->block_number != 1 )
5307 {
5308 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5309 hkdf->output_block,
5310 hash_length );
5311 if( status != PSA_SUCCESS )
5312 return( status );
5313 }
5314 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5315 hkdf->info,
5316 hkdf->info_length );
5317 if( status != PSA_SUCCESS )
5318 return( status );
5319 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5320 &hkdf->block_number, 1 );
5321 if( status != PSA_SUCCESS )
5322 return( status );
5323 status = psa_hmac_finish_internal( &hkdf->hmac,
5324 hkdf->output_block,
5325 sizeof( hkdf->output_block ) );
5326 if( status != PSA_SUCCESS )
5327 return( status );
5328 }
5329
5330 return( PSA_SUCCESS );
5331}
John Durkop07cc04a2020-11-16 22:08:34 -08005332#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005333
John Durkop07cc04a2020-11-16 22:08:34 -08005334#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5335 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005336static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5337 psa_tls12_prf_key_derivation_t *tls12_prf,
5338 psa_algorithm_t alg )
5339{
5340 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5341 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005342 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5343 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005344
Janos Follath7742fee2019-06-17 12:58:10 +01005345 /* We can't be wanting more output after block 0xff, otherwise
5346 * the capacity check in psa_key_derivation_output_bytes() would have
5347 * prevented this call. It could happen only if the operation
5348 * object was corrupted or if this function is called directly
5349 * inside the library. */
5350 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005351 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005352
5353 /* We need a new block */
5354 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005355 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005356
5357 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5358 *
5359 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5360 *
5361 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5362 * HMAC_hash(secret, A(2) + seed) +
5363 * HMAC_hash(secret, A(3) + seed) + ...
5364 *
5365 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005366 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005367 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005368 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005369 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005370 * is currently extracted as `output_block` and where i is
5371 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005372 */
5373
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 /* Save the hash context before using it, to preserve the hash state with
5375 * only the inner padding in it. We need this, because inner padding depends
5376 * on the key (secret in the RFC's terminology). */
5377 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5378 if( status != PSA_SUCCESS )
5379 goto cleanup;
5380
5381 /* Calculate A(i) where i = tls12_prf->block_number. */
5382 if( tls12_prf->block_number == 1 )
5383 {
5384 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5385 * the variable seed and in this instance means it in the context of the
5386 * P_hash function, where seed = label + seed.) */
5387 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5388 tls12_prf->label, tls12_prf->label_length );
5389 if( status != PSA_SUCCESS )
5390 goto cleanup;
5391 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5392 tls12_prf->seed, tls12_prf->seed_length );
5393 if( status != PSA_SUCCESS )
5394 goto cleanup;
5395 }
5396 else
5397 {
5398 /* A(i) = HMAC_hash(secret, A(i-1)) */
5399 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5400 tls12_prf->Ai, hash_length );
5401 if( status != PSA_SUCCESS )
5402 goto cleanup;
5403 }
5404
5405 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5406 tls12_prf->Ai, hash_length );
5407 if( status != PSA_SUCCESS )
5408 goto cleanup;
5409 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5410 if( status != PSA_SUCCESS )
5411 goto cleanup;
5412
5413 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5414 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5415 tls12_prf->Ai, hash_length );
5416 if( status != PSA_SUCCESS )
5417 goto cleanup;
5418 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5419 tls12_prf->label, tls12_prf->label_length );
5420 if( status != PSA_SUCCESS )
5421 goto cleanup;
5422 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5423 tls12_prf->seed, tls12_prf->seed_length );
5424 if( status != PSA_SUCCESS )
5425 goto cleanup;
5426 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5427 tls12_prf->output_block, hash_length );
5428 if( status != PSA_SUCCESS )
5429 goto cleanup;
5430 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5431 if( status != PSA_SUCCESS )
5432 goto cleanup;
5433
Janos Follath7742fee2019-06-17 12:58:10 +01005434
5435cleanup:
5436
Janos Follathea29bfb2019-06-19 12:21:20 +01005437 cleanup_status = psa_hash_abort( &backup );
5438 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5439 status = cleanup_status;
5440
5441 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005442}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005443
Janos Follath844eb0e2019-06-19 12:10:49 +01005444static psa_status_t psa_key_derivation_tls12_prf_read(
5445 psa_tls12_prf_key_derivation_t *tls12_prf,
5446 psa_algorithm_t alg,
5447 uint8_t *output,
5448 size_t output_length )
5449{
5450 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5451 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5452 psa_status_t status;
5453 uint8_t offset, length;
5454
5455 while( output_length != 0 )
5456 {
5457 /* Check if we have fully processed the current block. */
5458 if( tls12_prf->left_in_block == 0 )
5459 {
5460 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5461 alg );
5462 if( status != PSA_SUCCESS )
5463 return( status );
5464
5465 continue;
5466 }
5467
5468 if( tls12_prf->left_in_block > output_length )
5469 length = (uint8_t) output_length;
5470 else
5471 length = tls12_prf->left_in_block;
5472
5473 offset = hash_length - tls12_prf->left_in_block;
5474 memcpy( output, tls12_prf->output_block + offset, length );
5475 output += length;
5476 output_length -= length;
5477 tls12_prf->left_in_block -= length;
5478 }
5479
5480 return( PSA_SUCCESS );
5481}
John Durkop07cc04a2020-11-16 22:08:34 -08005482#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5483 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005484
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005485psa_status_t psa_key_derivation_output_bytes(
5486 psa_key_derivation_operation_t *operation,
5487 uint8_t *output,
5488 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489{
5490 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005494 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005496 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005497 }
5498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005499 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005500 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005502 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005504 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005505 goto exit;
5506 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005508 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005510 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005511 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5512 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005513 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005514 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005515 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005518
John Durkopd0321952020-10-29 21:37:36 -07005519#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005520 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005521 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005522 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005524 output, output_length );
5525 }
Janos Follathadbec812019-06-14 11:05:39 +01005526 else
John Durkop07cc04a2020-11-16 22:08:34 -08005527#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5528#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5529 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005530 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005531 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005532 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005534 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005535 output_length );
5536 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005537 else
John Durkop07cc04a2020-11-16 22:08:34 -08005538#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5539 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005540 {
5541 return( PSA_ERROR_BAD_STATE );
5542 }
5543
5544exit:
5545 if( status != PSA_SUCCESS )
5546 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005547 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 * This allows us to differentiate between exhausted operations and
5549 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5550 * operations. */
5551 psa_algorithm_t alg = operation->alg;
5552 psa_key_derivation_abort( operation );
5553 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005554 memset( output, '!', output_length );
5555 }
5556 return( status );
5557}
5558
Gilles Peskine08542d82018-07-19 17:05:42 +02005559#if defined(MBEDTLS_DES_C)
5560static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5561{
5562 if( data_size >= 8 )
5563 mbedtls_des_key_set_parity( data );
5564 if( data_size >= 16 )
5565 mbedtls_des_key_set_parity( data + 8 );
5566 if( data_size >= 24 )
5567 mbedtls_des_key_set_parity( data + 16 );
5568}
5569#endif /* MBEDTLS_DES_C */
5570
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005571static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005572 psa_key_slot_t *slot,
5573 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005574 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005575{
5576 uint8_t *data = NULL;
5577 size_t bytes = PSA_BITS_TO_BYTES( bits );
5578 psa_status_t status;
5579
Gilles Peskine8e338702019-07-30 20:06:31 +02005580 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581 return( PSA_ERROR_INVALID_ARGUMENT );
5582 if( bits % 8 != 0 )
5583 return( PSA_ERROR_INVALID_ARGUMENT );
5584 data = mbedtls_calloc( 1, bytes );
5585 if( data == NULL )
5586 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5587
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005588 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005589 if( status != PSA_SUCCESS )
5590 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005591#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005592 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005593 psa_des_set_key_parity( data, bytes );
5594#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005595 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005596
5597exit:
5598 mbedtls_free( data );
5599 return( status );
5600}
5601
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005602psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005603 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005604 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005605{
5606 psa_status_t status;
5607 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005608 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005609
Ronald Cron81709fc2020-11-14 12:10:32 +01005610 *key = MBEDTLS_SVC_KEY_ID_INIT;
5611
Gilles Peskine0f84d622019-09-12 19:03:13 +02005612 /* Reject any attempt to create a zero-length key so that we don't
5613 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5614 if( psa_get_key_bits( attributes ) == 0 )
5615 return( PSA_ERROR_INVALID_ARGUMENT );
5616
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005617 if( ! operation->can_output_key )
5618 return( PSA_ERROR_NOT_PERMITTED );
5619
Ronald Cron81709fc2020-11-14 12:10:32 +01005620 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5621 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5623 if( driver != NULL )
5624 {
5625 /* Deriving a key in a secure element is not implemented yet. */
5626 status = PSA_ERROR_NOT_SUPPORTED;
5627 }
5628#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005629 if( status == PSA_SUCCESS )
5630 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005631 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005632 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005634 }
5635 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005636 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005638 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005639
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005640 return( status );
5641}
5642
Gilles Peskineeab56e42018-07-12 17:12:33 +02005643
5644
5645/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005646/* Key derivation */
5647/****************************************************************/
5648
John Durkop07cc04a2020-11-16 22:08:34 -08005649#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005650static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005651 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005652 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005653{
John Durkop07cc04a2020-11-16 22:08:34 -08005654 int is_kdf_alg_supported;
5655
Janos Follath5fe19732019-06-20 15:09:30 +01005656 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5657 * initialisers for this union leave some bytes unspecified.) */
5658 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5659
Gilles Peskine969c5d62019-01-16 15:53:06 +01005660 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005661#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005662 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5663 is_kdf_alg_supported = 1;
5664 else
5665#endif
5666#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5667 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5668 is_kdf_alg_supported = 1;
5669 else
5670#endif
5671#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5672 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5673 is_kdf_alg_supported = 1;
5674 else
5675#endif
5676 is_kdf_alg_supported = 0;
5677
5678 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005679 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005680 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5682 if( hash_size == 0 )
5683 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005684 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5685 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005686 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005687 {
5688 return( PSA_ERROR_NOT_SUPPORTED );
5689 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005691 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005692 }
John Durkop07cc04a2020-11-16 22:08:34 -08005693
5694 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005695}
John Durkop07cc04a2020-11-16 22:08:34 -08005696#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005697
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005699 psa_algorithm_t alg )
5700{
5701 psa_status_t status;
5702
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005703 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005704 return( PSA_ERROR_BAD_STATE );
5705
Gilles Peskine6843c292019-01-18 16:44:49 +01005706 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5707 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005708#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005709 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005710 {
5711 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005712 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005713 }
5714 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5715 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005716 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005717 }
John Durkop07cc04a2020-11-16 22:08:34 -08005718#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719 else
5720 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721
5722 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005724 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005725}
5726
John Durkopd0321952020-10-29 21:37:36 -07005727#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005728static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729 psa_algorithm_t hash_alg,
5730 psa_key_derivation_step_t step,
5731 const uint8_t *data,
5732 size_t data_length )
5733{
5734 psa_status_t status;
5735 switch( step )
5736 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005737 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005738 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005739 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005740 status = psa_hmac_setup_internal( &hkdf->hmac,
5741 data, data_length,
5742 hash_alg );
5743 if( status != PSA_SUCCESS )
5744 return( status );
5745 hkdf->state = HKDF_STATE_STARTED;
5746 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005747 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005748 /* If no salt was provided, use an empty salt. */
5749 if( hkdf->state == HKDF_STATE_INIT )
5750 {
5751 status = psa_hmac_setup_internal( &hkdf->hmac,
5752 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005753 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005754 if( status != PSA_SUCCESS )
5755 return( status );
5756 hkdf->state = HKDF_STATE_STARTED;
5757 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005758 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005759 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005760 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5761 data, data_length );
5762 if( status != PSA_SUCCESS )
5763 return( status );
5764 status = psa_hmac_finish_internal( &hkdf->hmac,
5765 hkdf->prk,
5766 sizeof( hkdf->prk ) );
5767 if( status != PSA_SUCCESS )
5768 return( status );
5769 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5770 hkdf->block_number = 0;
5771 hkdf->state = HKDF_STATE_KEYED;
5772 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005773 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005774 if( hkdf->state == HKDF_STATE_OUTPUT )
5775 return( PSA_ERROR_BAD_STATE );
5776 if( hkdf->info_set )
5777 return( PSA_ERROR_BAD_STATE );
5778 hkdf->info_length = data_length;
5779 if( data_length != 0 )
5780 {
5781 hkdf->info = mbedtls_calloc( 1, data_length );
5782 if( hkdf->info == NULL )
5783 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5784 memcpy( hkdf->info, data, data_length );
5785 }
5786 hkdf->info_set = 1;
5787 return( PSA_SUCCESS );
5788 default:
5789 return( PSA_ERROR_INVALID_ARGUMENT );
5790 }
5791}
John Durkop07cc04a2020-11-16 22:08:34 -08005792#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005793
John Durkop07cc04a2020-11-16 22:08:34 -08005794#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5795 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005796static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5797 const uint8_t *data,
5798 size_t data_length )
5799{
5800 if( prf->state != TLS12_PRF_STATE_INIT )
5801 return( PSA_ERROR_BAD_STATE );
5802
Janos Follathd6dce9f2019-07-04 09:11:38 +01005803 if( data_length != 0 )
5804 {
5805 prf->seed = mbedtls_calloc( 1, data_length );
5806 if( prf->seed == NULL )
5807 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005808
Janos Follathd6dce9f2019-07-04 09:11:38 +01005809 memcpy( prf->seed, data, data_length );
5810 prf->seed_length = data_length;
5811 }
Janos Follathf08e2652019-06-13 09:05:41 +01005812
5813 prf->state = TLS12_PRF_STATE_SEED_SET;
5814
5815 return( PSA_SUCCESS );
5816}
5817
Janos Follath81550542019-06-13 14:26:34 +01005818static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5819 psa_algorithm_t hash_alg,
5820 const uint8_t *data,
5821 size_t data_length )
5822{
5823 psa_status_t status;
5824 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5825 return( PSA_ERROR_BAD_STATE );
5826
5827 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5828 if( status != PSA_SUCCESS )
5829 return( status );
5830
5831 prf->state = TLS12_PRF_STATE_KEY_SET;
5832
5833 return( PSA_SUCCESS );
5834}
5835
Janos Follath63028dd2019-06-13 09:15:47 +01005836static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5837 const uint8_t *data,
5838 size_t data_length )
5839{
5840 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5841 return( PSA_ERROR_BAD_STATE );
5842
Janos Follathd6dce9f2019-07-04 09:11:38 +01005843 if( data_length != 0 )
5844 {
5845 prf->label = mbedtls_calloc( 1, data_length );
5846 if( prf->label == NULL )
5847 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005848
Janos Follathd6dce9f2019-07-04 09:11:38 +01005849 memcpy( prf->label, data, data_length );
5850 prf->label_length = data_length;
5851 }
Janos Follath63028dd2019-06-13 09:15:47 +01005852
5853 prf->state = TLS12_PRF_STATE_LABEL_SET;
5854
5855 return( PSA_SUCCESS );
5856}
5857
Janos Follathb03233e2019-06-11 15:30:30 +01005858static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5859 psa_algorithm_t hash_alg,
5860 psa_key_derivation_step_t step,
5861 const uint8_t *data,
5862 size_t data_length )
5863{
Janos Follathb03233e2019-06-11 15:30:30 +01005864 switch( step )
5865 {
Janos Follathf08e2652019-06-13 09:05:41 +01005866 case PSA_KEY_DERIVATION_INPUT_SEED:
5867 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005868 case PSA_KEY_DERIVATION_INPUT_SECRET:
5869 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005870 case PSA_KEY_DERIVATION_INPUT_LABEL:
5871 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005872 default:
5873 return( PSA_ERROR_INVALID_ARGUMENT );
5874 }
5875}
John Durkop07cc04a2020-11-16 22:08:34 -08005876#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5877 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5878
5879#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5880static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5881 psa_tls12_prf_key_derivation_t *prf,
5882 psa_algorithm_t hash_alg,
5883 const uint8_t *data,
5884 size_t data_length )
5885{
5886 psa_status_t status;
5887 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5888 uint8_t *cur = pms;
5889
5890 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5891 return( PSA_ERROR_INVALID_ARGUMENT );
5892
5893 /* Quoting RFC 4279, Section 2:
5894 *
5895 * The premaster secret is formed as follows: if the PSK is N octets
5896 * long, concatenate a uint16 with the value N, N zero octets, a second
5897 * uint16 with the value N, and the PSK itself.
5898 */
5899
5900 *cur++ = ( data_length >> 8 ) & 0xff;
5901 *cur++ = ( data_length >> 0 ) & 0xff;
5902 memset( cur, 0, data_length );
5903 cur += data_length;
5904 *cur++ = pms[0];
5905 *cur++ = pms[1];
5906 memcpy( cur, data, data_length );
5907 cur += data_length;
5908
5909 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5910
5911 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5912 return( status );
5913}
Janos Follath6660f0e2019-06-17 08:44:03 +01005914
5915static psa_status_t psa_tls12_prf_psk_to_ms_input(
5916 psa_tls12_prf_key_derivation_t *prf,
5917 psa_algorithm_t hash_alg,
5918 psa_key_derivation_step_t step,
5919 const uint8_t *data,
5920 size_t data_length )
5921{
5922 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005923 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005924 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5925 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005926 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005927
5928 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5929}
John Durkop07cc04a2020-11-16 22:08:34 -08005930#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005931
Gilles Peskineb8965192019-09-24 16:21:10 +02005932/** Check whether the given key type is acceptable for the given
5933 * input step of a key derivation.
5934 *
5935 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5936 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5937 * Both secret and non-secret inputs can alternatively have the type
5938 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5939 * that the input was passed as a buffer rather than via a key object.
5940 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005941static int psa_key_derivation_check_input_type(
5942 psa_key_derivation_step_t step,
5943 psa_key_type_t key_type )
5944{
5945 switch( step )
5946 {
5947 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005948 if( key_type == PSA_KEY_TYPE_DERIVE )
5949 return( PSA_SUCCESS );
5950 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005951 return( PSA_SUCCESS );
5952 break;
5953 case PSA_KEY_DERIVATION_INPUT_LABEL:
5954 case PSA_KEY_DERIVATION_INPUT_SALT:
5955 case PSA_KEY_DERIVATION_INPUT_INFO:
5956 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005957 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5958 return( PSA_SUCCESS );
5959 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005960 return( PSA_SUCCESS );
5961 break;
5962 }
5963 return( PSA_ERROR_INVALID_ARGUMENT );
5964}
5965
Janos Follathb80a94e2019-06-12 15:54:46 +01005966static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005967 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005968 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005969 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005970 const uint8_t *data,
5971 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005972{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005973 psa_status_t status;
5974 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5975
5976 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005977 if( status != PSA_SUCCESS )
5978 goto exit;
5979
John Durkopd0321952020-10-29 21:37:36 -07005980#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005981 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005982 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005983 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005984 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005985 step, data, data_length );
5986 }
John Durkop07cc04a2020-11-16 22:08:34 -08005987 else
5988#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5989#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5990 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005991 {
Janos Follathb03233e2019-06-11 15:30:30 +01005992 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5993 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5994 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005995 }
John Durkop07cc04a2020-11-16 22:08:34 -08005996 else
5997#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5998#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5999 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006000 {
6001 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6002 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6003 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006004 }
6005 else
John Durkop07cc04a2020-11-16 22:08:34 -08006006#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006007 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006008 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006009 return( PSA_ERROR_BAD_STATE );
6010 }
6011
Gilles Peskine224b0d62019-09-23 18:13:17 +02006012exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006013 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006014 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006015 return( status );
6016}
6017
Janos Follath51f4a0f2019-06-14 11:35:55 +01006018psa_status_t psa_key_derivation_input_bytes(
6019 psa_key_derivation_operation_t *operation,
6020 psa_key_derivation_step_t step,
6021 const uint8_t *data,
6022 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006023{
Gilles Peskineb8965192019-09-24 16:21:10 +02006024 return( psa_key_derivation_input_internal( operation, step,
6025 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006026 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006027}
6028
Janos Follath51f4a0f2019-06-14 11:35:55 +01006029psa_status_t psa_key_derivation_input_key(
6030 psa_key_derivation_operation_t *operation,
6031 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006032 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006033{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006034 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006035 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006036 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006037
Ronald Cron5c522922020-11-14 16:35:34 +01006038 status = psa_get_and_lock_transparent_key_slot_with_policy(
6039 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006040 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006041 {
6042 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006043 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006044 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006045
6046 /* Passing a key object as a SECRET input unlocks the permission
6047 * to output to a key object. */
6048 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6049 operation->can_output_key = 1;
6050
Ronald Cronf95a2b12020-10-22 15:24:49 +02006051 status = psa_key_derivation_input_internal( operation,
6052 step, slot->attr.type,
6053 slot->data.key.data,
6054 slot->data.key.bytes );
6055
Ronald Cron5c522922020-11-14 16:35:34 +01006056 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006057
Ronald Cron5c522922020-11-14 16:35:34 +01006058 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006059}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006060
6061
6062
6063/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006064/* Key agreement */
6065/****************************************************************/
6066
John Durkop6ba40d12020-11-10 08:50:04 -08006067#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006068static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6069 size_t peer_key_length,
6070 const mbedtls_ecp_keypair *our_key,
6071 uint8_t *shared_secret,
6072 size_t shared_secret_size,
6073 size_t *shared_secret_length )
6074{
Steven Cooremand4867872020-08-05 16:31:39 +02006075 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006076 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006077 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006078 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006079 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006080 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006081
Steven Cooreman4fed4552020-08-03 14:46:03 +02006082 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6083 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006084 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006085 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006086 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006087 goto exit;
6088
Jaeden Amero97271b32019-01-10 19:38:51 +00006089 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006090 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006091 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006092 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006093 status = mbedtls_to_psa_error(
6094 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6095 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006096 goto exit;
6097
Jaeden Amero97271b32019-01-10 19:38:51 +00006098 status = mbedtls_to_psa_error(
6099 mbedtls_ecdh_calc_secret( &ecdh,
6100 shared_secret_length,
6101 shared_secret, shared_secret_size,
6102 mbedtls_ctr_drbg_random,
6103 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006104 if( status != PSA_SUCCESS )
6105 goto exit;
6106 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6107 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006108
6109exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006110 if( status != PSA_SUCCESS )
6111 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006112 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006113 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006114 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006115
Jaeden Amero97271b32019-01-10 19:38:51 +00006116 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006117}
John Durkop6ba40d12020-11-10 08:50:04 -08006118#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006119
Gilles Peskine01d718c2018-09-18 12:01:02 +02006120#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6121
Gilles Peskine0216fe12019-04-11 21:23:21 +02006122static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6123 psa_key_slot_t *private_key,
6124 const uint8_t *peer_key,
6125 size_t peer_key_length,
6126 uint8_t *shared_secret,
6127 size_t shared_secret_size,
6128 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006129{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006130 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006131 {
John Durkop6ba40d12020-11-10 08:50:04 -08006132#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006133 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006134 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006135 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006136 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006137 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006138 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006139 private_key->data.key.data,
6140 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006141 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006142 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006143 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006144 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006145 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006146 shared_secret, shared_secret_size,
6147 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006148 mbedtls_ecp_keypair_free( ecp );
6149 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006150 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006151#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006152 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006153 (void) private_key;
6154 (void) peer_key;
6155 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006156 (void) shared_secret;
6157 (void) shared_secret_size;
6158 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006159 return( PSA_ERROR_NOT_SUPPORTED );
6160 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006161}
6162
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006163/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006164 * to potentially free embedded data structures and wipe confidential data.
6165 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006166static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006167 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006168 psa_key_slot_t *private_key,
6169 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006170 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006171{
6172 psa_status_t status;
6173 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6174 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006175 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006176
6177 /* Step 1: run the secret agreement algorithm to generate the shared
6178 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006179 status = psa_key_agreement_raw_internal( ka_alg,
6180 private_key,
6181 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006182 shared_secret,
6183 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006184 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006185 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006186 goto exit;
6187
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006188 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006189 * the shared secret. A shared secret is permitted wherever a key
6190 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006191 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006192 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006193 shared_secret,
6194 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006195exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006196 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006197 return( status );
6198}
6199
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006200psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006201 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006202 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006203 const uint8_t *peer_key,
6204 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006205{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006207 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006208 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006209
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006210 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006211 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006212 status = psa_get_and_lock_transparent_key_slot_with_policy(
6213 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006214 if( status != PSA_SUCCESS )
6215 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006216 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006217 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006218 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006219 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006220 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006221 else
6222 {
6223 /* If a private key has been added as SECRET, we allow the derived
6224 * key material to be used as a key in PSA Crypto. */
6225 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6226 operation->can_output_key = 1;
6227 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006228
Ronald Cron5c522922020-11-14 16:35:34 +01006229 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006230
Ronald Cron5c522922020-11-14 16:35:34 +01006231 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006232}
6233
Gilles Peskinebe697d82019-05-16 18:00:41 +02006234psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006235 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006236 const uint8_t *peer_key,
6237 size_t peer_key_length,
6238 uint8_t *output,
6239 size_t output_size,
6240 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006241{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006243 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006244 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006245
6246 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6247 {
6248 status = PSA_ERROR_INVALID_ARGUMENT;
6249 goto exit;
6250 }
Ronald Cron5c522922020-11-14 16:35:34 +01006251 status = psa_get_and_lock_transparent_key_slot_with_policy(
6252 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006253 if( status != PSA_SUCCESS )
6254 goto exit;
6255
6256 status = psa_key_agreement_raw_internal( alg, slot,
6257 peer_key, peer_key_length,
6258 output, output_size,
6259 output_length );
6260
6261exit:
6262 if( status != PSA_SUCCESS )
6263 {
6264 /* If an error happens and is not handled properly, the output
6265 * may be used as a key to protect sensitive data. Arrange for such
6266 * a key to be random, which is likely to result in decryption or
6267 * verification errors. This is better than filling the buffer with
6268 * some constant data such as zeros, which would result in the data
6269 * being protected with a reproducible, easily knowable key.
6270 */
6271 psa_generate_random( output, output_size );
6272 *output_length = output_size;
6273 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006274
Ronald Cron5c522922020-11-14 16:35:34 +01006275 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006276
Ronald Cron5c522922020-11-14 16:35:34 +01006277 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006278}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006279
6280
6281/****************************************************************/
6282/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006283/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006284
Gilles Peskine4c317f42018-07-12 01:24:09 +02006285psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006286 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006287{
Janos Follath24eed8d2019-11-22 13:21:35 +00006288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289 GUARD_MODULE_INITIALIZED;
6290
Gilles Peskinef181eca2019-08-07 13:49:00 +02006291 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6292 {
6293 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6294 output,
6295 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6296 if( ret != 0 )
6297 return( mbedtls_to_psa_error( ret ) );
6298 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6299 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6300 }
6301
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6303 return( mbedtls_to_psa_error( ret ) );
6304}
6305
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006306#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6307#include "mbedtls/entropy_poll.h"
6308
Gilles Peskine7228da22019-07-15 11:06:15 +02006309psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006310 size_t seed_size )
6311{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006312 if( global_data.initialized )
6313 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006314
6315 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6316 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6317 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6318 return( PSA_ERROR_INVALID_ARGUMENT );
6319
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006320 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006321}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006322#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006323
John Durkop07cc04a2020-11-16 22:08:34 -08006324#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006325static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6326 size_t domain_parameters_size,
6327 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006328{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006329 size_t i;
6330 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006331
Gilles Peskinee56e8782019-04-26 17:34:02 +02006332 if( domain_parameters_size == 0 )
6333 {
6334 *exponent = 65537;
6335 return( PSA_SUCCESS );
6336 }
6337
6338 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6339 * support values that fit in a 32-bit integer, which is larger than
6340 * int on just about every platform anyway. */
6341 if( domain_parameters_size > sizeof( acc ) )
6342 return( PSA_ERROR_NOT_SUPPORTED );
6343 for( i = 0; i < domain_parameters_size; i++ )
6344 acc = ( acc << 8 ) | domain_parameters[i];
6345 if( acc > INT_MAX )
6346 return( PSA_ERROR_NOT_SUPPORTED );
6347 *exponent = acc;
6348 return( PSA_SUCCESS );
6349}
John Durkop07cc04a2020-11-16 22:08:34 -08006350#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006351
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006352static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006353 psa_key_slot_t *slot, size_t bits,
6354 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006355{
Gilles Peskine8e338702019-07-30 20:06:31 +02006356 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006357
Gilles Peskinee56e8782019-04-26 17:34:02 +02006358 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006359 return( PSA_ERROR_INVALID_ARGUMENT );
6360
Gilles Peskinee59236f2018-01-27 23:32:46 +01006361 if( key_type_is_raw_bytes( type ) )
6362 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006363 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006364
6365 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366 if( status != PSA_SUCCESS )
6367 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006368
6369 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006370 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6371 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006372 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006373
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006374 status = psa_generate_random( slot->data.key.data,
6375 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006377 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006378
6379 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006380#if defined(MBEDTLS_DES_C)
6381 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006382 psa_des_set_key_parity( slot->data.key.data,
6383 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006384#endif /* MBEDTLS_DES_C */
6385 }
6386 else
6387
John Durkop07cc04a2020-11-16 22:08:34 -08006388#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006389 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006390 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006391 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006393 int exponent;
6394 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006395 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6396 return( PSA_ERROR_NOT_SUPPORTED );
6397 /* Accept only byte-aligned keys, for the same reasons as
6398 * in psa_import_rsa_key(). */
6399 if( bits % 8 != 0 )
6400 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006401 status = psa_read_rsa_exponent( domain_parameters,
6402 domain_parameters_size,
6403 &exponent );
6404 if( status != PSA_SUCCESS )
6405 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006406 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6407 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006408 mbedtls_ctr_drbg_random,
6409 &global_data.ctr_drbg,
6410 (unsigned int) bits,
6411 exponent );
6412 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006414
6415 /* Make sure to always have an export representation available */
6416 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6417
Steven Cooreman75b74362020-07-28 14:30:13 +02006418 status = psa_allocate_buffer_to_slot( slot, bytes );
6419 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006420 {
6421 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006422 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006424
6425 status = psa_export_rsa_key( type,
6426 &rsa,
6427 slot->data.key.data,
6428 bytes,
6429 &slot->data.key.bytes );
6430 mbedtls_rsa_free( &rsa );
6431 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006432 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006433 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006434 }
6435 else
John Durkop07cc04a2020-11-16 22:08:34 -08006436#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006437
John Durkop9814fa22020-11-04 12:28:15 -08006438#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006439 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006441 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006442 mbedtls_ecp_group_id grp_id =
6443 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006444 const mbedtls_ecp_curve_info *curve_info =
6445 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006446 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006448 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006449 return( PSA_ERROR_NOT_SUPPORTED );
6450 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6451 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006452 mbedtls_ecp_keypair_init( &ecp );
6453 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006454 mbedtls_ctr_drbg_random,
6455 &global_data.ctr_drbg );
6456 if( ret != 0 )
6457 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006458 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459 return( mbedtls_to_psa_error( ret ) );
6460 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006461
6462
6463 /* Make sure to always have an export representation available */
6464 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006465 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6466 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006467 {
6468 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006469 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006470 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006471
6472 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006473 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6474
6475 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006476 if( status != PSA_SUCCESS ) {
6477 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006478 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006479 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006480 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006481 }
6482 else
John Durkop9814fa22020-11-04 12:28:15 -08006483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006484 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006485 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006486 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006487
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006488 return( PSA_SUCCESS );
6489}
Darryl Green0c6575a2018-11-07 16:05:30 +00006490
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006491psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006492 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006493{
6494 psa_status_t status;
6495 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006496 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006497
Ronald Cron81709fc2020-11-14 12:10:32 +01006498 *key = MBEDTLS_SVC_KEY_ID_INIT;
6499
Gilles Peskine0f84d622019-09-12 19:03:13 +02006500 /* Reject any attempt to create a zero-length key so that we don't
6501 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6502 if( psa_get_key_bits( attributes ) == 0 )
6503 return( PSA_ERROR_INVALID_ARGUMENT );
6504
Ronald Cron81709fc2020-11-14 12:10:32 +01006505 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6506 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006507 if( status != PSA_SUCCESS )
6508 goto exit;
6509
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006510 status = psa_driver_wrapper_generate_key( attributes,
6511 slot );
6512 if( status != PSA_ERROR_NOT_SUPPORTED ||
6513 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6514 goto exit;
6515
6516 status = psa_generate_key_internal(
6517 slot, attributes->core.bits,
6518 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006519
6520exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006521 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006522 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006523 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006524 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006525
Darryl Green0c6575a2018-11-07 16:05:30 +00006526 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527}
6528
6529
Gilles Peskinee59236f2018-01-27 23:32:46 +01006530
6531/****************************************************************/
6532/* Module setup */
6533/****************************************************************/
6534
Gilles Peskine5e769522018-11-20 21:59:56 +01006535psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6536 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6537 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6538{
6539 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6540 return( PSA_ERROR_BAD_STATE );
6541 global_data.entropy_init = entropy_init;
6542 global_data.entropy_free = entropy_free;
6543 return( PSA_SUCCESS );
6544}
6545
Gilles Peskinee59236f2018-01-27 23:32:46 +01006546void mbedtls_psa_crypto_free( void )
6547{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006548 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006549 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6550 {
6551 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006552 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006553 }
6554 /* Wipe all remaining data, including configuration.
6555 * In particular, this sets all state indicator to the value
6556 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006557 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006558#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006559 /* Unregister all secure element drivers, so that we restart from
6560 * a pristine state. */
6561 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006562#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563}
6564
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006565#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6566/** Recover a transaction that was interrupted by a power failure.
6567 *
6568 * This function is called during initialization, before psa_crypto_init()
6569 * returns. If this function returns a failure status, the initialization
6570 * fails.
6571 */
6572static psa_status_t psa_crypto_recover_transaction(
6573 const psa_crypto_transaction_t *transaction )
6574{
6575 switch( transaction->unknown.type )
6576 {
6577 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6578 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006579 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006580 * is implemented.
6581 * https://github.com/ARMmbed/mbed-crypto/issues/218
6582 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006583 default:
6584 /* We found an unsupported transaction in the storage.
6585 * We don't know what state the storage is in. Give up. */
6586 return( PSA_ERROR_STORAGE_FAILURE );
6587 }
6588}
6589#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6590
Gilles Peskinee59236f2018-01-27 23:32:46 +01006591psa_status_t psa_crypto_init( void )
6592{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006593 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006594 const unsigned char drbg_seed[] = "PSA";
6595
Gilles Peskinec6b69072018-11-20 21:42:52 +01006596 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006597 if( global_data.initialized != 0 )
6598 return( PSA_SUCCESS );
6599
Gilles Peskine5e769522018-11-20 21:59:56 +01006600 /* Set default configuration if
6601 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6602 if( global_data.entropy_init == NULL )
6603 global_data.entropy_init = mbedtls_entropy_init;
6604 if( global_data.entropy_free == NULL )
6605 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006606
Gilles Peskinec6b69072018-11-20 21:42:52 +01006607 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006608 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006609#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6610 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6611 /* The PSA entropy injection feature depends on using NV seed as an entropy
6612 * source. Add NV seed as an entropy source for PSA entropy injection. */
6613 mbedtls_entropy_add_source( &global_data.entropy,
6614 mbedtls_nv_seed_poll, NULL,
6615 MBEDTLS_ENTROPY_BLOCK_SIZE,
6616 MBEDTLS_ENTROPY_SOURCE_STRONG );
6617#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006618 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006619 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006620 status = mbedtls_to_psa_error(
6621 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6622 mbedtls_entropy_func,
6623 &global_data.entropy,
6624 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6625 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006626 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006627 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006628
Gilles Peskine66fb1262018-12-10 16:29:04 +01006629 status = psa_initialize_key_slots( );
6630 if( status != PSA_SUCCESS )
6631 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006632
Gilles Peskined9348f22019-10-01 15:22:29 +02006633#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6634 status = psa_init_all_se_drivers( );
6635 if( status != PSA_SUCCESS )
6636 goto exit;
6637#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6638
Gilles Peskine4b734222019-07-24 15:56:31 +02006639#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006640 status = psa_crypto_load_transaction( );
6641 if( status == PSA_SUCCESS )
6642 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006643 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6644 if( status != PSA_SUCCESS )
6645 goto exit;
6646 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006647 }
6648 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6649 {
6650 /* There's no transaction to complete. It's all good. */
6651 status = PSA_SUCCESS;
6652 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006653#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006654
Gilles Peskinec6b69072018-11-20 21:42:52 +01006655 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006656 global_data.initialized = 1;
6657
6658exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006659 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006660 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006661 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006662}
6663
6664#endif /* MBEDTLS_PSA_CRYPTO_C */