blob: 0ac62909881be674ed5b9aceb818acf5f3f49b97 [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 +0100378psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
379 size_t *bits )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200380{
381 switch( grpid )
382 {
383 case MBEDTLS_ECP_DP_SECP192R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100384 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100385 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100388 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100391 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100394 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100397 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100400 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100403 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100406 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100409 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100412 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100415 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100418 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100421 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200422 default:
Jaeden Amero1bde9cd2020-05-05 10:17:01 +0100423 *bits = 0;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200424 return( 0 );
425 }
426}
427
Gilles Peskine5055b232019-12-12 17:49:31 +0100428mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
429 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200430{
431 switch( curve )
432 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100433 case PSA_ECC_CURVE_SECP_R1:
434 switch( byte_length )
435 {
436 case PSA_BITS_TO_BYTES( 192 ):
437 return( MBEDTLS_ECP_DP_SECP192R1 );
438 case PSA_BITS_TO_BYTES( 224 ):
439 return( MBEDTLS_ECP_DP_SECP224R1 );
440 case PSA_BITS_TO_BYTES( 256 ):
441 return( MBEDTLS_ECP_DP_SECP256R1 );
442 case PSA_BITS_TO_BYTES( 384 ):
443 return( MBEDTLS_ECP_DP_SECP384R1 );
444 case PSA_BITS_TO_BYTES( 521 ):
445 return( MBEDTLS_ECP_DP_SECP521R1 );
446 default:
447 return( MBEDTLS_ECP_DP_NONE );
448 }
449 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100450
451 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
452 switch( byte_length )
453 {
454 case PSA_BITS_TO_BYTES( 256 ):
455 return( MBEDTLS_ECP_DP_BP256R1 );
456 case PSA_BITS_TO_BYTES( 384 ):
457 return( MBEDTLS_ECP_DP_BP384R1 );
458 case PSA_BITS_TO_BYTES( 512 ):
459 return( MBEDTLS_ECP_DP_BP512R1 );
460 default:
461 return( MBEDTLS_ECP_DP_NONE );
462 }
463 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100464
465 case PSA_ECC_CURVE_MONTGOMERY:
466 switch( byte_length )
467 {
468 case PSA_BITS_TO_BYTES( 255 ):
469 return( MBEDTLS_ECP_DP_CURVE25519 );
470 case PSA_BITS_TO_BYTES( 448 ):
471 return( MBEDTLS_ECP_DP_CURVE448 );
472 default:
473 return( MBEDTLS_ECP_DP_NONE );
474 }
475 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100476
477 case PSA_ECC_CURVE_SECP_K1:
478 switch( byte_length )
479 {
480 case PSA_BITS_TO_BYTES( 192 ):
481 return( MBEDTLS_ECP_DP_SECP192K1 );
482 case PSA_BITS_TO_BYTES( 224 ):
483 return( MBEDTLS_ECP_DP_SECP224K1 );
484 case PSA_BITS_TO_BYTES( 256 ):
485 return( MBEDTLS_ECP_DP_SECP256K1 );
486 default:
487 return( MBEDTLS_ECP_DP_NONE );
488 }
489 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100490
Gilles Peskine12313cd2018-06-20 00:20:32 +0200491 default:
492 return( MBEDTLS_ECP_DP_NONE );
493 }
494}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100495#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200496
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
498 size_t bits,
499 struct raw_data *raw )
500{
501 /* Check that the bit size is acceptable for the key type */
502 switch( type )
503 {
504 case PSA_KEY_TYPE_RAW_DATA:
505#if defined(MBEDTLS_MD_C)
506 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200507#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200508 case PSA_KEY_TYPE_DERIVE:
509 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510#if defined(MBEDTLS_AES_C)
511 case PSA_KEY_TYPE_AES:
512 if( bits != 128 && bits != 192 && bits != 256 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
516#if defined(MBEDTLS_CAMELLIA_C)
517 case PSA_KEY_TYPE_CAMELLIA:
518 if( bits != 128 && bits != 192 && bits != 256 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
522#if defined(MBEDTLS_DES_C)
523 case PSA_KEY_TYPE_DES:
524 if( bits != 64 && bits != 128 && bits != 192 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
528#if defined(MBEDTLS_ARC4_C)
529 case PSA_KEY_TYPE_ARC4:
530 if( bits < 8 || bits > 2048 )
531 return( PSA_ERROR_INVALID_ARGUMENT );
532 break;
533#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200534#if defined(MBEDTLS_CHACHA20_C)
535 case PSA_KEY_TYPE_CHACHA20:
536 if( bits != 256 )
537 return( PSA_ERROR_INVALID_ARGUMENT );
538 break;
539#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 default:
541 return( PSA_ERROR_NOT_SUPPORTED );
542 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200543 if( bits % 8 != 0 )
544 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545
546 /* Allocate memory for the key */
547 raw->bytes = PSA_BITS_TO_BYTES( bits );
548 raw->data = mbedtls_calloc( 1, raw->bytes );
549 if( raw->data == NULL )
550 {
551 raw->bytes = 0;
552 return( PSA_ERROR_INSUFFICIENT_MEMORY );
553 }
554 return( PSA_SUCCESS );
555}
556
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100558/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
559 * that are not a multiple of 8) well. For example, there is only
560 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
561 * way to return the exact bit size of a key.
562 * To keep things simple, reject non-byte-aligned key sizes. */
563static psa_status_t psa_check_rsa_key_byte_aligned(
564 const mbedtls_rsa_context *rsa )
565{
566 mbedtls_mpi n;
567 psa_status_t status;
568 mbedtls_mpi_init( &n );
569 status = mbedtls_to_psa_error(
570 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
571 if( status == PSA_SUCCESS )
572 {
573 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
574 status = PSA_ERROR_NOT_SUPPORTED;
575 }
576 mbedtls_mpi_free( &n );
577 return( status );
578}
579
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580static psa_status_t psa_import_rsa_key( psa_key_type_t type,
581 const uint8_t *data,
582 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583 mbedtls_rsa_context **p_rsa )
584{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585 psa_status_t status;
586 mbedtls_pk_context pk;
587 mbedtls_rsa_context *rsa;
588 size_t bits;
589
590 mbedtls_pk_init( &pk );
591
592 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200593 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000594 status = mbedtls_to_psa_error(
595 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200596 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000597 status = mbedtls_to_psa_error(
598 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
599 if( status != PSA_SUCCESS )
600 goto exit;
601
602 /* We have something that the pkparse module recognizes. If it is a
603 * valid RSA key, store it. */
604 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200605 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000606 status = PSA_ERROR_INVALID_ARGUMENT;
607 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200608 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000609
610 rsa = mbedtls_pk_rsa( pk );
611 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
612 * supports non-byte-aligned key sizes, but not well. For example,
613 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
614 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
615 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
616 {
617 status = PSA_ERROR_NOT_SUPPORTED;
618 goto exit;
619 }
620 status = psa_check_rsa_key_byte_aligned( rsa );
621
622exit:
623 /* Free the content of the pk object only on error. */
624 if( status != PSA_SUCCESS )
625 {
626 mbedtls_pk_free( &pk );
627 return( status );
628 }
629
630 /* On success, store the content of the object in the RSA context. */
631 *p_rsa = rsa;
632
633 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634}
635#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
636
Jaeden Ameroccdce902019-01-10 11:42:27 +0000637#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100638static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100639 size_t data_length,
640 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100641 mbedtls_ecp_keypair **p_ecp )
642{
643 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
644 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
645 if( *p_ecp == NULL )
646 return( PSA_ERROR_INSUFFICIENT_MEMORY );
647 mbedtls_ecp_keypair_init( *p_ecp );
648
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100649 if( is_public )
650 {
651 /* A public key is represented as:
652 * - The byte 0x04;
653 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
654 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
655 * So its data length is 2m+1 where n is the key size in bits.
656 */
657 if( ( data_length & 1 ) == 0 )
658 return( PSA_ERROR_INVALID_ARGUMENT );
659 data_length = data_length / 2;
660 }
661
Gilles Peskine4cd32772019-12-02 20:49:42 +0100662 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100663 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
664 if( grp_id == MBEDTLS_ECP_DP_NONE )
665 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100666 return( mbedtls_to_psa_error(
667 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
668}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000669
670/* Import a public key given as the uncompressed representation defined by SEC1
671 * 2.3.3 as the content of an ECPoint. */
672static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
673 const uint8_t *data,
674 size_t data_length,
675 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200676{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000678 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000679
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100680 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000681 if( status != PSA_SUCCESS )
682 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100683
Jaeden Ameroccdce902019-01-10 11:42:27 +0000684 /* Load the public value. */
685 status = mbedtls_to_psa_error(
686 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
687 data, data_length ) );
688 if( status != PSA_SUCCESS )
689 goto exit;
690
691 /* Check that the point is on the curve. */
692 status = mbedtls_to_psa_error(
693 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
694 if( status != PSA_SUCCESS )
695 goto exit;
696
697 *p_ecp = ecp;
698 return( PSA_SUCCESS );
699
700exit:
701 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200702 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000703 mbedtls_ecp_keypair_free( ecp );
704 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200705 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000706 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200708
Gilles Peskinef76aa772018-10-29 19:24:33 +0100709/* Import a private key given as a byte string which is the private value
710 * in big-endian order. */
711static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
712 const uint8_t *data,
713 size_t data_length,
714 mbedtls_ecp_keypair **p_ecp )
715{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100718
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100719 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100720 if( status != PSA_SUCCESS )
721 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100722
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 /* Load the secret value. */
724 status = mbedtls_to_psa_error(
725 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
726 if( status != PSA_SUCCESS )
727 goto exit;
728 /* Validate the private key. */
729 status = mbedtls_to_psa_error(
730 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
731 if( status != PSA_SUCCESS )
732 goto exit;
733 /* Calculate the public key from the private key. */
734 status = mbedtls_to_psa_error(
735 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
736 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
737 if( status != PSA_SUCCESS )
738 goto exit;
739
740 *p_ecp = ecp;
741 return( PSA_SUCCESS );
742
743exit:
744 if( ecp != NULL )
745 {
746 mbedtls_ecp_keypair_free( ecp );
747 mbedtls_free( ecp );
748 }
749 return( status );
750}
751#endif /* defined(MBEDTLS_ECP_C) */
752
Gilles Peskineb46bef22019-07-30 21:32:04 +0200753
754/** Return the size of the key in the given slot, in bits.
755 *
756 * \param[in] slot A key slot.
757 *
758 * \return The key size in bits, read from the metadata in the slot.
759 */
760static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
761{
762 return( slot->attr.bits );
763}
764
765/** Calculate the size of the key in the given slot, in bits.
766 *
767 * \param[in] slot A key slot containing a transparent key.
768 *
769 * \return The key size in bits, calculated from the key data.
770 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200771static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200772{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200773 size_t bits = 0; /* return 0 on an empty slot */
774
Gilles Peskineb46bef22019-07-30 21:32:04 +0200775 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200776 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200777#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200778 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
779 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200780#endif /* defined(MBEDTLS_RSA_C) */
781#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200782 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
783 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200784#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200785
786 /* We know that the size fits in psa_key_bits_t thanks to checks
787 * when the key was created. */
788 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200789}
790
Gilles Peskine8e338702019-07-30 20:06:31 +0200791/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100792 * previously. This function assumes that the slot does not contain
793 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100794psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
795 const uint8_t *data,
796 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200798 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799
Gilles Peskine8e338702019-07-30 20:06:31 +0200800 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200802 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200803 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100804 if( data_length > SIZE_MAX / 8 )
805 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200806 /* Enforce a size limit, and in particular ensure that the bit
807 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200808 if( bit_size > PSA_MAX_KEY_BITS )
809 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200810 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200811 &slot->data.raw );
812 if( status != PSA_SUCCESS )
813 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200814 if( data_length != 0 )
815 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 }
817 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100818#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200819 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100820 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200821 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100822 data, data_length,
823 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100824 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200825 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000827 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200828 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000829 data, data_length,
830 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100831 }
832 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100833#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000834#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200835 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100836 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200837 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000838 data, data_length,
839 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 }
841 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000842#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843 {
844 return( PSA_ERROR_NOT_SUPPORTED );
845 }
Darryl Green940d72c2018-07-13 13:18:51 +0100846
Gilles Peskineb46bef22019-07-30 21:32:04 +0200847 if( status == PSA_SUCCESS )
848 {
849 /* Write the actual key size to the slot.
850 * psa_start_key_creation() wrote the size declared by the
851 * caller, which may be 0 (meaning unspecified) or wrong. */
852 slot->attr.bits = psa_calculate_key_bits( slot );
853 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100854 return( status );
855}
856
Gilles Peskinef603c712019-01-19 13:40:11 +0100857/** Calculate the intersection of two algorithm usage policies.
858 *
859 * Return 0 (which allows no operation) on incompatibility.
860 */
861static psa_algorithm_t psa_key_policy_algorithm_intersection(
862 psa_algorithm_t alg1,
863 psa_algorithm_t alg2 )
864{
Gilles Peskine549ea862019-05-22 11:45:59 +0200865 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100866 if( alg1 == alg2 )
867 return( alg1 );
868 /* If the policies are from the same hash-and-sign family, check
869 * if one is a wildcard. If so the other has the specific algorithm. */
870 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
871 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
872 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
873 {
874 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
875 return( alg2 );
876 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
877 return( alg1 );
878 }
879 /* If the policies are incompatible, allow nothing. */
880 return( 0 );
881}
882
Gilles Peskined6f371b2019-05-10 19:33:38 +0200883static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
884 psa_algorithm_t requested_alg )
885{
Gilles Peskine549ea862019-05-22 11:45:59 +0200886 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200887 if( requested_alg == policy_alg )
888 return( 1 );
889 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200890 * and requested_alg is the same hash-and-sign family with any hash,
891 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200892 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
893 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
894 {
895 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
896 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
897 }
898 /* If it isn't permitted, it's forbidden. */
899 return( 0 );
900}
901
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100902/** Test whether a policy permits an algorithm.
903 *
904 * The caller must test usage flags separately.
905 */
906static int psa_key_policy_permits( const psa_key_policy_t *policy,
907 psa_algorithm_t alg )
908{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200909 return( psa_key_algorithm_permits( policy->alg, alg ) ||
910 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100911}
912
Gilles Peskinef603c712019-01-19 13:40:11 +0100913/** Restrict a key policy based on a constraint.
914 *
915 * \param[in,out] policy The policy to restrict.
916 * \param[in] constraint The policy constraint to apply.
917 *
918 * \retval #PSA_SUCCESS
919 * \c *policy contains the intersection of the original value of
920 * \c *policy and \c *constraint.
921 * \retval #PSA_ERROR_INVALID_ARGUMENT
922 * \c *policy and \c *constraint are incompatible.
923 * \c *policy is unchanged.
924 */
925static psa_status_t psa_restrict_key_policy(
926 psa_key_policy_t *policy,
927 const psa_key_policy_t *constraint )
928{
929 psa_algorithm_t intersection_alg =
930 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200931 psa_algorithm_t intersection_alg2 =
932 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100933 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
934 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200935 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
936 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100937 policy->usage &= constraint->usage;
938 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200939 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100940 return( PSA_SUCCESS );
941}
942
Darryl Green06fd18d2018-07-16 11:21:11 +0100943/** Retrieve a slot which must contain a key. The key must have allow all the
944 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
945 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100946static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100948 psa_key_usage_t usage,
949 psa_algorithm_t alg )
950{
951 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100952 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100953
954 *p_slot = NULL;
955
Gilles Peskinec5487a82018-12-03 18:08:14 +0100956 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100957 if( status != PSA_SUCCESS )
958 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100959
960 /* Enforce that usage policy for the key slot contains all the flags
961 * required by the usage parameter. There is one exception: public
962 * keys can always be exported, so we treat public key objects as
963 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200964 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100965 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100967 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100968
969 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200970 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100971 return( PSA_ERROR_NOT_PERMITTED );
972
973 *p_slot = slot;
974 return( PSA_SUCCESS );
975}
Darryl Green940d72c2018-07-13 13:18:51 +0100976
Gilles Peskine28f8f302019-07-24 13:30:31 +0200977/** Retrieve a slot which must contain a transparent key.
978 *
979 * A transparent key is a key for which the key material is directly
980 * available, as opposed to a key in a secure element.
981 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200982 * This is a temporary function to use instead of psa_get_key_from_slot()
983 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200984 */
985#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
986static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
987 psa_key_slot_t **p_slot,
988 psa_key_usage_t usage,
989 psa_algorithm_t alg )
990{
991 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
992 if( status != PSA_SUCCESS )
993 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200994 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200995 {
996 *p_slot = NULL;
997 return( PSA_ERROR_NOT_SUPPORTED );
998 }
999 return( PSA_SUCCESS );
1000}
1001#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1002/* With no secure element support, all keys are transparent. */
1003#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1004 psa_get_key_from_slot( handle, p_slot, usage, alg )
1005#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1006
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001008static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001009{
Gilles Peskine73167e12019-07-12 23:44:37 +02001010#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1011 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001012 {
1013 /* No key material to clean. */
1014 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001015 else
1016#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001017 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001018 {
1019 /* No key material to clean. */
1020 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001022 {
1023 mbedtls_free( slot->data.raw.data );
1024 }
1025 else
1026#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001027 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001028 {
1029 mbedtls_rsa_free( slot->data.rsa );
1030 mbedtls_free( slot->data.rsa );
1031 }
1032 else
1033#endif /* defined(MBEDTLS_RSA_C) */
1034#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001036 {
1037 mbedtls_ecp_keypair_free( slot->data.ecp );
1038 mbedtls_free( slot->data.ecp );
1039 }
1040 else
1041#endif /* defined(MBEDTLS_ECP_C) */
1042 {
1043 /* Shouldn't happen: the key type is not any type that we
1044 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001045 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001046 }
1047
1048 return( PSA_SUCCESS );
1049}
1050
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051/** Completely wipe a slot in memory, including its policy.
1052 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001053psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054{
1055 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001056 /* Multipart operations may still be using the key. This is safe
1057 * because all multipart operation objects are independent from
1058 * the key slot: if they need to access the key after the setup
1059 * phase, they have a copy of the key. Note that this means that
1060 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001061 /* At this point, key material and other type-specific content has
1062 * been wiped. Clear remaining metadata. We can call memset and not
1063 * zeroize because the metadata is not particularly sensitive. */
1064 memset( slot, 0, sizeof( *slot ) );
1065 return( status );
1066}
1067
Gilles Peskinec5487a82018-12-03 18:08:14 +01001068psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001070 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 psa_status_t status; /* status of the last operation */
1072 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001073#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1074 psa_se_drv_table_entry_t *driver;
1075#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076
Gilles Peskine1841cf42019-10-08 15:48:25 +02001077 if( handle == 0 )
1078 return( PSA_SUCCESS );
1079
Gilles Peskinec5487a82018-12-03 18:08:14 +01001080 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081 if( status != PSA_SUCCESS )
1082 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001083
1084#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001085 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001086 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001087 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001088 /* For a key in a secure element, we need to do three things:
1089 * remove the key file in internal storage, destroy the
1090 * key inside the secure element, and update the driver's
1091 * persistent data. Start a transaction that will encompass these
1092 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001093 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001094 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001095 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001096 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001097 status = psa_crypto_save_transaction( );
1098 if( status != PSA_SUCCESS )
1099 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001100 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001101 /* We should still try to destroy the key in the secure
1102 * element and the key metadata in storage. This is especially
1103 * important if the error is that the storage is full.
1104 * But how to do it exactly without risking an inconsistent
1105 * state after a reset?
1106 * https://github.com/ARMmbed/mbed-crypto/issues/215
1107 */
1108 overall_status = status;
1109 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001110 }
1111
Gilles Peskine354f7672019-07-12 23:46:38 +02001112 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001113 if( overall_status == PSA_SUCCESS )
1114 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001115 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001116#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1117
Darryl Greend49a4992018-06-18 17:27:26 +01001118#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001119 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001120 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001121 status = psa_destroy_persistent_key( slot->attr.id );
1122 if( overall_status == PSA_SUCCESS )
1123 overall_status = status;
1124
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001125 /* TODO: other slots may have a copy of the same key. We should
1126 * invalidate them.
1127 * https://github.com/ARMmbed/mbed-crypto/issues/214
1128 */
Darryl Greend49a4992018-06-18 17:27:26 +01001129 }
1130#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001131
Gilles Peskinefc762652019-07-22 19:30:34 +02001132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1133 if( driver != NULL )
1134 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001135 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 if( overall_status == PSA_SUCCESS )
1137 overall_status = status;
1138 status = psa_crypto_stop_transaction( );
1139 if( overall_status == PSA_SUCCESS )
1140 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001141 }
1142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1143
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1145exit:
1146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001147 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1149 if( overall_status == PSA_SUCCESS )
1150 overall_status = status;
1151 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152}
1153
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001154void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001155{
Gilles Peskineb699f072019-04-26 16:06:02 +02001156 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001157 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001158}
1159
Gilles Peskineb699f072019-04-26 16:06:02 +02001160psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1161 psa_key_type_t type,
1162 const uint8_t *data,
1163 size_t data_length )
1164{
1165 uint8_t *copy = NULL;
1166
1167 if( data_length != 0 )
1168 {
1169 copy = mbedtls_calloc( 1, data_length );
1170 if( copy == NULL )
1171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1172 memcpy( copy, data, data_length );
1173 }
1174 /* After this point, this function is guaranteed to succeed, so it
1175 * can start modifying `*attributes`. */
1176
1177 if( attributes->domain_parameters != NULL )
1178 {
1179 mbedtls_free( attributes->domain_parameters );
1180 attributes->domain_parameters = NULL;
1181 attributes->domain_parameters_size = 0;
1182 }
1183
1184 attributes->domain_parameters = copy;
1185 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001186 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001187 return( PSA_SUCCESS );
1188}
1189
1190psa_status_t psa_get_key_domain_parameters(
1191 const psa_key_attributes_t *attributes,
1192 uint8_t *data, size_t data_size, size_t *data_length )
1193{
1194 if( attributes->domain_parameters_size > data_size )
1195 return( PSA_ERROR_BUFFER_TOO_SMALL );
1196 *data_length = attributes->domain_parameters_size;
1197 if( attributes->domain_parameters_size != 0 )
1198 memcpy( data, attributes->domain_parameters,
1199 attributes->domain_parameters_size );
1200 return( PSA_SUCCESS );
1201}
1202
1203#if defined(MBEDTLS_RSA_C)
1204static psa_status_t psa_get_rsa_public_exponent(
1205 const mbedtls_rsa_context *rsa,
1206 psa_key_attributes_t *attributes )
1207{
1208 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 uint8_t *buffer = NULL;
1211 size_t buflen;
1212 mbedtls_mpi_init( &mpi );
1213
1214 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1215 if( ret != 0 )
1216 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001217 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1218 {
1219 /* It's the default value, which is reported as an empty string,
1220 * so there's nothing to do. */
1221 goto exit;
1222 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001223
1224 buflen = mbedtls_mpi_size( &mpi );
1225 buffer = mbedtls_calloc( 1, buflen );
1226 if( buffer == NULL )
1227 {
1228 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1229 goto exit;
1230 }
1231 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1232 if( ret != 0 )
1233 goto exit;
1234 attributes->domain_parameters = buffer;
1235 attributes->domain_parameters_size = buflen;
1236
1237exit:
1238 mbedtls_mpi_free( &mpi );
1239 if( ret != 0 )
1240 mbedtls_free( buffer );
1241 return( mbedtls_to_psa_error( ret ) );
1242}
1243#endif /* MBEDTLS_RSA_C */
1244
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001245/** Retrieve all the publicly-accessible attributes of a key.
1246 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001247psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1248 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001249{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001250 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001251 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001253 psa_reset_key_attributes( attributes );
1254
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001255 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001256 if( status != PSA_SUCCESS )
1257 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001258
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001259 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001260 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1261 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1262
1263#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1264 if( psa_key_slot_is_external( slot ) )
1265 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1266#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001267
Gilles Peskine8e338702019-07-30 20:06:31 +02001268 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001269 {
1270#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001271 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001272 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001273#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001274 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001275 * is not yet implemented.
1276 * https://github.com/ARMmbed/mbed-crypto/issues/216
1277 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001278 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001279 break;
1280#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001281 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1282 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001283#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 default:
1285 /* Nothing else to do. */
1286 break;
1287 }
1288
1289 if( status != PSA_SUCCESS )
1290 psa_reset_key_attributes( attributes );
1291 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292}
1293
Gilles Peskinec8000c02019-08-02 20:15:51 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1295psa_status_t psa_get_key_slot_number(
1296 const psa_key_attributes_t *attributes,
1297 psa_key_slot_number_t *slot_number )
1298{
1299 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1300 {
1301 *slot_number = attributes->slot_number;
1302 return( PSA_SUCCESS );
1303 }
1304 else
1305 return( PSA_ERROR_INVALID_ARGUMENT );
1306}
1307#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1308
Jaeden Ameroccdce902019-01-10 11:42:27 +00001309#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001310static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1311 unsigned char *buf, size_t size )
1312{
Janos Follath24eed8d2019-11-22 13:21:35 +00001313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001314 unsigned char *c;
1315 size_t len = 0;
1316
1317 c = buf + size;
1318
1319 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1320
1321 return( (int) len );
1322}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001323#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001324
Gilles Peskinef603c712019-01-19 13:40:11 +01001325static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1326 uint8_t *data,
1327 size_t data_size,
1328 size_t *data_length,
1329 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330{
Gilles Peskine5d309672019-07-12 23:47:28 +02001331#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1332 const psa_drv_se_t *drv;
1333 psa_drv_se_context_t *drv_context;
1334#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1335
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001336 *data_length = 0;
1337
Gilles Peskine8e338702019-07-30 20:06:31 +02001338 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001339 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001340
Gilles Peskinef9168942019-09-12 19:20:29 +02001341 /* Reject a zero-length output buffer now, since this can never be a
1342 * valid key representation. This way we know that data must be a valid
1343 * pointer and we can do things like memset(data, ..., data_size). */
1344 if( data_size == 0 )
1345 return( PSA_ERROR_BUFFER_TOO_SMALL );
1346
Gilles Peskine5d309672019-07-12 23:47:28 +02001347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001348 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001349 {
1350 psa_drv_se_export_key_t method;
1351 if( drv->key_management == NULL )
1352 return( PSA_ERROR_NOT_SUPPORTED );
1353 method = ( export_public_key ?
1354 drv->key_management->p_export_public :
1355 drv->key_management->p_export );
1356 if( method == NULL )
1357 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001358 return( method( drv_context,
1359 slot->data.se.slot_number,
1360 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001361 }
1362#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1363
Gilles Peskine8e338702019-07-30 20:06:31 +02001364 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365 {
1366 if( slot->data.raw.bytes > data_size )
1367 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001368 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1369 memset( data + slot->data.raw.bytes, 0,
1370 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001371 *data_length = slot->data.raw.bytes;
1372 return( PSA_SUCCESS );
1373 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001374#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001375 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001376 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001377 psa_status_t status;
1378
Gilles Peskineb46bef22019-07-30 21:32:04 +02001379 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001380 if( bytes > data_size )
1381 return( PSA_ERROR_BUFFER_TOO_SMALL );
1382 status = mbedtls_to_psa_error(
1383 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1384 if( status != PSA_SUCCESS )
1385 return( status );
1386 memset( data + bytes, 0, data_size - bytes );
1387 *data_length = bytes;
1388 return( PSA_SUCCESS );
1389 }
1390#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391 else
Moran Peker17e36e12018-05-02 12:55:20 +03001392 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001393#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001394 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1395 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001396 {
Moran Pekera998bc62018-04-16 18:16:20 +03001397 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001399 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001400 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001401#if defined(MBEDTLS_RSA_C)
1402 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001403 pk.pk_info = &mbedtls_rsa_info;
1404 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001405#else
1406 return( PSA_ERROR_NOT_SUPPORTED );
1407#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001408 }
1409 else
1410 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001411#if defined(MBEDTLS_ECP_C)
1412 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001413 pk.pk_info = &mbedtls_eckey_info;
1414 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001415#else
1416 return( PSA_ERROR_NOT_SUPPORTED );
1417#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001418 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001419 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001420 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001421 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001422 }
Moran Peker17e36e12018-05-02 12:55:20 +03001423 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001424 {
Moran Peker17e36e12018-05-02 12:55:20 +03001425 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001426 }
Moran Peker60364322018-04-29 11:34:58 +03001427 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001428 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001429 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001430 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001431 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001432 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1433 * Move the data to the beginning and erase remaining data
1434 * at the original location. */
1435 if( 2 * (size_t) ret <= data_size )
1436 {
1437 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001438 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001439 }
1440 else if( (size_t) ret < data_size )
1441 {
1442 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001443 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001444 }
Moran Pekera998bc62018-04-16 18:16:20 +03001445 *data_length = ret;
1446 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001447 }
1448 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001449#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001450 {
1451 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001452 it is valid for a special-purpose implementation to omit
1453 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001454 return( PSA_ERROR_NOT_SUPPORTED );
1455 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001456 }
1457}
1458
Gilles Peskinec5487a82018-12-03 18:08:14 +01001459psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001460 uint8_t *data,
1461 size_t data_size,
1462 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001463{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001464 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001465 psa_status_t status;
1466
1467 /* Set the key to empty now, so that even when there are errors, we always
1468 * set data_length to a value between 0 and data_size. On error, setting
1469 * the key to empty is a good choice because an empty key representation is
1470 * unlikely to be accepted anywhere. */
1471 *data_length = 0;
1472
1473 /* Export requires the EXPORT flag. There is an exception for public keys,
1474 * which don't require any flag, but psa_get_key_from_slot takes
1475 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001476 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001477 if( status != PSA_SUCCESS )
1478 return( status );
1479 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001480 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482
Gilles Peskinec5487a82018-12-03 18:08:14 +01001483psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001484 uint8_t *data,
1485 size_t data_size,
1486 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001487{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001488 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001489 psa_status_t status;
1490
1491 /* Set the key to empty now, so that even when there are errors, we always
1492 * set data_length to a value between 0 and data_size. On error, setting
1493 * the key to empty is a good choice because an empty key representation is
1494 * unlikely to be accepted anywhere. */
1495 *data_length = 0;
1496
1497 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001498 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001499 if( status != PSA_SUCCESS )
1500 return( status );
1501 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001502 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001503}
1504
Gilles Peskine91e8c332019-08-02 19:19:39 +02001505#if defined(static_assert)
1506static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1507 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001508static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001509 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001510static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001511 "One or more key attribute flag is listed as both internal-only and external-only" );
1512#endif
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514/** Validate that a key policy is internally well-formed.
1515 *
1516 * This function only rejects invalid policies. It does not validate the
1517 * consistency of the policy with respect to other attributes of the key
1518 * such as the key type.
1519 */
1520static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001521{
1522 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001523 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001524 PSA_KEY_USAGE_ENCRYPT |
1525 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001526 PSA_KEY_USAGE_SIGN_HASH |
1527 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001528 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1529 return( PSA_ERROR_INVALID_ARGUMENT );
1530
Gilles Peskine4747d192019-04-17 15:05:45 +02001531 return( PSA_SUCCESS );
1532}
1533
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001534/** Validate the internal consistency of key attributes.
1535 *
1536 * This function only rejects invalid attribute values. If does not
1537 * validate the consistency of the attributes with any key data that may
1538 * be involved in the creation of the key.
1539 *
1540 * Call this function early in the key creation process.
1541 *
1542 * \param[in] attributes Key attributes for the new key.
1543 * \param[out] p_drv On any return, the driver for the key, if any.
1544 * NULL for a transparent key.
1545 *
1546 */
1547static psa_status_t psa_validate_key_attributes(
1548 const psa_key_attributes_t *attributes,
1549 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001550{
1551 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552
1553 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001554 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001555 status = psa_validate_persistent_key_parameters(
1556 attributes->core.lifetime, attributes->core.id,
1557 p_drv, 1 );
1558 if( status != PSA_SUCCESS )
1559 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001560 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561
1562 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001563 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564 return( status );
1565
1566 /* Refuse to create overly large keys.
1567 * Note that this doesn't trigger on import if the attributes don't
1568 * explicitly specify a size (so psa_get_key_bits returns 0), so
1569 * psa_import_key() needs its own checks. */
1570 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1571 return( PSA_ERROR_NOT_SUPPORTED );
1572
Gilles Peskine91e8c332019-08-02 19:19:39 +02001573 /* Reject invalid flags. These should not be reachable through the API. */
1574 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1575 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1576 return( PSA_ERROR_INVALID_ARGUMENT );
1577
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001578 return( PSA_SUCCESS );
1579}
1580
Gilles Peskine4747d192019-04-17 15:05:45 +02001581/** Prepare a key slot to receive key material.
1582 *
1583 * This function allocates a key slot and sets its metadata.
1584 *
1585 * If this function fails, call psa_fail_key_creation().
1586 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001587 * This function is intended to be used as follows:
1588 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1589 * it with the specified attributes, and assign it a handle.
1590 * -# Populate the slot with the key material.
1591 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1592 * In case of failure at any step, stop the sequence and call
1593 * psa_fail_key_creation().
1594 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001595 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001596 * \param[in] attributes Key attributes for the new key.
1597 * \param[out] handle On success, a handle for the allocated slot.
1598 * \param[out] p_slot On success, a pointer to the prepared slot.
1599 * \param[out] p_drv On any return, the driver for the key, if any.
1600 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001601 *
1602 * \retval #PSA_SUCCESS
1603 * The key slot is ready to receive key material.
1604 * \return If this function fails, the key slot is an invalid state.
1605 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 */
1607static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001608 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 const psa_key_attributes_t *attributes,
1610 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001611 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001612 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001613{
1614 psa_status_t status;
1615 psa_key_slot_t *slot;
1616
Gilles Peskinedf179142019-07-15 22:02:14 +02001617 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001618 *p_drv = NULL;
1619
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001620 status = psa_validate_key_attributes( attributes, p_drv );
1621 if( status != PSA_SUCCESS )
1622 return( status );
1623
Gilles Peskineedbed562019-08-07 18:19:59 +02001624 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 if( status != PSA_SUCCESS )
1626 return( status );
1627 slot = *p_slot;
1628
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001629 /* We're storing the declared bit-size of the key. It's up to each
1630 * creation mechanism to verify that this information is correct.
1631 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001632 * an input (generate, device) but not for those where the bit-size
1633 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001634
1635 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001636
Gilles Peskine91e8c332019-08-02 19:19:39 +02001637 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001638 * external-only flags, query `attributes`. Thanks to the check
1639 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001640 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001641 * may have set. */
1642 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001643
Gilles Peskinecbaff462019-07-12 23:46:04 +02001644#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001645 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001646 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001647 * create the key file in internal storage, create the
1648 * key inside the secure element, and update the driver's
1649 * persistent data. Start a transaction that will encompass these
1650 * three actions. */
1651 /* The first thing to do is to find a slot number for the new key.
1652 * We save the slot number in persistent storage as part of the
1653 * transaction data. It will be needed to recover if the power
1654 * fails during the key creation process, to clean up on the secure
1655 * element side after restarting. Obtaining a slot number from the
1656 * secure element driver updates its persistent state, but we do not yet
1657 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001658 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001659 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001660 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001661 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001662 &slot->data.se.slot_number );
1663 if( status != PSA_SUCCESS )
1664 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001665 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001666 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001667 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001668 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001669 status = psa_crypto_save_transaction( );
1670 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001671 {
1672 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001673 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001674 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001675 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001676
1677 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1678 {
1679 /* Key registration only makes sense with a secure element. */
1680 return( PSA_ERROR_INVALID_ARGUMENT );
1681 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001682#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1683
Darryl Green0c6575a2018-11-07 16:05:30 +00001684 return( status );
1685}
Gilles Peskine4747d192019-04-17 15:05:45 +02001686
1687/** Finalize the creation of a key once its key material has been set.
1688 *
1689 * This entails writing the key to persistent storage.
1690 *
1691 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001692 * See the documentation of psa_start_key_creation() for the intended use
1693 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 * \param[in,out] slot Pointer to the slot with key material.
1696 * \param[in] driver The secure element driver for the key,
1697 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001698 *
1699 * \retval #PSA_SUCCESS
1700 * The key was successfully created. The handle is now valid.
1701 * \return If this function fails, the key slot is an invalid state.
1702 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001704static psa_status_t psa_finish_key_creation(
1705 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001706 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001707{
1708 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001709 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001711
1712#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001713 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001715#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1716 if( driver != NULL )
1717 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001718 psa_se_key_data_storage_t data;
1719#if defined(static_assert)
1720 static_assert( sizeof( slot->data.se.slot_number ) ==
1721 sizeof( data.slot_number ),
1722 "Slot number size does not match psa_se_key_data_storage_t" );
1723 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1724 "Bit-size size does not match psa_se_key_data_storage_t" );
1725#endif
1726 memcpy( &data.slot_number, &slot->data.se.slot_number,
1727 sizeof( slot->data.se.slot_number ) );
1728 memcpy( &data.bits, &slot->attr.bits,
1729 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001730 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001731 (uint8_t*) &data,
1732 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001733 }
1734 else
1735#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1736 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001737 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001738 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001739 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001740 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1741 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001742 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001743 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1744 status = psa_internal_export_key( slot,
1745 buffer, buffer_size, &length,
1746 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001747 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001748 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001749 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001750
Gilles Peskinef9168942019-09-12 19:20:29 +02001751 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001752 mbedtls_free( buffer );
1753 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001755#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1756
Gilles Peskinecbaff462019-07-12 23:46:04 +02001757#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001758 /* Finish the transaction for a key creation. This does not
1759 * happen when registering an existing key. Detect this case
1760 * by checking whether a transaction is in progress (actual
1761 * creation of a key in a secure element requires a transaction,
1762 * but registration doesn't use one). */
1763 if( driver != NULL &&
1764 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001765 {
1766 status = psa_save_se_persistent_data( driver );
1767 if( status != PSA_SUCCESS )
1768 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001769 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001770 return( status );
1771 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001772 status = psa_crypto_stop_transaction( );
1773 if( status != PSA_SUCCESS )
1774 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001775 }
1776#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1777
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 return( status );
1779}
1780
1781/** Abort the creation of a key.
1782 *
1783 * You may call this function after calling psa_start_key_creation(),
1784 * or after psa_finish_key_creation() fails. In other circumstances, this
1785 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001786 * See the documentation of psa_start_key_creation() for the intended use
1787 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001788 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 * \param[in,out] slot Pointer to the slot with key material.
1790 * \param[in] driver The secure element driver for the key,
1791 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001793static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001794 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001795{
Gilles Peskine011e4282019-06-26 18:34:38 +02001796 (void) driver;
1797
Gilles Peskine4747d192019-04-17 15:05:45 +02001798 if( slot == NULL )
1799 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001800
1801#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001802 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001803 * element, and the failure happened later (when saving metadata
1804 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001805 * element.
1806 * https://github.com/ARMmbed/mbed-crypto/issues/217
1807 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001808
Gilles Peskined7729582019-08-05 15:55:54 +02001809 /* Abort the ongoing transaction if any (there may not be one if
1810 * the creation process failed before starting one, or if the
1811 * key creation is a registration of a key in a secure element).
1812 * Earlier functions must already have done what it takes to undo any
1813 * partial creation. All that's left is to update the transaction data
1814 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001815 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001816#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1817
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 psa_wipe_key_slot( slot );
1819}
1820
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001821/** Validate optional attributes during key creation.
1822 *
1823 * Some key attributes are optional during key creation. If they are
1824 * specified in the attributes structure, check that they are consistent
1825 * with the data in the slot.
1826 *
1827 * This function should be called near the end of key creation, after
1828 * the slot in memory is fully populated but before saving persistent data.
1829 */
1830static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 const psa_key_slot_t *slot,
1832 const psa_key_attributes_t *attributes )
1833{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001834 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001836 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001837 return( PSA_ERROR_INVALID_ARGUMENT );
1838 }
1839
1840 if( attributes->domain_parameters_size != 0 )
1841 {
1842#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001843 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001844 {
1845 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001847 mbedtls_mpi_init( &actual );
1848 mbedtls_mpi_init( &required );
1849 ret = mbedtls_rsa_export( slot->data.rsa,
1850 NULL, NULL, NULL, NULL, &actual );
1851 if( ret != 0 )
1852 goto rsa_exit;
1853 ret = mbedtls_mpi_read_binary( &required,
1854 attributes->domain_parameters,
1855 attributes->domain_parameters_size );
1856 if( ret != 0 )
1857 goto rsa_exit;
1858 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1859 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1860 rsa_exit:
1861 mbedtls_mpi_free( &actual );
1862 mbedtls_mpi_free( &required );
1863 if( ret != 0)
1864 return( mbedtls_to_psa_error( ret ) );
1865 }
1866 else
1867#endif
1868 {
1869 return( PSA_ERROR_INVALID_ARGUMENT );
1870 }
1871 }
1872
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001873 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001874 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001875 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001876 return( PSA_ERROR_INVALID_ARGUMENT );
1877 }
1878
1879 return( PSA_SUCCESS );
1880}
1881
Gilles Peskine4747d192019-04-17 15:05:45 +02001882psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001883 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001884 size_t data_length,
1885 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001886{
1887 psa_status_t status;
1888 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001889 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890
Gilles Peskine0f84d622019-09-12 19:03:13 +02001891 /* Reject zero-length symmetric keys (including raw data key objects).
1892 * This also rejects any key which might be encoded as an empty string,
1893 * which is never valid. */
1894 if( data_length == 0 )
1895 return( PSA_ERROR_INVALID_ARGUMENT );
1896
Gilles Peskinedf179142019-07-15 22:02:14 +02001897 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1898 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 if( status != PSA_SUCCESS )
1900 goto exit;
1901
Gilles Peskine5d309672019-07-12 23:47:28 +02001902#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1903 if( driver != NULL )
1904 {
1905 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001906 /* The driver should set the number of key bits, however in
1907 * case it doesn't, we initialize bits to an invalid value. */
1908 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001909 if( drv->key_management == NULL ||
1910 drv->key_management->p_import == NULL )
1911 {
1912 status = PSA_ERROR_NOT_SUPPORTED;
1913 goto exit;
1914 }
1915 status = drv->key_management->p_import(
1916 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001917 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001918 &bits );
1919 if( status != PSA_SUCCESS )
1920 goto exit;
1921 if( bits > PSA_MAX_KEY_BITS )
1922 {
1923 status = PSA_ERROR_NOT_SUPPORTED;
1924 goto exit;
1925 }
1926 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001927 }
1928 else
1929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1930 {
1931 status = psa_import_key_into_slot( slot, data, data_length );
1932 if( status != PSA_SUCCESS )
1933 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001934 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001935 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001936 if( status != PSA_SUCCESS )
1937 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938
Gilles Peskine011e4282019-06-26 18:34:38 +02001939 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001941 if( status != PSA_SUCCESS )
1942 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001943 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001944 *handle = 0;
1945 }
1946 return( status );
1947}
1948
Gilles Peskined7729582019-08-05 15:55:54 +02001949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1950psa_status_t mbedtls_psa_register_se_key(
1951 const psa_key_attributes_t *attributes )
1952{
1953 psa_status_t status;
1954 psa_key_slot_t *slot = NULL;
1955 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001956 psa_key_handle_t handle = 0;
1957
1958 /* Leaving attributes unspecified is not currently supported.
1959 * It could make sense to query the key type and size from the
1960 * secure element, but not all secure elements support this
1961 * and the driver HAL doesn't currently support it. */
1962 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1963 return( PSA_ERROR_NOT_SUPPORTED );
1964 if( psa_get_key_bits( attributes ) == 0 )
1965 return( PSA_ERROR_NOT_SUPPORTED );
1966
1967 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1968 &handle, &slot, &driver );
1969 if( status != PSA_SUCCESS )
1970 goto exit;
1971
Gilles Peskined7729582019-08-05 15:55:54 +02001972 status = psa_finish_key_creation( slot, driver );
1973
1974exit:
1975 if( status != PSA_SUCCESS )
1976 {
1977 psa_fail_key_creation( slot, driver );
1978 }
1979 /* Registration doesn't keep the key in RAM. */
1980 psa_close_key( handle );
1981 return( status );
1982}
1983#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1984
Gilles Peskinef603c712019-01-19 13:40:11 +01001985static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001987{
1988 psa_status_t status;
1989 uint8_t *buffer = NULL;
1990 size_t buffer_size = 0;
1991 size_t length;
1992
Gilles Peskine8e338702019-07-30 20:06:31 +02001993 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001994 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001995 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001996 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001997 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1999 if( status != PSA_SUCCESS )
2000 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002001 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002003
2004exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002005 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002006 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002007 return( status );
2008}
2009
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2011 const psa_key_attributes_t *specified_attributes,
2012 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002013{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002015 psa_key_slot_t *source_slot = NULL;
2016 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002017 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002018 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002019
Gilles Peskine28f8f302019-07-24 13:30:31 +02002020 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002021 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002022 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002023 goto exit;
2024
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002025 status = psa_validate_optional_attributes( source_slot,
2026 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027 if( status != PSA_SUCCESS )
2028 goto exit;
2029
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002030 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002031 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002032 if( status != PSA_SUCCESS )
2033 goto exit;
2034
Gilles Peskinedf179142019-07-15 22:02:14 +02002035 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2036 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002037 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002038 if( status != PSA_SUCCESS )
2039 goto exit;
2040
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2042 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002043 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002044 /* Copying to a secure element is not implemented yet. */
2045 status = PSA_ERROR_NOT_SUPPORTED;
2046 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002047 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002048#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002049
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002050 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002051 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002053
Gilles Peskine011e4282019-06-26 18:34:38 +02002054 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002055exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056 if( status != PSA_SUCCESS )
2057 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002058 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002059 *target_handle = 0;
2060 }
2061 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002062}
2063
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002064
2065
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002066/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002067/* Message digests */
2068/****************************************************************/
2069
Gilles Peskineb16841e2019-10-10 20:36:12 +02002070#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002071static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002072{
2073 switch( alg )
2074 {
2075#if defined(MBEDTLS_MD2_C)
2076 case PSA_ALG_MD2:
2077 return( &mbedtls_md2_info );
2078#endif
2079#if defined(MBEDTLS_MD4_C)
2080 case PSA_ALG_MD4:
2081 return( &mbedtls_md4_info );
2082#endif
2083#if defined(MBEDTLS_MD5_C)
2084 case PSA_ALG_MD5:
2085 return( &mbedtls_md5_info );
2086#endif
2087#if defined(MBEDTLS_RIPEMD160_C)
2088 case PSA_ALG_RIPEMD160:
2089 return( &mbedtls_ripemd160_info );
2090#endif
2091#if defined(MBEDTLS_SHA1_C)
2092 case PSA_ALG_SHA_1:
2093 return( &mbedtls_sha1_info );
2094#endif
2095#if defined(MBEDTLS_SHA256_C)
2096 case PSA_ALG_SHA_224:
2097 return( &mbedtls_sha224_info );
2098 case PSA_ALG_SHA_256:
2099 return( &mbedtls_sha256_info );
2100#endif
2101#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002102#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002103 case PSA_ALG_SHA_384:
2104 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002105#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002106 case PSA_ALG_SHA_512:
2107 return( &mbedtls_sha512_info );
2108#endif
2109 default:
2110 return( NULL );
2111 }
2112}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002113#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002114
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2116{
2117 switch( operation->alg )
2118 {
Gilles Peskine81736312018-06-26 15:04:31 +02002119 case 0:
2120 /* The object has (apparently) been initialized but it is not
2121 * in use. It's ok to call abort on such an object, and there's
2122 * nothing to do. */
2123 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124#if defined(MBEDTLS_MD2_C)
2125 case PSA_ALG_MD2:
2126 mbedtls_md2_free( &operation->ctx.md2 );
2127 break;
2128#endif
2129#if defined(MBEDTLS_MD4_C)
2130 case PSA_ALG_MD4:
2131 mbedtls_md4_free( &operation->ctx.md4 );
2132 break;
2133#endif
2134#if defined(MBEDTLS_MD5_C)
2135 case PSA_ALG_MD5:
2136 mbedtls_md5_free( &operation->ctx.md5 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_RIPEMD160_C)
2140 case PSA_ALG_RIPEMD160:
2141 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2142 break;
2143#endif
2144#if defined(MBEDTLS_SHA1_C)
2145 case PSA_ALG_SHA_1:
2146 mbedtls_sha1_free( &operation->ctx.sha1 );
2147 break;
2148#endif
2149#if defined(MBEDTLS_SHA256_C)
2150 case PSA_ALG_SHA_224:
2151 case PSA_ALG_SHA_256:
2152 mbedtls_sha256_free( &operation->ctx.sha256 );
2153 break;
2154#endif
2155#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002156#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002158#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159 case PSA_ALG_SHA_512:
2160 mbedtls_sha512_free( &operation->ctx.sha512 );
2161 break;
2162#endif
2163 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002164 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165 }
2166 operation->alg = 0;
2167 return( PSA_SUCCESS );
2168}
2169
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002170psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171 psa_algorithm_t alg )
2172{
Janos Follath24eed8d2019-11-22 13:21:35 +00002173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002174
2175 /* A context must be freshly initialized before it can be set up. */
2176 if( operation->alg != 0 )
2177 {
2178 return( PSA_ERROR_BAD_STATE );
2179 }
2180
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181 switch( alg )
2182 {
2183#if defined(MBEDTLS_MD2_C)
2184 case PSA_ALG_MD2:
2185 mbedtls_md2_init( &operation->ctx.md2 );
2186 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2187 break;
2188#endif
2189#if defined(MBEDTLS_MD4_C)
2190 case PSA_ALG_MD4:
2191 mbedtls_md4_init( &operation->ctx.md4 );
2192 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2193 break;
2194#endif
2195#if defined(MBEDTLS_MD5_C)
2196 case PSA_ALG_MD5:
2197 mbedtls_md5_init( &operation->ctx.md5 );
2198 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2199 break;
2200#endif
2201#if defined(MBEDTLS_RIPEMD160_C)
2202 case PSA_ALG_RIPEMD160:
2203 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2204 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2205 break;
2206#endif
2207#if defined(MBEDTLS_SHA1_C)
2208 case PSA_ALG_SHA_1:
2209 mbedtls_sha1_init( &operation->ctx.sha1 );
2210 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2211 break;
2212#endif
2213#if defined(MBEDTLS_SHA256_C)
2214 case PSA_ALG_SHA_224:
2215 mbedtls_sha256_init( &operation->ctx.sha256 );
2216 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2217 break;
2218 case PSA_ALG_SHA_256:
2219 mbedtls_sha256_init( &operation->ctx.sha256 );
2220 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2221 break;
2222#endif
2223#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002224#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002225 case PSA_ALG_SHA_384:
2226 mbedtls_sha512_init( &operation->ctx.sha512 );
2227 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2228 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002229#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230 case PSA_ALG_SHA_512:
2231 mbedtls_sha512_init( &operation->ctx.sha512 );
2232 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2233 break;
2234#endif
2235 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002236 return( PSA_ALG_IS_HASH( alg ) ?
2237 PSA_ERROR_NOT_SUPPORTED :
2238 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 }
2240 if( ret == 0 )
2241 operation->alg = alg;
2242 else
2243 psa_hash_abort( operation );
2244 return( mbedtls_to_psa_error( ret ) );
2245}
2246
2247psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2248 const uint8_t *input,
2249 size_t input_length )
2250{
Janos Follath24eed8d2019-11-22 13:21:35 +00002251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002252
2253 /* Don't require hash implementations to behave correctly on a
2254 * zero-length input, which may have an invalid pointer. */
2255 if( input_length == 0 )
2256 return( PSA_SUCCESS );
2257
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 switch( operation->alg )
2259 {
2260#if defined(MBEDTLS_MD2_C)
2261 case PSA_ALG_MD2:
2262 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2263 input, input_length );
2264 break;
2265#endif
2266#if defined(MBEDTLS_MD4_C)
2267 case PSA_ALG_MD4:
2268 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2269 input, input_length );
2270 break;
2271#endif
2272#if defined(MBEDTLS_MD5_C)
2273 case PSA_ALG_MD5:
2274 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2275 input, input_length );
2276 break;
2277#endif
2278#if defined(MBEDTLS_RIPEMD160_C)
2279 case PSA_ALG_RIPEMD160:
2280 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2281 input, input_length );
2282 break;
2283#endif
2284#if defined(MBEDTLS_SHA1_C)
2285 case PSA_ALG_SHA_1:
2286 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2287 input, input_length );
2288 break;
2289#endif
2290#if defined(MBEDTLS_SHA256_C)
2291 case PSA_ALG_SHA_224:
2292 case PSA_ALG_SHA_256:
2293 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2294 input, input_length );
2295 break;
2296#endif
2297#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002298#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002300#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301 case PSA_ALG_SHA_512:
2302 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2303 input, input_length );
2304 break;
2305#endif
2306 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002307 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002309
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310 if( ret != 0 )
2311 psa_hash_abort( operation );
2312 return( mbedtls_to_psa_error( ret ) );
2313}
2314
2315psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2316 uint8_t *hash,
2317 size_t hash_size,
2318 size_t *hash_length )
2319{
itayzafrir40835d42018-08-02 13:14:17 +03002320 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002322 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323
2324 /* Fill the output buffer with something that isn't a valid hash
2325 * (barring an attack on the hash and deliberately-crafted input),
2326 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002327 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002328 /* If hash_size is 0 then hash may be NULL and then the
2329 * call to memset would have undefined behavior. */
2330 if( hash_size != 0 )
2331 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332
2333 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002334 {
2335 status = PSA_ERROR_BUFFER_TOO_SMALL;
2336 goto exit;
2337 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338
2339 switch( operation->alg )
2340 {
2341#if defined(MBEDTLS_MD2_C)
2342 case PSA_ALG_MD2:
2343 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2344 break;
2345#endif
2346#if defined(MBEDTLS_MD4_C)
2347 case PSA_ALG_MD4:
2348 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2349 break;
2350#endif
2351#if defined(MBEDTLS_MD5_C)
2352 case PSA_ALG_MD5:
2353 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2354 break;
2355#endif
2356#if defined(MBEDTLS_RIPEMD160_C)
2357 case PSA_ALG_RIPEMD160:
2358 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2359 break;
2360#endif
2361#if defined(MBEDTLS_SHA1_C)
2362 case PSA_ALG_SHA_1:
2363 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2364 break;
2365#endif
2366#if defined(MBEDTLS_SHA256_C)
2367 case PSA_ALG_SHA_224:
2368 case PSA_ALG_SHA_256:
2369 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2370 break;
2371#endif
2372#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002373#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002375#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376 case PSA_ALG_SHA_512:
2377 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2378 break;
2379#endif
2380 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002381 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382 }
itayzafrir40835d42018-08-02 13:14:17 +03002383 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002384
itayzafrir40835d42018-08-02 13:14:17 +03002385exit:
2386 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002388 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389 return( psa_hash_abort( operation ) );
2390 }
2391 else
2392 {
2393 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002394 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395 }
2396}
2397
Gilles Peskine2d277862018-06-18 15:41:12 +02002398psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2399 const uint8_t *hash,
2400 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401{
2402 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2403 size_t actual_hash_length;
2404 psa_status_t status = psa_hash_finish( operation,
2405 actual_hash, sizeof( actual_hash ),
2406 &actual_hash_length );
2407 if( status != PSA_SUCCESS )
2408 return( status );
2409 if( actual_hash_length != hash_length )
2410 return( PSA_ERROR_INVALID_SIGNATURE );
2411 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2412 return( PSA_ERROR_INVALID_SIGNATURE );
2413 return( PSA_SUCCESS );
2414}
2415
Gilles Peskine0a749c82019-11-28 19:33:58 +01002416psa_status_t psa_hash_compute( psa_algorithm_t alg,
2417 const uint8_t *input, size_t input_length,
2418 uint8_t *hash, size_t hash_size,
2419 size_t *hash_length )
2420{
2421 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2423
2424 *hash_length = hash_size;
2425 status = psa_hash_setup( &operation, alg );
2426 if( status != PSA_SUCCESS )
2427 goto exit;
2428 status = psa_hash_update( &operation, input, input_length );
2429 if( status != PSA_SUCCESS )
2430 goto exit;
2431 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2432 if( status != PSA_SUCCESS )
2433 goto exit;
2434
2435exit:
2436 if( status == PSA_SUCCESS )
2437 status = psa_hash_abort( &operation );
2438 else
2439 psa_hash_abort( &operation );
2440 return( status );
2441}
2442
2443psa_status_t psa_hash_compare( psa_algorithm_t alg,
2444 const uint8_t *input, size_t input_length,
2445 const uint8_t *hash, size_t hash_length )
2446{
2447 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2449
2450 status = psa_hash_setup( &operation, alg );
2451 if( status != PSA_SUCCESS )
2452 goto exit;
2453 status = psa_hash_update( &operation, input, input_length );
2454 if( status != PSA_SUCCESS )
2455 goto exit;
2456 status = psa_hash_verify( &operation, hash, hash_length );
2457 if( status != PSA_SUCCESS )
2458 goto exit;
2459
2460exit:
2461 if( status == PSA_SUCCESS )
2462 status = psa_hash_abort( &operation );
2463 else
2464 psa_hash_abort( &operation );
2465 return( status );
2466}
2467
Gilles Peskineeb35d782019-01-22 17:56:16 +01002468psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2469 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002470{
2471 if( target_operation->alg != 0 )
2472 return( PSA_ERROR_BAD_STATE );
2473
2474 switch( source_operation->alg )
2475 {
2476 case 0:
2477 return( PSA_ERROR_BAD_STATE );
2478#if defined(MBEDTLS_MD2_C)
2479 case PSA_ALG_MD2:
2480 mbedtls_md2_clone( &target_operation->ctx.md2,
2481 &source_operation->ctx.md2 );
2482 break;
2483#endif
2484#if defined(MBEDTLS_MD4_C)
2485 case PSA_ALG_MD4:
2486 mbedtls_md4_clone( &target_operation->ctx.md4,
2487 &source_operation->ctx.md4 );
2488 break;
2489#endif
2490#if defined(MBEDTLS_MD5_C)
2491 case PSA_ALG_MD5:
2492 mbedtls_md5_clone( &target_operation->ctx.md5,
2493 &source_operation->ctx.md5 );
2494 break;
2495#endif
2496#if defined(MBEDTLS_RIPEMD160_C)
2497 case PSA_ALG_RIPEMD160:
2498 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2499 &source_operation->ctx.ripemd160 );
2500 break;
2501#endif
2502#if defined(MBEDTLS_SHA1_C)
2503 case PSA_ALG_SHA_1:
2504 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2505 &source_operation->ctx.sha1 );
2506 break;
2507#endif
2508#if defined(MBEDTLS_SHA256_C)
2509 case PSA_ALG_SHA_224:
2510 case PSA_ALG_SHA_256:
2511 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2512 &source_operation->ctx.sha256 );
2513 break;
2514#endif
2515#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002516#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002517 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002518#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002519 case PSA_ALG_SHA_512:
2520 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2521 &source_operation->ctx.sha512 );
2522 break;
2523#endif
2524 default:
2525 return( PSA_ERROR_NOT_SUPPORTED );
2526 }
2527
2528 target_operation->alg = source_operation->alg;
2529 return( PSA_SUCCESS );
2530}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002531
2532
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534/* MAC */
2535/****************************************************************/
2536
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002537static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 psa_algorithm_t alg,
2539 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002540 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002541 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002544 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002546 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002547 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002548
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2550 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002551 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002553 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002554 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 mode = MBEDTLS_MODE_STREAM;
2556 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 case PSA_ALG_CTR:
2558 mode = MBEDTLS_MODE_CTR;
2559 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002560 case PSA_ALG_CFB:
2561 mode = MBEDTLS_MODE_CFB;
2562 break;
2563 case PSA_ALG_OFB:
2564 mode = MBEDTLS_MODE_OFB;
2565 break;
2566 case PSA_ALG_CBC_NO_PADDING:
2567 mode = MBEDTLS_MODE_CBC;
2568 break;
2569 case PSA_ALG_CBC_PKCS7:
2570 mode = MBEDTLS_MODE_CBC;
2571 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002572 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573 mode = MBEDTLS_MODE_CCM;
2574 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002575 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576 mode = MBEDTLS_MODE_GCM;
2577 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002578 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2579 mode = MBEDTLS_MODE_CHACHAPOLY;
2580 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581 default:
2582 return( NULL );
2583 }
2584 }
2585 else if( alg == PSA_ALG_CMAC )
2586 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587 else
2588 return( NULL );
2589
2590 switch( key_type )
2591 {
2592 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002593 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594 break;
2595 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002596 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2597 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002599 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002601 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002602 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2603 * but two-key Triple-DES is functionally three-key Triple-DES
2604 * with K1=K3, so that's how we present it to mbedtls. */
2605 if( key_bits == 128 )
2606 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 break;
2608 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002609 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610 break;
2611 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002612 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002614 case PSA_KEY_TYPE_CHACHA20:
2615 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2616 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 default:
2618 return( NULL );
2619 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002620 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002621 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622
Jaeden Amero23bbb752018-06-26 14:16:54 +01002623 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2624 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625}
2626
Gilles Peskinea05219c2018-11-16 16:02:56 +01002627#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002628static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002629{
Gilles Peskine2d277862018-06-18 15:41:12 +02002630 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002631 {
2632 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002633 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002634 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002635 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002636 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002637 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002638 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002639 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002640 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002641 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002642 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002643 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002644 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002645 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002646 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002647 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002648 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002649 return( 128 );
2650 default:
2651 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002652 }
2653}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002654#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002655
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656/* Initialize the MAC operation structure. Once this function has been
2657 * called, psa_mac_abort can run and will do the right thing. */
2658static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2659 psa_algorithm_t alg )
2660{
2661 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2662
2663 operation->alg = alg;
2664 operation->key_set = 0;
2665 operation->iv_set = 0;
2666 operation->iv_required = 0;
2667 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002668 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002669
2670#if defined(MBEDTLS_CMAC_C)
2671 if( alg == PSA_ALG_CMAC )
2672 {
2673 operation->iv_required = 0;
2674 mbedtls_cipher_init( &operation->ctx.cmac );
2675 status = PSA_SUCCESS;
2676 }
2677 else
2678#endif /* MBEDTLS_CMAC_C */
2679#if defined(MBEDTLS_MD_C)
2680 if( PSA_ALG_IS_HMAC( operation->alg ) )
2681 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002682 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2683 operation->ctx.hmac.hash_ctx.alg = 0;
2684 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002685 }
2686 else
2687#endif /* MBEDTLS_MD_C */
2688 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002689 if( ! PSA_ALG_IS_MAC( alg ) )
2690 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002691 }
2692
2693 if( status != PSA_SUCCESS )
2694 memset( operation, 0, sizeof( *operation ) );
2695 return( status );
2696}
2697
Gilles Peskine01126fa2018-07-12 17:04:55 +02002698#if defined(MBEDTLS_MD_C)
2699static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2700{
Gilles Peskine3f108122018-12-07 18:14:53 +01002701 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002702 return( psa_hash_abort( &hmac->hash_ctx ) );
2703}
2704#endif /* MBEDTLS_MD_C */
2705
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2707{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002708 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002710 /* The object has (apparently) been initialized but it is not
2711 * in use. It's ok to call abort on such an object, and there's
2712 * nothing to do. */
2713 return( PSA_SUCCESS );
2714 }
2715 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002717 if( operation->alg == PSA_ALG_CMAC )
2718 {
2719 mbedtls_cipher_free( &operation->ctx.cmac );
2720 }
2721 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002724 if( PSA_ALG_IS_HMAC( operation->alg ) )
2725 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002726 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002727 }
2728 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002730 {
2731 /* Sanity check (shouldn't happen: operation->alg should
2732 * always have been initialized to a valid value). */
2733 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734 }
Moran Peker41deec42018-04-04 15:43:05 +03002735
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736 operation->alg = 0;
2737 operation->key_set = 0;
2738 operation->iv_set = 0;
2739 operation->iv_required = 0;
2740 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002741 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002742
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002744
2745bad_state:
2746 /* If abort is called on an uninitialized object, we can't trust
2747 * anything. Wipe the object in case it contains confidential data.
2748 * This may result in a memory leak if a pointer gets overwritten,
2749 * but it's too late to do anything about this. */
2750 memset( operation, 0, sizeof( *operation ) );
2751 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752}
2753
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002754#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002755static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002757 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002758 const mbedtls_cipher_info_t *cipher_info )
2759{
Janos Follath24eed8d2019-11-22 13:21:35 +00002760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002761
2762 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763
2764 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2765 if( ret != 0 )
2766 return( ret );
2767
2768 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2769 slot->data.raw.data,
2770 key_bits );
2771 return( ret );
2772}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002773#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002774
Gilles Peskine248051a2018-06-20 16:09:38 +02002775#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002776static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2777 const uint8_t *key,
2778 size_t key_length,
2779 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002780{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002781 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002782 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002783 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002784 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785 psa_status_t status;
2786
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002787 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2788 * overflow below. This should never trigger if the hash algorithm
2789 * is implemented correctly. */
2790 /* The size checks against the ipad and opad buffers cannot be written
2791 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2792 * because that triggers -Wlogical-op on GCC 7.3. */
2793 if( block_size > sizeof( ipad ) )
2794 return( PSA_ERROR_NOT_SUPPORTED );
2795 if( block_size > sizeof( hmac->opad ) )
2796 return( PSA_ERROR_NOT_SUPPORTED );
2797 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002798 return( PSA_ERROR_NOT_SUPPORTED );
2799
Gilles Peskined223b522018-06-11 18:12:58 +02002800 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002802 status = psa_hash_compute( hash_alg, key, key_length,
2803 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002804 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002805 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002806 }
Gilles Peskine96889972018-07-12 17:07:03 +02002807 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2808 * but it is permitted. It is common when HMAC is used in HKDF, for
2809 * example. Don't call `memcpy` in the 0-length because `key` could be
2810 * an invalid pointer which would make the behavior undefined. */
2811 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002812 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002813
Gilles Peskined223b522018-06-11 18:12:58 +02002814 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2815 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002816 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002817 ipad[i] ^= 0x36;
2818 memset( ipad + key_length, 0x36, block_size - key_length );
2819
2820 /* Copy the key material from ipad to opad, flipping the requisite bits,
2821 * and filling the rest of opad with the requisite constant. */
2822 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002823 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2824 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002825
Gilles Peskine01126fa2018-07-12 17:04:55 +02002826 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002827 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002828 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002829
Gilles Peskine01126fa2018-07-12 17:04:55 +02002830 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002831
2832cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002833 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002834
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002835 return( status );
2836}
Gilles Peskine248051a2018-06-20 16:09:38 +02002837#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002838
Gilles Peskine89167cb2018-07-08 20:12:23 +02002839static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002840 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002841 psa_algorithm_t alg,
2842 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002845 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002847 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002848 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002849 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002850 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002852 /* A context must be freshly initialized before it can be set up. */
2853 if( operation->alg != 0 )
2854 {
2855 return( PSA_ERROR_BAD_STATE );
2856 }
2857
Gilles Peskined911eb72018-08-14 15:18:45 +02002858 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002859 if( status != PSA_SUCCESS )
2860 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002861 if( is_sign )
2862 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002863
Gilles Peskine28f8f302019-07-24 13:30:31 +02002864 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002865 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002867 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002868
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002870 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 {
2872 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002873 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002874 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 if( cipher_info == NULL )
2877 {
2878 status = PSA_ERROR_NOT_SUPPORTED;
2879 goto exit;
2880 }
2881 operation->mac_size = cipher_info->block_size;
2882 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2883 status = mbedtls_to_psa_error( ret );
2884 }
2885 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002887#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002888 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002890 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002891 if( hash_alg == 0 )
2892 {
2893 status = PSA_ERROR_NOT_SUPPORTED;
2894 goto exit;
2895 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002896
2897 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2898 /* Sanity check. This shouldn't fail on a valid configuration. */
2899 if( operation->mac_size == 0 ||
2900 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2901 {
2902 status = PSA_ERROR_NOT_SUPPORTED;
2903 goto exit;
2904 }
2905
Gilles Peskine8e338702019-07-30 20:06:31 +02002906 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002907 {
2908 status = PSA_ERROR_INVALID_ARGUMENT;
2909 goto exit;
2910 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002911
Gilles Peskine01126fa2018-07-12 17:04:55 +02002912 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2913 slot->data.raw.data,
2914 slot->data.raw.bytes,
2915 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002916 }
2917 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002919 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002920 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002921 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002923
Gilles Peskined911eb72018-08-14 15:18:45 +02002924 if( truncated == 0 )
2925 {
2926 /* The "normal" case: untruncated algorithm. Nothing to do. */
2927 }
2928 else if( truncated < 4 )
2929 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002930 /* A very short MAC is too short for security since it can be
2931 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2932 * so we make this our minimum, even though 32 bits is still
2933 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002934 status = PSA_ERROR_NOT_SUPPORTED;
2935 }
2936 else if( truncated > operation->mac_size )
2937 {
2938 /* It's impossible to "truncate" to a larger length. */
2939 status = PSA_ERROR_INVALID_ARGUMENT;
2940 }
2941 else
2942 operation->mac_size = truncated;
2943
Gilles Peskinefbfac682018-07-08 20:51:54 +02002944exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002947 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949 else
2950 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951 operation->key_set = 1;
2952 }
2953 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954}
2955
Gilles Peskine89167cb2018-07-08 20:12:23 +02002956psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002957 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002958 psa_algorithm_t alg )
2959{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002960 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002961}
2962
2963psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002964 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002965 psa_algorithm_t alg )
2966{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002967 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002968}
2969
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2971 const uint8_t *input,
2972 size_t input_length )
2973{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002974 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002976 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002977 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002978 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002979 operation->has_input = 1;
2980
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002982 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002983 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002984 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2985 input, input_length );
2986 status = mbedtls_to_psa_error( ret );
2987 }
2988 else
2989#endif /* MBEDTLS_CMAC_C */
2990#if defined(MBEDTLS_MD_C)
2991 if( PSA_ALG_IS_HMAC( operation->alg ) )
2992 {
2993 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2994 input_length );
2995 }
2996 else
2997#endif /* MBEDTLS_MD_C */
2998 {
2999 /* This shouldn't happen if `operation` was initialized by
3000 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003001 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003002 }
3003
Gilles Peskinefbfac682018-07-08 20:51:54 +02003004 if( status != PSA_SUCCESS )
3005 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003006 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007}
3008
Gilles Peskine01126fa2018-07-12 17:04:55 +02003009#if defined(MBEDTLS_MD_C)
3010static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3011 uint8_t *mac,
3012 size_t mac_size )
3013{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003014 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003015 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3016 size_t hash_size = 0;
3017 size_t block_size = psa_get_hash_block_size( hash_alg );
3018 psa_status_t status;
3019
Gilles Peskine01126fa2018-07-12 17:04:55 +02003020 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3021 if( status != PSA_SUCCESS )
3022 return( status );
3023 /* From here on, tmp needs to be wiped. */
3024
3025 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3026 if( status != PSA_SUCCESS )
3027 goto exit;
3028
3029 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3030 if( status != PSA_SUCCESS )
3031 goto exit;
3032
3033 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3034 if( status != PSA_SUCCESS )
3035 goto exit;
3036
Gilles Peskined911eb72018-08-14 15:18:45 +02003037 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3038 if( status != PSA_SUCCESS )
3039 goto exit;
3040
3041 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003042
3043exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003044 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003045 return( status );
3046}
3047#endif /* MBEDTLS_MD_C */
3048
mohammad16036df908f2018-04-02 08:34:15 -07003049static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003050 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003051 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003052{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003053 if( ! operation->key_set )
3054 return( PSA_ERROR_BAD_STATE );
3055 if( operation->iv_required && ! operation->iv_set )
3056 return( PSA_ERROR_BAD_STATE );
3057
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058 if( mac_size < operation->mac_size )
3059 return( PSA_ERROR_BUFFER_TOO_SMALL );
3060
Gilles Peskine8c9def32018-02-08 10:02:12 +01003061#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003062 if( operation->alg == PSA_ALG_CMAC )
3063 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003064 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3065 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3066 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003067 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003068 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003069 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003070 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003071 else
3072#endif /* MBEDTLS_CMAC_C */
3073#if defined(MBEDTLS_MD_C)
3074 if( PSA_ALG_IS_HMAC( operation->alg ) )
3075 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003076 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003077 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003078 }
3079 else
3080#endif /* MBEDTLS_MD_C */
3081 {
3082 /* This shouldn't happen if `operation` was initialized by
3083 * a setup function. */
3084 return( PSA_ERROR_BAD_STATE );
3085 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086}
3087
Gilles Peskineacd4be32018-07-08 19:56:25 +02003088psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3089 uint8_t *mac,
3090 size_t mac_size,
3091 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003092{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003093 psa_status_t status;
3094
Jaeden Amero252ef282019-02-15 14:05:35 +00003095 if( operation->alg == 0 )
3096 {
3097 return( PSA_ERROR_BAD_STATE );
3098 }
3099
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003100 /* Fill the output buffer with something that isn't a valid mac
3101 * (barring an attack on the mac and deliberately-crafted input),
3102 * in case the caller doesn't check the return status properly. */
3103 *mac_length = mac_size;
3104 /* If mac_size is 0 then mac may be NULL and then the
3105 * call to memset would have undefined behavior. */
3106 if( mac_size != 0 )
3107 memset( mac, '!', mac_size );
3108
Gilles Peskine89167cb2018-07-08 20:12:23 +02003109 if( ! operation->is_sign )
3110 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003111 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003112 }
mohammad16036df908f2018-04-02 08:34:15 -07003113
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003114 status = psa_mac_finish_internal( operation, mac, mac_size );
3115
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003116 if( status == PSA_SUCCESS )
3117 {
3118 status = psa_mac_abort( operation );
3119 if( status == PSA_SUCCESS )
3120 *mac_length = operation->mac_size;
3121 else
3122 memset( mac, '!', mac_size );
3123 }
3124 else
3125 psa_mac_abort( operation );
3126 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003127}
3128
Gilles Peskineacd4be32018-07-08 19:56:25 +02003129psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3130 const uint8_t *mac,
3131 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003132{
Gilles Peskine828ed142018-06-18 23:25:51 +02003133 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003134 psa_status_t status;
3135
Jaeden Amero252ef282019-02-15 14:05:35 +00003136 if( operation->alg == 0 )
3137 {
3138 return( PSA_ERROR_BAD_STATE );
3139 }
3140
Gilles Peskine89167cb2018-07-08 20:12:23 +02003141 if( operation->is_sign )
3142 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003143 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003144 }
3145 if( operation->mac_size != mac_length )
3146 {
3147 status = PSA_ERROR_INVALID_SIGNATURE;
3148 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003149 }
mohammad16036df908f2018-04-02 08:34:15 -07003150
3151 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003152 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003153 if( status != PSA_SUCCESS )
3154 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003155
3156 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3157 status = PSA_ERROR_INVALID_SIGNATURE;
3158
3159cleanup:
3160 if( status == PSA_SUCCESS )
3161 status = psa_mac_abort( operation );
3162 else
3163 psa_mac_abort( operation );
3164
Gilles Peskine3f108122018-12-07 18:14:53 +01003165 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003166
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003167 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168}
3169
3170
Gilles Peskine20035e32018-02-03 22:44:14 +01003171
Gilles Peskine20035e32018-02-03 22:44:14 +01003172/****************************************************************/
3173/* Asymmetric cryptography */
3174/****************************************************************/
3175
Gilles Peskine2b450e32018-06-27 15:42:46 +02003176#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003177/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003178 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003179static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3180 size_t hash_length,
3181 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003182{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003183 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003184 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003185 *md_alg = mbedtls_md_get_type( md_info );
3186
3187 /* The Mbed TLS RSA module uses an unsigned int for hash length
3188 * parameters. Validate that it fits so that we don't risk an
3189 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003190#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003191 if( hash_length > UINT_MAX )
3192 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003193#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003194
3195#if defined(MBEDTLS_PKCS1_V15)
3196 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3197 * must be correct. */
3198 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3199 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003200 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003201 if( md_info == NULL )
3202 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003203 if( mbedtls_md_get_size( md_info ) != hash_length )
3204 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003205 }
3206#endif /* MBEDTLS_PKCS1_V15 */
3207
3208#if defined(MBEDTLS_PKCS1_V21)
3209 /* PSS requires a hash internally. */
3210 if( PSA_ALG_IS_RSA_PSS( alg ) )
3211 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003212 if( md_info == NULL )
3213 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003214 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003215#endif /* MBEDTLS_PKCS1_V21 */
3216
Gilles Peskine61b91d42018-06-08 16:09:36 +02003217 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003218}
3219
Gilles Peskine2b450e32018-06-27 15:42:46 +02003220static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3221 psa_algorithm_t alg,
3222 const uint8_t *hash,
3223 size_t hash_length,
3224 uint8_t *signature,
3225 size_t signature_size,
3226 size_t *signature_length )
3227{
3228 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003230 mbedtls_md_type_t md_alg;
3231
3232 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3233 if( status != PSA_SUCCESS )
3234 return( status );
3235
Gilles Peskine630a18a2018-06-29 17:49:35 +02003236 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003237 return( PSA_ERROR_BUFFER_TOO_SMALL );
3238
3239#if defined(MBEDTLS_PKCS1_V15)
3240 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3241 {
3242 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3243 MBEDTLS_MD_NONE );
3244 ret = mbedtls_rsa_pkcs1_sign( rsa,
3245 mbedtls_ctr_drbg_random,
3246 &global_data.ctr_drbg,
3247 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003248 md_alg,
3249 (unsigned int) hash_length,
3250 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003251 signature );
3252 }
3253 else
3254#endif /* MBEDTLS_PKCS1_V15 */
3255#if defined(MBEDTLS_PKCS1_V21)
3256 if( PSA_ALG_IS_RSA_PSS( alg ) )
3257 {
3258 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3259 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3260 mbedtls_ctr_drbg_random,
3261 &global_data.ctr_drbg,
3262 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003263 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003264 (unsigned int) hash_length,
3265 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266 signature );
3267 }
3268 else
3269#endif /* MBEDTLS_PKCS1_V21 */
3270 {
3271 return( PSA_ERROR_INVALID_ARGUMENT );
3272 }
3273
3274 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003275 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003276 return( mbedtls_to_psa_error( ret ) );
3277}
3278
3279static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3280 psa_algorithm_t alg,
3281 const uint8_t *hash,
3282 size_t hash_length,
3283 const uint8_t *signature,
3284 size_t signature_length )
3285{
3286 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003288 mbedtls_md_type_t md_alg;
3289
3290 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3291 if( status != PSA_SUCCESS )
3292 return( status );
3293
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003294 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003296
3297#if defined(MBEDTLS_PKCS1_V15)
3298 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3299 {
3300 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3301 MBEDTLS_MD_NONE );
3302 ret = mbedtls_rsa_pkcs1_verify( rsa,
3303 mbedtls_ctr_drbg_random,
3304 &global_data.ctr_drbg,
3305 MBEDTLS_RSA_PUBLIC,
3306 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003307 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003308 hash,
3309 signature );
3310 }
3311 else
3312#endif /* MBEDTLS_PKCS1_V15 */
3313#if defined(MBEDTLS_PKCS1_V21)
3314 if( PSA_ALG_IS_RSA_PSS( alg ) )
3315 {
3316 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3317 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3318 mbedtls_ctr_drbg_random,
3319 &global_data.ctr_drbg,
3320 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003321 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003322 (unsigned int) hash_length,
3323 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003324 signature );
3325 }
3326 else
3327#endif /* MBEDTLS_PKCS1_V21 */
3328 {
3329 return( PSA_ERROR_INVALID_ARGUMENT );
3330 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003331
3332 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3333 * the rest of the signature is invalid". This has little use in
3334 * practice and PSA doesn't report this distinction. */
3335 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3336 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003337 return( mbedtls_to_psa_error( ret ) );
3338}
3339#endif /* MBEDTLS_RSA_C */
3340
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003341#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003342/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3343 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3344 * (even though these functions don't modify it). */
3345static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3346 psa_algorithm_t alg,
3347 const uint8_t *hash,
3348 size_t hash_length,
3349 uint8_t *signature,
3350 size_t signature_size,
3351 size_t *signature_length )
3352{
Janos Follath24eed8d2019-11-22 13:21:35 +00003353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003354 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003355 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003356 mbedtls_mpi_init( &r );
3357 mbedtls_mpi_init( &s );
3358
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003359 if( signature_size < 2 * curve_bytes )
3360 {
3361 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3362 goto cleanup;
3363 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003364
Gilles Peskinea05219c2018-11-16 16:02:56 +01003365#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003366 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3367 {
3368 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3369 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3370 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003371 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3372 &ecp->d, hash,
3373 hash_length, md_alg,
3374 mbedtls_ctr_drbg_random,
3375 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003376 }
3377 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003378#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003379 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003380 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003381 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3382 hash, hash_length,
3383 mbedtls_ctr_drbg_random,
3384 &global_data.ctr_drbg ) );
3385 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003386
3387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3388 signature,
3389 curve_bytes ) );
3390 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3391 signature + curve_bytes,
3392 curve_bytes ) );
3393
3394cleanup:
3395 mbedtls_mpi_free( &r );
3396 mbedtls_mpi_free( &s );
3397 if( ret == 0 )
3398 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003399 return( mbedtls_to_psa_error( ret ) );
3400}
3401
3402static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3403 const uint8_t *hash,
3404 size_t hash_length,
3405 const uint8_t *signature,
3406 size_t signature_length )
3407{
Janos Follath24eed8d2019-11-22 13:21:35 +00003408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003409 mbedtls_mpi r, s;
3410 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3411 mbedtls_mpi_init( &r );
3412 mbedtls_mpi_init( &s );
3413
3414 if( signature_length != 2 * curve_bytes )
3415 return( PSA_ERROR_INVALID_SIGNATURE );
3416
3417 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3418 signature,
3419 curve_bytes ) );
3420 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3421 signature + curve_bytes,
3422 curve_bytes ) );
3423
3424 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3425 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003426
3427cleanup:
3428 mbedtls_mpi_free( &r );
3429 mbedtls_mpi_free( &s );
3430 return( mbedtls_to_psa_error( ret ) );
3431}
3432#endif /* MBEDTLS_ECDSA_C */
3433
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003434psa_status_t psa_sign_hash( psa_key_handle_t handle,
3435 psa_algorithm_t alg,
3436 const uint8_t *hash,
3437 size_t hash_length,
3438 uint8_t *signature,
3439 size_t signature_size,
3440 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003441{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003442 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003443 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003444#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3445 const psa_drv_se_t *drv;
3446 psa_drv_se_context_t *drv_context;
3447#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003448
3449 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003450 /* Immediately reject a zero-length signature buffer. This guarantees
3451 * that signature must be a valid pointer. (On the other hand, the hash
3452 * buffer can in principle be empty since it doesn't actually have
3453 * to be a hash.) */
3454 if( signature_size == 0 )
3455 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003456
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003457 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003458 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003459 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003460 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003461 {
3462 status = PSA_ERROR_INVALID_ARGUMENT;
3463 goto exit;
3464 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003465
Gilles Peskineedc64242019-08-07 21:05:07 +02003466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3467 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3468 {
3469 if( drv->asymmetric == NULL ||
3470 drv->asymmetric->p_sign == NULL )
3471 {
3472 status = PSA_ERROR_NOT_SUPPORTED;
3473 goto exit;
3474 }
3475 status = drv->asymmetric->p_sign( drv_context,
3476 slot->data.se.slot_number,
3477 alg,
3478 hash, hash_length,
3479 signature, signature_size,
3480 signature_length );
3481 }
3482 else
3483#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003484#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003485 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003486 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003487 status = psa_rsa_sign( slot->data.rsa,
3488 alg,
3489 hash, hash_length,
3490 signature, signature_size,
3491 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003492 }
3493 else
3494#endif /* defined(MBEDTLS_RSA_C) */
3495#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003496 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003497 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003498#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003499 if(
3500#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3501 PSA_ALG_IS_ECDSA( alg )
3502#else
3503 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3504#endif
3505 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003506 status = psa_ecdsa_sign( slot->data.ecp,
3507 alg,
3508 hash, hash_length,
3509 signature, signature_size,
3510 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003511 else
3512#endif /* defined(MBEDTLS_ECDSA_C) */
3513 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003514 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003515 }
itayzafrir5c753392018-05-08 11:18:38 +03003516 }
3517 else
3518#endif /* defined(MBEDTLS_ECP_C) */
3519 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003520 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003521 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003522
3523exit:
3524 /* Fill the unused part of the output buffer (the whole buffer on error,
3525 * the trailing part on success) with something that isn't a valid mac
3526 * (barring an attack on the mac and deliberately-crafted input),
3527 * in case the caller doesn't check the return status properly. */
3528 if( status == PSA_SUCCESS )
3529 memset( signature + *signature_length, '!',
3530 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003531 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003532 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003533 /* If signature_size is 0 then we have nothing to do. We must not call
3534 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003535 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003536}
3537
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003538psa_status_t psa_verify_hash( psa_key_handle_t handle,
3539 psa_algorithm_t alg,
3540 const uint8_t *hash,
3541 size_t hash_length,
3542 const uint8_t *signature,
3543 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003544{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003545 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003546 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003547#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3548 const psa_drv_se_t *drv;
3549 psa_drv_se_context_t *drv_context;
3550#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003551
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003552 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003553 if( status != PSA_SUCCESS )
3554 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003555
Gilles Peskineedc64242019-08-07 21:05:07 +02003556#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3557 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3558 {
3559 if( drv->asymmetric == NULL ||
3560 drv->asymmetric->p_verify == NULL )
3561 return( PSA_ERROR_NOT_SUPPORTED );
3562 return( drv->asymmetric->p_verify( drv_context,
3563 slot->data.se.slot_number,
3564 alg,
3565 hash, hash_length,
3566 signature, signature_length ) );
3567 }
3568 else
3569#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003570#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003571 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003573 return( psa_rsa_verify( slot->data.rsa,
3574 alg,
3575 hash, hash_length,
3576 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577 }
3578 else
3579#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003580#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003581 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003582 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003583#if defined(MBEDTLS_ECDSA_C)
3584 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003585 return( psa_ecdsa_verify( slot->data.ecp,
3586 hash, hash_length,
3587 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003588 else
3589#endif /* defined(MBEDTLS_ECDSA_C) */
3590 {
3591 return( PSA_ERROR_INVALID_ARGUMENT );
3592 }
itayzafrir5c753392018-05-08 11:18:38 +03003593 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003594 else
3595#endif /* defined(MBEDTLS_ECP_C) */
3596 {
3597 return( PSA_ERROR_NOT_SUPPORTED );
3598 }
3599}
3600
Gilles Peskine072ac562018-06-30 00:21:29 +02003601#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3602static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3603 mbedtls_rsa_context *rsa )
3604{
3605 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3606 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3607 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3608 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3609}
3610#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3611
Gilles Peskinec5487a82018-12-03 18:08:14 +01003612psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003613 psa_algorithm_t alg,
3614 const uint8_t *input,
3615 size_t input_length,
3616 const uint8_t *salt,
3617 size_t salt_length,
3618 uint8_t *output,
3619 size_t output_size,
3620 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003622 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003623 psa_status_t status;
3624
Darryl Green5cc689a2018-07-24 15:34:10 +01003625 (void) input;
3626 (void) input_length;
3627 (void) salt;
3628 (void) output;
3629 (void) output_size;
3630
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003631 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003633 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3634 return( PSA_ERROR_INVALID_ARGUMENT );
3635
Gilles Peskine28f8f302019-07-24 13:30:31 +02003636 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003637 if( status != PSA_SUCCESS )
3638 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003639 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3640 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642
3643#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003644 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645 {
3646 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003648 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003649 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003651 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003653 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3654 mbedtls_ctr_drbg_random,
3655 &global_data.ctr_drbg,
3656 MBEDTLS_RSA_PUBLIC,
3657 input_length,
3658 input,
3659 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660 }
3661 else
3662#endif /* MBEDTLS_PKCS1_V15 */
3663#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003664 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003665 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003666 psa_rsa_oaep_set_padding_mode( alg, rsa );
3667 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3668 mbedtls_ctr_drbg_random,
3669 &global_data.ctr_drbg,
3670 MBEDTLS_RSA_PUBLIC,
3671 salt, salt_length,
3672 input_length,
3673 input,
3674 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675 }
3676 else
3677#endif /* MBEDTLS_PKCS1_V21 */
3678 {
3679 return( PSA_ERROR_INVALID_ARGUMENT );
3680 }
3681 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003682 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683 return( mbedtls_to_psa_error( ret ) );
3684 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003686#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687 {
3688 return( PSA_ERROR_NOT_SUPPORTED );
3689 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003690}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003691
Gilles Peskinec5487a82018-12-03 18:08:14 +01003692psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003693 psa_algorithm_t alg,
3694 const uint8_t *input,
3695 size_t input_length,
3696 const uint8_t *salt,
3697 size_t salt_length,
3698 uint8_t *output,
3699 size_t output_size,
3700 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003701{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003702 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003703 psa_status_t status;
3704
Darryl Green5cc689a2018-07-24 15:34:10 +01003705 (void) input;
3706 (void) input_length;
3707 (void) salt;
3708 (void) output;
3709 (void) output_size;
3710
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003711 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003713 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3714 return( PSA_ERROR_INVALID_ARGUMENT );
3715
Gilles Peskine28f8f302019-07-24 13:30:31 +02003716 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003717 if( status != PSA_SUCCESS )
3718 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003719 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720 return( PSA_ERROR_INVALID_ARGUMENT );
3721
3722#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003723 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 {
3725 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727
Gilles Peskine630a18a2018-06-29 17:49:35 +02003728 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729 return( PSA_ERROR_INVALID_ARGUMENT );
3730
3731#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003732 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003734 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3735 mbedtls_ctr_drbg_random,
3736 &global_data.ctr_drbg,
3737 MBEDTLS_RSA_PRIVATE,
3738 output_length,
3739 input,
3740 output,
3741 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742 }
3743 else
3744#endif /* MBEDTLS_PKCS1_V15 */
3745#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003746 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003748 psa_rsa_oaep_set_padding_mode( alg, rsa );
3749 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3750 mbedtls_ctr_drbg_random,
3751 &global_data.ctr_drbg,
3752 MBEDTLS_RSA_PRIVATE,
3753 salt, salt_length,
3754 output_length,
3755 input,
3756 output,
3757 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003758 }
3759 else
3760#endif /* MBEDTLS_PKCS1_V21 */
3761 {
3762 return( PSA_ERROR_INVALID_ARGUMENT );
3763 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003764
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765 return( mbedtls_to_psa_error( ret ) );
3766 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003767 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003768#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003769 {
3770 return( PSA_ERROR_NOT_SUPPORTED );
3771 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003772}
Gilles Peskine20035e32018-02-03 22:44:14 +01003773
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003774
3775
mohammad1603503973b2018-03-12 15:59:30 +02003776/****************************************************************/
3777/* Symmetric cryptography */
3778/****************************************************************/
3779
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003780/* Initialize the cipher operation structure. Once this function has been
3781 * called, psa_cipher_abort can run and will do the right thing. */
3782static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3783 psa_algorithm_t alg )
3784{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003785 if( ! PSA_ALG_IS_CIPHER( alg ) )
3786 {
3787 memset( operation, 0, sizeof( *operation ) );
3788 return( PSA_ERROR_INVALID_ARGUMENT );
3789 }
3790
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003791 operation->alg = alg;
3792 operation->key_set = 0;
3793 operation->iv_set = 0;
3794 operation->iv_required = 1;
3795 operation->iv_size = 0;
3796 operation->block_size = 0;
3797 mbedtls_cipher_init( &operation->ctx.cipher );
3798 return( PSA_SUCCESS );
3799}
3800
Gilles Peskinee553c652018-06-04 16:22:46 +02003801static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003802 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003803 psa_algorithm_t alg,
3804 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003805{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003806 int ret = 0;
3807 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003808 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003809 size_t key_bits;
3810 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003811 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3812 PSA_KEY_USAGE_ENCRYPT :
3813 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003814
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003815 /* A context must be freshly initialized before it can be set up. */
3816 if( operation->alg != 0 )
3817 {
3818 return( PSA_ERROR_BAD_STATE );
3819 }
3820
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003821 status = psa_cipher_init( operation, alg );
3822 if( status != PSA_SUCCESS )
3823 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003824
Gilles Peskine28f8f302019-07-24 13:30:31 +02003825 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003826 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003827 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003828 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003829
Gilles Peskine8e338702019-07-30 20:06:31 +02003830 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003831 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003832 {
3833 status = PSA_ERROR_NOT_SUPPORTED;
3834 goto exit;
3835 }
mohammad1603503973b2018-03-12 15:59:30 +02003836
mohammad1603503973b2018-03-12 15:59:30 +02003837 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003838 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003839 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003840
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003841#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003842 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003843 {
3844 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003845 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003846 memcpy( keys, slot->data.raw.data, 16 );
3847 memcpy( keys + 16, slot->data.raw.data, 8 );
3848 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3849 keys,
3850 192, cipher_operation );
3851 }
3852 else
3853#endif
3854 {
3855 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3856 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003857 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003858 }
Moran Peker41deec42018-04-04 15:43:05 +03003859 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003860 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003861
mohammad16038481e742018-03-18 13:57:31 +02003862#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003863 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003864 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003865 case PSA_ALG_CBC_NO_PADDING:
3866 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3867 MBEDTLS_PADDING_NONE );
3868 break;
3869 case PSA_ALG_CBC_PKCS7:
3870 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3871 MBEDTLS_PADDING_PKCS7 );
3872 break;
3873 default:
3874 /* The algorithm doesn't involve padding. */
3875 ret = 0;
3876 break;
3877 }
3878 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003879 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003880#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3881
mohammad1603503973b2018-03-12 15:59:30 +02003882 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003883 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003884 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003885 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003886 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003887 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003888 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003889#if defined(MBEDTLS_CHACHA20_C)
3890 else
3891 if( alg == PSA_ALG_CHACHA20 )
3892 operation->iv_size = 12;
3893#endif
mohammad1603503973b2018-03-12 15:59:30 +02003894
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003895exit:
3896 if( status == 0 )
3897 status = mbedtls_to_psa_error( ret );
3898 if( status != 0 )
3899 psa_cipher_abort( operation );
3900 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003901}
3902
Gilles Peskinefe119512018-07-08 21:39:34 +02003903psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003904 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003905 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003906{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003907 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003908}
3909
Gilles Peskinefe119512018-07-08 21:39:34 +02003910psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003911 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003912 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003913{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003914 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003915}
3916
Gilles Peskinefe119512018-07-08 21:39:34 +02003917psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003918 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003919 size_t iv_size,
3920 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003921{
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003923 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003924 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003925 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003926 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 }
Moran Peker41deec42018-04-04 15:43:05 +03003928 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003929 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003930 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003931 goto exit;
3932 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003933 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3934 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003935 if( ret != 0 )
3936 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003937 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003938 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003939 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003940
mohammad16038481e742018-03-18 13:57:31 +02003941 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003942 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003943
Moran Peker395db872018-05-31 14:07:14 +03003944exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003945 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003946 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003947 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003948}
3949
Gilles Peskinefe119512018-07-08 21:39:34 +02003950psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003951 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003952 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003953{
itayzafrir534bd7c2018-08-02 13:56:32 +03003954 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003956 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003957 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003958 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003959 }
Moran Pekera28258c2018-05-29 16:25:04 +03003960 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003961 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003962 status = PSA_ERROR_INVALID_ARGUMENT;
3963 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003964 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003965 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3966 status = mbedtls_to_psa_error( ret );
3967exit:
3968 if( status == PSA_SUCCESS )
3969 operation->iv_set = 1;
3970 else
mohammad1603503973b2018-03-12 15:59:30 +02003971 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003972 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003973}
3974
Gilles Peskinee553c652018-06-04 16:22:46 +02003975psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3976 const uint8_t *input,
3977 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003978 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003979 size_t output_size,
3980 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003981{
itayzafrir534bd7c2018-08-02 13:56:32 +03003982 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003984 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003985
3986 if( operation->alg == 0 )
3987 {
3988 return( PSA_ERROR_BAD_STATE );
3989 }
3990
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003991 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003992 {
3993 /* Take the unprocessed partial block left over from previous
3994 * update calls, if any, plus the input to this call. Remove
3995 * the last partial block, if any. You get the data that will be
3996 * output in this call. */
3997 expected_output_size =
3998 ( operation->ctx.cipher.unprocessed_len + input_length )
3999 / operation->block_size * operation->block_size;
4000 }
4001 else
4002 {
4003 expected_output_size = input_length;
4004 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004005
Gilles Peskine89d789c2018-06-04 17:17:16 +02004006 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004007 {
4008 status = PSA_ERROR_BUFFER_TOO_SMALL;
4009 goto exit;
4010 }
mohammad160382759612018-03-12 18:16:40 +02004011
mohammad1603503973b2018-03-12 15:59:30 +02004012 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004013 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004014 status = mbedtls_to_psa_error( ret );
4015exit:
4016 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004017 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004018 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004019}
4020
Gilles Peskinee553c652018-06-04 16:22:46 +02004021psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4022 uint8_t *output,
4023 size_t output_size,
4024 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004025{
David Saadab4ecc272019-02-14 13:48:10 +02004026 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004027 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004028 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004029
mohammad1603503973b2018-03-12 15:59:30 +02004030 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004031 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004032 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004033 }
4034 if( operation->iv_required && ! operation->iv_set )
4035 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004036 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004037 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004038
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004039 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004040 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4041 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004042 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004043 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004044 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004045 }
4046
Janos Follath315b51c2018-07-09 16:04:51 +01004047 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4048 temp_output_buffer,
4049 output_length );
4050 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004051 {
Janos Follath315b51c2018-07-09 16:04:51 +01004052 status = mbedtls_to_psa_error( cipher_ret );
4053 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004054 }
Janos Follath315b51c2018-07-09 16:04:51 +01004055
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004056 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004057 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004058 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004059 memcpy( output, temp_output_buffer, *output_length );
4060 else
4061 {
Janos Follath315b51c2018-07-09 16:04:51 +01004062 status = PSA_ERROR_BUFFER_TOO_SMALL;
4063 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004064 }
mohammad1603503973b2018-03-12 15:59:30 +02004065
Gilles Peskine3f108122018-12-07 18:14:53 +01004066 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004067 status = psa_cipher_abort( operation );
4068
4069 return( status );
4070
4071error:
4072
4073 *output_length = 0;
4074
Gilles Peskine3f108122018-12-07 18:14:53 +01004075 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004076 (void) psa_cipher_abort( operation );
4077
4078 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004079}
4080
Gilles Peskinee553c652018-06-04 16:22:46 +02004081psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4082{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004083 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004084 {
4085 /* The object has (apparently) been initialized but it is not
4086 * in use. It's ok to call abort on such an object, and there's
4087 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004088 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004089 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004090
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004091 /* Sanity check (shouldn't happen: operation->alg should
4092 * always have been initialized to a valid value). */
4093 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4094 return( PSA_ERROR_BAD_STATE );
4095
mohammad1603503973b2018-03-12 15:59:30 +02004096 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004097
Moran Peker41deec42018-04-04 15:43:05 +03004098 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004099 operation->key_set = 0;
4100 operation->iv_set = 0;
4101 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004102 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004103 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004104
Moran Peker395db872018-05-31 14:07:14 +03004105 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004106}
4107
Gilles Peskinea0655c32018-04-30 17:06:50 +02004108
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004109
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004110
mohammad16035955c982018-04-26 00:53:03 +03004111/****************************************************************/
4112/* AEAD */
4113/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004114
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115typedef struct
4116{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004117 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004118 const mbedtls_cipher_info_t *cipher_info;
4119 union
4120 {
4121#if defined(MBEDTLS_CCM_C)
4122 mbedtls_ccm_context ccm;
4123#endif /* MBEDTLS_CCM_C */
4124#if defined(MBEDTLS_GCM_C)
4125 mbedtls_gcm_context gcm;
4126#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004127#if defined(MBEDTLS_CHACHAPOLY_C)
4128 mbedtls_chachapoly_context chachapoly;
4129#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004130 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004131 psa_algorithm_t core_alg;
4132 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004133 uint8_t tag_length;
4134} aead_operation_t;
4135
Gilles Peskine30a9e412019-01-14 18:36:12 +01004136static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004137{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004138 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004139 {
4140#if defined(MBEDTLS_CCM_C)
4141 case PSA_ALG_CCM:
4142 mbedtls_ccm_free( &operation->ctx.ccm );
4143 break;
4144#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004145#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004146 case PSA_ALG_GCM:
4147 mbedtls_gcm_free( &operation->ctx.gcm );
4148 break;
4149#endif /* MBEDTLS_GCM_C */
4150 }
4151}
4152
4153static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004154 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004155 psa_key_usage_t usage,
4156 psa_algorithm_t alg )
4157{
4158 psa_status_t status;
4159 size_t key_bits;
4160 mbedtls_cipher_id_t cipher_id;
4161
Gilles Peskine28f8f302019-07-24 13:30:31 +02004162 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004163 if( status != PSA_SUCCESS )
4164 return( status );
4165
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004166 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167
4168 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004169 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004170 &cipher_id );
4171 if( operation->cipher_info == NULL )
4172 return( PSA_ERROR_NOT_SUPPORTED );
4173
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004174 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004175 {
4176#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004177 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4178 operation->core_alg = PSA_ALG_CCM;
4179 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004180 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4181 * The call to mbedtls_ccm_encrypt_and_tag or
4182 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004183 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004184 return( PSA_ERROR_INVALID_ARGUMENT );
4185 mbedtls_ccm_init( &operation->ctx.ccm );
4186 status = mbedtls_to_psa_error(
4187 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4188 operation->slot->data.raw.data,
4189 (unsigned int) key_bits ) );
4190 if( status != 0 )
4191 goto cleanup;
4192 break;
4193#endif /* MBEDTLS_CCM_C */
4194
4195#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004196 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4197 operation->core_alg = PSA_ALG_GCM;
4198 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004199 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4200 * The call to mbedtls_gcm_crypt_and_tag or
4201 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004202 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004203 return( PSA_ERROR_INVALID_ARGUMENT );
4204 mbedtls_gcm_init( &operation->ctx.gcm );
4205 status = mbedtls_to_psa_error(
4206 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4207 operation->slot->data.raw.data,
4208 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004209 if( status != 0 )
4210 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004211 break;
4212#endif /* MBEDTLS_GCM_C */
4213
Gilles Peskine26869f22019-05-06 15:25:00 +02004214#if defined(MBEDTLS_CHACHAPOLY_C)
4215 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4216 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4217 operation->full_tag_length = 16;
4218 /* We only support the default tag length. */
4219 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4220 return( PSA_ERROR_NOT_SUPPORTED );
4221 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4222 status = mbedtls_to_psa_error(
4223 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4224 operation->slot->data.raw.data ) );
4225 if( status != 0 )
4226 goto cleanup;
4227 break;
4228#endif /* MBEDTLS_CHACHAPOLY_C */
4229
Gilles Peskineedf9a652018-08-17 18:11:56 +02004230 default:
4231 return( PSA_ERROR_NOT_SUPPORTED );
4232 }
4233
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004234 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4235 {
4236 status = PSA_ERROR_INVALID_ARGUMENT;
4237 goto cleanup;
4238 }
4239 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004240
Gilles Peskineedf9a652018-08-17 18:11:56 +02004241 return( PSA_SUCCESS );
4242
4243cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004244 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004245 return( status );
4246}
4247
Gilles Peskinec5487a82018-12-03 18:08:14 +01004248psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004249 psa_algorithm_t alg,
4250 const uint8_t *nonce,
4251 size_t nonce_length,
4252 const uint8_t *additional_data,
4253 size_t additional_data_length,
4254 const uint8_t *plaintext,
4255 size_t plaintext_length,
4256 uint8_t *ciphertext,
4257 size_t ciphertext_size,
4258 size_t *ciphertext_length )
4259{
mohammad16035955c982018-04-26 00:53:03 +03004260 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004261 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004262 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004263
mohammad1603f08a5502018-06-03 15:05:47 +03004264 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004265
Gilles Peskinec5487a82018-12-03 18:08:14 +01004266 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004267 if( status != PSA_SUCCESS )
4268 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004269
Gilles Peskineedf9a652018-08-17 18:11:56 +02004270 /* For all currently supported modes, the tag is at the end of the
4271 * ciphertext. */
4272 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4273 {
4274 status = PSA_ERROR_BUFFER_TOO_SMALL;
4275 goto exit;
4276 }
4277 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004278
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004279#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004280 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004281 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004282 status = mbedtls_to_psa_error(
4283 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4284 MBEDTLS_GCM_ENCRYPT,
4285 plaintext_length,
4286 nonce, nonce_length,
4287 additional_data, additional_data_length,
4288 plaintext, ciphertext,
4289 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004290 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004291 else
4292#endif /* MBEDTLS_GCM_C */
4293#if defined(MBEDTLS_CCM_C)
4294 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004295 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004296 status = mbedtls_to_psa_error(
4297 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4298 plaintext_length,
4299 nonce, nonce_length,
4300 additional_data,
4301 additional_data_length,
4302 plaintext, ciphertext,
4303 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004304 }
mohammad16035c8845f2018-05-09 05:40:09 -07004305 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004306#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004307#if defined(MBEDTLS_CHACHAPOLY_C)
4308 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4309 {
4310 if( nonce_length != 12 || operation.tag_length != 16 )
4311 {
4312 status = PSA_ERROR_NOT_SUPPORTED;
4313 goto exit;
4314 }
4315 status = mbedtls_to_psa_error(
4316 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4317 plaintext_length,
4318 nonce,
4319 additional_data,
4320 additional_data_length,
4321 plaintext,
4322 ciphertext,
4323 tag ) );
4324 }
4325 else
4326#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004327 {
mohammad1603554faad2018-06-03 15:07:38 +03004328 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004329 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004330
Gilles Peskineedf9a652018-08-17 18:11:56 +02004331 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4332 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004333
Gilles Peskineedf9a652018-08-17 18:11:56 +02004334exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004335 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004336 if( status == PSA_SUCCESS )
4337 *ciphertext_length = plaintext_length + operation.tag_length;
4338 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004339}
4340
Gilles Peskineee652a32018-06-01 19:23:52 +02004341/* Locate the tag in a ciphertext buffer containing the encrypted data
4342 * followed by the tag. Return the length of the part preceding the tag in
4343 * *plaintext_length. This is the size of the plaintext in modes where
4344 * the encrypted data has the same size as the plaintext, such as
4345 * CCM and GCM. */
4346static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4347 const uint8_t *ciphertext,
4348 size_t ciphertext_length,
4349 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004350 const uint8_t **p_tag )
4351{
4352 size_t payload_length;
4353 if( tag_length > ciphertext_length )
4354 return( PSA_ERROR_INVALID_ARGUMENT );
4355 payload_length = ciphertext_length - tag_length;
4356 if( payload_length > plaintext_size )
4357 return( PSA_ERROR_BUFFER_TOO_SMALL );
4358 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004359 return( PSA_SUCCESS );
4360}
4361
Gilles Peskinec5487a82018-12-03 18:08:14 +01004362psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004363 psa_algorithm_t alg,
4364 const uint8_t *nonce,
4365 size_t nonce_length,
4366 const uint8_t *additional_data,
4367 size_t additional_data_length,
4368 const uint8_t *ciphertext,
4369 size_t ciphertext_length,
4370 uint8_t *plaintext,
4371 size_t plaintext_size,
4372 size_t *plaintext_length )
4373{
mohammad16035955c982018-04-26 00:53:03 +03004374 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004375 aead_operation_t operation;
4376 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004377
Gilles Peskineee652a32018-06-01 19:23:52 +02004378 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004379
Gilles Peskinec5487a82018-12-03 18:08:14 +01004380 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004381 if( status != PSA_SUCCESS )
4382 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004383
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004384 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4385 ciphertext, ciphertext_length,
4386 plaintext_size, &tag );
4387 if( status != PSA_SUCCESS )
4388 goto exit;
4389
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004390#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004391 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004392 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004393 status = mbedtls_to_psa_error(
4394 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4395 ciphertext_length - operation.tag_length,
4396 nonce, nonce_length,
4397 additional_data,
4398 additional_data_length,
4399 tag, operation.tag_length,
4400 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004401 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004402 else
4403#endif /* MBEDTLS_GCM_C */
4404#if defined(MBEDTLS_CCM_C)
4405 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004406 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004407 status = mbedtls_to_psa_error(
4408 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4409 ciphertext_length - operation.tag_length,
4410 nonce, nonce_length,
4411 additional_data,
4412 additional_data_length,
4413 ciphertext, plaintext,
4414 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004415 }
mohammad160339574652018-06-01 04:39:53 -07004416 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004417#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004418#if defined(MBEDTLS_CHACHAPOLY_C)
4419 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4420 {
4421 if( nonce_length != 12 || operation.tag_length != 16 )
4422 {
4423 status = PSA_ERROR_NOT_SUPPORTED;
4424 goto exit;
4425 }
4426 status = mbedtls_to_psa_error(
4427 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4428 ciphertext_length - operation.tag_length,
4429 nonce,
4430 additional_data,
4431 additional_data_length,
4432 tag,
4433 ciphertext,
4434 plaintext ) );
4435 }
4436 else
4437#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004438 {
mohammad1603554faad2018-06-03 15:07:38 +03004439 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004440 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004441
Gilles Peskineedf9a652018-08-17 18:11:56 +02004442 if( status != PSA_SUCCESS && plaintext_size != 0 )
4443 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004444
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004446 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004447 if( status == PSA_SUCCESS )
4448 *plaintext_length = ciphertext_length - operation.tag_length;
4449 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004450}
4451
Gilles Peskinea0655c32018-04-30 17:06:50 +02004452
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004453
Gilles Peskine20035e32018-02-03 22:44:14 +01004454/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004455/* Generators */
4456/****************************************************************/
4457
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004458#define HKDF_STATE_INIT 0 /* no input yet */
4459#define HKDF_STATE_STARTED 1 /* got salt */
4460#define HKDF_STATE_KEYED 2 /* got key */
4461#define HKDF_STATE_OUTPUT 3 /* output started */
4462
Gilles Peskinecbe66502019-05-16 16:59:18 +02004463static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004465{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004466 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4467 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004468 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004470}
4471
4472
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004474{
4475 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004476 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004477 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004478 {
4479 /* The object has (apparently) been initialized but it is not
4480 * in use. It's ok to call abort on such an object, and there's
4481 * nothing to do. */
4482 }
4483 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004484#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004485 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004486 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 mbedtls_free( operation->ctx.hkdf.info );
4488 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004489 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004490 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004492 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004493 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004494 if( operation->ctx.tls12_prf.seed != NULL )
4495 {
4496 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4497 operation->ctx.tls12_prf.seed_length );
4498 mbedtls_free( operation->ctx.tls12_prf.seed );
4499 }
4500
4501 if( operation->ctx.tls12_prf.label != NULL )
4502 {
4503 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4504 operation->ctx.tls12_prf.label_length );
4505 mbedtls_free( operation->ctx.tls12_prf.label );
4506 }
4507
4508 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4509
4510 /* We leave the fields Ai and output_block to be erased safely by the
4511 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004512 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004513 else
4514#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004515 {
4516 status = PSA_ERROR_BAD_STATE;
4517 }
Janos Follath083036a2019-06-11 10:22:26 +01004518 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004519 return( status );
4520}
4521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004523 size_t *capacity)
4524{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004525 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004526 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004528 return PSA_ERROR_BAD_STATE;
4529 }
4530
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004532 return( PSA_SUCCESS );
4533}
4534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004536 size_t capacity )
4537{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004539 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004541 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004543 return( PSA_SUCCESS );
4544}
4545
Gilles Peskinebef7f142018-07-12 17:22:21 +02004546#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004548 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004549static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004550 psa_algorithm_t hash_alg,
4551 uint8_t *output,
4552 size_t output_length )
4553{
4554 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4555 psa_status_t status;
4556
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004557 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4558 return( PSA_ERROR_BAD_STATE );
4559 hkdf->state = HKDF_STATE_OUTPUT;
4560
Gilles Peskinebef7f142018-07-12 17:22:21 +02004561 while( output_length != 0 )
4562 {
4563 /* Copy what remains of the current block */
4564 uint8_t n = hash_length - hkdf->offset_in_block;
4565 if( n > output_length )
4566 n = (uint8_t) output_length;
4567 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4568 output += n;
4569 output_length -= n;
4570 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004571 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004572 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004573 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004574 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004576 * object was corrupted or if this function is called directly
4577 * inside the library. */
4578 if( hkdf->block_number == 0xff )
4579 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004580
4581 /* We need a new block */
4582 ++hkdf->block_number;
4583 hkdf->offset_in_block = 0;
4584 status = psa_hmac_setup_internal( &hkdf->hmac,
4585 hkdf->prk, hash_length,
4586 hash_alg );
4587 if( status != PSA_SUCCESS )
4588 return( status );
4589 if( hkdf->block_number != 1 )
4590 {
4591 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4592 hkdf->output_block,
4593 hash_length );
4594 if( status != PSA_SUCCESS )
4595 return( status );
4596 }
4597 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4598 hkdf->info,
4599 hkdf->info_length );
4600 if( status != PSA_SUCCESS )
4601 return( status );
4602 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4603 &hkdf->block_number, 1 );
4604 if( status != PSA_SUCCESS )
4605 return( status );
4606 status = psa_hmac_finish_internal( &hkdf->hmac,
4607 hkdf->output_block,
4608 sizeof( hkdf->output_block ) );
4609 if( status != PSA_SUCCESS )
4610 return( status );
4611 }
4612
4613 return( PSA_SUCCESS );
4614}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004615
Janos Follath7742fee2019-06-17 12:58:10 +01004616static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4617 psa_tls12_prf_key_derivation_t *tls12_prf,
4618 psa_algorithm_t alg )
4619{
4620 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4621 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004622 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4623 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004624
Janos Follath7742fee2019-06-17 12:58:10 +01004625 /* We can't be wanting more output after block 0xff, otherwise
4626 * the capacity check in psa_key_derivation_output_bytes() would have
4627 * prevented this call. It could happen only if the operation
4628 * object was corrupted or if this function is called directly
4629 * inside the library. */
4630 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004631 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004632
4633 /* We need a new block */
4634 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004635 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004636
4637 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4638 *
4639 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4640 *
4641 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4642 * HMAC_hash(secret, A(2) + seed) +
4643 * HMAC_hash(secret, A(3) + seed) + ...
4644 *
4645 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004646 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004647 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004648 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004649 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004650 * is currently extracted as `output_block` and where i is
4651 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004652 */
4653
Janos Follathea29bfb2019-06-19 12:21:20 +01004654 /* Save the hash context before using it, to preserve the hash state with
4655 * only the inner padding in it. We need this, because inner padding depends
4656 * on the key (secret in the RFC's terminology). */
4657 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4658 if( status != PSA_SUCCESS )
4659 goto cleanup;
4660
4661 /* Calculate A(i) where i = tls12_prf->block_number. */
4662 if( tls12_prf->block_number == 1 )
4663 {
4664 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4665 * the variable seed and in this instance means it in the context of the
4666 * P_hash function, where seed = label + seed.) */
4667 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4668 tls12_prf->label, tls12_prf->label_length );
4669 if( status != PSA_SUCCESS )
4670 goto cleanup;
4671 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4672 tls12_prf->seed, tls12_prf->seed_length );
4673 if( status != PSA_SUCCESS )
4674 goto cleanup;
4675 }
4676 else
4677 {
4678 /* A(i) = HMAC_hash(secret, A(i-1)) */
4679 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4680 tls12_prf->Ai, hash_length );
4681 if( status != PSA_SUCCESS )
4682 goto cleanup;
4683 }
4684
4685 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4686 tls12_prf->Ai, hash_length );
4687 if( status != PSA_SUCCESS )
4688 goto cleanup;
4689 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4690 if( status != PSA_SUCCESS )
4691 goto cleanup;
4692
4693 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4694 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4695 tls12_prf->Ai, hash_length );
4696 if( status != PSA_SUCCESS )
4697 goto cleanup;
4698 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4699 tls12_prf->label, tls12_prf->label_length );
4700 if( status != PSA_SUCCESS )
4701 goto cleanup;
4702 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4703 tls12_prf->seed, tls12_prf->seed_length );
4704 if( status != PSA_SUCCESS )
4705 goto cleanup;
4706 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4707 tls12_prf->output_block, hash_length );
4708 if( status != PSA_SUCCESS )
4709 goto cleanup;
4710 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4711 if( status != PSA_SUCCESS )
4712 goto cleanup;
4713
Janos Follath7742fee2019-06-17 12:58:10 +01004714
4715cleanup:
4716
Janos Follathea29bfb2019-06-19 12:21:20 +01004717 cleanup_status = psa_hash_abort( &backup );
4718 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4719 status = cleanup_status;
4720
4721 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004722}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004723
Janos Follath844eb0e2019-06-19 12:10:49 +01004724static psa_status_t psa_key_derivation_tls12_prf_read(
4725 psa_tls12_prf_key_derivation_t *tls12_prf,
4726 psa_algorithm_t alg,
4727 uint8_t *output,
4728 size_t output_length )
4729{
4730 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4731 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4732 psa_status_t status;
4733 uint8_t offset, length;
4734
4735 while( output_length != 0 )
4736 {
4737 /* Check if we have fully processed the current block. */
4738 if( tls12_prf->left_in_block == 0 )
4739 {
4740 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4741 alg );
4742 if( status != PSA_SUCCESS )
4743 return( status );
4744
4745 continue;
4746 }
4747
4748 if( tls12_prf->left_in_block > output_length )
4749 length = (uint8_t) output_length;
4750 else
4751 length = tls12_prf->left_in_block;
4752
4753 offset = hash_length - tls12_prf->left_in_block;
4754 memcpy( output, tls12_prf->output_block + offset, length );
4755 output += length;
4756 output_length -= length;
4757 tls12_prf->left_in_block -= length;
4758 }
4759
4760 return( PSA_SUCCESS );
4761}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004762#endif /* MBEDTLS_MD_C */
4763
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004764psa_status_t psa_key_derivation_output_bytes(
4765 psa_key_derivation_operation_t *operation,
4766 uint8_t *output,
4767 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004768{
4769 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004771
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004773 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004775 return PSA_ERROR_BAD_STATE;
4776 }
4777
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004779 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004781 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004783 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004784 goto exit;
4785 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004787 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004789 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4791 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004793 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004794 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004795 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004797
Gilles Peskinebef7f142018-07-12 17:22:21 +02004798#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004799 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004800 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004803 output, output_length );
4804 }
Janos Follathadbec812019-06-14 11:05:39 +01004805 else
Janos Follathadbec812019-06-14 11:05:39 +01004806 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004807 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004808 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004810 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004811 output_length );
4812 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004813 else
4814#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 {
4816 return( PSA_ERROR_BAD_STATE );
4817 }
4818
4819exit:
4820 if( status != PSA_SUCCESS )
4821 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004822 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 * This allows us to differentiate between exhausted operations and
4824 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4825 * operations. */
4826 psa_algorithm_t alg = operation->alg;
4827 psa_key_derivation_abort( operation );
4828 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004829 memset( output, '!', output_length );
4830 }
4831 return( status );
4832}
4833
Gilles Peskine08542d82018-07-19 17:05:42 +02004834#if defined(MBEDTLS_DES_C)
4835static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4836{
4837 if( data_size >= 8 )
4838 mbedtls_des_key_set_parity( data );
4839 if( data_size >= 16 )
4840 mbedtls_des_key_set_parity( data + 8 );
4841 if( data_size >= 24 )
4842 mbedtls_des_key_set_parity( data + 16 );
4843}
4844#endif /* MBEDTLS_DES_C */
4845
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004846static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004847 psa_key_slot_t *slot,
4848 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004850{
4851 uint8_t *data = NULL;
4852 size_t bytes = PSA_BITS_TO_BYTES( bits );
4853 psa_status_t status;
4854
Gilles Peskine8e338702019-07-30 20:06:31 +02004855 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004856 return( PSA_ERROR_INVALID_ARGUMENT );
4857 if( bits % 8 != 0 )
4858 return( PSA_ERROR_INVALID_ARGUMENT );
4859 data = mbedtls_calloc( 1, bytes );
4860 if( data == NULL )
4861 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4862
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004864 if( status != PSA_SUCCESS )
4865 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004866#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004867 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004868 psa_des_set_key_parity( data, bytes );
4869#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004871
4872exit:
4873 mbedtls_free( data );
4874 return( status );
4875}
4876
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004877psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004879 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004880{
4881 psa_status_t status;
4882 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004883 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004884
4885 /* Reject any attempt to create a zero-length key so that we don't
4886 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4887 if( psa_get_key_bits( attributes ) == 0 )
4888 return( PSA_ERROR_INVALID_ARGUMENT );
4889
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004890 if( ! operation->can_output_key )
4891 return( PSA_ERROR_NOT_PERMITTED );
4892
Gilles Peskinedf179142019-07-15 22:02:14 +02004893 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4894 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004895#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4896 if( driver != NULL )
4897 {
4898 /* Deriving a key in a secure element is not implemented yet. */
4899 status = PSA_ERROR_NOT_SUPPORTED;
4900 }
4901#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004902 if( status == PSA_SUCCESS )
4903 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004904 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004905 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004907 }
4908 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004909 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004910 if( status != PSA_SUCCESS )
4911 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004912 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004913 *handle = 0;
4914 }
4915 return( status );
4916}
4917
Gilles Peskineeab56e42018-07-12 17:12:33 +02004918
4919
4920/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004921/* Key derivation */
4922/****************************************************************/
4923
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927{
Janos Follath5fe19732019-06-20 15:09:30 +01004928 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4929 * initialisers for this union leave some bytes unspecified.) */
4930 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4931
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004933#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004934 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4935 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4936 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004937 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004938 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004939 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4940 if( hash_size == 0 )
4941 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004942 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4943 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004944 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 {
4946 return( PSA_ERROR_NOT_SUPPORTED );
4947 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004950 }
4951#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004952 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004953 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954}
4955
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004957 psa_algorithm_t alg )
4958{
4959 psa_status_t status;
4960
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962 return( PSA_ERROR_BAD_STATE );
4963
Gilles Peskine6843c292019-01-18 16:44:49 +01004964 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4965 return( PSA_ERROR_INVALID_ARGUMENT );
4966 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004967 {
4968 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004970 }
4971 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4972 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004975 else
4976 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004977
4978 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004980 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004981}
4982
4983#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004984static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 psa_algorithm_t hash_alg,
4986 psa_key_derivation_step_t step,
4987 const uint8_t *data,
4988 size_t data_length )
4989{
4990 psa_status_t status;
4991 switch( step )
4992 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004993 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004994 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004995 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004996 status = psa_hmac_setup_internal( &hkdf->hmac,
4997 data, data_length,
4998 hash_alg );
4999 if( status != PSA_SUCCESS )
5000 return( status );
5001 hkdf->state = HKDF_STATE_STARTED;
5002 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005003 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005004 /* If no salt was provided, use an empty salt. */
5005 if( hkdf->state == HKDF_STATE_INIT )
5006 {
5007 status = psa_hmac_setup_internal( &hkdf->hmac,
5008 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005009 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005010 if( status != PSA_SUCCESS )
5011 return( status );
5012 hkdf->state = HKDF_STATE_STARTED;
5013 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005014 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005015 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005016 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5017 data, data_length );
5018 if( status != PSA_SUCCESS )
5019 return( status );
5020 status = psa_hmac_finish_internal( &hkdf->hmac,
5021 hkdf->prk,
5022 sizeof( hkdf->prk ) );
5023 if( status != PSA_SUCCESS )
5024 return( status );
5025 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5026 hkdf->block_number = 0;
5027 hkdf->state = HKDF_STATE_KEYED;
5028 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005029 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005030 if( hkdf->state == HKDF_STATE_OUTPUT )
5031 return( PSA_ERROR_BAD_STATE );
5032 if( hkdf->info_set )
5033 return( PSA_ERROR_BAD_STATE );
5034 hkdf->info_length = data_length;
5035 if( data_length != 0 )
5036 {
5037 hkdf->info = mbedtls_calloc( 1, data_length );
5038 if( hkdf->info == NULL )
5039 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5040 memcpy( hkdf->info, data, data_length );
5041 }
5042 hkdf->info_set = 1;
5043 return( PSA_SUCCESS );
5044 default:
5045 return( PSA_ERROR_INVALID_ARGUMENT );
5046 }
5047}
Janos Follathb03233e2019-06-11 15:30:30 +01005048
Janos Follathf08e2652019-06-13 09:05:41 +01005049static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5050 const uint8_t *data,
5051 size_t data_length )
5052{
5053 if( prf->state != TLS12_PRF_STATE_INIT )
5054 return( PSA_ERROR_BAD_STATE );
5055
Janos Follathd6dce9f2019-07-04 09:11:38 +01005056 if( data_length != 0 )
5057 {
5058 prf->seed = mbedtls_calloc( 1, data_length );
5059 if( prf->seed == NULL )
5060 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005061
Janos Follathd6dce9f2019-07-04 09:11:38 +01005062 memcpy( prf->seed, data, data_length );
5063 prf->seed_length = data_length;
5064 }
Janos Follathf08e2652019-06-13 09:05:41 +01005065
5066 prf->state = TLS12_PRF_STATE_SEED_SET;
5067
5068 return( PSA_SUCCESS );
5069}
5070
Janos Follath81550542019-06-13 14:26:34 +01005071static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5072 psa_algorithm_t hash_alg,
5073 const uint8_t *data,
5074 size_t data_length )
5075{
5076 psa_status_t status;
5077 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5078 return( PSA_ERROR_BAD_STATE );
5079
5080 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5081 if( status != PSA_SUCCESS )
5082 return( status );
5083
5084 prf->state = TLS12_PRF_STATE_KEY_SET;
5085
5086 return( PSA_SUCCESS );
5087}
5088
Janos Follath6660f0e2019-06-17 08:44:03 +01005089static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5090 psa_tls12_prf_key_derivation_t *prf,
5091 psa_algorithm_t hash_alg,
5092 const uint8_t *data,
5093 size_t data_length )
5094{
5095 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005096 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5097 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005098
5099 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5100 return( PSA_ERROR_INVALID_ARGUMENT );
5101
5102 /* Quoting RFC 4279, Section 2:
5103 *
5104 * The premaster secret is formed as follows: if the PSK is N octets
5105 * long, concatenate a uint16 with the value N, N zero octets, a second
5106 * uint16 with the value N, and the PSK itself.
5107 */
5108
Janos Follath40e13932019-06-26 13:22:29 +01005109 *cur++ = ( data_length >> 8 ) & 0xff;
5110 *cur++ = ( data_length >> 0 ) & 0xff;
5111 memset( cur, 0, data_length );
5112 cur += data_length;
5113 *cur++ = pms[0];
5114 *cur++ = pms[1];
5115 memcpy( cur, data, data_length );
5116 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005117
Janos Follath40e13932019-06-26 13:22:29 +01005118 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005119
5120 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5121 return( status );
5122}
5123
Janos Follath63028dd2019-06-13 09:15:47 +01005124static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5125 const uint8_t *data,
5126 size_t data_length )
5127{
5128 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5129 return( PSA_ERROR_BAD_STATE );
5130
Janos Follathd6dce9f2019-07-04 09:11:38 +01005131 if( data_length != 0 )
5132 {
5133 prf->label = mbedtls_calloc( 1, data_length );
5134 if( prf->label == NULL )
5135 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005136
Janos Follathd6dce9f2019-07-04 09:11:38 +01005137 memcpy( prf->label, data, data_length );
5138 prf->label_length = data_length;
5139 }
Janos Follath63028dd2019-06-13 09:15:47 +01005140
5141 prf->state = TLS12_PRF_STATE_LABEL_SET;
5142
5143 return( PSA_SUCCESS );
5144}
5145
Janos Follathb03233e2019-06-11 15:30:30 +01005146static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5147 psa_algorithm_t hash_alg,
5148 psa_key_derivation_step_t step,
5149 const uint8_t *data,
5150 size_t data_length )
5151{
Janos Follathb03233e2019-06-11 15:30:30 +01005152 switch( step )
5153 {
Janos Follathf08e2652019-06-13 09:05:41 +01005154 case PSA_KEY_DERIVATION_INPUT_SEED:
5155 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005156 case PSA_KEY_DERIVATION_INPUT_SECRET:
5157 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005158 case PSA_KEY_DERIVATION_INPUT_LABEL:
5159 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005160 default:
5161 return( PSA_ERROR_INVALID_ARGUMENT );
5162 }
5163}
Janos Follath6660f0e2019-06-17 08:44:03 +01005164
5165static psa_status_t psa_tls12_prf_psk_to_ms_input(
5166 psa_tls12_prf_key_derivation_t *prf,
5167 psa_algorithm_t hash_alg,
5168 psa_key_derivation_step_t step,
5169 const uint8_t *data,
5170 size_t data_length )
5171{
5172 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005173 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005174 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5175 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005176 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005177
5178 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5179}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180#endif /* MBEDTLS_MD_C */
5181
Gilles Peskineb8965192019-09-24 16:21:10 +02005182/** Check whether the given key type is acceptable for the given
5183 * input step of a key derivation.
5184 *
5185 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5186 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5187 * Both secret and non-secret inputs can alternatively have the type
5188 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5189 * that the input was passed as a buffer rather than via a key object.
5190 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005191static int psa_key_derivation_check_input_type(
5192 psa_key_derivation_step_t step,
5193 psa_key_type_t key_type )
5194{
5195 switch( step )
5196 {
5197 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005198 if( key_type == PSA_KEY_TYPE_DERIVE )
5199 return( PSA_SUCCESS );
5200 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005201 return( PSA_SUCCESS );
5202 break;
5203 case PSA_KEY_DERIVATION_INPUT_LABEL:
5204 case PSA_KEY_DERIVATION_INPUT_SALT:
5205 case PSA_KEY_DERIVATION_INPUT_INFO:
5206 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005207 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5208 return( PSA_SUCCESS );
5209 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005210 return( PSA_SUCCESS );
5211 break;
5212 }
5213 return( PSA_ERROR_INVALID_ARGUMENT );
5214}
5215
Janos Follathb80a94e2019-06-12 15:54:46 +01005216static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005218 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005219 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005220 const uint8_t *data,
5221 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005223 psa_status_t status;
5224 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5225
5226 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005227 if( status != PSA_SUCCESS )
5228 goto exit;
5229
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005230#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005231 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005232 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005234 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235 step, data, data_length );
5236 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005237 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005238 {
Janos Follathb03233e2019-06-11 15:30:30 +01005239 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5240 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5241 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005242 }
5243 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5244 {
5245 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5246 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5247 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005248 }
5249 else
5250#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005251 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253 return( PSA_ERROR_BAD_STATE );
5254 }
5255
Gilles Peskine224b0d62019-09-23 18:13:17 +02005256exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005259 return( status );
5260}
5261
Janos Follath51f4a0f2019-06-14 11:35:55 +01005262psa_status_t psa_key_derivation_input_bytes(
5263 psa_key_derivation_operation_t *operation,
5264 psa_key_derivation_step_t step,
5265 const uint8_t *data,
5266 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005267{
Gilles Peskineb8965192019-09-24 16:21:10 +02005268 return( psa_key_derivation_input_internal( operation, step,
5269 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005270 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005271}
5272
Janos Follath51f4a0f2019-06-14 11:35:55 +01005273psa_status_t psa_key_derivation_input_key(
5274 psa_key_derivation_operation_t *operation,
5275 psa_key_derivation_step_t step,
5276 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005277{
5278 psa_key_slot_t *slot;
5279 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005280
Gilles Peskine28f8f302019-07-24 13:30:31 +02005281 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005282 PSA_KEY_USAGE_DERIVE,
5283 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005284 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005285 {
5286 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005288 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005289
5290 /* Passing a key object as a SECRET input unlocks the permission
5291 * to output to a key object. */
5292 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5293 operation->can_output_key = 1;
5294
Janos Follathb80a94e2019-06-12 15:54:46 +01005295 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005296 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005297 slot->data.raw.data,
5298 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005299}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005300
5301
5302
5303/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005304/* Key agreement */
5305/****************************************************************/
5306
Gilles Peskinea05219c2018-11-16 16:02:56 +01005307#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005308static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5309 size_t peer_key_length,
5310 const mbedtls_ecp_keypair *our_key,
5311 uint8_t *shared_secret,
5312 size_t shared_secret_size,
5313 size_t *shared_secret_length )
5314{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005315 mbedtls_ecp_keypair *their_key = NULL;
5316 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005317 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005318 size_t bits = 0;
5319 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005321
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005322 status = psa_import_ec_public_key( curve,
5323 peer_key, peer_key_length,
5324 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005325 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005326 goto exit;
5327
Jaeden Amero97271b32019-01-10 19:38:51 +00005328 status = mbedtls_to_psa_error(
5329 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5330 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005331 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005332 status = mbedtls_to_psa_error(
5333 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5334 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005335 goto exit;
5336
Jaeden Amero97271b32019-01-10 19:38:51 +00005337 status = mbedtls_to_psa_error(
5338 mbedtls_ecdh_calc_secret( &ecdh,
5339 shared_secret_length,
5340 shared_secret, shared_secret_size,
5341 mbedtls_ctr_drbg_random,
5342 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005343 if( status != PSA_SUCCESS )
5344 goto exit;
5345 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5346 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005347
5348exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005349 if( status != PSA_SUCCESS )
5350 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005351 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005352 mbedtls_ecp_keypair_free( their_key );
5353 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005354 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005355}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005356#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005357
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5359
Gilles Peskine0216fe12019-04-11 21:23:21 +02005360static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5361 psa_key_slot_t *private_key,
5362 const uint8_t *peer_key,
5363 size_t peer_key_length,
5364 uint8_t *shared_secret,
5365 size_t shared_secret_size,
5366 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005367{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005368 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005369 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005370#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005371 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005372 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005373 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005374 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5375 private_key->data.ecp,
5376 shared_secret, shared_secret_size,
5377 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005378#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005379 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005380 (void) private_key;
5381 (void) peer_key;
5382 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005383 (void) shared_secret;
5384 (void) shared_secret_size;
5385 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005386 return( PSA_ERROR_NOT_SUPPORTED );
5387 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005388}
5389
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005390/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005391 * to potentially free embedded data structures and wipe confidential data.
5392 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005394 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395 psa_key_slot_t *private_key,
5396 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005397 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005398{
5399 psa_status_t status;
5400 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5401 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005402 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005403
5404 /* Step 1: run the secret agreement algorithm to generate the shared
5405 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005406 status = psa_key_agreement_raw_internal( ka_alg,
5407 private_key,
5408 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005409 shared_secret,
5410 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005411 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005412 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005413 goto exit;
5414
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005415 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005416 * the shared secret. A shared secret is permitted wherever a key
5417 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005418 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005419 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005420 shared_secret,
5421 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005422
Gilles Peskine12313cd2018-06-20 00:20:32 +02005423exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005424 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005425 return( status );
5426}
5427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005429 psa_key_derivation_step_t step,
5430 psa_key_handle_t private_key,
5431 const uint8_t *peer_key,
5432 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005433{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005434 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005435 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005437 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005438 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005439 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005440 if( status != PSA_SUCCESS )
5441 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005443 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005445 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005447 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005448}
5449
Gilles Peskinebe697d82019-05-16 18:00:41 +02005450psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5451 psa_key_handle_t private_key,
5452 const uint8_t *peer_key,
5453 size_t peer_key_length,
5454 uint8_t *output,
5455 size_t output_size,
5456 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005457{
5458 psa_key_slot_t *slot;
5459 psa_status_t status;
5460
5461 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5462 {
5463 status = PSA_ERROR_INVALID_ARGUMENT;
5464 goto exit;
5465 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005466 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005467 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005468 if( status != PSA_SUCCESS )
5469 goto exit;
5470
5471 status = psa_key_agreement_raw_internal( alg, slot,
5472 peer_key, peer_key_length,
5473 output, output_size,
5474 output_length );
5475
5476exit:
5477 if( status != PSA_SUCCESS )
5478 {
5479 /* If an error happens and is not handled properly, the output
5480 * may be used as a key to protect sensitive data. Arrange for such
5481 * a key to be random, which is likely to result in decryption or
5482 * verification errors. This is better than filling the buffer with
5483 * some constant data such as zeros, which would result in the data
5484 * being protected with a reproducible, easily knowable key.
5485 */
5486 psa_generate_random( output, output_size );
5487 *output_length = output_size;
5488 }
5489 return( status );
5490}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005491
5492
5493/****************************************************************/
5494/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005495/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005496
Gilles Peskine4c317f42018-07-12 01:24:09 +02005497psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005498 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005499{
Janos Follath24eed8d2019-11-22 13:21:35 +00005500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005501 GUARD_MODULE_INITIALIZED;
5502
Gilles Peskinef181eca2019-08-07 13:49:00 +02005503 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5504 {
5505 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5506 output,
5507 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5508 if( ret != 0 )
5509 return( mbedtls_to_psa_error( ret ) );
5510 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5511 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5512 }
5513
Gilles Peskinee59236f2018-01-27 23:32:46 +01005514 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5515 return( mbedtls_to_psa_error( ret ) );
5516}
5517
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005518#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5519#include "mbedtls/entropy_poll.h"
5520
Gilles Peskine7228da22019-07-15 11:06:15 +02005521psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005522 size_t seed_size )
5523{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005524 if( global_data.initialized )
5525 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005526
5527 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5528 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5529 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5530 return( PSA_ERROR_INVALID_ARGUMENT );
5531
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005532 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005533}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005534#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005535
Gilles Peskinee56e8782019-04-26 17:34:02 +02005536#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5537static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5538 size_t domain_parameters_size,
5539 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005541 size_t i;
5542 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005543
Gilles Peskinee56e8782019-04-26 17:34:02 +02005544 if( domain_parameters_size == 0 )
5545 {
5546 *exponent = 65537;
5547 return( PSA_SUCCESS );
5548 }
5549
5550 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5551 * support values that fit in a 32-bit integer, which is larger than
5552 * int on just about every platform anyway. */
5553 if( domain_parameters_size > sizeof( acc ) )
5554 return( PSA_ERROR_NOT_SUPPORTED );
5555 for( i = 0; i < domain_parameters_size; i++ )
5556 acc = ( acc << 8 ) | domain_parameters[i];
5557 if( acc > INT_MAX )
5558 return( PSA_ERROR_NOT_SUPPORTED );
5559 *exponent = acc;
5560 return( PSA_SUCCESS );
5561}
5562#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5563
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005564static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005565 psa_key_slot_t *slot, size_t bits,
5566 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005567{
Gilles Peskine8e338702019-07-30 20:06:31 +02005568 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005569
Gilles Peskinee56e8782019-04-26 17:34:02 +02005570 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005571 return( PSA_ERROR_INVALID_ARGUMENT );
5572
Gilles Peskinee59236f2018-01-27 23:32:46 +01005573 if( key_type_is_raw_bytes( type ) )
5574 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005575 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005576 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5577 if( status != PSA_SUCCESS )
5578 return( status );
5579 status = psa_generate_random( slot->data.raw.data,
5580 slot->data.raw.bytes );
5581 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005582 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005583#if defined(MBEDTLS_DES_C)
5584 if( type == PSA_KEY_TYPE_DES )
5585 psa_des_set_key_parity( slot->data.raw.data,
5586 slot->data.raw.bytes );
5587#endif /* MBEDTLS_DES_C */
5588 }
5589 else
5590
5591#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005592 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593 {
5594 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005596 int exponent;
5597 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005598 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5599 return( PSA_ERROR_NOT_SUPPORTED );
5600 /* Accept only byte-aligned keys, for the same reasons as
5601 * in psa_import_rsa_key(). */
5602 if( bits % 8 != 0 )
5603 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005604 status = psa_read_rsa_exponent( domain_parameters,
5605 domain_parameters_size,
5606 &exponent );
5607 if( status != PSA_SUCCESS )
5608 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005609 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5610 if( rsa == NULL )
5611 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5612 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5613 ret = mbedtls_rsa_gen_key( rsa,
5614 mbedtls_ctr_drbg_random,
5615 &global_data.ctr_drbg,
5616 (unsigned int) bits,
5617 exponent );
5618 if( ret != 0 )
5619 {
5620 mbedtls_rsa_free( rsa );
5621 mbedtls_free( rsa );
5622 return( mbedtls_to_psa_error( ret ) );
5623 }
5624 slot->data.rsa = rsa;
5625 }
5626 else
5627#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5628
5629#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005630 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005631 {
5632 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005633 mbedtls_ecp_group_id grp_id =
5634 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005635 const mbedtls_ecp_curve_info *curve_info =
5636 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5637 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005639 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005640 return( PSA_ERROR_NOT_SUPPORTED );
5641 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5642 return( PSA_ERROR_NOT_SUPPORTED );
5643 if( curve_info->bit_size != bits )
5644 return( PSA_ERROR_INVALID_ARGUMENT );
5645 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5646 if( ecp == NULL )
5647 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5648 mbedtls_ecp_keypair_init( ecp );
5649 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5650 mbedtls_ctr_drbg_random,
5651 &global_data.ctr_drbg );
5652 if( ret != 0 )
5653 {
5654 mbedtls_ecp_keypair_free( ecp );
5655 mbedtls_free( ecp );
5656 return( mbedtls_to_psa_error( ret ) );
5657 }
5658 slot->data.ecp = ecp;
5659 }
5660 else
5661#endif /* MBEDTLS_ECP_C */
5662
5663 return( PSA_ERROR_NOT_SUPPORTED );
5664
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 return( PSA_SUCCESS );
5666}
Darryl Green0c6575a2018-11-07 16:05:30 +00005667
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005668psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005669 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670{
5671 psa_status_t status;
5672 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005673 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005674
Gilles Peskine0f84d622019-09-12 19:03:13 +02005675 /* Reject any attempt to create a zero-length key so that we don't
5676 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5677 if( psa_get_key_bits( attributes ) == 0 )
5678 return( PSA_ERROR_INVALID_ARGUMENT );
5679
Gilles Peskinedf179142019-07-15 22:02:14 +02005680 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5681 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005682 if( status != PSA_SUCCESS )
5683 goto exit;
5684
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005685#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5686 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005687 {
Gilles Peskine11792082019-08-06 18:36:36 +02005688 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5689 size_t pubkey_length = 0; /* We don't support this feature yet */
5690 if( drv->key_management == NULL ||
5691 drv->key_management->p_generate == NULL )
5692 {
5693 status = PSA_ERROR_NOT_SUPPORTED;
5694 goto exit;
5695 }
5696 status = drv->key_management->p_generate(
5697 psa_get_se_driver_context( driver ),
5698 slot->data.se.slot_number, attributes,
5699 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005700 }
Gilles Peskine11792082019-08-06 18:36:36 +02005701 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005702#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005703 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005704 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005705 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005706 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 }
Gilles Peskine11792082019-08-06 18:36:36 +02005708
5709exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005710 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005711 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005712 if( status != PSA_SUCCESS )
5713 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005714 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005715 *handle = 0;
5716 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005717 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005718}
5719
5720
Gilles Peskinee59236f2018-01-27 23:32:46 +01005721
5722/****************************************************************/
5723/* Module setup */
5724/****************************************************************/
5725
Gilles Peskine5e769522018-11-20 21:59:56 +01005726psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5727 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5728 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5729{
5730 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5731 return( PSA_ERROR_BAD_STATE );
5732 global_data.entropy_init = entropy_init;
5733 global_data.entropy_free = entropy_free;
5734 return( PSA_SUCCESS );
5735}
5736
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737void mbedtls_psa_crypto_free( void )
5738{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005739 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005740 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5741 {
5742 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005743 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005744 }
5745 /* Wipe all remaining data, including configuration.
5746 * In particular, this sets all state indicator to the value
5747 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005748 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005750 /* Unregister all secure element drivers, so that we restart from
5751 * a pristine state. */
5752 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005753#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005754}
5755
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005756#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5757/** Recover a transaction that was interrupted by a power failure.
5758 *
5759 * This function is called during initialization, before psa_crypto_init()
5760 * returns. If this function returns a failure status, the initialization
5761 * fails.
5762 */
5763static psa_status_t psa_crypto_recover_transaction(
5764 const psa_crypto_transaction_t *transaction )
5765{
5766 switch( transaction->unknown.type )
5767 {
5768 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5769 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005770 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005771 * is implemented.
5772 * https://github.com/ARMmbed/mbed-crypto/issues/218
5773 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005774 default:
5775 /* We found an unsupported transaction in the storage.
5776 * We don't know what state the storage is in. Give up. */
5777 return( PSA_ERROR_STORAGE_FAILURE );
5778 }
5779}
5780#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5781
Gilles Peskinee59236f2018-01-27 23:32:46 +01005782psa_status_t psa_crypto_init( void )
5783{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005784 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 const unsigned char drbg_seed[] = "PSA";
5786
Gilles Peskinec6b69072018-11-20 21:42:52 +01005787 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005788 if( global_data.initialized != 0 )
5789 return( PSA_SUCCESS );
5790
Gilles Peskine5e769522018-11-20 21:59:56 +01005791 /* Set default configuration if
5792 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5793 if( global_data.entropy_init == NULL )
5794 global_data.entropy_init = mbedtls_entropy_init;
5795 if( global_data.entropy_free == NULL )
5796 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797
Gilles Peskinec6b69072018-11-20 21:42:52 +01005798 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005799 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005800#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5801 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5802 /* The PSA entropy injection feature depends on using NV seed as an entropy
5803 * source. Add NV seed as an entropy source for PSA entropy injection. */
5804 mbedtls_entropy_add_source( &global_data.entropy,
5805 mbedtls_nv_seed_poll, NULL,
5806 MBEDTLS_ENTROPY_BLOCK_SIZE,
5807 MBEDTLS_ENTROPY_SOURCE_STRONG );
5808#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005809 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005810 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005811 status = mbedtls_to_psa_error(
5812 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5813 mbedtls_entropy_func,
5814 &global_data.entropy,
5815 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5816 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005817 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005818 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005819
Gilles Peskine66fb1262018-12-10 16:29:04 +01005820 status = psa_initialize_key_slots( );
5821 if( status != PSA_SUCCESS )
5822 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005823
Gilles Peskined9348f22019-10-01 15:22:29 +02005824#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5825 status = psa_init_all_se_drivers( );
5826 if( status != PSA_SUCCESS )
5827 goto exit;
5828#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5829
Gilles Peskine4b734222019-07-24 15:56:31 +02005830#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005831 status = psa_crypto_load_transaction( );
5832 if( status == PSA_SUCCESS )
5833 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005834 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5835 if( status != PSA_SUCCESS )
5836 goto exit;
5837 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005838 }
5839 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5840 {
5841 /* There's no transaction to complete. It's all good. */
5842 status = PSA_SUCCESS;
5843 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005844#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005845
Gilles Peskinec6b69072018-11-20 21:42:52 +01005846 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847 global_data.initialized = 1;
5848
5849exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005850 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005851 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005852 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005853}
5854
5855#endif /* MBEDTLS_PSA_CRYPTO_C */