blob: a511c27748a4c5cbcbf3f3edb7f5db4320f649ca [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132static psa_status_t mbedtls_to_psa_error( int ret )
133{
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:
206 return( PSA_ERROR_BAD_STATE );
207 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
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
379 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200380{
381 switch( curve )
382 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 case PSA_ECC_CURVE_SECP_R1:
384 switch( byte_length )
385 {
386 case PSA_BITS_TO_BYTES( 192 ):
387 return( MBEDTLS_ECP_DP_SECP192R1 );
388 case PSA_BITS_TO_BYTES( 224 ):
389 return( MBEDTLS_ECP_DP_SECP224R1 );
390 case PSA_BITS_TO_BYTES( 256 ):
391 return( MBEDTLS_ECP_DP_SECP256R1 );
392 case PSA_BITS_TO_BYTES( 384 ):
393 return( MBEDTLS_ECP_DP_SECP384R1 );
394 case PSA_BITS_TO_BYTES( 521 ):
395 return( MBEDTLS_ECP_DP_SECP521R1 );
396 default:
397 return( MBEDTLS_ECP_DP_NONE );
398 }
399 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100400
401 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
402 switch( byte_length )
403 {
404 case PSA_BITS_TO_BYTES( 256 ):
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_BITS_TO_BYTES( 384 ):
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_BITS_TO_BYTES( 512 ):
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 default:
411 return( MBEDTLS_ECP_DP_NONE );
412 }
413 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100414
415 case PSA_ECC_CURVE_MONTGOMERY:
416 switch( byte_length )
417 {
418 case PSA_BITS_TO_BYTES( 255 ):
419 return( MBEDTLS_ECP_DP_CURVE25519 );
420 case PSA_BITS_TO_BYTES( 448 ):
421 return( MBEDTLS_ECP_DP_CURVE448 );
422 default:
423 return( MBEDTLS_ECP_DP_NONE );
424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
427 case PSA_ECC_CURVE_SECP_K1:
428 switch( byte_length )
429 {
430 case PSA_BITS_TO_BYTES( 192 ):
431 return( MBEDTLS_ECP_DP_SECP192K1 );
432 case PSA_BITS_TO_BYTES( 224 ):
433 return( MBEDTLS_ECP_DP_SECP224K1 );
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_SECP256K1 );
436 default:
437 return( MBEDTLS_ECP_DP_NONE );
438 }
439 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100440
Gilles Peskine12313cd2018-06-20 00:20:32 +0200441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
455#if defined(MBEDTLS_MD_C)
456 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200457#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200458 case PSA_KEY_TYPE_DERIVE:
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_AES_C)
461 case PSA_KEY_TYPE_AES:
462 if( bits != 128 && bits != 192 && bits != 256 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466#if defined(MBEDTLS_CAMELLIA_C)
467 case PSA_KEY_TYPE_CAMELLIA:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_DES_C)
473 case PSA_KEY_TYPE_DES:
474 if( bits != 64 && bits != 128 && bits != 192 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_ARC4_C)
479 case PSA_KEY_TYPE_ARC4:
480 if( bits < 8 || bits > 2048 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200484#if defined(MBEDTLS_CHACHA20_C)
485 case PSA_KEY_TYPE_CHACHA20:
486 if( bits != 256 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 default:
491 return( PSA_ERROR_NOT_SUPPORTED );
492 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200493 if( bits % 8 != 0 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495
496 /* Allocate memory for the key */
497 raw->bytes = PSA_BITS_TO_BYTES( bits );
498 raw->data = mbedtls_calloc( 1, raw->bytes );
499 if( raw->data == NULL )
500 {
501 raw->bytes = 0;
502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
503 }
504 return( PSA_SUCCESS );
505}
506
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100508/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
509 * that are not a multiple of 8) well. For example, there is only
510 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
511 * way to return the exact bit size of a key.
512 * To keep things simple, reject non-byte-aligned key sizes. */
513static psa_status_t psa_check_rsa_key_byte_aligned(
514 const mbedtls_rsa_context *rsa )
515{
516 mbedtls_mpi n;
517 psa_status_t status;
518 mbedtls_mpi_init( &n );
519 status = mbedtls_to_psa_error(
520 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
521 if( status == PSA_SUCCESS )
522 {
523 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
524 status = PSA_ERROR_NOT_SUPPORTED;
525 }
526 mbedtls_mpi_free( &n );
527 return( status );
528}
529
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000530static psa_status_t psa_import_rsa_key( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 mbedtls_rsa_context **p_rsa )
534{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 psa_status_t status;
536 mbedtls_pk_context pk;
537 mbedtls_rsa_context *rsa;
538 size_t bits;
539
540 mbedtls_pk_init( &pk );
541
542 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
548 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
554 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
560 rsa = mbedtls_pk_rsa( pk );
561 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
562 * supports non-byte-aligned key sizes, but not well. For example,
563 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
564 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
565 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
566 {
567 status = PSA_ERROR_NOT_SUPPORTED;
568 goto exit;
569 }
570 status = psa_check_rsa_key_byte_aligned( rsa );
571
572exit:
573 /* Free the content of the pk object only on error. */
574 if( status != PSA_SUCCESS )
575 {
576 mbedtls_pk_free( &pk );
577 return( status );
578 }
579
580 /* On success, store the content of the object in the RSA context. */
581 *p_rsa = rsa;
582
583 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200584}
585#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Jaeden Ameroccdce902019-01-10 11:42:27 +0000587#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100589 size_t data_length,
590 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100591 mbedtls_ecp_keypair **p_ecp )
592{
593 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
594 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
595 if( *p_ecp == NULL )
596 return( PSA_ERROR_INSUFFICIENT_MEMORY );
597 mbedtls_ecp_keypair_init( *p_ecp );
598
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100599 if( is_public )
600 {
601 /* A public key is represented as:
602 * - The byte 0x04;
603 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
604 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
605 * So its data length is 2m+1 where n is the key size in bits.
606 */
607 if( ( data_length & 1 ) == 0 )
608 return( PSA_ERROR_INVALID_ARGUMENT );
609 data_length = data_length / 2;
610 }
611
Gilles Peskine4cd32772019-12-02 20:49:42 +0100612 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100613 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
614 if( grp_id == MBEDTLS_ECP_DP_NONE )
615 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100616 return( mbedtls_to_psa_error(
617 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
618}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619
620/* Import a public key given as the uncompressed representation defined by SEC1
621 * 2.3.3 as the content of an ECPoint. */
622static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
623 const uint8_t *data,
624 size_t data_length,
625 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100630 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 if( status != PSA_SUCCESS )
632 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100633
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634 /* Load the public value. */
635 status = mbedtls_to_psa_error(
636 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
637 data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640
641 /* Check that the point is on the curve. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646
647 *p_ecp = ecp;
648 return( PSA_SUCCESS );
649
650exit:
651 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 mbedtls_ecp_keypair_free( ecp );
654 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000656 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200657}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200658
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659/* Import a private key given as a byte string which is the private value
660 * in big-endian order. */
661static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
662 const uint8_t *data,
663 size_t data_length,
664 mbedtls_ecp_keypair **p_ecp )
665{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100669 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 if( status != PSA_SUCCESS )
671 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100672
Gilles Peskinef76aa772018-10-29 19:24:33 +0100673 /* Load the secret value. */
674 status = mbedtls_to_psa_error(
675 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Validate the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
681 if( status != PSA_SUCCESS )
682 goto exit;
683 /* Calculate the public key from the private key. */
684 status = mbedtls_to_psa_error(
685 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
686 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
687 if( status != PSA_SUCCESS )
688 goto exit;
689
690 *p_ecp = ecp;
691 return( PSA_SUCCESS );
692
693exit:
694 if( ecp != NULL )
695 {
696 mbedtls_ecp_keypair_free( ecp );
697 mbedtls_free( ecp );
698 }
699 return( status );
700}
701#endif /* defined(MBEDTLS_ECP_C) */
702
Gilles Peskineb46bef22019-07-30 21:32:04 +0200703
704/** Return the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot.
707 *
708 * \return The key size in bits, read from the metadata in the slot.
709 */
710static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
711{
712 return( slot->attr.bits );
713}
714
715/** Calculate the size of the key in the given slot, in bits.
716 *
717 * \param[in] slot A key slot containing a transparent key.
718 *
719 * \return The key size in bits, calculated from the key data.
720 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 size_t bits = 0; /* return 0 on an empty slot */
724
Gilles Peskineb46bef22019-07-30 21:32:04 +0200725 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200726 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
729 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_RSA_C) */
731#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200732 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
733 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200734#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200735
736 /* We know that the size fits in psa_key_bits_t thanks to checks
737 * when the key was created. */
738 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200739}
740
Gilles Peskine8e338702019-07-30 20:06:31 +0200741/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100742 * previously. This function assumes that the slot does not contain
743 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100744psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
745 const uint8_t *data,
746 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749
Gilles Peskine8e338702019-07-30 20:06:31 +0200750 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200752 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200753 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754 if( data_length > SIZE_MAX / 8 )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200756 /* Enforce a size limit, and in particular ensure that the bit
757 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200758 if( bit_size > PSA_MAX_KEY_BITS )
759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200761 &slot->data.raw );
762 if( status != PSA_SUCCESS )
763 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200764 if( data_length != 0 )
765 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 }
767 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200769 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100772 data, data_length,
773 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100774 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000777 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000779 data, data_length,
780 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 }
782 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100783#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200785 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200787 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000788 data, data_length,
789 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 }
791 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000792#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 {
794 return( PSA_ERROR_NOT_SUPPORTED );
795 }
Darryl Green940d72c2018-07-13 13:18:51 +0100796
Gilles Peskineb46bef22019-07-30 21:32:04 +0200797 if( status == PSA_SUCCESS )
798 {
799 /* Write the actual key size to the slot.
800 * psa_start_key_creation() wrote the size declared by the
801 * caller, which may be 0 (meaning unspecified) or wrong. */
802 slot->attr.bits = psa_calculate_key_bits( slot );
803 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100804 return( status );
805}
806
Gilles Peskinef603c712019-01-19 13:40:11 +0100807/** Calculate the intersection of two algorithm usage policies.
808 *
809 * Return 0 (which allows no operation) on incompatibility.
810 */
811static psa_algorithm_t psa_key_policy_algorithm_intersection(
812 psa_algorithm_t alg1,
813 psa_algorithm_t alg2 )
814{
Gilles Peskine549ea862019-05-22 11:45:59 +0200815 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100816 if( alg1 == alg2 )
817 return( alg1 );
818 /* If the policies are from the same hash-and-sign family, check
819 * if one is a wildcard. If so the other has the specific algorithm. */
820 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
821 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
822 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
823 {
824 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
825 return( alg2 );
826 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
827 return( alg1 );
828 }
829 /* If the policies are incompatible, allow nothing. */
830 return( 0 );
831}
832
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
834 psa_algorithm_t requested_alg )
835{
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837 if( requested_alg == policy_alg )
838 return( 1 );
839 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 * and requested_alg is the same hash-and-sign family with any hash,
841 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
843 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
844 {
845 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
846 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
847 }
848 /* If it isn't permitted, it's forbidden. */
849 return( 0 );
850}
851
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852/** Test whether a policy permits an algorithm.
853 *
854 * The caller must test usage flags separately.
855 */
856static int psa_key_policy_permits( const psa_key_policy_t *policy,
857 psa_algorithm_t alg )
858{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200859 return( psa_key_algorithm_permits( policy->alg, alg ) ||
860 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861}
862
Gilles Peskinef603c712019-01-19 13:40:11 +0100863/** Restrict a key policy based on a constraint.
864 *
865 * \param[in,out] policy The policy to restrict.
866 * \param[in] constraint The policy constraint to apply.
867 *
868 * \retval #PSA_SUCCESS
869 * \c *policy contains the intersection of the original value of
870 * \c *policy and \c *constraint.
871 * \retval #PSA_ERROR_INVALID_ARGUMENT
872 * \c *policy and \c *constraint are incompatible.
873 * \c *policy is unchanged.
874 */
875static psa_status_t psa_restrict_key_policy(
876 psa_key_policy_t *policy,
877 const psa_key_policy_t *constraint )
878{
879 psa_algorithm_t intersection_alg =
880 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 psa_algorithm_t intersection_alg2 =
882 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
884 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
886 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 policy->usage &= constraint->usage;
888 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200889 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100890 return( PSA_SUCCESS );
891}
892
Darryl Green06fd18d2018-07-16 11:21:11 +0100893/** Retrieve a slot which must contain a key. The key must have allow all the
894 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
895 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100896static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100897 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100898 psa_key_usage_t usage,
899 psa_algorithm_t alg )
900{
901 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100902 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100903
904 *p_slot = NULL;
905
Gilles Peskinec5487a82018-12-03 18:08:14 +0100906 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100907 if( status != PSA_SUCCESS )
908 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100909
910 /* Enforce that usage policy for the key slot contains all the flags
911 * required by the usage parameter. There is one exception: public
912 * keys can always be exported, so we treat public key objects as
913 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200914 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100915 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918
919 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200920 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100921 return( PSA_ERROR_NOT_PERMITTED );
922
923 *p_slot = slot;
924 return( PSA_SUCCESS );
925}
Darryl Green940d72c2018-07-13 13:18:51 +0100926
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927/** Retrieve a slot which must contain a transparent key.
928 *
929 * A transparent key is a key for which the key material is directly
930 * available, as opposed to a key in a secure element.
931 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200932 * This is a temporary function to use instead of psa_get_key_from_slot()
933 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 */
935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
936static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
937 psa_key_slot_t **p_slot,
938 psa_key_usage_t usage,
939 psa_algorithm_t alg )
940{
941 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
942 if( status != PSA_SUCCESS )
943 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200944 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 {
946 *p_slot = NULL;
947 return( PSA_ERROR_NOT_SUPPORTED );
948 }
949 return( PSA_SUCCESS );
950}
951#else /* MBEDTLS_PSA_CRYPTO_SE_C */
952/* With no secure element support, all keys are transparent. */
953#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
954 psa_get_key_from_slot( handle, p_slot, usage, alg )
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100958static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000959{
Gilles Peskine73167e12019-07-12 23:44:37 +0200960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
961 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000962 {
963 /* No key material to clean. */
964 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200965 else
966#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 /* No key material to clean. */
970 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200971 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000972 {
973 mbedtls_free( slot->data.raw.data );
974 }
975 else
976#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000978 {
979 mbedtls_rsa_free( slot->data.rsa );
980 mbedtls_free( slot->data.rsa );
981 }
982 else
983#endif /* defined(MBEDTLS_RSA_C) */
984#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000986 {
987 mbedtls_ecp_keypair_free( slot->data.ecp );
988 mbedtls_free( slot->data.ecp );
989 }
990 else
991#endif /* defined(MBEDTLS_ECP_C) */
992 {
993 /* Shouldn't happen: the key type is not any type that we
994 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200995 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000996 }
997
998 return( PSA_SUCCESS );
999}
1000
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001/** Completely wipe a slot in memory, including its policy.
1002 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001003psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001004{
1005 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001006 /* Multipart operations may still be using the key. This is safe
1007 * because all multipart operation objects are independent from
1008 * the key slot: if they need to access the key after the setup
1009 * phase, they have a copy of the key. Note that this means that
1010 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001011 /* At this point, key material and other type-specific content has
1012 * been wiped. Clear remaining metadata. We can call memset and not
1013 * zeroize because the metadata is not particularly sensitive. */
1014 memset( slot, 0, sizeof( *slot ) );
1015 return( status );
1016}
1017
Gilles Peskinec5487a82018-12-03 18:08:14 +01001018psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001020 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001021 psa_status_t status; /* status of the last operation */
1022 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1024 psa_se_drv_table_entry_t *driver;
1025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026
Gilles Peskine1841cf42019-10-08 15:48:25 +02001027 if( handle == 0 )
1028 return( PSA_SUCCESS );
1029
Gilles Peskinec5487a82018-12-03 18:08:14 +01001030 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
1032 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001033
1034#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001036 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001038 /* For a key in a secure element, we need to do three things:
1039 * remove the key file in internal storage, destroy the
1040 * key inside the secure element, and update the driver's
1041 * persistent data. Start a transaction that will encompass these
1042 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001044 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001045 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 status = psa_crypto_save_transaction( );
1048 if( status != PSA_SUCCESS )
1049 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001050 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001051 /* We should still try to destroy the key in the secure
1052 * element and the key metadata in storage. This is especially
1053 * important if the error is that the storage is full.
1054 * But how to do it exactly without risking an inconsistent
1055 * state after a reset?
1056 * https://github.com/ARMmbed/mbed-crypto/issues/215
1057 */
1058 overall_status = status;
1059 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001060 }
1061
Gilles Peskine354f7672019-07-12 23:46:38 +02001062 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001063 if( overall_status == PSA_SUCCESS )
1064 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1067
Darryl Greend49a4992018-06-18 17:27:26 +01001068#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001069 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001070 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 status = psa_destroy_persistent_key( slot->attr.id );
1072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001075 /* TODO: other slots may have a copy of the same key. We should
1076 * invalidate them.
1077 * https://github.com/ARMmbed/mbed-crypto/issues/214
1078 */
Darryl Greend49a4992018-06-18 17:27:26 +01001079 }
1080#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001081
Gilles Peskinefc762652019-07-22 19:30:34 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083 if( driver != NULL )
1084 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001085 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
1088 status = psa_crypto_stop_transaction( );
1089 if( overall_status == PSA_SUCCESS )
1090 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 }
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1093
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1095exit:
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001097 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1099 if( overall_status == PSA_SUCCESS )
1100 overall_status = status;
1101 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102}
1103
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001105{
Gilles Peskineb699f072019-04-26 16:06:02 +02001106 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001107 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001108}
1109
Gilles Peskineb699f072019-04-26 16:06:02 +02001110psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1111 psa_key_type_t type,
1112 const uint8_t *data,
1113 size_t data_length )
1114{
1115 uint8_t *copy = NULL;
1116
1117 if( data_length != 0 )
1118 {
1119 copy = mbedtls_calloc( 1, data_length );
1120 if( copy == NULL )
1121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1122 memcpy( copy, data, data_length );
1123 }
1124 /* After this point, this function is guaranteed to succeed, so it
1125 * can start modifying `*attributes`. */
1126
1127 if( attributes->domain_parameters != NULL )
1128 {
1129 mbedtls_free( attributes->domain_parameters );
1130 attributes->domain_parameters = NULL;
1131 attributes->domain_parameters_size = 0;
1132 }
1133
1134 attributes->domain_parameters = copy;
1135 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001136 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001137 return( PSA_SUCCESS );
1138}
1139
1140psa_status_t psa_get_key_domain_parameters(
1141 const psa_key_attributes_t *attributes,
1142 uint8_t *data, size_t data_size, size_t *data_length )
1143{
1144 if( attributes->domain_parameters_size > data_size )
1145 return( PSA_ERROR_BUFFER_TOO_SMALL );
1146 *data_length = attributes->domain_parameters_size;
1147 if( attributes->domain_parameters_size != 0 )
1148 memcpy( data, attributes->domain_parameters,
1149 attributes->domain_parameters_size );
1150 return( PSA_SUCCESS );
1151}
1152
1153#if defined(MBEDTLS_RSA_C)
1154static psa_status_t psa_get_rsa_public_exponent(
1155 const mbedtls_rsa_context *rsa,
1156 psa_key_attributes_t *attributes )
1157{
1158 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001160 uint8_t *buffer = NULL;
1161 size_t buflen;
1162 mbedtls_mpi_init( &mpi );
1163
1164 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1165 if( ret != 0 )
1166 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001167 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1168 {
1169 /* It's the default value, which is reported as an empty string,
1170 * so there's nothing to do. */
1171 goto exit;
1172 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
1174 buflen = mbedtls_mpi_size( &mpi );
1175 buffer = mbedtls_calloc( 1, buflen );
1176 if( buffer == NULL )
1177 {
1178 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1179 goto exit;
1180 }
1181 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1182 if( ret != 0 )
1183 goto exit;
1184 attributes->domain_parameters = buffer;
1185 attributes->domain_parameters_size = buflen;
1186
1187exit:
1188 mbedtls_mpi_free( &mpi );
1189 if( ret != 0 )
1190 mbedtls_free( buffer );
1191 return( mbedtls_to_psa_error( ret ) );
1192}
1193#endif /* MBEDTLS_RSA_C */
1194
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001195/** Retrieve all the publicly-accessible attributes of a key.
1196 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001197psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1198 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001201 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203 psa_reset_key_attributes( attributes );
1204
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001205 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001206 if( status != PSA_SUCCESS )
1207 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001208
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001209 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001210 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1211 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1212
1213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214 if( psa_key_slot_is_external( slot ) )
1215 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217
Gilles Peskine8e338702019-07-30 20:06:31 +02001218 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 {
1220#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001221 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001224 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001225 * is not yet implemented.
1226 * https://github.com/ARMmbed/mbed-crypto/issues/216
1227 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001228 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001229 break;
1230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1232 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001233#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 default:
1235 /* Nothing else to do. */
1236 break;
1237 }
1238
1239 if( status != PSA_SUCCESS )
1240 psa_reset_key_attributes( attributes );
1241 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242}
1243
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1245psa_status_t psa_get_key_slot_number(
1246 const psa_key_attributes_t *attributes,
1247 psa_key_slot_number_t *slot_number )
1248{
1249 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1250 {
1251 *slot_number = attributes->slot_number;
1252 return( PSA_SUCCESS );
1253 }
1254 else
1255 return( PSA_ERROR_INVALID_ARGUMENT );
1256}
1257#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1258
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001260static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1261 unsigned char *buf, size_t size )
1262{
Janos Follath24eed8d2019-11-22 13:21:35 +00001263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001264 unsigned char *c;
1265 size_t len = 0;
1266
1267 c = buf + size;
1268
1269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1270
1271 return( (int) len );
1272}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001273#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001274
Gilles Peskinef603c712019-01-19 13:40:11 +01001275static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1276 uint8_t *data,
1277 size_t data_size,
1278 size_t *data_length,
1279 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Gilles Peskine5d309672019-07-12 23:47:28 +02001281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1282 const psa_drv_se_t *drv;
1283 psa_drv_se_context_t *drv_context;
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001286 *data_length = 0;
1287
Gilles Peskine8e338702019-07-30 20:06:31 +02001288 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001289 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001290
Gilles Peskinef9168942019-09-12 19:20:29 +02001291 /* Reject a zero-length output buffer now, since this can never be a
1292 * valid key representation. This way we know that data must be a valid
1293 * pointer and we can do things like memset(data, ..., data_size). */
1294 if( data_size == 0 )
1295 return( PSA_ERROR_BUFFER_TOO_SMALL );
1296
Gilles Peskine5d309672019-07-12 23:47:28 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001299 {
1300 psa_drv_se_export_key_t method;
1301 if( drv->key_management == NULL )
1302 return( PSA_ERROR_NOT_SUPPORTED );
1303 method = ( export_public_key ?
1304 drv->key_management->p_export_public :
1305 drv->key_management->p_export );
1306 if( method == NULL )
1307 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001308 return( method( drv_context,
1309 slot->data.se.slot_number,
1310 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001311 }
1312#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1313
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 {
1316 if( slot->data.raw.bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001318 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1319 memset( data + slot->data.raw.bytes, 0,
1320 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321 *data_length = slot->data.raw.bytes;
1322 return( PSA_SUCCESS );
1323 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001324#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001325 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001326 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001327 psa_status_t status;
1328
Gilles Peskineb46bef22019-07-30 21:32:04 +02001329 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001330 if( bytes > data_size )
1331 return( PSA_ERROR_BUFFER_TOO_SMALL );
1332 status = mbedtls_to_psa_error(
1333 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1334 if( status != PSA_SUCCESS )
1335 return( status );
1336 memset( data + bytes, 0, data_size - bytes );
1337 *data_length = bytes;
1338 return( PSA_SUCCESS );
1339 }
1340#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341 else
Moran Peker17e36e12018-05-02 12:55:20 +03001342 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001343#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1345 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001346 {
Moran Pekera998bc62018-04-16 18:16:20 +03001347 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001349 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001350 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#if defined(MBEDTLS_RSA_C)
1352 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001353 pk.pk_info = &mbedtls_rsa_info;
1354 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001358 }
1359 else
1360 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001361#if defined(MBEDTLS_ECP_C)
1362 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001363 pk.pk_info = &mbedtls_eckey_info;
1364 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001365#else
1366 return( PSA_ERROR_NOT_SUPPORTED );
1367#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001368 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001369 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001371 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 }
Moran Peker17e36e12018-05-02 12:55:20 +03001373 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 {
Moran Peker17e36e12018-05-02 12:55:20 +03001375 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001376 }
Moran Peker60364322018-04-29 11:34:58 +03001377 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001379 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001380 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1383 * Move the data to the beginning and erase remaining data
1384 * at the original location. */
1385 if( 2 * (size_t) ret <= data_size )
1386 {
1387 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001388 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001389 }
1390 else if( (size_t) ret < data_size )
1391 {
1392 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001393 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001394 }
Moran Pekera998bc62018-04-16 18:16:20 +03001395 *data_length = ret;
1396 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001397 }
1398 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001399#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001400 {
1401 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001402 it is valid for a special-purpose implementation to omit
1403 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001404 return( PSA_ERROR_NOT_SUPPORTED );
1405 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406 }
1407}
1408
Gilles Peskinec5487a82018-12-03 18:08:14 +01001409psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 uint8_t *data,
1411 size_t data_size,
1412 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001413{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 psa_status_t status;
1416
1417 /* Set the key to empty now, so that even when there are errors, we always
1418 * set data_length to a value between 0 and data_size. On error, setting
1419 * the key to empty is a good choice because an empty key representation is
1420 * unlikely to be accepted anywhere. */
1421 *data_length = 0;
1422
1423 /* Export requires the EXPORT flag. There is an exception for public keys,
1424 * which don't require any flag, but psa_get_key_from_slot takes
1425 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
1429 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001430 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432
Gilles Peskinec5487a82018-12-03 18:08:14 +01001433psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 uint8_t *data,
1435 size_t data_size,
1436 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001437{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001438 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001439 psa_status_t status;
1440
1441 /* Set the key to empty now, so that even when there are errors, we always
1442 * set data_length to a value between 0 and data_size. On error, setting
1443 * the key to empty is a good choice because an empty key representation is
1444 * unlikely to be accepted anywhere. */
1445 *data_length = 0;
1446
1447 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001448 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001449 if( status != PSA_SUCCESS )
1450 return( status );
1451 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001452 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001453}
1454
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455#if defined(static_assert)
1456static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1457 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001458static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001459 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001460static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001461 "One or more key attribute flag is listed as both internal-only and external-only" );
1462#endif
1463
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001464/** Validate that a key policy is internally well-formed.
1465 *
1466 * This function only rejects invalid policies. It does not validate the
1467 * consistency of the policy with respect to other attributes of the key
1468 * such as the key type.
1469 */
1470static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001471{
1472 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001473 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 PSA_KEY_USAGE_ENCRYPT |
1475 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001476 PSA_KEY_USAGE_SIGN_HASH |
1477 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1479 return( PSA_ERROR_INVALID_ARGUMENT );
1480
Gilles Peskine4747d192019-04-17 15:05:45 +02001481 return( PSA_SUCCESS );
1482}
1483
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001484/** Validate the internal consistency of key attributes.
1485 *
1486 * This function only rejects invalid attribute values. If does not
1487 * validate the consistency of the attributes with any key data that may
1488 * be involved in the creation of the key.
1489 *
1490 * Call this function early in the key creation process.
1491 *
1492 * \param[in] attributes Key attributes for the new key.
1493 * \param[out] p_drv On any return, the driver for the key, if any.
1494 * NULL for a transparent key.
1495 *
1496 */
1497static psa_status_t psa_validate_key_attributes(
1498 const psa_key_attributes_t *attributes,
1499 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001500{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001501 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001503 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1504 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001505 if( status != PSA_SUCCESS )
1506 return( status );
1507
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001508 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1509 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001510 if( status != PSA_SUCCESS )
1511 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512
1513 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001514 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001515 return( status );
1516
1517 /* Refuse to create overly large keys.
1518 * Note that this doesn't trigger on import if the attributes don't
1519 * explicitly specify a size (so psa_get_key_bits returns 0), so
1520 * psa_import_key() needs its own checks. */
1521 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1522 return( PSA_ERROR_NOT_SUPPORTED );
1523
Gilles Peskine91e8c332019-08-02 19:19:39 +02001524 /* Reject invalid flags. These should not be reachable through the API. */
1525 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1526 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1527 return( PSA_ERROR_INVALID_ARGUMENT );
1528
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529 return( PSA_SUCCESS );
1530}
1531
Gilles Peskine4747d192019-04-17 15:05:45 +02001532/** Prepare a key slot to receive key material.
1533 *
1534 * This function allocates a key slot and sets its metadata.
1535 *
1536 * If this function fails, call psa_fail_key_creation().
1537 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * This function is intended to be used as follows:
1539 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1540 * it with the specified attributes, and assign it a handle.
1541 * -# Populate the slot with the key material.
1542 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1543 * In case of failure at any step, stop the sequence and call
1544 * psa_fail_key_creation().
1545 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 * \param[in] attributes Key attributes for the new key.
1548 * \param[out] handle On success, a handle for the allocated slot.
1549 * \param[out] p_slot On success, a pointer to the prepared slot.
1550 * \param[out] p_drv On any return, the driver for the key, if any.
1551 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001552 *
1553 * \retval #PSA_SUCCESS
1554 * The key slot is ready to receive key material.
1555 * \return If this function fails, the key slot is an invalid state.
1556 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 */
1558static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001559 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 const psa_key_attributes_t *attributes,
1561 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001562 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001563 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001564{
1565 psa_status_t status;
1566 psa_key_slot_t *slot;
1567
Gilles Peskinedf179142019-07-15 22:02:14 +02001568 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001569 *p_drv = NULL;
1570
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571 status = psa_validate_key_attributes( attributes, p_drv );
1572 if( status != PSA_SUCCESS )
1573 return( status );
1574
Gilles Peskineedbed562019-08-07 18:19:59 +02001575 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001576 if( status != PSA_SUCCESS )
1577 return( status );
1578 slot = *p_slot;
1579
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001580 /* We're storing the declared bit-size of the key. It's up to each
1581 * creation mechanism to verify that this information is correct.
1582 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001583 * an input (generate, device) but not for those where the bit-size
1584 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585
1586 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001587
Gilles Peskine91e8c332019-08-02 19:19:39 +02001588 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001589 * external-only flags, query `attributes`. Thanks to the check
1590 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001591 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001592 * may have set. */
1593 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001594
Gilles Peskinecbaff462019-07-12 23:46:04 +02001595#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001596 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001597 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001598 * create the key file in internal storage, create the
1599 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001600 * persistent data. This is done by starting a transaction that will
1601 * encompass these three actions.
1602 * For registering a volatile key, we just need to find an appropriate
1603 * slot number inside the SE. Since the key is designated volatile, creating
1604 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001605 /* The first thing to do is to find a slot number for the new key.
1606 * We save the slot number in persistent storage as part of the
1607 * transaction data. It will be needed to recover if the power
1608 * fails during the key creation process, to clean up on the secure
1609 * element side after restarting. Obtaining a slot number from the
1610 * secure element driver updates its persistent state, but we do not yet
1611 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001612 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001613 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001615 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001616 &slot->data.se.slot_number );
1617 if( status != PSA_SUCCESS )
1618 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001619
1620 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001621 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001622 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1623 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1624 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1625 psa_crypto_transaction.key.id = slot->attr.id;
1626 status = psa_crypto_save_transaction( );
1627 if( status != PSA_SUCCESS )
1628 {
1629 (void) psa_crypto_stop_transaction( );
1630 return( status );
1631 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001632 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001633 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001634
1635 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1636 {
1637 /* Key registration only makes sense with a secure element. */
1638 return( PSA_ERROR_INVALID_ARGUMENT );
1639 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001640#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1641
Darryl Green0c6575a2018-11-07 16:05:30 +00001642 return( status );
1643}
Gilles Peskine4747d192019-04-17 15:05:45 +02001644
1645/** Finalize the creation of a key once its key material has been set.
1646 *
1647 * This entails writing the key to persistent storage.
1648 *
1649 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001650 * See the documentation of psa_start_key_creation() for the intended use
1651 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001653 * \param[in,out] slot Pointer to the slot with key material.
1654 * \param[in] driver The secure element driver for the key,
1655 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001656 *
1657 * \retval #PSA_SUCCESS
1658 * The key was successfully created. The handle is now valid.
1659 * \return If this function fails, the key slot is an invalid state.
1660 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001662static psa_status_t psa_finish_key_creation(
1663 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001664 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001665{
1666 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001667 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001668 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001669
1670#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001671 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001672 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001673#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1674 if( driver != NULL )
1675 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001676 psa_se_key_data_storage_t data;
1677#if defined(static_assert)
1678 static_assert( sizeof( slot->data.se.slot_number ) ==
1679 sizeof( data.slot_number ),
1680 "Slot number size does not match psa_se_key_data_storage_t" );
1681 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1682 "Bit-size size does not match psa_se_key_data_storage_t" );
1683#endif
1684 memcpy( &data.slot_number, &slot->data.se.slot_number,
1685 sizeof( slot->data.se.slot_number ) );
1686 memcpy( &data.bits, &slot->attr.bits,
1687 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001688 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001689 (uint8_t*) &data,
1690 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001691 }
1692 else
1693#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1694 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001695 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001696 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001697 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001698 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1699 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001700 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001701 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1702 status = psa_internal_export_key( slot,
1703 buffer, buffer_size, &length,
1704 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001705 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001706 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001707 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001708
Gilles Peskinef9168942019-09-12 19:20:29 +02001709 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001710 mbedtls_free( buffer );
1711 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001712 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001713#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1714
Gilles Peskinecbaff462019-07-12 23:46:04 +02001715#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001716 /* Finish the transaction for a key creation. This does not
1717 * happen when registering an existing key. Detect this case
1718 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001719 * creation of a persistent key in a secure element requires a transaction,
1720 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001721 if( driver != NULL &&
1722 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001723 {
1724 status = psa_save_se_persistent_data( driver );
1725 if( status != PSA_SUCCESS )
1726 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001727 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001728 return( status );
1729 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001730 status = psa_crypto_stop_transaction( );
1731 if( status != PSA_SUCCESS )
1732 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001733 }
1734#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1735
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 return( status );
1737}
1738
1739/** Abort the creation of a key.
1740 *
1741 * You may call this function after calling psa_start_key_creation(),
1742 * or after psa_finish_key_creation() fails. In other circumstances, this
1743 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001744 * See the documentation of psa_start_key_creation() for the intended use
1745 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001746 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 * \param[in,out] slot Pointer to the slot with key material.
1748 * \param[in] driver The secure element driver for the key,
1749 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001751static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001752 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001753{
Gilles Peskine011e4282019-06-26 18:34:38 +02001754 (void) driver;
1755
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 if( slot == NULL )
1757 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001758
1759#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001760 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * element, and the failure happened later (when saving metadata
1762 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001763 * element.
1764 * https://github.com/ARMmbed/mbed-crypto/issues/217
1765 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001766
Gilles Peskined7729582019-08-05 15:55:54 +02001767 /* Abort the ongoing transaction if any (there may not be one if
1768 * the creation process failed before starting one, or if the
1769 * key creation is a registration of a key in a secure element).
1770 * Earlier functions must already have done what it takes to undo any
1771 * partial creation. All that's left is to update the transaction data
1772 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001773 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001774#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1775
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 psa_wipe_key_slot( slot );
1777}
1778
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001779/** Validate optional attributes during key creation.
1780 *
1781 * Some key attributes are optional during key creation. If they are
1782 * specified in the attributes structure, check that they are consistent
1783 * with the data in the slot.
1784 *
1785 * This function should be called near the end of key creation, after
1786 * the slot in memory is fully populated but before saving persistent data.
1787 */
1788static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001789 const psa_key_slot_t *slot,
1790 const psa_key_attributes_t *attributes )
1791{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001792 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001793 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001794 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001795 return( PSA_ERROR_INVALID_ARGUMENT );
1796 }
1797
1798 if( attributes->domain_parameters_size != 0 )
1799 {
1800#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001801 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 {
1803 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001805 mbedtls_mpi_init( &actual );
1806 mbedtls_mpi_init( &required );
1807 ret = mbedtls_rsa_export( slot->data.rsa,
1808 NULL, NULL, NULL, NULL, &actual );
1809 if( ret != 0 )
1810 goto rsa_exit;
1811 ret = mbedtls_mpi_read_binary( &required,
1812 attributes->domain_parameters,
1813 attributes->domain_parameters_size );
1814 if( ret != 0 )
1815 goto rsa_exit;
1816 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1817 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1818 rsa_exit:
1819 mbedtls_mpi_free( &actual );
1820 mbedtls_mpi_free( &required );
1821 if( ret != 0)
1822 return( mbedtls_to_psa_error( ret ) );
1823 }
1824 else
1825#endif
1826 {
1827 return( PSA_ERROR_INVALID_ARGUMENT );
1828 }
1829 }
1830
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001831 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001832 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001833 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001834 return( PSA_ERROR_INVALID_ARGUMENT );
1835 }
1836
1837 return( PSA_SUCCESS );
1838}
1839
Gilles Peskine4747d192019-04-17 15:05:45 +02001840psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001842 size_t data_length,
1843 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001844{
1845 psa_status_t status;
1846 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001847 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001848
Gilles Peskine0f84d622019-09-12 19:03:13 +02001849 /* Reject zero-length symmetric keys (including raw data key objects).
1850 * This also rejects any key which might be encoded as an empty string,
1851 * which is never valid. */
1852 if( data_length == 0 )
1853 return( PSA_ERROR_INVALID_ARGUMENT );
1854
Gilles Peskinedf179142019-07-15 22:02:14 +02001855 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1856 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001857 if( status != PSA_SUCCESS )
1858 goto exit;
1859
Gilles Peskine5d309672019-07-12 23:47:28 +02001860#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1861 if( driver != NULL )
1862 {
1863 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001864 /* The driver should set the number of key bits, however in
1865 * case it doesn't, we initialize bits to an invalid value. */
1866 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001867 if( drv->key_management == NULL ||
1868 drv->key_management->p_import == NULL )
1869 {
1870 status = PSA_ERROR_NOT_SUPPORTED;
1871 goto exit;
1872 }
1873 status = drv->key_management->p_import(
1874 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001875 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001876 &bits );
1877 if( status != PSA_SUCCESS )
1878 goto exit;
1879 if( bits > PSA_MAX_KEY_BITS )
1880 {
1881 status = PSA_ERROR_NOT_SUPPORTED;
1882 goto exit;
1883 }
1884 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001885 }
1886 else
1887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1888 {
1889 status = psa_import_key_into_slot( slot, data, data_length );
1890 if( status != PSA_SUCCESS )
1891 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001892 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001893 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001894 if( status != PSA_SUCCESS )
1895 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896
Gilles Peskine011e4282019-06-26 18:34:38 +02001897 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 if( status != PSA_SUCCESS )
1900 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001901 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 *handle = 0;
1903 }
1904 return( status );
1905}
1906
Gilles Peskined7729582019-08-05 15:55:54 +02001907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1908psa_status_t mbedtls_psa_register_se_key(
1909 const psa_key_attributes_t *attributes )
1910{
1911 psa_status_t status;
1912 psa_key_slot_t *slot = NULL;
1913 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001914 psa_key_handle_t handle = 0;
1915
1916 /* Leaving attributes unspecified is not currently supported.
1917 * It could make sense to query the key type and size from the
1918 * secure element, but not all secure elements support this
1919 * and the driver HAL doesn't currently support it. */
1920 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1921 return( PSA_ERROR_NOT_SUPPORTED );
1922 if( psa_get_key_bits( attributes ) == 0 )
1923 return( PSA_ERROR_NOT_SUPPORTED );
1924
1925 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1926 &handle, &slot, &driver );
1927 if( status != PSA_SUCCESS )
1928 goto exit;
1929
Gilles Peskined7729582019-08-05 15:55:54 +02001930 status = psa_finish_key_creation( slot, driver );
1931
1932exit:
1933 if( status != PSA_SUCCESS )
1934 {
1935 psa_fail_key_creation( slot, driver );
1936 }
1937 /* Registration doesn't keep the key in RAM. */
1938 psa_close_key( handle );
1939 return( status );
1940}
1941#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1942
Gilles Peskinef603c712019-01-19 13:40:11 +01001943static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001944 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001945{
1946 psa_status_t status;
1947 uint8_t *buffer = NULL;
1948 size_t buffer_size = 0;
1949 size_t length;
1950
Gilles Peskine8e338702019-07-30 20:06:31 +02001951 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001952 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001953 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001954 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001955 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001956 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1957 if( status != PSA_SUCCESS )
1958 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001959 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001960 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001961
1962exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001963 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001964 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001965 return( status );
1966}
1967
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1969 const psa_key_attributes_t *specified_attributes,
1970 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001971{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001972 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001973 psa_key_slot_t *source_slot = NULL;
1974 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001975 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001976 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001977
Gilles Peskine28f8f302019-07-24 13:30:31 +02001978 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001979 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001980 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001981 goto exit;
1982
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001983 status = psa_validate_optional_attributes( source_slot,
1984 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001985 if( status != PSA_SUCCESS )
1986 goto exit;
1987
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001988 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001989 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
Gilles Peskinedf179142019-07-15 22:02:14 +02001993 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1994 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001995 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001996 if( status != PSA_SUCCESS )
1997 goto exit;
1998
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001999#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2000 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002001 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002002 /* Copying to a secure element is not implemented yet. */
2003 status = PSA_ERROR_NOT_SUPPORTED;
2004 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002005 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002006#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002007
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002009 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002011
Gilles Peskine011e4282019-06-26 18:34:38 +02002012 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014 if( status != PSA_SUCCESS )
2015 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002016 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002017 *target_handle = 0;
2018 }
2019 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002020}
2021
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002022
2023
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002024/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002025/* Message digests */
2026/****************************************************************/
2027
Gilles Peskineb16841e2019-10-10 20:36:12 +02002028#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002029static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002030{
2031 switch( alg )
2032 {
2033#if defined(MBEDTLS_MD2_C)
2034 case PSA_ALG_MD2:
2035 return( &mbedtls_md2_info );
2036#endif
2037#if defined(MBEDTLS_MD4_C)
2038 case PSA_ALG_MD4:
2039 return( &mbedtls_md4_info );
2040#endif
2041#if defined(MBEDTLS_MD5_C)
2042 case PSA_ALG_MD5:
2043 return( &mbedtls_md5_info );
2044#endif
2045#if defined(MBEDTLS_RIPEMD160_C)
2046 case PSA_ALG_RIPEMD160:
2047 return( &mbedtls_ripemd160_info );
2048#endif
2049#if defined(MBEDTLS_SHA1_C)
2050 case PSA_ALG_SHA_1:
2051 return( &mbedtls_sha1_info );
2052#endif
2053#if defined(MBEDTLS_SHA256_C)
2054 case PSA_ALG_SHA_224:
2055 return( &mbedtls_sha224_info );
2056 case PSA_ALG_SHA_256:
2057 return( &mbedtls_sha256_info );
2058#endif
2059#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002060#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002061 case PSA_ALG_SHA_384:
2062 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002063#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002064 case PSA_ALG_SHA_512:
2065 return( &mbedtls_sha512_info );
2066#endif
2067 default:
2068 return( NULL );
2069 }
2070}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002071#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002072
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002073psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2074{
2075 switch( operation->alg )
2076 {
Gilles Peskine81736312018-06-26 15:04:31 +02002077 case 0:
2078 /* The object has (apparently) been initialized but it is not
2079 * in use. It's ok to call abort on such an object, and there's
2080 * nothing to do. */
2081 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082#if defined(MBEDTLS_MD2_C)
2083 case PSA_ALG_MD2:
2084 mbedtls_md2_free( &operation->ctx.md2 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_MD4_C)
2088 case PSA_ALG_MD4:
2089 mbedtls_md4_free( &operation->ctx.md4 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_MD5_C)
2093 case PSA_ALG_MD5:
2094 mbedtls_md5_free( &operation->ctx.md5 );
2095 break;
2096#endif
2097#if defined(MBEDTLS_RIPEMD160_C)
2098 case PSA_ALG_RIPEMD160:
2099 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2100 break;
2101#endif
2102#if defined(MBEDTLS_SHA1_C)
2103 case PSA_ALG_SHA_1:
2104 mbedtls_sha1_free( &operation->ctx.sha1 );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA256_C)
2108 case PSA_ALG_SHA_224:
2109 case PSA_ALG_SHA_256:
2110 mbedtls_sha256_free( &operation->ctx.sha256 );
2111 break;
2112#endif
2113#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002114#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002116#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117 case PSA_ALG_SHA_512:
2118 mbedtls_sha512_free( &operation->ctx.sha512 );
2119 break;
2120#endif
2121 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002122 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 }
2124 operation->alg = 0;
2125 return( PSA_SUCCESS );
2126}
2127
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002128psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 psa_algorithm_t alg )
2130{
Janos Follath24eed8d2019-11-22 13:21:35 +00002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002132
2133 /* A context must be freshly initialized before it can be set up. */
2134 if( operation->alg != 0 )
2135 {
2136 return( PSA_ERROR_BAD_STATE );
2137 }
2138
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002139 switch( alg )
2140 {
2141#if defined(MBEDTLS_MD2_C)
2142 case PSA_ALG_MD2:
2143 mbedtls_md2_init( &operation->ctx.md2 );
2144 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2145 break;
2146#endif
2147#if defined(MBEDTLS_MD4_C)
2148 case PSA_ALG_MD4:
2149 mbedtls_md4_init( &operation->ctx.md4 );
2150 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_MD5_C)
2154 case PSA_ALG_MD5:
2155 mbedtls_md5_init( &operation->ctx.md5 );
2156 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_RIPEMD160_C)
2160 case PSA_ALG_RIPEMD160:
2161 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2162 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2163 break;
2164#endif
2165#if defined(MBEDTLS_SHA1_C)
2166 case PSA_ALG_SHA_1:
2167 mbedtls_sha1_init( &operation->ctx.sha1 );
2168 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2169 break;
2170#endif
2171#if defined(MBEDTLS_SHA256_C)
2172 case PSA_ALG_SHA_224:
2173 mbedtls_sha256_init( &operation->ctx.sha256 );
2174 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2175 break;
2176 case PSA_ALG_SHA_256:
2177 mbedtls_sha256_init( &operation->ctx.sha256 );
2178 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2179 break;
2180#endif
2181#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002182#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002183 case PSA_ALG_SHA_384:
2184 mbedtls_sha512_init( &operation->ctx.sha512 );
2185 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2186 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002187#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188 case PSA_ALG_SHA_512:
2189 mbedtls_sha512_init( &operation->ctx.sha512 );
2190 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2191 break;
2192#endif
2193 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002194 return( PSA_ALG_IS_HASH( alg ) ?
2195 PSA_ERROR_NOT_SUPPORTED :
2196 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 }
2198 if( ret == 0 )
2199 operation->alg = alg;
2200 else
2201 psa_hash_abort( operation );
2202 return( mbedtls_to_psa_error( ret ) );
2203}
2204
2205psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2206 const uint8_t *input,
2207 size_t input_length )
2208{
Janos Follath24eed8d2019-11-22 13:21:35 +00002209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002210
2211 /* Don't require hash implementations to behave correctly on a
2212 * zero-length input, which may have an invalid pointer. */
2213 if( input_length == 0 )
2214 return( PSA_SUCCESS );
2215
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216 switch( operation->alg )
2217 {
2218#if defined(MBEDTLS_MD2_C)
2219 case PSA_ALG_MD2:
2220 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2221 input, input_length );
2222 break;
2223#endif
2224#if defined(MBEDTLS_MD4_C)
2225 case PSA_ALG_MD4:
2226 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2227 input, input_length );
2228 break;
2229#endif
2230#if defined(MBEDTLS_MD5_C)
2231 case PSA_ALG_MD5:
2232 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2233 input, input_length );
2234 break;
2235#endif
2236#if defined(MBEDTLS_RIPEMD160_C)
2237 case PSA_ALG_RIPEMD160:
2238 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2239 input, input_length );
2240 break;
2241#endif
2242#if defined(MBEDTLS_SHA1_C)
2243 case PSA_ALG_SHA_1:
2244 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2245 input, input_length );
2246 break;
2247#endif
2248#if defined(MBEDTLS_SHA256_C)
2249 case PSA_ALG_SHA_224:
2250 case PSA_ALG_SHA_256:
2251 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2252 input, input_length );
2253 break;
2254#endif
2255#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002256#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002258#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259 case PSA_ALG_SHA_512:
2260 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2261 input, input_length );
2262 break;
2263#endif
2264 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002265 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002267
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268 if( ret != 0 )
2269 psa_hash_abort( operation );
2270 return( mbedtls_to_psa_error( ret ) );
2271}
2272
2273psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2274 uint8_t *hash,
2275 size_t hash_size,
2276 size_t *hash_length )
2277{
itayzafrir40835d42018-08-02 13:14:17 +03002278 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002280 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281
2282 /* Fill the output buffer with something that isn't a valid hash
2283 * (barring an attack on the hash and deliberately-crafted input),
2284 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002285 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002286 /* If hash_size is 0 then hash may be NULL and then the
2287 * call to memset would have undefined behavior. */
2288 if( hash_size != 0 )
2289 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290
2291 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002292 {
2293 status = PSA_ERROR_BUFFER_TOO_SMALL;
2294 goto exit;
2295 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296
2297 switch( operation->alg )
2298 {
2299#if defined(MBEDTLS_MD2_C)
2300 case PSA_ALG_MD2:
2301 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2302 break;
2303#endif
2304#if defined(MBEDTLS_MD4_C)
2305 case PSA_ALG_MD4:
2306 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2307 break;
2308#endif
2309#if defined(MBEDTLS_MD5_C)
2310 case PSA_ALG_MD5:
2311 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2312 break;
2313#endif
2314#if defined(MBEDTLS_RIPEMD160_C)
2315 case PSA_ALG_RIPEMD160:
2316 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2317 break;
2318#endif
2319#if defined(MBEDTLS_SHA1_C)
2320 case PSA_ALG_SHA_1:
2321 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA256_C)
2325 case PSA_ALG_SHA_224:
2326 case PSA_ALG_SHA_256:
2327 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2328 break;
2329#endif
2330#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002331#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002333#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 case PSA_ALG_SHA_512:
2335 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2336 break;
2337#endif
2338 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002339 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 }
itayzafrir40835d42018-08-02 13:14:17 +03002341 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342
itayzafrir40835d42018-08-02 13:14:17 +03002343exit:
2344 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002346 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 return( psa_hash_abort( operation ) );
2348 }
2349 else
2350 {
2351 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002352 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353 }
2354}
2355
Gilles Peskine2d277862018-06-18 15:41:12 +02002356psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2357 const uint8_t *hash,
2358 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359{
2360 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2361 size_t actual_hash_length;
2362 psa_status_t status = psa_hash_finish( operation,
2363 actual_hash, sizeof( actual_hash ),
2364 &actual_hash_length );
2365 if( status != PSA_SUCCESS )
2366 return( status );
2367 if( actual_hash_length != hash_length )
2368 return( PSA_ERROR_INVALID_SIGNATURE );
2369 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2370 return( PSA_ERROR_INVALID_SIGNATURE );
2371 return( PSA_SUCCESS );
2372}
2373
Gilles Peskine0a749c82019-11-28 19:33:58 +01002374psa_status_t psa_hash_compute( psa_algorithm_t alg,
2375 const uint8_t *input, size_t input_length,
2376 uint8_t *hash, size_t hash_size,
2377 size_t *hash_length )
2378{
2379 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2381
2382 *hash_length = hash_size;
2383 status = psa_hash_setup( &operation, alg );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
2386 status = psa_hash_update( &operation, input, input_length );
2387 if( status != PSA_SUCCESS )
2388 goto exit;
2389 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2390 if( status != PSA_SUCCESS )
2391 goto exit;
2392
2393exit:
2394 if( status == PSA_SUCCESS )
2395 status = psa_hash_abort( &operation );
2396 else
2397 psa_hash_abort( &operation );
2398 return( status );
2399}
2400
2401psa_status_t psa_hash_compare( psa_algorithm_t alg,
2402 const uint8_t *input, size_t input_length,
2403 const uint8_t *hash, size_t hash_length )
2404{
2405 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2407
2408 status = psa_hash_setup( &operation, alg );
2409 if( status != PSA_SUCCESS )
2410 goto exit;
2411 status = psa_hash_update( &operation, input, input_length );
2412 if( status != PSA_SUCCESS )
2413 goto exit;
2414 status = psa_hash_verify( &operation, hash, hash_length );
2415 if( status != PSA_SUCCESS )
2416 goto exit;
2417
2418exit:
2419 if( status == PSA_SUCCESS )
2420 status = psa_hash_abort( &operation );
2421 else
2422 psa_hash_abort( &operation );
2423 return( status );
2424}
2425
Gilles Peskineeb35d782019-01-22 17:56:16 +01002426psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2427 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002428{
2429 if( target_operation->alg != 0 )
2430 return( PSA_ERROR_BAD_STATE );
2431
2432 switch( source_operation->alg )
2433 {
2434 case 0:
2435 return( PSA_ERROR_BAD_STATE );
2436#if defined(MBEDTLS_MD2_C)
2437 case PSA_ALG_MD2:
2438 mbedtls_md2_clone( &target_operation->ctx.md2,
2439 &source_operation->ctx.md2 );
2440 break;
2441#endif
2442#if defined(MBEDTLS_MD4_C)
2443 case PSA_ALG_MD4:
2444 mbedtls_md4_clone( &target_operation->ctx.md4,
2445 &source_operation->ctx.md4 );
2446 break;
2447#endif
2448#if defined(MBEDTLS_MD5_C)
2449 case PSA_ALG_MD5:
2450 mbedtls_md5_clone( &target_operation->ctx.md5,
2451 &source_operation->ctx.md5 );
2452 break;
2453#endif
2454#if defined(MBEDTLS_RIPEMD160_C)
2455 case PSA_ALG_RIPEMD160:
2456 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2457 &source_operation->ctx.ripemd160 );
2458 break;
2459#endif
2460#if defined(MBEDTLS_SHA1_C)
2461 case PSA_ALG_SHA_1:
2462 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2463 &source_operation->ctx.sha1 );
2464 break;
2465#endif
2466#if defined(MBEDTLS_SHA256_C)
2467 case PSA_ALG_SHA_224:
2468 case PSA_ALG_SHA_256:
2469 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2470 &source_operation->ctx.sha256 );
2471 break;
2472#endif
2473#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002474#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002475 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002476#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002477 case PSA_ALG_SHA_512:
2478 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2479 &source_operation->ctx.sha512 );
2480 break;
2481#endif
2482 default:
2483 return( PSA_ERROR_NOT_SUPPORTED );
2484 }
2485
2486 target_operation->alg = source_operation->alg;
2487 return( PSA_SUCCESS );
2488}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489
2490
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492/* MAC */
2493/****************************************************************/
2494
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002495static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496 psa_algorithm_t alg,
2497 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002498 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002499 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002502 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002504 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002505 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002506
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2508 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002509 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002511 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002512 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 mode = MBEDTLS_MODE_STREAM;
2514 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515 case PSA_ALG_CTR:
2516 mode = MBEDTLS_MODE_CTR;
2517 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002518 case PSA_ALG_CFB:
2519 mode = MBEDTLS_MODE_CFB;
2520 break;
2521 case PSA_ALG_OFB:
2522 mode = MBEDTLS_MODE_OFB;
2523 break;
2524 case PSA_ALG_CBC_NO_PADDING:
2525 mode = MBEDTLS_MODE_CBC;
2526 break;
2527 case PSA_ALG_CBC_PKCS7:
2528 mode = MBEDTLS_MODE_CBC;
2529 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002530 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 mode = MBEDTLS_MODE_CCM;
2532 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002533 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534 mode = MBEDTLS_MODE_GCM;
2535 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002536 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2537 mode = MBEDTLS_MODE_CHACHAPOLY;
2538 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 default:
2540 return( NULL );
2541 }
2542 }
2543 else if( alg == PSA_ALG_CMAC )
2544 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 else
2546 return( NULL );
2547
2548 switch( key_type )
2549 {
2550 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002551 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 break;
2553 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002554 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2555 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002557 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002559 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002560 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2561 * but two-key Triple-DES is functionally three-key Triple-DES
2562 * with K1=K3, so that's how we present it to mbedtls. */
2563 if( key_bits == 128 )
2564 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 break;
2566 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002567 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 break;
2569 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002570 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002572 case PSA_KEY_TYPE_CHACHA20:
2573 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2574 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 default:
2576 return( NULL );
2577 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002578 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002579 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580
Jaeden Amero23bbb752018-06-26 14:16:54 +01002581 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2582 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583}
2584
Gilles Peskinea05219c2018-11-16 16:02:56 +01002585#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002586static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002587{
Gilles Peskine2d277862018-06-18 15:41:12 +02002588 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002589 {
2590 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002598 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002599 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002601 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002602 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002603 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002604 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002605 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002606 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002607 return( 128 );
2608 default:
2609 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002610 }
2611}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002612#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002613
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614/* Initialize the MAC operation structure. Once this function has been
2615 * called, psa_mac_abort can run and will do the right thing. */
2616static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2617 psa_algorithm_t alg )
2618{
2619 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2620
2621 operation->alg = alg;
2622 operation->key_set = 0;
2623 operation->iv_set = 0;
2624 operation->iv_required = 0;
2625 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002626 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627
2628#if defined(MBEDTLS_CMAC_C)
2629 if( alg == PSA_ALG_CMAC )
2630 {
2631 operation->iv_required = 0;
2632 mbedtls_cipher_init( &operation->ctx.cmac );
2633 status = PSA_SUCCESS;
2634 }
2635 else
2636#endif /* MBEDTLS_CMAC_C */
2637#if defined(MBEDTLS_MD_C)
2638 if( PSA_ALG_IS_HMAC( operation->alg ) )
2639 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002640 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2641 operation->ctx.hmac.hash_ctx.alg = 0;
2642 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643 }
2644 else
2645#endif /* MBEDTLS_MD_C */
2646 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002647 if( ! PSA_ALG_IS_MAC( alg ) )
2648 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002649 }
2650
2651 if( status != PSA_SUCCESS )
2652 memset( operation, 0, sizeof( *operation ) );
2653 return( status );
2654}
2655
Gilles Peskine01126fa2018-07-12 17:04:55 +02002656#if defined(MBEDTLS_MD_C)
2657static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2658{
Gilles Peskine3f108122018-12-07 18:14:53 +01002659 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002660 return( psa_hash_abort( &hmac->hash_ctx ) );
2661}
2662#endif /* MBEDTLS_MD_C */
2663
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2665{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002666 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002668 /* The object has (apparently) been initialized but it is not
2669 * in use. It's ok to call abort on such an object, and there's
2670 * nothing to do. */
2671 return( PSA_SUCCESS );
2672 }
2673 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002675 if( operation->alg == PSA_ALG_CMAC )
2676 {
2677 mbedtls_cipher_free( &operation->ctx.cmac );
2678 }
2679 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 if( PSA_ALG_IS_HMAC( operation->alg ) )
2683 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002684 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002685 }
2686 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002688 {
2689 /* Sanity check (shouldn't happen: operation->alg should
2690 * always have been initialized to a valid value). */
2691 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692 }
Moran Peker41deec42018-04-04 15:43:05 +03002693
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 operation->alg = 0;
2695 operation->key_set = 0;
2696 operation->iv_set = 0;
2697 operation->iv_required = 0;
2698 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002699 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002700
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002702
2703bad_state:
2704 /* If abort is called on an uninitialized object, we can't trust
2705 * anything. Wipe the object in case it contains confidential data.
2706 * This may result in a memory leak if a pointer gets overwritten,
2707 * but it's too late to do anything about this. */
2708 memset( operation, 0, sizeof( *operation ) );
2709 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710}
2711
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002712#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002713static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002714 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002715 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716 const mbedtls_cipher_info_t *cipher_info )
2717{
Janos Follath24eed8d2019-11-22 13:21:35 +00002718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719
2720 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721
2722 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2723 if( ret != 0 )
2724 return( ret );
2725
2726 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2727 slot->data.raw.data,
2728 key_bits );
2729 return( ret );
2730}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002731#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732
Gilles Peskine248051a2018-06-20 16:09:38 +02002733#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002734static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2735 const uint8_t *key,
2736 size_t key_length,
2737 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002738{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002739 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002741 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002742 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002743 psa_status_t status;
2744
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002745 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2746 * overflow below. This should never trigger if the hash algorithm
2747 * is implemented correctly. */
2748 /* The size checks against the ipad and opad buffers cannot be written
2749 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2750 * because that triggers -Wlogical-op on GCC 7.3. */
2751 if( block_size > sizeof( ipad ) )
2752 return( PSA_ERROR_NOT_SUPPORTED );
2753 if( block_size > sizeof( hmac->opad ) )
2754 return( PSA_ERROR_NOT_SUPPORTED );
2755 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 return( PSA_ERROR_NOT_SUPPORTED );
2757
Gilles Peskined223b522018-06-11 18:12:58 +02002758 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002759 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002760 status = psa_hash_compute( hash_alg, key, key_length,
2761 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002763 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002764 }
Gilles Peskine96889972018-07-12 17:07:03 +02002765 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2766 * but it is permitted. It is common when HMAC is used in HKDF, for
2767 * example. Don't call `memcpy` in the 0-length because `key` could be
2768 * an invalid pointer which would make the behavior undefined. */
2769 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002771
Gilles Peskined223b522018-06-11 18:12:58 +02002772 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2773 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002774 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002775 ipad[i] ^= 0x36;
2776 memset( ipad + key_length, 0x36, block_size - key_length );
2777
2778 /* Copy the key material from ipad to opad, flipping the requisite bits,
2779 * and filling the rest of opad with the requisite constant. */
2780 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002781 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2782 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002783
Gilles Peskine01126fa2018-07-12 17:04:55 +02002784 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002786 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002787
Gilles Peskine01126fa2018-07-12 17:04:55 +02002788 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002789
2790cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002791 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002792
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002793 return( status );
2794}
Gilles Peskine248051a2018-06-20 16:09:38 +02002795#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002796
Gilles Peskine89167cb2018-07-08 20:12:23 +02002797static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002798 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002799 psa_algorithm_t alg,
2800 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002803 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002804 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002805 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002806 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002807 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002808 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002810 /* A context must be freshly initialized before it can be set up. */
2811 if( operation->alg != 0 )
2812 {
2813 return( PSA_ERROR_BAD_STATE );
2814 }
2815
Gilles Peskined911eb72018-08-14 15:18:45 +02002816 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002817 if( status != PSA_SUCCESS )
2818 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002819 if( is_sign )
2820 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821
Gilles Peskine28f8f302019-07-24 13:30:31 +02002822 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002823 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002824 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002825 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002826
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002828 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002829 {
2830 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002831 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002832 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002834 if( cipher_info == NULL )
2835 {
2836 status = PSA_ERROR_NOT_SUPPORTED;
2837 goto exit;
2838 }
2839 operation->mac_size = cipher_info->block_size;
2840 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2841 status = mbedtls_to_psa_error( ret );
2842 }
2843 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002846 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002847 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002848 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002849 if( hash_alg == 0 )
2850 {
2851 status = PSA_ERROR_NOT_SUPPORTED;
2852 goto exit;
2853 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002854
2855 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2856 /* Sanity check. This shouldn't fail on a valid configuration. */
2857 if( operation->mac_size == 0 ||
2858 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2859 {
2860 status = PSA_ERROR_NOT_SUPPORTED;
2861 goto exit;
2862 }
2863
Gilles Peskine8e338702019-07-30 20:06:31 +02002864 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002865 {
2866 status = PSA_ERROR_INVALID_ARGUMENT;
2867 goto exit;
2868 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002869
Gilles Peskine01126fa2018-07-12 17:04:55 +02002870 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2871 slot->data.raw.data,
2872 slot->data.raw.bytes,
2873 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002874 }
2875 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002877 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002878 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002881
Gilles Peskined911eb72018-08-14 15:18:45 +02002882 if( truncated == 0 )
2883 {
2884 /* The "normal" case: untruncated algorithm. Nothing to do. */
2885 }
2886 else if( truncated < 4 )
2887 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002888 /* A very short MAC is too short for security since it can be
2889 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2890 * so we make this our minimum, even though 32 bits is still
2891 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002892 status = PSA_ERROR_NOT_SUPPORTED;
2893 }
2894 else if( truncated > operation->mac_size )
2895 {
2896 /* It's impossible to "truncate" to a larger length. */
2897 status = PSA_ERROR_INVALID_ARGUMENT;
2898 }
2899 else
2900 operation->mac_size = truncated;
2901
Gilles Peskinefbfac682018-07-08 20:51:54 +02002902exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002905 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907 else
2908 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002909 operation->key_set = 1;
2910 }
2911 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912}
2913
Gilles Peskine89167cb2018-07-08 20:12:23 +02002914psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002915 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002916 psa_algorithm_t alg )
2917{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002918 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002919}
2920
2921psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002922 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002923 psa_algorithm_t alg )
2924{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002925 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002926}
2927
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2929 const uint8_t *input,
2930 size_t input_length )
2931{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002932 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002934 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002936 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937 operation->has_input = 1;
2938
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002940 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002941 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002942 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2943 input, input_length );
2944 status = mbedtls_to_psa_error( ret );
2945 }
2946 else
2947#endif /* MBEDTLS_CMAC_C */
2948#if defined(MBEDTLS_MD_C)
2949 if( PSA_ALG_IS_HMAC( operation->alg ) )
2950 {
2951 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2952 input_length );
2953 }
2954 else
2955#endif /* MBEDTLS_MD_C */
2956 {
2957 /* This shouldn't happen if `operation` was initialized by
2958 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002959 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002960 }
2961
Gilles Peskinefbfac682018-07-08 20:51:54 +02002962 if( status != PSA_SUCCESS )
2963 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002964 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965}
2966
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967#if defined(MBEDTLS_MD_C)
2968static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2969 uint8_t *mac,
2970 size_t mac_size )
2971{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002972 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002973 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2974 size_t hash_size = 0;
2975 size_t block_size = psa_get_hash_block_size( hash_alg );
2976 psa_status_t status;
2977
Gilles Peskine01126fa2018-07-12 17:04:55 +02002978 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2979 if( status != PSA_SUCCESS )
2980 return( status );
2981 /* From here on, tmp needs to be wiped. */
2982
2983 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2984 if( status != PSA_SUCCESS )
2985 goto exit;
2986
2987 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2988 if( status != PSA_SUCCESS )
2989 goto exit;
2990
2991 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2992 if( status != PSA_SUCCESS )
2993 goto exit;
2994
Gilles Peskined911eb72018-08-14 15:18:45 +02002995 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2996 if( status != PSA_SUCCESS )
2997 goto exit;
2998
2999 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003000
3001exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003002 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003003 return( status );
3004}
3005#endif /* MBEDTLS_MD_C */
3006
mohammad16036df908f2018-04-02 08:34:15 -07003007static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003008 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003009 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003011 if( ! operation->key_set )
3012 return( PSA_ERROR_BAD_STATE );
3013 if( operation->iv_required && ! operation->iv_set )
3014 return( PSA_ERROR_BAD_STATE );
3015
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 if( mac_size < operation->mac_size )
3017 return( PSA_ERROR_BUFFER_TOO_SMALL );
3018
Gilles Peskine8c9def32018-02-08 10:02:12 +01003019#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003020 if( operation->alg == PSA_ALG_CMAC )
3021 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003022 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3023 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3024 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003025 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003026 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003027 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 else
3030#endif /* MBEDTLS_CMAC_C */
3031#if defined(MBEDTLS_MD_C)
3032 if( PSA_ALG_IS_HMAC( operation->alg ) )
3033 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003034 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003035 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003036 }
3037 else
3038#endif /* MBEDTLS_MD_C */
3039 {
3040 /* This shouldn't happen if `operation` was initialized by
3041 * a setup function. */
3042 return( PSA_ERROR_BAD_STATE );
3043 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003044}
3045
Gilles Peskineacd4be32018-07-08 19:56:25 +02003046psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3047 uint8_t *mac,
3048 size_t mac_size,
3049 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003050{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003051 psa_status_t status;
3052
Jaeden Amero252ef282019-02-15 14:05:35 +00003053 if( operation->alg == 0 )
3054 {
3055 return( PSA_ERROR_BAD_STATE );
3056 }
3057
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003058 /* Fill the output buffer with something that isn't a valid mac
3059 * (barring an attack on the mac and deliberately-crafted input),
3060 * in case the caller doesn't check the return status properly. */
3061 *mac_length = mac_size;
3062 /* If mac_size is 0 then mac may be NULL and then the
3063 * call to memset would have undefined behavior. */
3064 if( mac_size != 0 )
3065 memset( mac, '!', mac_size );
3066
Gilles Peskine89167cb2018-07-08 20:12:23 +02003067 if( ! operation->is_sign )
3068 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003069 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003070 }
mohammad16036df908f2018-04-02 08:34:15 -07003071
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003072 status = psa_mac_finish_internal( operation, mac, mac_size );
3073
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003074 if( status == PSA_SUCCESS )
3075 {
3076 status = psa_mac_abort( operation );
3077 if( status == PSA_SUCCESS )
3078 *mac_length = operation->mac_size;
3079 else
3080 memset( mac, '!', mac_size );
3081 }
3082 else
3083 psa_mac_abort( operation );
3084 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003085}
3086
Gilles Peskineacd4be32018-07-08 19:56:25 +02003087psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3088 const uint8_t *mac,
3089 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003090{
Gilles Peskine828ed142018-06-18 23:25:51 +02003091 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003092 psa_status_t status;
3093
Jaeden Amero252ef282019-02-15 14:05:35 +00003094 if( operation->alg == 0 )
3095 {
3096 return( PSA_ERROR_BAD_STATE );
3097 }
3098
Gilles Peskine89167cb2018-07-08 20:12:23 +02003099 if( operation->is_sign )
3100 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003101 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003102 }
3103 if( operation->mac_size != mac_length )
3104 {
3105 status = PSA_ERROR_INVALID_SIGNATURE;
3106 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003107 }
mohammad16036df908f2018-04-02 08:34:15 -07003108
3109 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003110 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003111 if( status != PSA_SUCCESS )
3112 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003113
3114 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3115 status = PSA_ERROR_INVALID_SIGNATURE;
3116
3117cleanup:
3118 if( status == PSA_SUCCESS )
3119 status = psa_mac_abort( operation );
3120 else
3121 psa_mac_abort( operation );
3122
Gilles Peskine3f108122018-12-07 18:14:53 +01003123 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003124
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003125 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003126}
3127
3128
Gilles Peskine20035e32018-02-03 22:44:14 +01003129
Gilles Peskine20035e32018-02-03 22:44:14 +01003130/****************************************************************/
3131/* Asymmetric cryptography */
3132/****************************************************************/
3133
Gilles Peskine2b450e32018-06-27 15:42:46 +02003134#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003135/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003136 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003137static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3138 size_t hash_length,
3139 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003140{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003141 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003142 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003143 *md_alg = mbedtls_md_get_type( md_info );
3144
3145 /* The Mbed TLS RSA module uses an unsigned int for hash length
3146 * parameters. Validate that it fits so that we don't risk an
3147 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003148#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003149 if( hash_length > UINT_MAX )
3150 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003151#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003152
3153#if defined(MBEDTLS_PKCS1_V15)
3154 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3155 * must be correct. */
3156 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3157 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003158 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003159 if( md_info == NULL )
3160 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003161 if( mbedtls_md_get_size( md_info ) != hash_length )
3162 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003163 }
3164#endif /* MBEDTLS_PKCS1_V15 */
3165
3166#if defined(MBEDTLS_PKCS1_V21)
3167 /* PSS requires a hash internally. */
3168 if( PSA_ALG_IS_RSA_PSS( alg ) )
3169 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003170 if( md_info == NULL )
3171 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003172 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003173#endif /* MBEDTLS_PKCS1_V21 */
3174
Gilles Peskine61b91d42018-06-08 16:09:36 +02003175 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003176}
3177
Gilles Peskine2b450e32018-06-27 15:42:46 +02003178static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3179 psa_algorithm_t alg,
3180 const uint8_t *hash,
3181 size_t hash_length,
3182 uint8_t *signature,
3183 size_t signature_size,
3184 size_t *signature_length )
3185{
3186 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003188 mbedtls_md_type_t md_alg;
3189
3190 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3191 if( status != PSA_SUCCESS )
3192 return( status );
3193
Gilles Peskine630a18a2018-06-29 17:49:35 +02003194 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003195 return( PSA_ERROR_BUFFER_TOO_SMALL );
3196
3197#if defined(MBEDTLS_PKCS1_V15)
3198 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3199 {
3200 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3201 MBEDTLS_MD_NONE );
3202 ret = mbedtls_rsa_pkcs1_sign( rsa,
3203 mbedtls_ctr_drbg_random,
3204 &global_data.ctr_drbg,
3205 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003206 md_alg,
3207 (unsigned int) hash_length,
3208 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003209 signature );
3210 }
3211 else
3212#endif /* MBEDTLS_PKCS1_V15 */
3213#if defined(MBEDTLS_PKCS1_V21)
3214 if( PSA_ALG_IS_RSA_PSS( alg ) )
3215 {
3216 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3217 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3218 mbedtls_ctr_drbg_random,
3219 &global_data.ctr_drbg,
3220 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003221 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003222 (unsigned int) hash_length,
3223 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003224 signature );
3225 }
3226 else
3227#endif /* MBEDTLS_PKCS1_V21 */
3228 {
3229 return( PSA_ERROR_INVALID_ARGUMENT );
3230 }
3231
3232 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003233 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003234 return( mbedtls_to_psa_error( ret ) );
3235}
3236
3237static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3238 psa_algorithm_t alg,
3239 const uint8_t *hash,
3240 size_t hash_length,
3241 const uint8_t *signature,
3242 size_t signature_length )
3243{
3244 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003246 mbedtls_md_type_t md_alg;
3247
3248 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3249 if( status != PSA_SUCCESS )
3250 return( status );
3251
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003252 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3253 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003254
3255#if defined(MBEDTLS_PKCS1_V15)
3256 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3257 {
3258 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3259 MBEDTLS_MD_NONE );
3260 ret = mbedtls_rsa_pkcs1_verify( rsa,
3261 mbedtls_ctr_drbg_random,
3262 &global_data.ctr_drbg,
3263 MBEDTLS_RSA_PUBLIC,
3264 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003265 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266 hash,
3267 signature );
3268 }
3269 else
3270#endif /* MBEDTLS_PKCS1_V15 */
3271#if defined(MBEDTLS_PKCS1_V21)
3272 if( PSA_ALG_IS_RSA_PSS( alg ) )
3273 {
3274 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3275 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3276 mbedtls_ctr_drbg_random,
3277 &global_data.ctr_drbg,
3278 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003279 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003280 (unsigned int) hash_length,
3281 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003282 signature );
3283 }
3284 else
3285#endif /* MBEDTLS_PKCS1_V21 */
3286 {
3287 return( PSA_ERROR_INVALID_ARGUMENT );
3288 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003289
3290 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3291 * the rest of the signature is invalid". This has little use in
3292 * practice and PSA doesn't report this distinction. */
3293 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003295 return( mbedtls_to_psa_error( ret ) );
3296}
3297#endif /* MBEDTLS_RSA_C */
3298
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003299#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003300/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3301 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3302 * (even though these functions don't modify it). */
3303static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3304 psa_algorithm_t alg,
3305 const uint8_t *hash,
3306 size_t hash_length,
3307 uint8_t *signature,
3308 size_t signature_size,
3309 size_t *signature_length )
3310{
Janos Follath24eed8d2019-11-22 13:21:35 +00003311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003312 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003313 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003314 mbedtls_mpi_init( &r );
3315 mbedtls_mpi_init( &s );
3316
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003317 if( signature_size < 2 * curve_bytes )
3318 {
3319 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3320 goto cleanup;
3321 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003322
Gilles Peskinea05219c2018-11-16 16:02:56 +01003323#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003324 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3325 {
3326 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3327 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3328 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003329 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3330 &ecp->d, hash,
3331 hash_length, md_alg,
3332 mbedtls_ctr_drbg_random,
3333 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003334 }
3335 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003336#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003337 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003338 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003339 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3340 hash, hash_length,
3341 mbedtls_ctr_drbg_random,
3342 &global_data.ctr_drbg ) );
3343 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003344
3345 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3346 signature,
3347 curve_bytes ) );
3348 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3349 signature + curve_bytes,
3350 curve_bytes ) );
3351
3352cleanup:
3353 mbedtls_mpi_free( &r );
3354 mbedtls_mpi_free( &s );
3355 if( ret == 0 )
3356 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003357 return( mbedtls_to_psa_error( ret ) );
3358}
3359
3360static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3361 const uint8_t *hash,
3362 size_t hash_length,
3363 const uint8_t *signature,
3364 size_t signature_length )
3365{
Janos Follath24eed8d2019-11-22 13:21:35 +00003366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003367 mbedtls_mpi r, s;
3368 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3369 mbedtls_mpi_init( &r );
3370 mbedtls_mpi_init( &s );
3371
3372 if( signature_length != 2 * curve_bytes )
3373 return( PSA_ERROR_INVALID_SIGNATURE );
3374
3375 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3376 signature,
3377 curve_bytes ) );
3378 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3379 signature + curve_bytes,
3380 curve_bytes ) );
3381
3382 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3383 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003384
3385cleanup:
3386 mbedtls_mpi_free( &r );
3387 mbedtls_mpi_free( &s );
3388 return( mbedtls_to_psa_error( ret ) );
3389}
3390#endif /* MBEDTLS_ECDSA_C */
3391
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003392psa_status_t psa_sign_hash( psa_key_handle_t handle,
3393 psa_algorithm_t alg,
3394 const uint8_t *hash,
3395 size_t hash_length,
3396 uint8_t *signature,
3397 size_t signature_size,
3398 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003400 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003401 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003402#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3403 const psa_drv_se_t *drv;
3404 psa_drv_se_context_t *drv_context;
3405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406
3407 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003408 /* Immediately reject a zero-length signature buffer. This guarantees
3409 * that signature must be a valid pointer. (On the other hand, the hash
3410 * buffer can in principle be empty since it doesn't actually have
3411 * to be a hash.) */
3412 if( signature_size == 0 )
3413 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003415 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003416 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003418 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003419 {
3420 status = PSA_ERROR_INVALID_ARGUMENT;
3421 goto exit;
3422 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003423
Gilles Peskineedc64242019-08-07 21:05:07 +02003424#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3425 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3426 {
3427 if( drv->asymmetric == NULL ||
3428 drv->asymmetric->p_sign == NULL )
3429 {
3430 status = PSA_ERROR_NOT_SUPPORTED;
3431 goto exit;
3432 }
3433 status = drv->asymmetric->p_sign( drv_context,
3434 slot->data.se.slot_number,
3435 alg,
3436 hash, hash_length,
3437 signature, signature_size,
3438 signature_length );
3439 }
3440 else
3441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003442#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003443 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003444 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003445 status = psa_rsa_sign( slot->data.rsa,
3446 alg,
3447 hash, hash_length,
3448 signature, signature_size,
3449 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003450 }
3451 else
3452#endif /* defined(MBEDTLS_RSA_C) */
3453#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003454 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003455 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003456#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003457 if(
3458#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3459 PSA_ALG_IS_ECDSA( alg )
3460#else
3461 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3462#endif
3463 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003464 status = psa_ecdsa_sign( slot->data.ecp,
3465 alg,
3466 hash, hash_length,
3467 signature, signature_size,
3468 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003469 else
3470#endif /* defined(MBEDTLS_ECDSA_C) */
3471 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003472 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003473 }
itayzafrir5c753392018-05-08 11:18:38 +03003474 }
3475 else
3476#endif /* defined(MBEDTLS_ECP_C) */
3477 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003478 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003479 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003480
3481exit:
3482 /* Fill the unused part of the output buffer (the whole buffer on error,
3483 * the trailing part on success) with something that isn't a valid mac
3484 * (barring an attack on the mac and deliberately-crafted input),
3485 * in case the caller doesn't check the return status properly. */
3486 if( status == PSA_SUCCESS )
3487 memset( signature + *signature_length, '!',
3488 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003489 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003490 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003491 /* If signature_size is 0 then we have nothing to do. We must not call
3492 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003493 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003494}
3495
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003496psa_status_t psa_verify_hash( psa_key_handle_t handle,
3497 psa_algorithm_t alg,
3498 const uint8_t *hash,
3499 size_t hash_length,
3500 const uint8_t *signature,
3501 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003502{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003503 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003504 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003505#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3506 const psa_drv_se_t *drv;
3507 psa_drv_se_context_t *drv_context;
3508#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003509
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003510 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003511 if( status != PSA_SUCCESS )
3512 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003513
Gilles Peskineedc64242019-08-07 21:05:07 +02003514#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3515 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3516 {
3517 if( drv->asymmetric == NULL ||
3518 drv->asymmetric->p_verify == NULL )
3519 return( PSA_ERROR_NOT_SUPPORTED );
3520 return( drv->asymmetric->p_verify( drv_context,
3521 slot->data.se.slot_number,
3522 alg,
3523 hash, hash_length,
3524 signature, signature_length ) );
3525 }
3526 else
3527#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003528#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003529 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003530 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003531 return( psa_rsa_verify( slot->data.rsa,
3532 alg,
3533 hash, hash_length,
3534 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535 }
3536 else
3537#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003538#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003539 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003540 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541#if defined(MBEDTLS_ECDSA_C)
3542 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003543 return( psa_ecdsa_verify( slot->data.ecp,
3544 hash, hash_length,
3545 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003546 else
3547#endif /* defined(MBEDTLS_ECDSA_C) */
3548 {
3549 return( PSA_ERROR_INVALID_ARGUMENT );
3550 }
itayzafrir5c753392018-05-08 11:18:38 +03003551 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003552 else
3553#endif /* defined(MBEDTLS_ECP_C) */
3554 {
3555 return( PSA_ERROR_NOT_SUPPORTED );
3556 }
3557}
3558
Gilles Peskine072ac562018-06-30 00:21:29 +02003559#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3560static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3561 mbedtls_rsa_context *rsa )
3562{
3563 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3564 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3565 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3566 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3567}
3568#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3569
Gilles Peskinec5487a82018-12-03 18:08:14 +01003570psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003571 psa_algorithm_t alg,
3572 const uint8_t *input,
3573 size_t input_length,
3574 const uint8_t *salt,
3575 size_t salt_length,
3576 uint8_t *output,
3577 size_t output_size,
3578 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003580 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003581 psa_status_t status;
3582
Darryl Green5cc689a2018-07-24 15:34:10 +01003583 (void) input;
3584 (void) input_length;
3585 (void) salt;
3586 (void) output;
3587 (void) output_size;
3588
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003589 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003591 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3592 return( PSA_ERROR_INVALID_ARGUMENT );
3593
Gilles Peskine28f8f302019-07-24 13:30:31 +02003594 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003595 if( status != PSA_SUCCESS )
3596 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003597 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3598 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600
3601#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003602 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603 {
3604 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003606 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003607 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003609 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003611 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3612 mbedtls_ctr_drbg_random,
3613 &global_data.ctr_drbg,
3614 MBEDTLS_RSA_PUBLIC,
3615 input_length,
3616 input,
3617 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618 }
3619 else
3620#endif /* MBEDTLS_PKCS1_V15 */
3621#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003622 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003624 psa_rsa_oaep_set_padding_mode( alg, rsa );
3625 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3626 mbedtls_ctr_drbg_random,
3627 &global_data.ctr_drbg,
3628 MBEDTLS_RSA_PUBLIC,
3629 salt, salt_length,
3630 input_length,
3631 input,
3632 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 }
3634 else
3635#endif /* MBEDTLS_PKCS1_V21 */
3636 {
3637 return( PSA_ERROR_INVALID_ARGUMENT );
3638 }
3639 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003640 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 return( mbedtls_to_psa_error( ret ) );
3642 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003644#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645 {
3646 return( PSA_ERROR_NOT_SUPPORTED );
3647 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003648}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649
Gilles Peskinec5487a82018-12-03 18:08:14 +01003650psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003651 psa_algorithm_t alg,
3652 const uint8_t *input,
3653 size_t input_length,
3654 const uint8_t *salt,
3655 size_t salt_length,
3656 uint8_t *output,
3657 size_t output_size,
3658 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003660 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003661 psa_status_t status;
3662
Darryl Green5cc689a2018-07-24 15:34:10 +01003663 (void) input;
3664 (void) input_length;
3665 (void) salt;
3666 (void) output;
3667 (void) output_size;
3668
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003669 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003671 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3672 return( PSA_ERROR_INVALID_ARGUMENT );
3673
Gilles Peskine28f8f302019-07-24 13:30:31 +02003674 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003675 if( status != PSA_SUCCESS )
3676 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003677 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678 return( PSA_ERROR_INVALID_ARGUMENT );
3679
3680#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003681 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682 {
3683 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685
Gilles Peskine630a18a2018-06-29 17:49:35 +02003686 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687 return( PSA_ERROR_INVALID_ARGUMENT );
3688
3689#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003690 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003691 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003692 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3693 mbedtls_ctr_drbg_random,
3694 &global_data.ctr_drbg,
3695 MBEDTLS_RSA_PRIVATE,
3696 output_length,
3697 input,
3698 output,
3699 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003700 }
3701 else
3702#endif /* MBEDTLS_PKCS1_V15 */
3703#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003704 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003706 psa_rsa_oaep_set_padding_mode( alg, rsa );
3707 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3708 mbedtls_ctr_drbg_random,
3709 &global_data.ctr_drbg,
3710 MBEDTLS_RSA_PRIVATE,
3711 salt, salt_length,
3712 output_length,
3713 input,
3714 output,
3715 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003716 }
3717 else
3718#endif /* MBEDTLS_PKCS1_V21 */
3719 {
3720 return( PSA_ERROR_INVALID_ARGUMENT );
3721 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003722
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 return( mbedtls_to_psa_error( ret ) );
3724 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003726#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727 {
3728 return( PSA_ERROR_NOT_SUPPORTED );
3729 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003730}
Gilles Peskine20035e32018-02-03 22:44:14 +01003731
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003732
3733
mohammad1603503973b2018-03-12 15:59:30 +02003734/****************************************************************/
3735/* Symmetric cryptography */
3736/****************************************************************/
3737
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003738/* Initialize the cipher operation structure. Once this function has been
3739 * called, psa_cipher_abort can run and will do the right thing. */
3740static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3741 psa_algorithm_t alg )
3742{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003743 if( ! PSA_ALG_IS_CIPHER( alg ) )
3744 {
3745 memset( operation, 0, sizeof( *operation ) );
3746 return( PSA_ERROR_INVALID_ARGUMENT );
3747 }
3748
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003749 operation->alg = alg;
3750 operation->key_set = 0;
3751 operation->iv_set = 0;
3752 operation->iv_required = 1;
3753 operation->iv_size = 0;
3754 operation->block_size = 0;
3755 mbedtls_cipher_init( &operation->ctx.cipher );
3756 return( PSA_SUCCESS );
3757}
3758
Gilles Peskinee553c652018-06-04 16:22:46 +02003759static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003760 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003761 psa_algorithm_t alg,
3762 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003763{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003764 int ret = 0;
3765 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003766 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003767 size_t key_bits;
3768 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003769 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3770 PSA_KEY_USAGE_ENCRYPT :
3771 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003772
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003773 /* A context must be freshly initialized before it can be set up. */
3774 if( operation->alg != 0 )
3775 {
3776 return( PSA_ERROR_BAD_STATE );
3777 }
3778
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003779 status = psa_cipher_init( operation, alg );
3780 if( status != PSA_SUCCESS )
3781 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003782
Gilles Peskine28f8f302019-07-24 13:30:31 +02003783 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003784 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003785 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003786 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003787
Gilles Peskine8e338702019-07-30 20:06:31 +02003788 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003789 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003790 {
3791 status = PSA_ERROR_NOT_SUPPORTED;
3792 goto exit;
3793 }
mohammad1603503973b2018-03-12 15:59:30 +02003794
mohammad1603503973b2018-03-12 15:59:30 +02003795 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003796 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003797 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003798
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003799#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003800 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003801 {
3802 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003803 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003804 memcpy( keys, slot->data.raw.data, 16 );
3805 memcpy( keys + 16, slot->data.raw.data, 8 );
3806 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3807 keys,
3808 192, cipher_operation );
3809 }
3810 else
3811#endif
3812 {
3813 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3814 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003815 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003816 }
Moran Peker41deec42018-04-04 15:43:05 +03003817 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003818 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003819
mohammad16038481e742018-03-18 13:57:31 +02003820#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003821 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003822 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003823 case PSA_ALG_CBC_NO_PADDING:
3824 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3825 MBEDTLS_PADDING_NONE );
3826 break;
3827 case PSA_ALG_CBC_PKCS7:
3828 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3829 MBEDTLS_PADDING_PKCS7 );
3830 break;
3831 default:
3832 /* The algorithm doesn't involve padding. */
3833 ret = 0;
3834 break;
3835 }
3836 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003837 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003838#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3839
mohammad1603503973b2018-03-12 15:59:30 +02003840 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003841 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003842 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003843 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003844 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003845 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003846 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003847#if defined(MBEDTLS_CHACHA20_C)
3848 else
3849 if( alg == PSA_ALG_CHACHA20 )
3850 operation->iv_size = 12;
3851#endif
mohammad1603503973b2018-03-12 15:59:30 +02003852
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003853exit:
3854 if( status == 0 )
3855 status = mbedtls_to_psa_error( ret );
3856 if( status != 0 )
3857 psa_cipher_abort( operation );
3858 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003859}
3860
Gilles Peskinefe119512018-07-08 21:39:34 +02003861psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003862 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003863 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003864{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003865 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003866}
3867
Gilles Peskinefe119512018-07-08 21:39:34 +02003868psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003869 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003870 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003871{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003872 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003873}
3874
Gilles Peskinefe119512018-07-08 21:39:34 +02003875psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003876 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003877 size_t iv_size,
3878 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003879{
itayzafrir534bd7c2018-08-02 13:56:32 +03003880 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003882 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003883 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003884 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 }
Moran Peker41deec42018-04-04 15:43:05 +03003886 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003887 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003888 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003889 goto exit;
3890 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003891 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3892 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003893 if( ret != 0 )
3894 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003896 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003897 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003898
mohammad16038481e742018-03-18 13:57:31 +02003899 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003900 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003901
Moran Peker395db872018-05-31 14:07:14 +03003902exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003903 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003904 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003905 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003906}
3907
Gilles Peskinefe119512018-07-08 21:39:34 +02003908psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003909 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003910 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003911{
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003914 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003916 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003917 }
Moran Pekera28258c2018-05-29 16:25:04 +03003918 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003919 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003920 status = PSA_ERROR_INVALID_ARGUMENT;
3921 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003922 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003923 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3924 status = mbedtls_to_psa_error( ret );
3925exit:
3926 if( status == PSA_SUCCESS )
3927 operation->iv_set = 1;
3928 else
mohammad1603503973b2018-03-12 15:59:30 +02003929 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003930 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003931}
3932
Gilles Peskinee553c652018-06-04 16:22:46 +02003933psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3934 const uint8_t *input,
3935 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003936 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003937 size_t output_size,
3938 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003939{
itayzafrir534bd7c2018-08-02 13:56:32 +03003940 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003942 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003943
3944 if( operation->alg == 0 )
3945 {
3946 return( PSA_ERROR_BAD_STATE );
3947 }
3948
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003949 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003950 {
3951 /* Take the unprocessed partial block left over from previous
3952 * update calls, if any, plus the input to this call. Remove
3953 * the last partial block, if any. You get the data that will be
3954 * output in this call. */
3955 expected_output_size =
3956 ( operation->ctx.cipher.unprocessed_len + input_length )
3957 / operation->block_size * operation->block_size;
3958 }
3959 else
3960 {
3961 expected_output_size = input_length;
3962 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003963
Gilles Peskine89d789c2018-06-04 17:17:16 +02003964 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003965 {
3966 status = PSA_ERROR_BUFFER_TOO_SMALL;
3967 goto exit;
3968 }
mohammad160382759612018-03-12 18:16:40 +02003969
mohammad1603503973b2018-03-12 15:59:30 +02003970 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003971 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003972 status = mbedtls_to_psa_error( ret );
3973exit:
3974 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003975 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003976 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003977}
3978
Gilles Peskinee553c652018-06-04 16:22:46 +02003979psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3980 uint8_t *output,
3981 size_t output_size,
3982 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003983{
David Saadab4ecc272019-02-14 13:48:10 +02003984 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003985 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003986 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003987
mohammad1603503973b2018-03-12 15:59:30 +02003988 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003989 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003990 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003991 }
3992 if( operation->iv_required && ! operation->iv_set )
3993 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003994 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003995 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003996
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003997 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003998 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3999 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004000 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004001 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004002 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004003 }
4004
Janos Follath315b51c2018-07-09 16:04:51 +01004005 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4006 temp_output_buffer,
4007 output_length );
4008 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004009 {
Janos Follath315b51c2018-07-09 16:04:51 +01004010 status = mbedtls_to_psa_error( cipher_ret );
4011 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004012 }
Janos Follath315b51c2018-07-09 16:04:51 +01004013
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004014 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004015 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004016 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004017 memcpy( output, temp_output_buffer, *output_length );
4018 else
4019 {
Janos Follath315b51c2018-07-09 16:04:51 +01004020 status = PSA_ERROR_BUFFER_TOO_SMALL;
4021 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004022 }
mohammad1603503973b2018-03-12 15:59:30 +02004023
Gilles Peskine3f108122018-12-07 18:14:53 +01004024 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004025 status = psa_cipher_abort( operation );
4026
4027 return( status );
4028
4029error:
4030
4031 *output_length = 0;
4032
Gilles Peskine3f108122018-12-07 18:14:53 +01004033 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004034 (void) psa_cipher_abort( operation );
4035
4036 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004037}
4038
Gilles Peskinee553c652018-06-04 16:22:46 +02004039psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4040{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004041 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004042 {
4043 /* The object has (apparently) been initialized but it is not
4044 * in use. It's ok to call abort on such an object, and there's
4045 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004046 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004047 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004048
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004049 /* Sanity check (shouldn't happen: operation->alg should
4050 * always have been initialized to a valid value). */
4051 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4052 return( PSA_ERROR_BAD_STATE );
4053
mohammad1603503973b2018-03-12 15:59:30 +02004054 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004055
Moran Peker41deec42018-04-04 15:43:05 +03004056 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004057 operation->key_set = 0;
4058 operation->iv_set = 0;
4059 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004060 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004061 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004062
Moran Peker395db872018-05-31 14:07:14 +03004063 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004064}
4065
Gilles Peskinea0655c32018-04-30 17:06:50 +02004066
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004067
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004068
mohammad16035955c982018-04-26 00:53:03 +03004069/****************************************************************/
4070/* AEAD */
4071/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004072
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073typedef struct
4074{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004075 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004076 const mbedtls_cipher_info_t *cipher_info;
4077 union
4078 {
4079#if defined(MBEDTLS_CCM_C)
4080 mbedtls_ccm_context ccm;
4081#endif /* MBEDTLS_CCM_C */
4082#if defined(MBEDTLS_GCM_C)
4083 mbedtls_gcm_context gcm;
4084#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004085#if defined(MBEDTLS_CHACHAPOLY_C)
4086 mbedtls_chachapoly_context chachapoly;
4087#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004089 psa_algorithm_t core_alg;
4090 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004091 uint8_t tag_length;
4092} aead_operation_t;
4093
Gilles Peskine30a9e412019-01-14 18:36:12 +01004094static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004095{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004096 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 {
4098#if defined(MBEDTLS_CCM_C)
4099 case PSA_ALG_CCM:
4100 mbedtls_ccm_free( &operation->ctx.ccm );
4101 break;
4102#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004103#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004104 case PSA_ALG_GCM:
4105 mbedtls_gcm_free( &operation->ctx.gcm );
4106 break;
4107#endif /* MBEDTLS_GCM_C */
4108 }
4109}
4110
4111static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004112 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004113 psa_key_usage_t usage,
4114 psa_algorithm_t alg )
4115{
4116 psa_status_t status;
4117 size_t key_bits;
4118 mbedtls_cipher_id_t cipher_id;
4119
Gilles Peskine28f8f302019-07-24 13:30:31 +02004120 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004121 if( status != PSA_SUCCESS )
4122 return( status );
4123
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004124 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125
4126 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004127 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004128 &cipher_id );
4129 if( operation->cipher_info == NULL )
4130 return( PSA_ERROR_NOT_SUPPORTED );
4131
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004132 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004133 {
4134#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004135 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4136 operation->core_alg = PSA_ALG_CCM;
4137 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004138 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4139 * The call to mbedtls_ccm_encrypt_and_tag or
4140 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004141 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004142 return( PSA_ERROR_INVALID_ARGUMENT );
4143 mbedtls_ccm_init( &operation->ctx.ccm );
4144 status = mbedtls_to_psa_error(
4145 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4146 operation->slot->data.raw.data,
4147 (unsigned int) key_bits ) );
4148 if( status != 0 )
4149 goto cleanup;
4150 break;
4151#endif /* MBEDTLS_CCM_C */
4152
4153#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004154 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4155 operation->core_alg = PSA_ALG_GCM;
4156 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004157 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4158 * The call to mbedtls_gcm_crypt_and_tag or
4159 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004160 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 return( PSA_ERROR_INVALID_ARGUMENT );
4162 mbedtls_gcm_init( &operation->ctx.gcm );
4163 status = mbedtls_to_psa_error(
4164 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4165 operation->slot->data.raw.data,
4166 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004167 if( status != 0 )
4168 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004169 break;
4170#endif /* MBEDTLS_GCM_C */
4171
Gilles Peskine26869f22019-05-06 15:25:00 +02004172#if defined(MBEDTLS_CHACHAPOLY_C)
4173 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4174 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4175 operation->full_tag_length = 16;
4176 /* We only support the default tag length. */
4177 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4178 return( PSA_ERROR_NOT_SUPPORTED );
4179 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4180 status = mbedtls_to_psa_error(
4181 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4182 operation->slot->data.raw.data ) );
4183 if( status != 0 )
4184 goto cleanup;
4185 break;
4186#endif /* MBEDTLS_CHACHAPOLY_C */
4187
Gilles Peskineedf9a652018-08-17 18:11:56 +02004188 default:
4189 return( PSA_ERROR_NOT_SUPPORTED );
4190 }
4191
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004192 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4193 {
4194 status = PSA_ERROR_INVALID_ARGUMENT;
4195 goto cleanup;
4196 }
4197 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004198
Gilles Peskineedf9a652018-08-17 18:11:56 +02004199 return( PSA_SUCCESS );
4200
4201cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004202 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004203 return( status );
4204}
4205
Gilles Peskinec5487a82018-12-03 18:08:14 +01004206psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004207 psa_algorithm_t alg,
4208 const uint8_t *nonce,
4209 size_t nonce_length,
4210 const uint8_t *additional_data,
4211 size_t additional_data_length,
4212 const uint8_t *plaintext,
4213 size_t plaintext_length,
4214 uint8_t *ciphertext,
4215 size_t ciphertext_size,
4216 size_t *ciphertext_length )
4217{
mohammad16035955c982018-04-26 00:53:03 +03004218 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004219 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004220 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004221
mohammad1603f08a5502018-06-03 15:05:47 +03004222 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004223
Gilles Peskinec5487a82018-12-03 18:08:14 +01004224 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004225 if( status != PSA_SUCCESS )
4226 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004227
Gilles Peskineedf9a652018-08-17 18:11:56 +02004228 /* For all currently supported modes, the tag is at the end of the
4229 * ciphertext. */
4230 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4231 {
4232 status = PSA_ERROR_BUFFER_TOO_SMALL;
4233 goto exit;
4234 }
4235 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004236
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004237#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004238 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004239 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004240 status = mbedtls_to_psa_error(
4241 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4242 MBEDTLS_GCM_ENCRYPT,
4243 plaintext_length,
4244 nonce, nonce_length,
4245 additional_data, additional_data_length,
4246 plaintext, ciphertext,
4247 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004248 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004249 else
4250#endif /* MBEDTLS_GCM_C */
4251#if defined(MBEDTLS_CCM_C)
4252 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004253 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004254 status = mbedtls_to_psa_error(
4255 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4256 plaintext_length,
4257 nonce, nonce_length,
4258 additional_data,
4259 additional_data_length,
4260 plaintext, ciphertext,
4261 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004262 }
mohammad16035c8845f2018-05-09 05:40:09 -07004263 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004264#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004265#if defined(MBEDTLS_CHACHAPOLY_C)
4266 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4267 {
4268 if( nonce_length != 12 || operation.tag_length != 16 )
4269 {
4270 status = PSA_ERROR_NOT_SUPPORTED;
4271 goto exit;
4272 }
4273 status = mbedtls_to_psa_error(
4274 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4275 plaintext_length,
4276 nonce,
4277 additional_data,
4278 additional_data_length,
4279 plaintext,
4280 ciphertext,
4281 tag ) );
4282 }
4283 else
4284#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004285 {
mohammad1603554faad2018-06-03 15:07:38 +03004286 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004287 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004288
Gilles Peskineedf9a652018-08-17 18:11:56 +02004289 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4290 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004291
Gilles Peskineedf9a652018-08-17 18:11:56 +02004292exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004293 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004294 if( status == PSA_SUCCESS )
4295 *ciphertext_length = plaintext_length + operation.tag_length;
4296 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004297}
4298
Gilles Peskineee652a32018-06-01 19:23:52 +02004299/* Locate the tag in a ciphertext buffer containing the encrypted data
4300 * followed by the tag. Return the length of the part preceding the tag in
4301 * *plaintext_length. This is the size of the plaintext in modes where
4302 * the encrypted data has the same size as the plaintext, such as
4303 * CCM and GCM. */
4304static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4305 const uint8_t *ciphertext,
4306 size_t ciphertext_length,
4307 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004308 const uint8_t **p_tag )
4309{
4310 size_t payload_length;
4311 if( tag_length > ciphertext_length )
4312 return( PSA_ERROR_INVALID_ARGUMENT );
4313 payload_length = ciphertext_length - tag_length;
4314 if( payload_length > plaintext_size )
4315 return( PSA_ERROR_BUFFER_TOO_SMALL );
4316 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004317 return( PSA_SUCCESS );
4318}
4319
Gilles Peskinec5487a82018-12-03 18:08:14 +01004320psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004321 psa_algorithm_t alg,
4322 const uint8_t *nonce,
4323 size_t nonce_length,
4324 const uint8_t *additional_data,
4325 size_t additional_data_length,
4326 const uint8_t *ciphertext,
4327 size_t ciphertext_length,
4328 uint8_t *plaintext,
4329 size_t plaintext_size,
4330 size_t *plaintext_length )
4331{
mohammad16035955c982018-04-26 00:53:03 +03004332 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004333 aead_operation_t operation;
4334 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004335
Gilles Peskineee652a32018-06-01 19:23:52 +02004336 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004337
Gilles Peskinec5487a82018-12-03 18:08:14 +01004338 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004339 if( status != PSA_SUCCESS )
4340 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004341
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004342 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4343 ciphertext, ciphertext_length,
4344 plaintext_size, &tag );
4345 if( status != PSA_SUCCESS )
4346 goto exit;
4347
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004348#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004349 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004350 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004351 status = mbedtls_to_psa_error(
4352 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4353 ciphertext_length - operation.tag_length,
4354 nonce, nonce_length,
4355 additional_data,
4356 additional_data_length,
4357 tag, operation.tag_length,
4358 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004359 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004360 else
4361#endif /* MBEDTLS_GCM_C */
4362#if defined(MBEDTLS_CCM_C)
4363 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004364 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004365 status = mbedtls_to_psa_error(
4366 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4367 ciphertext_length - operation.tag_length,
4368 nonce, nonce_length,
4369 additional_data,
4370 additional_data_length,
4371 ciphertext, plaintext,
4372 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004373 }
mohammad160339574652018-06-01 04:39:53 -07004374 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004375#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004376#if defined(MBEDTLS_CHACHAPOLY_C)
4377 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4378 {
4379 if( nonce_length != 12 || operation.tag_length != 16 )
4380 {
4381 status = PSA_ERROR_NOT_SUPPORTED;
4382 goto exit;
4383 }
4384 status = mbedtls_to_psa_error(
4385 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4386 ciphertext_length - operation.tag_length,
4387 nonce,
4388 additional_data,
4389 additional_data_length,
4390 tag,
4391 ciphertext,
4392 plaintext ) );
4393 }
4394 else
4395#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004396 {
mohammad1603554faad2018-06-03 15:07:38 +03004397 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004398 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004399
Gilles Peskineedf9a652018-08-17 18:11:56 +02004400 if( status != PSA_SUCCESS && plaintext_size != 0 )
4401 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004402
Gilles Peskineedf9a652018-08-17 18:11:56 +02004403exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004404 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405 if( status == PSA_SUCCESS )
4406 *plaintext_length = ciphertext_length - operation.tag_length;
4407 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004408}
4409
Gilles Peskinea0655c32018-04-30 17:06:50 +02004410
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004411
Gilles Peskine20035e32018-02-03 22:44:14 +01004412/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413/* Generators */
4414/****************************************************************/
4415
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004416#define HKDF_STATE_INIT 0 /* no input yet */
4417#define HKDF_STATE_STARTED 1 /* got salt */
4418#define HKDF_STATE_KEYED 2 /* got key */
4419#define HKDF_STATE_OUTPUT 3 /* output started */
4420
Gilles Peskinecbe66502019-05-16 16:59:18 +02004421static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004423{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4425 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004426 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004428}
4429
4430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004432{
4433 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004435 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004436 {
4437 /* The object has (apparently) been initialized but it is not
4438 * in use. It's ok to call abort on such an object, and there's
4439 * nothing to do. */
4440 }
4441 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004442#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004443 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004444 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 mbedtls_free( operation->ctx.hkdf.info );
4446 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004447 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004448 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004450 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004451 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004452 if( operation->ctx.tls12_prf.seed != NULL )
4453 {
4454 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4455 operation->ctx.tls12_prf.seed_length );
4456 mbedtls_free( operation->ctx.tls12_prf.seed );
4457 }
4458
4459 if( operation->ctx.tls12_prf.label != NULL )
4460 {
4461 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4462 operation->ctx.tls12_prf.label_length );
4463 mbedtls_free( operation->ctx.tls12_prf.label );
4464 }
4465
4466 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4467
4468 /* We leave the fields Ai and output_block to be erased safely by the
4469 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004470 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004471 else
4472#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004473 {
4474 status = PSA_ERROR_BAD_STATE;
4475 }
Janos Follath083036a2019-06-11 10:22:26 +01004476 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004477 return( status );
4478}
4479
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004481 size_t *capacity)
4482{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004484 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004486 return PSA_ERROR_BAD_STATE;
4487 }
4488
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004490 return( PSA_SUCCESS );
4491}
4492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004494 size_t capacity )
4495{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004497 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004500 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004501 return( PSA_SUCCESS );
4502}
4503
Gilles Peskinebef7f142018-07-12 17:22:21 +02004504#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004506 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004507static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004508 psa_algorithm_t hash_alg,
4509 uint8_t *output,
4510 size_t output_length )
4511{
4512 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4513 psa_status_t status;
4514
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004515 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4516 return( PSA_ERROR_BAD_STATE );
4517 hkdf->state = HKDF_STATE_OUTPUT;
4518
Gilles Peskinebef7f142018-07-12 17:22:21 +02004519 while( output_length != 0 )
4520 {
4521 /* Copy what remains of the current block */
4522 uint8_t n = hash_length - hkdf->offset_in_block;
4523 if( n > output_length )
4524 n = (uint8_t) output_length;
4525 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4526 output += n;
4527 output_length -= n;
4528 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004529 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004530 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004531 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004532 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004534 * object was corrupted or if this function is called directly
4535 * inside the library. */
4536 if( hkdf->block_number == 0xff )
4537 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004538
4539 /* We need a new block */
4540 ++hkdf->block_number;
4541 hkdf->offset_in_block = 0;
4542 status = psa_hmac_setup_internal( &hkdf->hmac,
4543 hkdf->prk, hash_length,
4544 hash_alg );
4545 if( status != PSA_SUCCESS )
4546 return( status );
4547 if( hkdf->block_number != 1 )
4548 {
4549 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4550 hkdf->output_block,
4551 hash_length );
4552 if( status != PSA_SUCCESS )
4553 return( status );
4554 }
4555 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4556 hkdf->info,
4557 hkdf->info_length );
4558 if( status != PSA_SUCCESS )
4559 return( status );
4560 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4561 &hkdf->block_number, 1 );
4562 if( status != PSA_SUCCESS )
4563 return( status );
4564 status = psa_hmac_finish_internal( &hkdf->hmac,
4565 hkdf->output_block,
4566 sizeof( hkdf->output_block ) );
4567 if( status != PSA_SUCCESS )
4568 return( status );
4569 }
4570
4571 return( PSA_SUCCESS );
4572}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004573
Janos Follath7742fee2019-06-17 12:58:10 +01004574static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4575 psa_tls12_prf_key_derivation_t *tls12_prf,
4576 psa_algorithm_t alg )
4577{
4578 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4579 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004580 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4581 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004582
Janos Follath7742fee2019-06-17 12:58:10 +01004583 /* We can't be wanting more output after block 0xff, otherwise
4584 * the capacity check in psa_key_derivation_output_bytes() would have
4585 * prevented this call. It could happen only if the operation
4586 * object was corrupted or if this function is called directly
4587 * inside the library. */
4588 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004589 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004590
4591 /* We need a new block */
4592 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004593 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004594
4595 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4596 *
4597 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4598 *
4599 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4600 * HMAC_hash(secret, A(2) + seed) +
4601 * HMAC_hash(secret, A(3) + seed) + ...
4602 *
4603 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004604 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004605 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004606 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004607 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004608 * is currently extracted as `output_block` and where i is
4609 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004610 */
4611
Janos Follathea29bfb2019-06-19 12:21:20 +01004612 /* Save the hash context before using it, to preserve the hash state with
4613 * only the inner padding in it. We need this, because inner padding depends
4614 * on the key (secret in the RFC's terminology). */
4615 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4616 if( status != PSA_SUCCESS )
4617 goto cleanup;
4618
4619 /* Calculate A(i) where i = tls12_prf->block_number. */
4620 if( tls12_prf->block_number == 1 )
4621 {
4622 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4623 * the variable seed and in this instance means it in the context of the
4624 * P_hash function, where seed = label + seed.) */
4625 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4626 tls12_prf->label, tls12_prf->label_length );
4627 if( status != PSA_SUCCESS )
4628 goto cleanup;
4629 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4630 tls12_prf->seed, tls12_prf->seed_length );
4631 if( status != PSA_SUCCESS )
4632 goto cleanup;
4633 }
4634 else
4635 {
4636 /* A(i) = HMAC_hash(secret, A(i-1)) */
4637 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4638 tls12_prf->Ai, hash_length );
4639 if( status != PSA_SUCCESS )
4640 goto cleanup;
4641 }
4642
4643 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4644 tls12_prf->Ai, hash_length );
4645 if( status != PSA_SUCCESS )
4646 goto cleanup;
4647 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4648 if( status != PSA_SUCCESS )
4649 goto cleanup;
4650
4651 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4652 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4653 tls12_prf->Ai, hash_length );
4654 if( status != PSA_SUCCESS )
4655 goto cleanup;
4656 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4657 tls12_prf->label, tls12_prf->label_length );
4658 if( status != PSA_SUCCESS )
4659 goto cleanup;
4660 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4661 tls12_prf->seed, tls12_prf->seed_length );
4662 if( status != PSA_SUCCESS )
4663 goto cleanup;
4664 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4665 tls12_prf->output_block, hash_length );
4666 if( status != PSA_SUCCESS )
4667 goto cleanup;
4668 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4669 if( status != PSA_SUCCESS )
4670 goto cleanup;
4671
Janos Follath7742fee2019-06-17 12:58:10 +01004672
4673cleanup:
4674
Janos Follathea29bfb2019-06-19 12:21:20 +01004675 cleanup_status = psa_hash_abort( &backup );
4676 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4677 status = cleanup_status;
4678
4679 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004680}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004681
Janos Follath844eb0e2019-06-19 12:10:49 +01004682static psa_status_t psa_key_derivation_tls12_prf_read(
4683 psa_tls12_prf_key_derivation_t *tls12_prf,
4684 psa_algorithm_t alg,
4685 uint8_t *output,
4686 size_t output_length )
4687{
4688 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4689 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4690 psa_status_t status;
4691 uint8_t offset, length;
4692
4693 while( output_length != 0 )
4694 {
4695 /* Check if we have fully processed the current block. */
4696 if( tls12_prf->left_in_block == 0 )
4697 {
4698 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4699 alg );
4700 if( status != PSA_SUCCESS )
4701 return( status );
4702
4703 continue;
4704 }
4705
4706 if( tls12_prf->left_in_block > output_length )
4707 length = (uint8_t) output_length;
4708 else
4709 length = tls12_prf->left_in_block;
4710
4711 offset = hash_length - tls12_prf->left_in_block;
4712 memcpy( output, tls12_prf->output_block + offset, length );
4713 output += length;
4714 output_length -= length;
4715 tls12_prf->left_in_block -= length;
4716 }
4717
4718 return( PSA_SUCCESS );
4719}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004720#endif /* MBEDTLS_MD_C */
4721
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004722psa_status_t psa_key_derivation_output_bytes(
4723 psa_key_derivation_operation_t *operation,
4724 uint8_t *output,
4725 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726{
4727 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004731 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004733 return PSA_ERROR_BAD_STATE;
4734 }
4735
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004739 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004741 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004742 goto exit;
4743 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004747 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4749 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004751 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004752 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004755
Gilles Peskinebef7f142018-07-12 17:22:21 +02004756#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004758 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004759 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004761 output, output_length );
4762 }
Janos Follathadbec812019-06-14 11:05:39 +01004763 else
Janos Follathadbec812019-06-14 11:05:39 +01004764 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004765 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004766 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004768 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004769 output_length );
4770 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004771 else
4772#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004773 {
4774 return( PSA_ERROR_BAD_STATE );
4775 }
4776
4777exit:
4778 if( status != PSA_SUCCESS )
4779 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004780 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 * This allows us to differentiate between exhausted operations and
4782 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4783 * operations. */
4784 psa_algorithm_t alg = operation->alg;
4785 psa_key_derivation_abort( operation );
4786 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004787 memset( output, '!', output_length );
4788 }
4789 return( status );
4790}
4791
Gilles Peskine08542d82018-07-19 17:05:42 +02004792#if defined(MBEDTLS_DES_C)
4793static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4794{
4795 if( data_size >= 8 )
4796 mbedtls_des_key_set_parity( data );
4797 if( data_size >= 16 )
4798 mbedtls_des_key_set_parity( data + 8 );
4799 if( data_size >= 24 )
4800 mbedtls_des_key_set_parity( data + 16 );
4801}
4802#endif /* MBEDTLS_DES_C */
4803
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004804static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004805 psa_key_slot_t *slot,
4806 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004808{
4809 uint8_t *data = NULL;
4810 size_t bytes = PSA_BITS_TO_BYTES( bits );
4811 psa_status_t status;
4812
Gilles Peskine8e338702019-07-30 20:06:31 +02004813 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814 return( PSA_ERROR_INVALID_ARGUMENT );
4815 if( bits % 8 != 0 )
4816 return( PSA_ERROR_INVALID_ARGUMENT );
4817 data = mbedtls_calloc( 1, bytes );
4818 if( data == NULL )
4819 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004822 if( status != PSA_SUCCESS )
4823 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004824#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004825 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004826 psa_des_set_key_parity( data, bytes );
4827#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004828 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004829
4830exit:
4831 mbedtls_free( data );
4832 return( status );
4833}
4834
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004835psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004837 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004838{
4839 psa_status_t status;
4840 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004841 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004842
4843 /* Reject any attempt to create a zero-length key so that we don't
4844 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4845 if( psa_get_key_bits( attributes ) == 0 )
4846 return( PSA_ERROR_INVALID_ARGUMENT );
4847
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004848 if( ! operation->can_output_key )
4849 return( PSA_ERROR_NOT_PERMITTED );
4850
Gilles Peskinedf179142019-07-15 22:02:14 +02004851 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4852 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004853#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4854 if( driver != NULL )
4855 {
4856 /* Deriving a key in a secure element is not implemented yet. */
4857 status = PSA_ERROR_NOT_SUPPORTED;
4858 }
4859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 if( status == PSA_SUCCESS )
4861 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004862 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004863 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 }
4866 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004867 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004868 if( status != PSA_SUCCESS )
4869 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004870 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004871 *handle = 0;
4872 }
4873 return( status );
4874}
4875
Gilles Peskineeab56e42018-07-12 17:12:33 +02004876
4877
4878/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004879/* Key derivation */
4880/****************************************************************/
4881
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004883 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004884 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885{
Janos Follath5fe19732019-06-20 15:09:30 +01004886 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4887 * initialisers for this union leave some bytes unspecified.) */
4888 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4889
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004891#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004892 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4893 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4894 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4898 if( hash_size == 0 )
4899 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4901 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004902 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903 {
4904 return( PSA_ERROR_NOT_SUPPORTED );
4905 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908 }
4909#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004911 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912}
4913
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 psa_algorithm_t alg )
4916{
4917 psa_status_t status;
4918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004920 return( PSA_ERROR_BAD_STATE );
4921
Gilles Peskine6843c292019-01-18 16:44:49 +01004922 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4923 return( PSA_ERROR_INVALID_ARGUMENT );
4924 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 {
4926 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928 }
4929 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4930 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004933 else
4934 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004935
4936 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004938 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004939}
4940
4941#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004942static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004943 psa_algorithm_t hash_alg,
4944 psa_key_derivation_step_t step,
4945 const uint8_t *data,
4946 size_t data_length )
4947{
4948 psa_status_t status;
4949 switch( step )
4950 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004951 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004952 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004953 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004954 status = psa_hmac_setup_internal( &hkdf->hmac,
4955 data, data_length,
4956 hash_alg );
4957 if( status != PSA_SUCCESS )
4958 return( status );
4959 hkdf->state = HKDF_STATE_STARTED;
4960 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004961 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004962 /* If no salt was provided, use an empty salt. */
4963 if( hkdf->state == HKDF_STATE_INIT )
4964 {
4965 status = psa_hmac_setup_internal( &hkdf->hmac,
4966 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004967 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004968 if( status != PSA_SUCCESS )
4969 return( status );
4970 hkdf->state = HKDF_STATE_STARTED;
4971 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004972 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004974 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4975 data, data_length );
4976 if( status != PSA_SUCCESS )
4977 return( status );
4978 status = psa_hmac_finish_internal( &hkdf->hmac,
4979 hkdf->prk,
4980 sizeof( hkdf->prk ) );
4981 if( status != PSA_SUCCESS )
4982 return( status );
4983 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4984 hkdf->block_number = 0;
4985 hkdf->state = HKDF_STATE_KEYED;
4986 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004987 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004988 if( hkdf->state == HKDF_STATE_OUTPUT )
4989 return( PSA_ERROR_BAD_STATE );
4990 if( hkdf->info_set )
4991 return( PSA_ERROR_BAD_STATE );
4992 hkdf->info_length = data_length;
4993 if( data_length != 0 )
4994 {
4995 hkdf->info = mbedtls_calloc( 1, data_length );
4996 if( hkdf->info == NULL )
4997 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4998 memcpy( hkdf->info, data, data_length );
4999 }
5000 hkdf->info_set = 1;
5001 return( PSA_SUCCESS );
5002 default:
5003 return( PSA_ERROR_INVALID_ARGUMENT );
5004 }
5005}
Janos Follathb03233e2019-06-11 15:30:30 +01005006
Janos Follathf08e2652019-06-13 09:05:41 +01005007static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5008 const uint8_t *data,
5009 size_t data_length )
5010{
5011 if( prf->state != TLS12_PRF_STATE_INIT )
5012 return( PSA_ERROR_BAD_STATE );
5013
Janos Follathd6dce9f2019-07-04 09:11:38 +01005014 if( data_length != 0 )
5015 {
5016 prf->seed = mbedtls_calloc( 1, data_length );
5017 if( prf->seed == NULL )
5018 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005019
Janos Follathd6dce9f2019-07-04 09:11:38 +01005020 memcpy( prf->seed, data, data_length );
5021 prf->seed_length = data_length;
5022 }
Janos Follathf08e2652019-06-13 09:05:41 +01005023
5024 prf->state = TLS12_PRF_STATE_SEED_SET;
5025
5026 return( PSA_SUCCESS );
5027}
5028
Janos Follath81550542019-06-13 14:26:34 +01005029static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5030 psa_algorithm_t hash_alg,
5031 const uint8_t *data,
5032 size_t data_length )
5033{
5034 psa_status_t status;
5035 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5036 return( PSA_ERROR_BAD_STATE );
5037
5038 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5039 if( status != PSA_SUCCESS )
5040 return( status );
5041
5042 prf->state = TLS12_PRF_STATE_KEY_SET;
5043
5044 return( PSA_SUCCESS );
5045}
5046
Janos Follath6660f0e2019-06-17 08:44:03 +01005047static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5048 psa_tls12_prf_key_derivation_t *prf,
5049 psa_algorithm_t hash_alg,
5050 const uint8_t *data,
5051 size_t data_length )
5052{
5053 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005054 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5055 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005056
5057 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5058 return( PSA_ERROR_INVALID_ARGUMENT );
5059
5060 /* Quoting RFC 4279, Section 2:
5061 *
5062 * The premaster secret is formed as follows: if the PSK is N octets
5063 * long, concatenate a uint16 with the value N, N zero octets, a second
5064 * uint16 with the value N, and the PSK itself.
5065 */
5066
Janos Follath40e13932019-06-26 13:22:29 +01005067 *cur++ = ( data_length >> 8 ) & 0xff;
5068 *cur++ = ( data_length >> 0 ) & 0xff;
5069 memset( cur, 0, data_length );
5070 cur += data_length;
5071 *cur++ = pms[0];
5072 *cur++ = pms[1];
5073 memcpy( cur, data, data_length );
5074 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005075
Janos Follath40e13932019-06-26 13:22:29 +01005076 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005077
5078 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5079 return( status );
5080}
5081
Janos Follath63028dd2019-06-13 09:15:47 +01005082static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5083 const uint8_t *data,
5084 size_t data_length )
5085{
5086 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5087 return( PSA_ERROR_BAD_STATE );
5088
Janos Follathd6dce9f2019-07-04 09:11:38 +01005089 if( data_length != 0 )
5090 {
5091 prf->label = mbedtls_calloc( 1, data_length );
5092 if( prf->label == NULL )
5093 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005094
Janos Follathd6dce9f2019-07-04 09:11:38 +01005095 memcpy( prf->label, data, data_length );
5096 prf->label_length = data_length;
5097 }
Janos Follath63028dd2019-06-13 09:15:47 +01005098
5099 prf->state = TLS12_PRF_STATE_LABEL_SET;
5100
5101 return( PSA_SUCCESS );
5102}
5103
Janos Follathb03233e2019-06-11 15:30:30 +01005104static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5105 psa_algorithm_t hash_alg,
5106 psa_key_derivation_step_t step,
5107 const uint8_t *data,
5108 size_t data_length )
5109{
Janos Follathb03233e2019-06-11 15:30:30 +01005110 switch( step )
5111 {
Janos Follathf08e2652019-06-13 09:05:41 +01005112 case PSA_KEY_DERIVATION_INPUT_SEED:
5113 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005114 case PSA_KEY_DERIVATION_INPUT_SECRET:
5115 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005116 case PSA_KEY_DERIVATION_INPUT_LABEL:
5117 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005118 default:
5119 return( PSA_ERROR_INVALID_ARGUMENT );
5120 }
5121}
Janos Follath6660f0e2019-06-17 08:44:03 +01005122
5123static psa_status_t psa_tls12_prf_psk_to_ms_input(
5124 psa_tls12_prf_key_derivation_t *prf,
5125 psa_algorithm_t hash_alg,
5126 psa_key_derivation_step_t step,
5127 const uint8_t *data,
5128 size_t data_length )
5129{
5130 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005131 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005132 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5133 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005134 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005135
5136 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5137}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138#endif /* MBEDTLS_MD_C */
5139
Gilles Peskineb8965192019-09-24 16:21:10 +02005140/** Check whether the given key type is acceptable for the given
5141 * input step of a key derivation.
5142 *
5143 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5144 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5145 * Both secret and non-secret inputs can alternatively have the type
5146 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5147 * that the input was passed as a buffer rather than via a key object.
5148 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005149static int psa_key_derivation_check_input_type(
5150 psa_key_derivation_step_t step,
5151 psa_key_type_t key_type )
5152{
5153 switch( step )
5154 {
5155 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005156 if( key_type == PSA_KEY_TYPE_DERIVE )
5157 return( PSA_SUCCESS );
5158 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005159 return( PSA_SUCCESS );
5160 break;
5161 case PSA_KEY_DERIVATION_INPUT_LABEL:
5162 case PSA_KEY_DERIVATION_INPUT_SALT:
5163 case PSA_KEY_DERIVATION_INPUT_INFO:
5164 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005165 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5166 return( PSA_SUCCESS );
5167 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005168 return( PSA_SUCCESS );
5169 break;
5170 }
5171 return( PSA_ERROR_INVALID_ARGUMENT );
5172}
5173
Janos Follathb80a94e2019-06-12 15:54:46 +01005174static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005176 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005177 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005178 const uint8_t *data,
5179 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005181 psa_status_t status;
5182 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5183
5184 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005185 if( status != PSA_SUCCESS )
5186 goto exit;
5187
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005192 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193 step, data, data_length );
5194 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005195 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005196 {
Janos Follathb03233e2019-06-11 15:30:30 +01005197 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5198 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5199 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005200 }
5201 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5202 {
5203 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5204 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5205 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005206 }
5207 else
5208#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211 return( PSA_ERROR_BAD_STATE );
5212 }
5213
Gilles Peskine224b0d62019-09-23 18:13:17 +02005214exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217 return( status );
5218}
5219
Janos Follath51f4a0f2019-06-14 11:35:55 +01005220psa_status_t psa_key_derivation_input_bytes(
5221 psa_key_derivation_operation_t *operation,
5222 psa_key_derivation_step_t step,
5223 const uint8_t *data,
5224 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005225{
Gilles Peskineb8965192019-09-24 16:21:10 +02005226 return( psa_key_derivation_input_internal( operation, step,
5227 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005228 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005229}
5230
Janos Follath51f4a0f2019-06-14 11:35:55 +01005231psa_status_t psa_key_derivation_input_key(
5232 psa_key_derivation_operation_t *operation,
5233 psa_key_derivation_step_t step,
5234 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235{
5236 psa_key_slot_t *slot;
5237 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005238
Gilles Peskine28f8f302019-07-24 13:30:31 +02005239 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005240 PSA_KEY_USAGE_DERIVE,
5241 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005242 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005243 {
5244 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005245 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005246 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005247
5248 /* Passing a key object as a SECRET input unlocks the permission
5249 * to output to a key object. */
5250 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5251 operation->can_output_key = 1;
5252
Janos Follathb80a94e2019-06-12 15:54:46 +01005253 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005254 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005255 slot->data.raw.data,
5256 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005258
5259
5260
5261/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005262/* Key agreement */
5263/****************************************************************/
5264
Gilles Peskinea05219c2018-11-16 16:02:56 +01005265#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005266static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5267 size_t peer_key_length,
5268 const mbedtls_ecp_keypair *our_key,
5269 uint8_t *shared_secret,
5270 size_t shared_secret_size,
5271 size_t *shared_secret_length )
5272{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005273 mbedtls_ecp_keypair *their_key = NULL;
5274 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005275 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005276 size_t bits = 0;
5277 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005278 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005279
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005280 status = psa_import_ec_public_key( curve,
5281 peer_key, peer_key_length,
5282 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005283 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005284 goto exit;
5285
Jaeden Amero97271b32019-01-10 19:38:51 +00005286 status = mbedtls_to_psa_error(
5287 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5288 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005289 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005290 status = mbedtls_to_psa_error(
5291 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5292 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005293 goto exit;
5294
Jaeden Amero97271b32019-01-10 19:38:51 +00005295 status = mbedtls_to_psa_error(
5296 mbedtls_ecdh_calc_secret( &ecdh,
5297 shared_secret_length,
5298 shared_secret, shared_secret_size,
5299 mbedtls_ctr_drbg_random,
5300 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005301 if( status != PSA_SUCCESS )
5302 goto exit;
5303 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5304 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305
5306exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005307 if( status != PSA_SUCCESS )
5308 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005309 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005310 mbedtls_ecp_keypair_free( their_key );
5311 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005312 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005313}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005314#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005315
Gilles Peskine01d718c2018-09-18 12:01:02 +02005316#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5317
Gilles Peskine0216fe12019-04-11 21:23:21 +02005318static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5319 psa_key_slot_t *private_key,
5320 const uint8_t *peer_key,
5321 size_t peer_key_length,
5322 uint8_t *shared_secret,
5323 size_t shared_secret_size,
5324 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005325{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005326 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005327 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005328#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005329 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005330 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005331 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005332 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5333 private_key->data.ecp,
5334 shared_secret, shared_secret_size,
5335 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005336#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005337 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005338 (void) private_key;
5339 (void) peer_key;
5340 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005341 (void) shared_secret;
5342 (void) shared_secret_size;
5343 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005344 return( PSA_ERROR_NOT_SUPPORTED );
5345 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005346}
5347
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005348/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349 * to potentially free embedded data structures and wipe confidential data.
5350 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005351static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005352 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005353 psa_key_slot_t *private_key,
5354 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005355 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005356{
5357 psa_status_t status;
5358 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5359 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005360 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005361
5362 /* Step 1: run the secret agreement algorithm to generate the shared
5363 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005364 status = psa_key_agreement_raw_internal( ka_alg,
5365 private_key,
5366 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005367 shared_secret,
5368 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005369 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005370 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371 goto exit;
5372
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005373 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005374 * the shared secret. A shared secret is permitted wherever a key
5375 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005376 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005377 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005378 shared_secret,
5379 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005380
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005382 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005383 return( status );
5384}
5385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005387 psa_key_derivation_step_t step,
5388 psa_key_handle_t private_key,
5389 const uint8_t *peer_key,
5390 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005392 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005393 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005394 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005395 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005396 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005397 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005398 if( status != PSA_SUCCESS )
5399 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005400 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005401 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005402 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005403 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005405 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005406}
5407
Gilles Peskinebe697d82019-05-16 18:00:41 +02005408psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5409 psa_key_handle_t private_key,
5410 const uint8_t *peer_key,
5411 size_t peer_key_length,
5412 uint8_t *output,
5413 size_t output_size,
5414 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005415{
5416 psa_key_slot_t *slot;
5417 psa_status_t status;
5418
5419 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5420 {
5421 status = PSA_ERROR_INVALID_ARGUMENT;
5422 goto exit;
5423 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005424 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005425 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005426 if( status != PSA_SUCCESS )
5427 goto exit;
5428
5429 status = psa_key_agreement_raw_internal( alg, slot,
5430 peer_key, peer_key_length,
5431 output, output_size,
5432 output_length );
5433
5434exit:
5435 if( status != PSA_SUCCESS )
5436 {
5437 /* If an error happens and is not handled properly, the output
5438 * may be used as a key to protect sensitive data. Arrange for such
5439 * a key to be random, which is likely to result in decryption or
5440 * verification errors. This is better than filling the buffer with
5441 * some constant data such as zeros, which would result in the data
5442 * being protected with a reproducible, easily knowable key.
5443 */
5444 psa_generate_random( output, output_size );
5445 *output_length = output_size;
5446 }
5447 return( status );
5448}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005449
5450
5451/****************************************************************/
5452/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005453/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005454
Gilles Peskine4c317f42018-07-12 01:24:09 +02005455psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005456 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005457{
Janos Follath24eed8d2019-11-22 13:21:35 +00005458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005459 GUARD_MODULE_INITIALIZED;
5460
Gilles Peskinef181eca2019-08-07 13:49:00 +02005461 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5462 {
5463 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5464 output,
5465 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5466 if( ret != 0 )
5467 return( mbedtls_to_psa_error( ret ) );
5468 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5469 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5470 }
5471
Gilles Peskinee59236f2018-01-27 23:32:46 +01005472 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5473 return( mbedtls_to_psa_error( ret ) );
5474}
5475
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005476#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5477#include "mbedtls/entropy_poll.h"
5478
Gilles Peskine7228da22019-07-15 11:06:15 +02005479psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005480 size_t seed_size )
5481{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005482 if( global_data.initialized )
5483 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005484
5485 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5486 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5487 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5488 return( PSA_ERROR_INVALID_ARGUMENT );
5489
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005490 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005491}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005492#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005493
Gilles Peskinee56e8782019-04-26 17:34:02 +02005494#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5495static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5496 size_t domain_parameters_size,
5497 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005498{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005499 size_t i;
5500 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005501
Gilles Peskinee56e8782019-04-26 17:34:02 +02005502 if( domain_parameters_size == 0 )
5503 {
5504 *exponent = 65537;
5505 return( PSA_SUCCESS );
5506 }
5507
5508 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5509 * support values that fit in a 32-bit integer, which is larger than
5510 * int on just about every platform anyway. */
5511 if( domain_parameters_size > sizeof( acc ) )
5512 return( PSA_ERROR_NOT_SUPPORTED );
5513 for( i = 0; i < domain_parameters_size; i++ )
5514 acc = ( acc << 8 ) | domain_parameters[i];
5515 if( acc > INT_MAX )
5516 return( PSA_ERROR_NOT_SUPPORTED );
5517 *exponent = acc;
5518 return( PSA_SUCCESS );
5519}
5520#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5521
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005522static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005523 psa_key_slot_t *slot, size_t bits,
5524 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005525{
Gilles Peskine8e338702019-07-30 20:06:31 +02005526 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005527
Gilles Peskinee56e8782019-04-26 17:34:02 +02005528 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005529 return( PSA_ERROR_INVALID_ARGUMENT );
5530
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531 if( key_type_is_raw_bytes( type ) )
5532 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005533 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5535 if( status != PSA_SUCCESS )
5536 return( status );
5537 status = psa_generate_random( slot->data.raw.data,
5538 slot->data.raw.bytes );
5539 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005541#if defined(MBEDTLS_DES_C)
5542 if( type == PSA_KEY_TYPE_DES )
5543 psa_des_set_key_parity( slot->data.raw.data,
5544 slot->data.raw.bytes );
5545#endif /* MBEDTLS_DES_C */
5546 }
5547 else
5548
5549#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005550 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005551 {
5552 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005554 int exponent;
5555 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5557 return( PSA_ERROR_NOT_SUPPORTED );
5558 /* Accept only byte-aligned keys, for the same reasons as
5559 * in psa_import_rsa_key(). */
5560 if( bits % 8 != 0 )
5561 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005562 status = psa_read_rsa_exponent( domain_parameters,
5563 domain_parameters_size,
5564 &exponent );
5565 if( status != PSA_SUCCESS )
5566 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005567 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5568 if( rsa == NULL )
5569 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5570 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5571 ret = mbedtls_rsa_gen_key( rsa,
5572 mbedtls_ctr_drbg_random,
5573 &global_data.ctr_drbg,
5574 (unsigned int) bits,
5575 exponent );
5576 if( ret != 0 )
5577 {
5578 mbedtls_rsa_free( rsa );
5579 mbedtls_free( rsa );
5580 return( mbedtls_to_psa_error( ret ) );
5581 }
5582 slot->data.rsa = rsa;
5583 }
5584 else
5585#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5586
5587#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005588 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005589 {
5590 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005591 mbedtls_ecp_group_id grp_id =
5592 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593 const mbedtls_ecp_curve_info *curve_info =
5594 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5595 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005597 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005598 return( PSA_ERROR_NOT_SUPPORTED );
5599 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5600 return( PSA_ERROR_NOT_SUPPORTED );
5601 if( curve_info->bit_size != bits )
5602 return( PSA_ERROR_INVALID_ARGUMENT );
5603 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5604 if( ecp == NULL )
5605 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5606 mbedtls_ecp_keypair_init( ecp );
5607 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5608 mbedtls_ctr_drbg_random,
5609 &global_data.ctr_drbg );
5610 if( ret != 0 )
5611 {
5612 mbedtls_ecp_keypair_free( ecp );
5613 mbedtls_free( ecp );
5614 return( mbedtls_to_psa_error( ret ) );
5615 }
5616 slot->data.ecp = ecp;
5617 }
5618 else
5619#endif /* MBEDTLS_ECP_C */
5620
5621 return( PSA_ERROR_NOT_SUPPORTED );
5622
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005623 return( PSA_SUCCESS );
5624}
Darryl Green0c6575a2018-11-07 16:05:30 +00005625
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005626psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005627 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005628{
5629 psa_status_t status;
5630 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005631 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005632
Gilles Peskine0f84d622019-09-12 19:03:13 +02005633 /* Reject any attempt to create a zero-length key so that we don't
5634 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5635 if( psa_get_key_bits( attributes ) == 0 )
5636 return( PSA_ERROR_INVALID_ARGUMENT );
5637
Gilles Peskinedf179142019-07-15 22:02:14 +02005638 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5639 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005640 if( status != PSA_SUCCESS )
5641 goto exit;
5642
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005643#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5644 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005645 {
Gilles Peskine11792082019-08-06 18:36:36 +02005646 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5647 size_t pubkey_length = 0; /* We don't support this feature yet */
5648 if( drv->key_management == NULL ||
5649 drv->key_management->p_generate == NULL )
5650 {
5651 status = PSA_ERROR_NOT_SUPPORTED;
5652 goto exit;
5653 }
5654 status = drv->key_management->p_generate(
5655 psa_get_se_driver_context( driver ),
5656 slot->data.se.slot_number, attributes,
5657 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005658 }
Gilles Peskine11792082019-08-06 18:36:36 +02005659 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005660#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005661 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005662 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005663 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005664 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 }
Gilles Peskine11792082019-08-06 18:36:36 +02005666
5667exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005668 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005669 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670 if( status != PSA_SUCCESS )
5671 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005672 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005673 *handle = 0;
5674 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005675 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005676}
5677
5678
Gilles Peskinee59236f2018-01-27 23:32:46 +01005679
5680/****************************************************************/
5681/* Module setup */
5682/****************************************************************/
5683
Gilles Peskine5e769522018-11-20 21:59:56 +01005684psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5685 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5686 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5687{
5688 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5689 return( PSA_ERROR_BAD_STATE );
5690 global_data.entropy_init = entropy_init;
5691 global_data.entropy_free = entropy_free;
5692 return( PSA_SUCCESS );
5693}
5694
Gilles Peskinee59236f2018-01-27 23:32:46 +01005695void mbedtls_psa_crypto_free( void )
5696{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005697 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005698 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5699 {
5700 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005701 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005702 }
5703 /* Wipe all remaining data, including configuration.
5704 * In particular, this sets all state indicator to the value
5705 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005706 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005708 /* Unregister all secure element drivers, so that we restart from
5709 * a pristine state. */
5710 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005711#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005712}
5713
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005714#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5715/** Recover a transaction that was interrupted by a power failure.
5716 *
5717 * This function is called during initialization, before psa_crypto_init()
5718 * returns. If this function returns a failure status, the initialization
5719 * fails.
5720 */
5721static psa_status_t psa_crypto_recover_transaction(
5722 const psa_crypto_transaction_t *transaction )
5723{
5724 switch( transaction->unknown.type )
5725 {
5726 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5727 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005728 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005729 * is implemented.
5730 * https://github.com/ARMmbed/mbed-crypto/issues/218
5731 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005732 default:
5733 /* We found an unsupported transaction in the storage.
5734 * We don't know what state the storage is in. Give up. */
5735 return( PSA_ERROR_STORAGE_FAILURE );
5736 }
5737}
5738#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5739
Gilles Peskinee59236f2018-01-27 23:32:46 +01005740psa_status_t psa_crypto_init( void )
5741{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005742 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005743 const unsigned char drbg_seed[] = "PSA";
5744
Gilles Peskinec6b69072018-11-20 21:42:52 +01005745 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005746 if( global_data.initialized != 0 )
5747 return( PSA_SUCCESS );
5748
Gilles Peskine5e769522018-11-20 21:59:56 +01005749 /* Set default configuration if
5750 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5751 if( global_data.entropy_init == NULL )
5752 global_data.entropy_init = mbedtls_entropy_init;
5753 if( global_data.entropy_free == NULL )
5754 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005755
Gilles Peskinec6b69072018-11-20 21:42:52 +01005756 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005757 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005758#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5759 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5760 /* The PSA entropy injection feature depends on using NV seed as an entropy
5761 * source. Add NV seed as an entropy source for PSA entropy injection. */
5762 mbedtls_entropy_add_source( &global_data.entropy,
5763 mbedtls_nv_seed_poll, NULL,
5764 MBEDTLS_ENTROPY_BLOCK_SIZE,
5765 MBEDTLS_ENTROPY_SOURCE_STRONG );
5766#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005767 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005768 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005769 status = mbedtls_to_psa_error(
5770 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5771 mbedtls_entropy_func,
5772 &global_data.entropy,
5773 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5774 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005775 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005776 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005777
Gilles Peskine66fb1262018-12-10 16:29:04 +01005778 status = psa_initialize_key_slots( );
5779 if( status != PSA_SUCCESS )
5780 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005781
Gilles Peskined9348f22019-10-01 15:22:29 +02005782#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5783 status = psa_init_all_se_drivers( );
5784 if( status != PSA_SUCCESS )
5785 goto exit;
5786#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5787
Gilles Peskine4b734222019-07-24 15:56:31 +02005788#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005789 status = psa_crypto_load_transaction( );
5790 if( status == PSA_SUCCESS )
5791 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005792 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5793 if( status != PSA_SUCCESS )
5794 goto exit;
5795 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005796 }
5797 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5798 {
5799 /* There's no transaction to complete. It's all good. */
5800 status = PSA_SUCCESS;
5801 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005802#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005803
Gilles Peskinec6b69072018-11-20 21:42:52 +01005804 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005805 global_data.initialized = 1;
5806
5807exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005808 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005809 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005810 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811}
5812
5813#endif /* MBEDTLS_PSA_CRYPTO_C */