blob: 8fc021a92d638b5a875c519a162baf774566755b [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 Peskinec7ef5b32019-12-12 16:58:00 +0100378static psa_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 Peskine34ef7f52018-06-18 20:47:51 +0200385 return( PSA_ECC_CURVE_SECP192R1 );
386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388 return( PSA_ECC_CURVE_SECP224R1 );
389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200391 return( PSA_ECC_CURVE_SECP256R1 );
392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200394 return( PSA_ECC_CURVE_SECP384R1 );
395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200397 return( PSA_ECC_CURVE_SECP521R1 );
398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200400 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200403 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200406 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200412 return( PSA_ECC_CURVE_SECP192K1 );
413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200415 return( PSA_ECC_CURVE_SECP224K1 );
416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200418 return( PSA_ECC_CURVE_SECP256K1 );
419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200421 return( PSA_ECC_CURVE_CURVE448 );
422 default:
423 return( 0 );
424 }
425}
426
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100427static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
428 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429{
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100430 (void) byte_length;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200431 switch( curve )
432 {
433 case PSA_ECC_CURVE_SECP192R1:
434 return( MBEDTLS_ECP_DP_SECP192R1 );
435 case PSA_ECC_CURVE_SECP224R1:
436 return( MBEDTLS_ECP_DP_SECP224R1 );
437 case PSA_ECC_CURVE_SECP256R1:
438 return( MBEDTLS_ECP_DP_SECP256R1 );
439 case PSA_ECC_CURVE_SECP384R1:
440 return( MBEDTLS_ECP_DP_SECP384R1 );
441 case PSA_ECC_CURVE_SECP521R1:
442 return( MBEDTLS_ECP_DP_SECP521R1 );
443 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
444 return( MBEDTLS_ECP_DP_BP256R1 );
445 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
446 return( MBEDTLS_ECP_DP_BP384R1 );
447 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
448 return( MBEDTLS_ECP_DP_BP512R1 );
449 case PSA_ECC_CURVE_CURVE25519:
450 return( MBEDTLS_ECP_DP_CURVE25519 );
451 case PSA_ECC_CURVE_SECP192K1:
452 return( MBEDTLS_ECP_DP_SECP192K1 );
453 case PSA_ECC_CURVE_SECP224K1:
454 return( MBEDTLS_ECP_DP_SECP224K1 );
455 case PSA_ECC_CURVE_SECP256K1:
456 return( MBEDTLS_ECP_DP_SECP256K1 );
457 case PSA_ECC_CURVE_CURVE448:
458 return( MBEDTLS_ECP_DP_CURVE448 );
459 default:
460 return( MBEDTLS_ECP_DP_NONE );
461 }
462}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100463#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
466 size_t bits,
467 struct raw_data *raw )
468{
469 /* Check that the bit size is acceptable for the key type */
470 switch( type )
471 {
472 case PSA_KEY_TYPE_RAW_DATA:
473#if defined(MBEDTLS_MD_C)
474 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200475#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200476 case PSA_KEY_TYPE_DERIVE:
477 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478#if defined(MBEDTLS_AES_C)
479 case PSA_KEY_TYPE_AES:
480 if( bits != 128 && bits != 192 && bits != 256 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_CAMELLIA_C)
485 case PSA_KEY_TYPE_CAMELLIA:
486 if( bits != 128 && bits != 192 && bits != 256 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
490#if defined(MBEDTLS_DES_C)
491 case PSA_KEY_TYPE_DES:
492 if( bits != 64 && bits != 128 && bits != 192 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
496#if defined(MBEDTLS_ARC4_C)
497 case PSA_KEY_TYPE_ARC4:
498 if( bits < 8 || bits > 2048 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
500 break;
501#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200502#if defined(MBEDTLS_CHACHA20_C)
503 case PSA_KEY_TYPE_CHACHA20:
504 if( bits != 256 )
505 return( PSA_ERROR_INVALID_ARGUMENT );
506 break;
507#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 default:
509 return( PSA_ERROR_NOT_SUPPORTED );
510 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200511 if( bits % 8 != 0 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513
514 /* Allocate memory for the key */
515 raw->bytes = PSA_BITS_TO_BYTES( bits );
516 raw->data = mbedtls_calloc( 1, raw->bytes );
517 if( raw->data == NULL )
518 {
519 raw->bytes = 0;
520 return( PSA_ERROR_INSUFFICIENT_MEMORY );
521 }
522 return( PSA_SUCCESS );
523}
524
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200525#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100526/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
527 * that are not a multiple of 8) well. For example, there is only
528 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
529 * way to return the exact bit size of a key.
530 * To keep things simple, reject non-byte-aligned key sizes. */
531static psa_status_t psa_check_rsa_key_byte_aligned(
532 const mbedtls_rsa_context *rsa )
533{
534 mbedtls_mpi n;
535 psa_status_t status;
536 mbedtls_mpi_init( &n );
537 status = mbedtls_to_psa_error(
538 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
539 if( status == PSA_SUCCESS )
540 {
541 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
542 status = PSA_ERROR_NOT_SUPPORTED;
543 }
544 mbedtls_mpi_free( &n );
545 return( status );
546}
547
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548static psa_status_t psa_import_rsa_key( psa_key_type_t type,
549 const uint8_t *data,
550 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 mbedtls_rsa_context **p_rsa )
552{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 psa_status_t status;
554 mbedtls_pk_context pk;
555 mbedtls_rsa_context *rsa;
556 size_t bits;
557
558 mbedtls_pk_init( &pk );
559
560 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200561 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000562 status = mbedtls_to_psa_error(
563 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000565 status = mbedtls_to_psa_error(
566 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
567 if( status != PSA_SUCCESS )
568 goto exit;
569
570 /* We have something that the pkparse module recognizes. If it is a
571 * valid RSA key, store it. */
572 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200573 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000574 status = PSA_ERROR_INVALID_ARGUMENT;
575 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200576 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000577
578 rsa = mbedtls_pk_rsa( pk );
579 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
580 * supports non-byte-aligned key sizes, but not well. For example,
581 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
582 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
583 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
584 {
585 status = PSA_ERROR_NOT_SUPPORTED;
586 goto exit;
587 }
588 status = psa_check_rsa_key_byte_aligned( rsa );
589
590exit:
591 /* Free the content of the pk object only on error. */
592 if( status != PSA_SUCCESS )
593 {
594 mbedtls_pk_free( &pk );
595 return( status );
596 }
597
598 /* On success, store the content of the object in the RSA context. */
599 *p_rsa = rsa;
600
601 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200602}
603#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
604
Jaeden Ameroccdce902019-01-10 11:42:27 +0000605#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100606static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100607 size_t data_length,
608 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100609 mbedtls_ecp_keypair **p_ecp )
610{
611 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
612 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
613 if( *p_ecp == NULL )
614 return( PSA_ERROR_INSUFFICIENT_MEMORY );
615 mbedtls_ecp_keypair_init( *p_ecp );
616
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100617 if( is_public )
618 {
619 /* A public key is represented as:
620 * - The byte 0x04;
621 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
622 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
623 * So its data length is 2m+1 where n is the key size in bits.
624 */
625 if( ( data_length & 1 ) == 0 )
626 return( PSA_ERROR_INVALID_ARGUMENT );
627 data_length = data_length / 2;
628 }
629
Gilles Peskine4cd32772019-12-02 20:49:42 +0100630 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100631 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
632 if( grp_id == MBEDTLS_ECP_DP_NONE )
633 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100634 return( mbedtls_to_psa_error(
635 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
636}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000637
638/* Import a public key given as the uncompressed representation defined by SEC1
639 * 2.3.3 as the content of an ECPoint. */
640static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
641 const uint8_t *data,
642 size_t data_length,
643 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200644{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000646 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000647
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100648 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000649 if( status != PSA_SUCCESS )
650 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100651
Jaeden Ameroccdce902019-01-10 11:42:27 +0000652 /* Load the public value. */
653 status = mbedtls_to_psa_error(
654 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
655 data, data_length ) );
656 if( status != PSA_SUCCESS )
657 goto exit;
658
659 /* Check that the point is on the curve. */
660 status = mbedtls_to_psa_error(
661 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
662 if( status != PSA_SUCCESS )
663 goto exit;
664
665 *p_ecp = ecp;
666 return( PSA_SUCCESS );
667
668exit:
669 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200670 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000671 mbedtls_ecp_keypair_free( ecp );
672 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200673 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000674 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200675}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200676
Gilles Peskinef76aa772018-10-29 19:24:33 +0100677/* Import a private key given as a byte string which is the private value
678 * in big-endian order. */
679static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
680 const uint8_t *data,
681 size_t data_length,
682 mbedtls_ecp_keypair **p_ecp )
683{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200684 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100685 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100686
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200687 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
688 return( PSA_ERROR_INVALID_ARGUMENT );
689
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100690 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100691 if( status != PSA_SUCCESS )
692 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100693
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 /* Load the secret value. */
695 status = mbedtls_to_psa_error(
696 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
697 if( status != PSA_SUCCESS )
698 goto exit;
699 /* Validate the private key. */
700 status = mbedtls_to_psa_error(
701 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
702 if( status != PSA_SUCCESS )
703 goto exit;
704 /* Calculate the public key from the private key. */
705 status = mbedtls_to_psa_error(
706 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
707 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
708 if( status != PSA_SUCCESS )
709 goto exit;
710
711 *p_ecp = ecp;
712 return( PSA_SUCCESS );
713
714exit:
715 if( ecp != NULL )
716 {
717 mbedtls_ecp_keypair_free( ecp );
718 mbedtls_free( ecp );
719 }
720 return( status );
721}
722#endif /* defined(MBEDTLS_ECP_C) */
723
Gilles Peskineb46bef22019-07-30 21:32:04 +0200724
725/** Return the size of the key in the given slot, in bits.
726 *
727 * \param[in] slot A key slot.
728 *
729 * \return The key size in bits, read from the metadata in the slot.
730 */
731static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
732{
733 return( slot->attr.bits );
734}
735
736/** Calculate the size of the key in the given slot, in bits.
737 *
738 * \param[in] slot A key slot containing a transparent key.
739 *
740 * \return The key size in bits, calculated from the key data.
741 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200742static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200743{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200744 size_t bits = 0; /* return 0 on an empty slot */
745
Gilles Peskineb46bef22019-07-30 21:32:04 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200747 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200748#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200749 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
750 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200751#endif /* defined(MBEDTLS_RSA_C) */
752#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200753 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
754 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200755#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200756
757 /* We know that the size fits in psa_key_bits_t thanks to checks
758 * when the key was created. */
759 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200760}
761
Gilles Peskine8e338702019-07-30 20:06:31 +0200762/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100763 * previously. This function assumes that the slot does not contain
764 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100765psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
766 const uint8_t *data,
767 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100768{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200769 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200773 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200774 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 if( data_length > SIZE_MAX / 8 )
776 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200777 /* Enforce a size limit, and in particular ensure that the bit
778 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200779 if( bit_size > PSA_MAX_KEY_BITS )
780 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200782 &slot->data.raw );
783 if( status != PSA_SUCCESS )
784 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200785 if( data_length != 0 )
786 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787 }
788 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100789#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200790 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100791 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200792 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100793 data, data_length,
794 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100795 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200796 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000798 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200799 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000800 data, data_length,
801 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 }
803 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100804#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000805#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200806 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100807 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200808 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000809 data, data_length,
810 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 }
812 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000813#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 {
815 return( PSA_ERROR_NOT_SUPPORTED );
816 }
Darryl Green940d72c2018-07-13 13:18:51 +0100817
Gilles Peskineb46bef22019-07-30 21:32:04 +0200818 if( status == PSA_SUCCESS )
819 {
820 /* Write the actual key size to the slot.
821 * psa_start_key_creation() wrote the size declared by the
822 * caller, which may be 0 (meaning unspecified) or wrong. */
823 slot->attr.bits = psa_calculate_key_bits( slot );
824 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100825 return( status );
826}
827
Gilles Peskinef603c712019-01-19 13:40:11 +0100828/** Calculate the intersection of two algorithm usage policies.
829 *
830 * Return 0 (which allows no operation) on incompatibility.
831 */
832static psa_algorithm_t psa_key_policy_algorithm_intersection(
833 psa_algorithm_t alg1,
834 psa_algorithm_t alg2 )
835{
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100837 if( alg1 == alg2 )
838 return( alg1 );
839 /* If the policies are from the same hash-and-sign family, check
840 * if one is a wildcard. If so the other has the specific algorithm. */
841 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
842 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
843 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
844 {
845 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
846 return( alg2 );
847 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
848 return( alg1 );
849 }
850 /* If the policies are incompatible, allow nothing. */
851 return( 0 );
852}
853
Gilles Peskined6f371b2019-05-10 19:33:38 +0200854static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
855 psa_algorithm_t requested_alg )
856{
Gilles Peskine549ea862019-05-22 11:45:59 +0200857 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200858 if( requested_alg == policy_alg )
859 return( 1 );
860 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200861 * and requested_alg is the same hash-and-sign family with any hash,
862 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200863 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
864 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
865 {
866 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
867 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
868 }
869 /* If it isn't permitted, it's forbidden. */
870 return( 0 );
871}
872
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100873/** Test whether a policy permits an algorithm.
874 *
875 * The caller must test usage flags separately.
876 */
877static int psa_key_policy_permits( const psa_key_policy_t *policy,
878 psa_algorithm_t alg )
879{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200880 return( psa_key_algorithm_permits( policy->alg, alg ) ||
881 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882}
883
Gilles Peskinef603c712019-01-19 13:40:11 +0100884/** Restrict a key policy based on a constraint.
885 *
886 * \param[in,out] policy The policy to restrict.
887 * \param[in] constraint The policy constraint to apply.
888 *
889 * \retval #PSA_SUCCESS
890 * \c *policy contains the intersection of the original value of
891 * \c *policy and \c *constraint.
892 * \retval #PSA_ERROR_INVALID_ARGUMENT
893 * \c *policy and \c *constraint are incompatible.
894 * \c *policy is unchanged.
895 */
896static psa_status_t psa_restrict_key_policy(
897 psa_key_policy_t *policy,
898 const psa_key_policy_t *constraint )
899{
900 psa_algorithm_t intersection_alg =
901 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200902 psa_algorithm_t intersection_alg2 =
903 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100904 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
905 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200906 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
907 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100908 policy->usage &= constraint->usage;
909 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200910 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100911 return( PSA_SUCCESS );
912}
913
Darryl Green06fd18d2018-07-16 11:21:11 +0100914/** Retrieve a slot which must contain a key. The key must have allow all the
915 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
916 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100917static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100918 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100919 psa_key_usage_t usage,
920 psa_algorithm_t alg )
921{
922 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100923 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100924
925 *p_slot = NULL;
926
Gilles Peskinec5487a82018-12-03 18:08:14 +0100927 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100928 if( status != PSA_SUCCESS )
929 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100930
931 /* Enforce that usage policy for the key slot contains all the flags
932 * required by the usage parameter. There is one exception: public
933 * keys can always be exported, so we treat public key objects as
934 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200935 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100936 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200937 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100938 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100939
940 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200941 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100942 return( PSA_ERROR_NOT_PERMITTED );
943
944 *p_slot = slot;
945 return( PSA_SUCCESS );
946}
Darryl Green940d72c2018-07-13 13:18:51 +0100947
Gilles Peskine28f8f302019-07-24 13:30:31 +0200948/** Retrieve a slot which must contain a transparent key.
949 *
950 * A transparent key is a key for which the key material is directly
951 * available, as opposed to a key in a secure element.
952 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200953 * This is a temporary function to use instead of psa_get_key_from_slot()
954 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200955 */
956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
958 psa_key_slot_t **p_slot,
959 psa_key_usage_t usage,
960 psa_algorithm_t alg )
961{
962 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
963 if( status != PSA_SUCCESS )
964 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200965 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966 {
967 *p_slot = NULL;
968 return( PSA_ERROR_NOT_SUPPORTED );
969 }
970 return( PSA_SUCCESS );
971}
972#else /* MBEDTLS_PSA_CRYPTO_SE_C */
973/* With no secure element support, all keys are transparent. */
974#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
975 psa_get_key_from_slot( handle, p_slot, usage, alg )
976#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
977
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100978/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100979static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000980{
Gilles Peskine73167e12019-07-12 23:44:37 +0200981#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
982 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000983 {
984 /* No key material to clean. */
985 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200986 else
987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200988 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000989 {
990 /* No key material to clean. */
991 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200992 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000993 {
994 mbedtls_free( slot->data.raw.data );
995 }
996 else
997#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200998 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000999 {
1000 mbedtls_rsa_free( slot->data.rsa );
1001 mbedtls_free( slot->data.rsa );
1002 }
1003 else
1004#endif /* defined(MBEDTLS_RSA_C) */
1005#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001006 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001007 {
1008 mbedtls_ecp_keypair_free( slot->data.ecp );
1009 mbedtls_free( slot->data.ecp );
1010 }
1011 else
1012#endif /* defined(MBEDTLS_ECP_C) */
1013 {
1014 /* Shouldn't happen: the key type is not any type that we
1015 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001016 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001017 }
1018
1019 return( PSA_SUCCESS );
1020}
1021
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001022/** Completely wipe a slot in memory, including its policy.
1023 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001024psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001025{
1026 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001027 /* Multipart operations may still be using the key. This is safe
1028 * because all multipart operation objects are independent from
1029 * the key slot: if they need to access the key after the setup
1030 * phase, they have a copy of the key. Note that this means that
1031 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001032 /* At this point, key material and other type-specific content has
1033 * been wiped. Clear remaining metadata. We can call memset and not
1034 * zeroize because the metadata is not particularly sensitive. */
1035 memset( slot, 0, sizeof( *slot ) );
1036 return( status );
1037}
1038
Gilles Peskinec5487a82018-12-03 18:08:14 +01001039psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001041 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001042 psa_status_t status; /* status of the last operation */
1043 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001044#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1045 psa_se_drv_table_entry_t *driver;
1046#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047
Gilles Peskine1841cf42019-10-08 15:48:25 +02001048 if( handle == 0 )
1049 return( PSA_SUCCESS );
1050
Gilles Peskinec5487a82018-12-03 18:08:14 +01001051 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001052 if( status != PSA_SUCCESS )
1053 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001054
1055#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001056 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001057 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001058 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001059 /* For a key in a secure element, we need to do three things:
1060 * remove the key file in internal storage, destroy the
1061 * key inside the secure element, and update the driver's
1062 * persistent data. Start a transaction that will encompass these
1063 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001064 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001065 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001066 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001067 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001068 status = psa_crypto_save_transaction( );
1069 if( status != PSA_SUCCESS )
1070 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001071 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 /* We should still try to destroy the key in the secure
1073 * element and the key metadata in storage. This is especially
1074 * important if the error is that the storage is full.
1075 * But how to do it exactly without risking an inconsistent
1076 * state after a reset?
1077 * https://github.com/ARMmbed/mbed-crypto/issues/215
1078 */
1079 overall_status = status;
1080 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001081 }
1082
Gilles Peskine354f7672019-07-12 23:46:38 +02001083 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001084 if( overall_status == PSA_SUCCESS )
1085 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001086 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001087#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1088
Darryl Greend49a4992018-06-18 17:27:26 +01001089#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001090 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001091 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001092 status = psa_destroy_persistent_key( slot->attr.id );
1093 if( overall_status == PSA_SUCCESS )
1094 overall_status = status;
1095
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001096 /* TODO: other slots may have a copy of the same key. We should
1097 * invalidate them.
1098 * https://github.com/ARMmbed/mbed-crypto/issues/214
1099 */
Darryl Greend49a4992018-06-18 17:27:26 +01001100 }
1101#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001102
Gilles Peskinefc762652019-07-22 19:30:34 +02001103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1104 if( driver != NULL )
1105 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001106 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107 if( overall_status == PSA_SUCCESS )
1108 overall_status = status;
1109 status = psa_crypto_stop_transaction( );
1110 if( overall_status == PSA_SUCCESS )
1111 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001112 }
1113#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1114
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001115#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1116exit:
1117#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001118 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001119 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1120 if( overall_status == PSA_SUCCESS )
1121 overall_status = status;
1122 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123}
1124
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001125void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001126{
Gilles Peskineb699f072019-04-26 16:06:02 +02001127 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001128 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001129}
1130
Gilles Peskineb699f072019-04-26 16:06:02 +02001131psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1132 psa_key_type_t type,
1133 const uint8_t *data,
1134 size_t data_length )
1135{
1136 uint8_t *copy = NULL;
1137
1138 if( data_length != 0 )
1139 {
1140 copy = mbedtls_calloc( 1, data_length );
1141 if( copy == NULL )
1142 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1143 memcpy( copy, data, data_length );
1144 }
1145 /* After this point, this function is guaranteed to succeed, so it
1146 * can start modifying `*attributes`. */
1147
1148 if( attributes->domain_parameters != NULL )
1149 {
1150 mbedtls_free( attributes->domain_parameters );
1151 attributes->domain_parameters = NULL;
1152 attributes->domain_parameters_size = 0;
1153 }
1154
1155 attributes->domain_parameters = copy;
1156 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001157 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001158 return( PSA_SUCCESS );
1159}
1160
1161psa_status_t psa_get_key_domain_parameters(
1162 const psa_key_attributes_t *attributes,
1163 uint8_t *data, size_t data_size, size_t *data_length )
1164{
1165 if( attributes->domain_parameters_size > data_size )
1166 return( PSA_ERROR_BUFFER_TOO_SMALL );
1167 *data_length = attributes->domain_parameters_size;
1168 if( attributes->domain_parameters_size != 0 )
1169 memcpy( data, attributes->domain_parameters,
1170 attributes->domain_parameters_size );
1171 return( PSA_SUCCESS );
1172}
1173
1174#if defined(MBEDTLS_RSA_C)
1175static psa_status_t psa_get_rsa_public_exponent(
1176 const mbedtls_rsa_context *rsa,
1177 psa_key_attributes_t *attributes )
1178{
1179 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001181 uint8_t *buffer = NULL;
1182 size_t buflen;
1183 mbedtls_mpi_init( &mpi );
1184
1185 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1186 if( ret != 0 )
1187 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001188 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1189 {
1190 /* It's the default value, which is reported as an empty string,
1191 * so there's nothing to do. */
1192 goto exit;
1193 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001194
1195 buflen = mbedtls_mpi_size( &mpi );
1196 buffer = mbedtls_calloc( 1, buflen );
1197 if( buffer == NULL )
1198 {
1199 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1200 goto exit;
1201 }
1202 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1203 if( ret != 0 )
1204 goto exit;
1205 attributes->domain_parameters = buffer;
1206 attributes->domain_parameters_size = buflen;
1207
1208exit:
1209 mbedtls_mpi_free( &mpi );
1210 if( ret != 0 )
1211 mbedtls_free( buffer );
1212 return( mbedtls_to_psa_error( ret ) );
1213}
1214#endif /* MBEDTLS_RSA_C */
1215
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001216/** Retrieve all the publicly-accessible attributes of a key.
1217 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001218psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1219 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001220{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001221 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001222 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001223
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001224 psa_reset_key_attributes( attributes );
1225
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001226 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001227 if( status != PSA_SUCCESS )
1228 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001229
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001230 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001231 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1232 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1233
1234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1235 if( psa_key_slot_is_external( slot ) )
1236 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1237#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001238
Gilles Peskine8e338702019-07-30 20:06:31 +02001239 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001240 {
1241#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001242 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001243 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001245 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001246 * is not yet implemented.
1247 * https://github.com/ARMmbed/mbed-crypto/issues/216
1248 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001249 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001250 break;
1251#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001252 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1253 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001254#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001255 default:
1256 /* Nothing else to do. */
1257 break;
1258 }
1259
1260 if( status != PSA_SUCCESS )
1261 psa_reset_key_attributes( attributes );
1262 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263}
1264
Gilles Peskinec8000c02019-08-02 20:15:51 +02001265#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1266psa_status_t psa_get_key_slot_number(
1267 const psa_key_attributes_t *attributes,
1268 psa_key_slot_number_t *slot_number )
1269{
1270 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1271 {
1272 *slot_number = attributes->slot_number;
1273 return( PSA_SUCCESS );
1274 }
1275 else
1276 return( PSA_ERROR_INVALID_ARGUMENT );
1277}
1278#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1279
Jaeden Ameroccdce902019-01-10 11:42:27 +00001280#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001281static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1282 unsigned char *buf, size_t size )
1283{
Janos Follath24eed8d2019-11-22 13:21:35 +00001284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001285 unsigned char *c;
1286 size_t len = 0;
1287
1288 c = buf + size;
1289
1290 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1291
1292 return( (int) len );
1293}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001294#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001295
Gilles Peskinef603c712019-01-19 13:40:11 +01001296static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1297 uint8_t *data,
1298 size_t data_size,
1299 size_t *data_length,
1300 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301{
Gilles Peskine5d309672019-07-12 23:47:28 +02001302#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1303 const psa_drv_se_t *drv;
1304 psa_drv_se_context_t *drv_context;
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1306
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001307 *data_length = 0;
1308
Gilles Peskine8e338702019-07-30 20:06:31 +02001309 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001310 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001311
Gilles Peskinef9168942019-09-12 19:20:29 +02001312 /* Reject a zero-length output buffer now, since this can never be a
1313 * valid key representation. This way we know that data must be a valid
1314 * pointer and we can do things like memset(data, ..., data_size). */
1315 if( data_size == 0 )
1316 return( PSA_ERROR_BUFFER_TOO_SMALL );
1317
Gilles Peskine5d309672019-07-12 23:47:28 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001319 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001320 {
1321 psa_drv_se_export_key_t method;
1322 if( drv->key_management == NULL )
1323 return( PSA_ERROR_NOT_SUPPORTED );
1324 method = ( export_public_key ?
1325 drv->key_management->p_export_public :
1326 drv->key_management->p_export );
1327 if( method == NULL )
1328 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001329 return( method( drv_context,
1330 slot->data.se.slot_number,
1331 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001332 }
1333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1334
Gilles Peskine8e338702019-07-30 20:06:31 +02001335 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001336 {
1337 if( slot->data.raw.bytes > data_size )
1338 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001339 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1340 memset( data + slot->data.raw.bytes, 0,
1341 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001342 *data_length = slot->data.raw.bytes;
1343 return( PSA_SUCCESS );
1344 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001345#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001346 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001347 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001348 psa_status_t status;
1349
Gilles Peskineb46bef22019-07-30 21:32:04 +02001350 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001351 if( bytes > data_size )
1352 return( PSA_ERROR_BUFFER_TOO_SMALL );
1353 status = mbedtls_to_psa_error(
1354 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1355 if( status != PSA_SUCCESS )
1356 return( status );
1357 memset( data + bytes, 0, data_size - bytes );
1358 *data_length = bytes;
1359 return( PSA_SUCCESS );
1360 }
1361#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362 else
Moran Peker17e36e12018-05-02 12:55:20 +03001363 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001364#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001365 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1366 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001367 {
Moran Pekera998bc62018-04-16 18:16:20 +03001368 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001370 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001371 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001372#if defined(MBEDTLS_RSA_C)
1373 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001374 pk.pk_info = &mbedtls_rsa_info;
1375 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001376#else
1377 return( PSA_ERROR_NOT_SUPPORTED );
1378#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001379 }
1380 else
1381 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001382#if defined(MBEDTLS_ECP_C)
1383 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001384 pk.pk_info = &mbedtls_eckey_info;
1385 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001386#else
1387 return( PSA_ERROR_NOT_SUPPORTED );
1388#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001389 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001390 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001391 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001392 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001393 }
Moran Peker17e36e12018-05-02 12:55:20 +03001394 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001395 {
Moran Peker17e36e12018-05-02 12:55:20 +03001396 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001397 }
Moran Peker60364322018-04-29 11:34:58 +03001398 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001399 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001400 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001401 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001402 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001403 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1404 * Move the data to the beginning and erase remaining data
1405 * at the original location. */
1406 if( 2 * (size_t) ret <= data_size )
1407 {
1408 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001409 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001410 }
1411 else if( (size_t) ret < data_size )
1412 {
1413 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001414 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001415 }
Moran Pekera998bc62018-04-16 18:16:20 +03001416 *data_length = ret;
1417 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001418 }
1419 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001420#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001421 {
1422 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001423 it is valid for a special-purpose implementation to omit
1424 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001425 return( PSA_ERROR_NOT_SUPPORTED );
1426 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001427 }
1428}
1429
Gilles Peskinec5487a82018-12-03 18:08:14 +01001430psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001431 uint8_t *data,
1432 size_t data_size,
1433 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001434{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001435 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001436 psa_status_t status;
1437
1438 /* Set the key to empty now, so that even when there are errors, we always
1439 * set data_length to a value between 0 and data_size. On error, setting
1440 * the key to empty is a good choice because an empty key representation is
1441 * unlikely to be accepted anywhere. */
1442 *data_length = 0;
1443
1444 /* Export requires the EXPORT flag. There is an exception for public keys,
1445 * which don't require any flag, but psa_get_key_from_slot takes
1446 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001447 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001448 if( status != PSA_SUCCESS )
1449 return( status );
1450 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001451 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001452}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453
Gilles Peskinec5487a82018-12-03 18:08:14 +01001454psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001455 uint8_t *data,
1456 size_t data_size,
1457 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001458{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001459 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001460 psa_status_t status;
1461
1462 /* Set the key to empty now, so that even when there are errors, we always
1463 * set data_length to a value between 0 and data_size. On error, setting
1464 * the key to empty is a good choice because an empty key representation is
1465 * unlikely to be accepted anywhere. */
1466 *data_length = 0;
1467
1468 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001469 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001470 if( status != PSA_SUCCESS )
1471 return( status );
1472 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001473 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001474}
1475
Gilles Peskine91e8c332019-08-02 19:19:39 +02001476#if defined(static_assert)
1477static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1478 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001479static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001480 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001481static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001482 "One or more key attribute flag is listed as both internal-only and external-only" );
1483#endif
1484
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485/** Validate that a key policy is internally well-formed.
1486 *
1487 * This function only rejects invalid policies. It does not validate the
1488 * consistency of the policy with respect to other attributes of the key
1489 * such as the key type.
1490 */
1491static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001492{
1493 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001494 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001495 PSA_KEY_USAGE_ENCRYPT |
1496 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001497 PSA_KEY_USAGE_SIGN_HASH |
1498 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001499 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1500 return( PSA_ERROR_INVALID_ARGUMENT );
1501
Gilles Peskine4747d192019-04-17 15:05:45 +02001502 return( PSA_SUCCESS );
1503}
1504
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001505/** Validate the internal consistency of key attributes.
1506 *
1507 * This function only rejects invalid attribute values. If does not
1508 * validate the consistency of the attributes with any key data that may
1509 * be involved in the creation of the key.
1510 *
1511 * Call this function early in the key creation process.
1512 *
1513 * \param[in] attributes Key attributes for the new key.
1514 * \param[out] p_drv On any return, the driver for the key, if any.
1515 * NULL for a transparent key.
1516 *
1517 */
1518static psa_status_t psa_validate_key_attributes(
1519 const psa_key_attributes_t *attributes,
1520 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001521{
1522 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001523
1524 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001525 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526 status = psa_validate_persistent_key_parameters(
1527 attributes->core.lifetime, attributes->core.id,
1528 p_drv, 1 );
1529 if( status != PSA_SUCCESS )
1530 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001531 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001532
1533 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001534 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001535 return( status );
1536
1537 /* Refuse to create overly large keys.
1538 * Note that this doesn't trigger on import if the attributes don't
1539 * explicitly specify a size (so psa_get_key_bits returns 0), so
1540 * psa_import_key() needs its own checks. */
1541 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1542 return( PSA_ERROR_NOT_SUPPORTED );
1543
Gilles Peskine91e8c332019-08-02 19:19:39 +02001544 /* Reject invalid flags. These should not be reachable through the API. */
1545 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1546 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1547 return( PSA_ERROR_INVALID_ARGUMENT );
1548
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001549 return( PSA_SUCCESS );
1550}
1551
Gilles Peskine4747d192019-04-17 15:05:45 +02001552/** Prepare a key slot to receive key material.
1553 *
1554 * This function allocates a key slot and sets its metadata.
1555 *
1556 * If this function fails, call psa_fail_key_creation().
1557 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001558 * This function is intended to be used as follows:
1559 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1560 * it with the specified attributes, and assign it a handle.
1561 * -# Populate the slot with the key material.
1562 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1563 * In case of failure at any step, stop the sequence and call
1564 * psa_fail_key_creation().
1565 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001566 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001567 * \param[in] attributes Key attributes for the new key.
1568 * \param[out] handle On success, a handle for the allocated slot.
1569 * \param[out] p_slot On success, a pointer to the prepared slot.
1570 * \param[out] p_drv On any return, the driver for the key, if any.
1571 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001572 *
1573 * \retval #PSA_SUCCESS
1574 * The key slot is ready to receive key material.
1575 * \return If this function fails, the key slot is an invalid state.
1576 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 */
1578static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001579 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001580 const psa_key_attributes_t *attributes,
1581 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001582 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001583 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001584{
1585 psa_status_t status;
1586 psa_key_slot_t *slot;
1587
Gilles Peskinedf179142019-07-15 22:02:14 +02001588 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001589 *p_drv = NULL;
1590
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001591 status = psa_validate_key_attributes( attributes, p_drv );
1592 if( status != PSA_SUCCESS )
1593 return( status );
1594
Gilles Peskineedbed562019-08-07 18:19:59 +02001595 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001596 if( status != PSA_SUCCESS )
1597 return( status );
1598 slot = *p_slot;
1599
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001600 /* We're storing the declared bit-size of the key. It's up to each
1601 * creation mechanism to verify that this information is correct.
1602 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001603 * an input (generate, device) but not for those where the bit-size
1604 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001605
1606 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001607
Gilles Peskine91e8c332019-08-02 19:19:39 +02001608 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001609 * external-only flags, query `attributes`. Thanks to the check
1610 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001611 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001612 * may have set. */
1613 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001614
Gilles Peskinecbaff462019-07-12 23:46:04 +02001615#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001616 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001617 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001618 * create the key file in internal storage, create the
1619 * key inside the secure element, and update the driver's
1620 * persistent data. Start a transaction that will encompass these
1621 * three actions. */
1622 /* The first thing to do is to find a slot number for the new key.
1623 * We save the slot number in persistent storage as part of the
1624 * transaction data. It will be needed to recover if the power
1625 * fails during the key creation process, to clean up on the secure
1626 * element side after restarting. Obtaining a slot number from the
1627 * secure element driver updates its persistent state, but we do not yet
1628 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001629 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001630 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001631 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001632 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001633 &slot->data.se.slot_number );
1634 if( status != PSA_SUCCESS )
1635 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001636 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001637 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001638 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001639 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001640 status = psa_crypto_save_transaction( );
1641 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001642 {
1643 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001644 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001645 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001646 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001647
1648 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1649 {
1650 /* Key registration only makes sense with a secure element. */
1651 return( PSA_ERROR_INVALID_ARGUMENT );
1652 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001653#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1654
Darryl Green0c6575a2018-11-07 16:05:30 +00001655 return( status );
1656}
Gilles Peskine4747d192019-04-17 15:05:45 +02001657
1658/** Finalize the creation of a key once its key material has been set.
1659 *
1660 * This entails writing the key to persistent storage.
1661 *
1662 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001663 * See the documentation of psa_start_key_creation() for the intended use
1664 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001666 * \param[in,out] slot Pointer to the slot with key material.
1667 * \param[in] driver The secure element driver for the key,
1668 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001669 *
1670 * \retval #PSA_SUCCESS
1671 * The key was successfully created. The handle is now valid.
1672 * \return If this function fails, the key slot is an invalid state.
1673 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001674 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001675static psa_status_t psa_finish_key_creation(
1676 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001677 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001678{
1679 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001680 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001681 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001682
1683#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001684 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001685 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001686#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1687 if( driver != NULL )
1688 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001689 psa_se_key_data_storage_t data;
1690#if defined(static_assert)
1691 static_assert( sizeof( slot->data.se.slot_number ) ==
1692 sizeof( data.slot_number ),
1693 "Slot number size does not match psa_se_key_data_storage_t" );
1694 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1695 "Bit-size size does not match psa_se_key_data_storage_t" );
1696#endif
1697 memcpy( &data.slot_number, &slot->data.se.slot_number,
1698 sizeof( slot->data.se.slot_number ) );
1699 memcpy( &data.bits, &slot->attr.bits,
1700 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001701 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001702 (uint8_t*) &data,
1703 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001704 }
1705 else
1706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1707 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001708 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001709 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001710 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001711 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1712 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001713 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001714 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1715 status = psa_internal_export_key( slot,
1716 buffer, buffer_size, &length,
1717 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001718 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001719 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001720 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001721
Gilles Peskinef9168942019-09-12 19:20:29 +02001722 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001723 mbedtls_free( buffer );
1724 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001726#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1727
Gilles Peskinecbaff462019-07-12 23:46:04 +02001728#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001729 /* Finish the transaction for a key creation. This does not
1730 * happen when registering an existing key. Detect this case
1731 * by checking whether a transaction is in progress (actual
1732 * creation of a key in a secure element requires a transaction,
1733 * but registration doesn't use one). */
1734 if( driver != NULL &&
1735 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001736 {
1737 status = psa_save_se_persistent_data( driver );
1738 if( status != PSA_SUCCESS )
1739 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001740 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001741 return( status );
1742 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001743 status = psa_crypto_stop_transaction( );
1744 if( status != PSA_SUCCESS )
1745 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001746 }
1747#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1748
Gilles Peskine4747d192019-04-17 15:05:45 +02001749 return( status );
1750}
1751
1752/** Abort the creation of a key.
1753 *
1754 * You may call this function after calling psa_start_key_creation(),
1755 * or after psa_finish_key_creation() fails. In other circumstances, this
1756 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * See the documentation of psa_start_key_creation() for the intended use
1758 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in,out] slot Pointer to the slot with key material.
1761 * \param[in] driver The secure element driver for the key,
1762 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001764static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001765 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001766{
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 (void) driver;
1768
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 if( slot == NULL )
1770 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001771
1772#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001773 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 * element, and the failure happened later (when saving metadata
1775 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001776 * element.
1777 * https://github.com/ARMmbed/mbed-crypto/issues/217
1778 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001779
Gilles Peskined7729582019-08-05 15:55:54 +02001780 /* Abort the ongoing transaction if any (there may not be one if
1781 * the creation process failed before starting one, or if the
1782 * key creation is a registration of a key in a secure element).
1783 * Earlier functions must already have done what it takes to undo any
1784 * partial creation. All that's left is to update the transaction data
1785 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001786 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001787#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1788
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 psa_wipe_key_slot( slot );
1790}
1791
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001792/** Validate optional attributes during key creation.
1793 *
1794 * Some key attributes are optional during key creation. If they are
1795 * specified in the attributes structure, check that they are consistent
1796 * with the data in the slot.
1797 *
1798 * This function should be called near the end of key creation, after
1799 * the slot in memory is fully populated but before saving persistent data.
1800 */
1801static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 const psa_key_slot_t *slot,
1803 const psa_key_attributes_t *attributes )
1804{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001805 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001806 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001807 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001808 return( PSA_ERROR_INVALID_ARGUMENT );
1809 }
1810
1811 if( attributes->domain_parameters_size != 0 )
1812 {
1813#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001814 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 {
1816 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001818 mbedtls_mpi_init( &actual );
1819 mbedtls_mpi_init( &required );
1820 ret = mbedtls_rsa_export( slot->data.rsa,
1821 NULL, NULL, NULL, NULL, &actual );
1822 if( ret != 0 )
1823 goto rsa_exit;
1824 ret = mbedtls_mpi_read_binary( &required,
1825 attributes->domain_parameters,
1826 attributes->domain_parameters_size );
1827 if( ret != 0 )
1828 goto rsa_exit;
1829 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1830 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1831 rsa_exit:
1832 mbedtls_mpi_free( &actual );
1833 mbedtls_mpi_free( &required );
1834 if( ret != 0)
1835 return( mbedtls_to_psa_error( ret ) );
1836 }
1837 else
1838#endif
1839 {
1840 return( PSA_ERROR_INVALID_ARGUMENT );
1841 }
1842 }
1843
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001844 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001846 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001847 return( PSA_ERROR_INVALID_ARGUMENT );
1848 }
1849
1850 return( PSA_SUCCESS );
1851}
1852
Gilles Peskine4747d192019-04-17 15:05:45 +02001853psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001855 size_t data_length,
1856 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001857{
1858 psa_status_t status;
1859 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001860 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001861
Gilles Peskine0f84d622019-09-12 19:03:13 +02001862 /* Reject zero-length symmetric keys (including raw data key objects).
1863 * This also rejects any key which might be encoded as an empty string,
1864 * which is never valid. */
1865 if( data_length == 0 )
1866 return( PSA_ERROR_INVALID_ARGUMENT );
1867
Gilles Peskinedf179142019-07-15 22:02:14 +02001868 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1869 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870 if( status != PSA_SUCCESS )
1871 goto exit;
1872
Gilles Peskine5d309672019-07-12 23:47:28 +02001873#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1874 if( driver != NULL )
1875 {
1876 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001877 /* The driver should set the number of key bits, however in
1878 * case it doesn't, we initialize bits to an invalid value. */
1879 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001880 if( drv->key_management == NULL ||
1881 drv->key_management->p_import == NULL )
1882 {
1883 status = PSA_ERROR_NOT_SUPPORTED;
1884 goto exit;
1885 }
1886 status = drv->key_management->p_import(
1887 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001888 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001889 &bits );
1890 if( status != PSA_SUCCESS )
1891 goto exit;
1892 if( bits > PSA_MAX_KEY_BITS )
1893 {
1894 status = PSA_ERROR_NOT_SUPPORTED;
1895 goto exit;
1896 }
1897 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001898 }
1899 else
1900#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1901 {
1902 status = psa_import_key_into_slot( slot, data, data_length );
1903 if( status != PSA_SUCCESS )
1904 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001905 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001906 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001907 if( status != PSA_SUCCESS )
1908 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001909
Gilles Peskine011e4282019-06-26 18:34:38 +02001910 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001911exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 if( status != PSA_SUCCESS )
1913 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001914 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001915 *handle = 0;
1916 }
1917 return( status );
1918}
1919
Gilles Peskined7729582019-08-05 15:55:54 +02001920#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1921psa_status_t mbedtls_psa_register_se_key(
1922 const psa_key_attributes_t *attributes )
1923{
1924 psa_status_t status;
1925 psa_key_slot_t *slot = NULL;
1926 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001927 psa_key_handle_t handle = 0;
1928
1929 /* Leaving attributes unspecified is not currently supported.
1930 * It could make sense to query the key type and size from the
1931 * secure element, but not all secure elements support this
1932 * and the driver HAL doesn't currently support it. */
1933 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1934 return( PSA_ERROR_NOT_SUPPORTED );
1935 if( psa_get_key_bits( attributes ) == 0 )
1936 return( PSA_ERROR_NOT_SUPPORTED );
1937
1938 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1939 &handle, &slot, &driver );
1940 if( status != PSA_SUCCESS )
1941 goto exit;
1942
Gilles Peskined7729582019-08-05 15:55:54 +02001943 status = psa_finish_key_creation( slot, driver );
1944
1945exit:
1946 if( status != PSA_SUCCESS )
1947 {
1948 psa_fail_key_creation( slot, driver );
1949 }
1950 /* Registration doesn't keep the key in RAM. */
1951 psa_close_key( handle );
1952 return( status );
1953}
1954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1955
Gilles Peskinef603c712019-01-19 13:40:11 +01001956static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001957 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001958{
1959 psa_status_t status;
1960 uint8_t *buffer = NULL;
1961 size_t buffer_size = 0;
1962 size_t length;
1963
Gilles Peskine8e338702019-07-30 20:06:31 +02001964 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001965 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001966 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001967 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001968 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001969 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1970 if( status != PSA_SUCCESS )
1971 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001972 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001974
1975exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001976 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001977 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001978 return( status );
1979}
1980
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001981psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1982 const psa_key_attributes_t *specified_attributes,
1983 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001984{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001986 psa_key_slot_t *source_slot = NULL;
1987 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001988 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001989 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001990
Gilles Peskine28f8f302019-07-24 13:30:31 +02001991 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001992 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001993 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 goto exit;
1995
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001996 status = psa_validate_optional_attributes( source_slot,
1997 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001998 if( status != PSA_SUCCESS )
1999 goto exit;
2000
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002001 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002002 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 if( status != PSA_SUCCESS )
2004 goto exit;
2005
Gilles Peskinedf179142019-07-15 22:02:14 +02002006 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2007 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002008 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002009 if( status != PSA_SUCCESS )
2010 goto exit;
2011
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2013 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002014 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002015 /* Copying to a secure element is not implemented yet. */
2016 status = PSA_ERROR_NOT_SUPPORTED;
2017 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002018 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002019#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002020
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002022 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002023 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002024
Gilles Peskine011e4282019-06-26 18:34:38 +02002025 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002026exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002027 if( status != PSA_SUCCESS )
2028 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002029 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002030 *target_handle = 0;
2031 }
2032 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002033}
2034
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002035
2036
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002037/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002038/* Message digests */
2039/****************************************************************/
2040
Gilles Peskineb16841e2019-10-10 20:36:12 +02002041#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002042static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002043{
2044 switch( alg )
2045 {
2046#if defined(MBEDTLS_MD2_C)
2047 case PSA_ALG_MD2:
2048 return( &mbedtls_md2_info );
2049#endif
2050#if defined(MBEDTLS_MD4_C)
2051 case PSA_ALG_MD4:
2052 return( &mbedtls_md4_info );
2053#endif
2054#if defined(MBEDTLS_MD5_C)
2055 case PSA_ALG_MD5:
2056 return( &mbedtls_md5_info );
2057#endif
2058#if defined(MBEDTLS_RIPEMD160_C)
2059 case PSA_ALG_RIPEMD160:
2060 return( &mbedtls_ripemd160_info );
2061#endif
2062#if defined(MBEDTLS_SHA1_C)
2063 case PSA_ALG_SHA_1:
2064 return( &mbedtls_sha1_info );
2065#endif
2066#if defined(MBEDTLS_SHA256_C)
2067 case PSA_ALG_SHA_224:
2068 return( &mbedtls_sha224_info );
2069 case PSA_ALG_SHA_256:
2070 return( &mbedtls_sha256_info );
2071#endif
2072#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002073#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002074 case PSA_ALG_SHA_384:
2075 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002076#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002077 case PSA_ALG_SHA_512:
2078 return( &mbedtls_sha512_info );
2079#endif
2080 default:
2081 return( NULL );
2082 }
2083}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002084#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002085
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002086psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2087{
2088 switch( operation->alg )
2089 {
Gilles Peskine81736312018-06-26 15:04:31 +02002090 case 0:
2091 /* The object has (apparently) been initialized but it is not
2092 * in use. It's ok to call abort on such an object, and there's
2093 * nothing to do. */
2094 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002095#if defined(MBEDTLS_MD2_C)
2096 case PSA_ALG_MD2:
2097 mbedtls_md2_free( &operation->ctx.md2 );
2098 break;
2099#endif
2100#if defined(MBEDTLS_MD4_C)
2101 case PSA_ALG_MD4:
2102 mbedtls_md4_free( &operation->ctx.md4 );
2103 break;
2104#endif
2105#if defined(MBEDTLS_MD5_C)
2106 case PSA_ALG_MD5:
2107 mbedtls_md5_free( &operation->ctx.md5 );
2108 break;
2109#endif
2110#if defined(MBEDTLS_RIPEMD160_C)
2111 case PSA_ALG_RIPEMD160:
2112 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2113 break;
2114#endif
2115#if defined(MBEDTLS_SHA1_C)
2116 case PSA_ALG_SHA_1:
2117 mbedtls_sha1_free( &operation->ctx.sha1 );
2118 break;
2119#endif
2120#if defined(MBEDTLS_SHA256_C)
2121 case PSA_ALG_SHA_224:
2122 case PSA_ALG_SHA_256:
2123 mbedtls_sha256_free( &operation->ctx.sha256 );
2124 break;
2125#endif
2126#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002127#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002129#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002130 case PSA_ALG_SHA_512:
2131 mbedtls_sha512_free( &operation->ctx.sha512 );
2132 break;
2133#endif
2134 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002135 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002136 }
2137 operation->alg = 0;
2138 return( PSA_SUCCESS );
2139}
2140
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002141psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002142 psa_algorithm_t alg )
2143{
Janos Follath24eed8d2019-11-22 13:21:35 +00002144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002145
2146 /* A context must be freshly initialized before it can be set up. */
2147 if( operation->alg != 0 )
2148 {
2149 return( PSA_ERROR_BAD_STATE );
2150 }
2151
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152 switch( alg )
2153 {
2154#if defined(MBEDTLS_MD2_C)
2155 case PSA_ALG_MD2:
2156 mbedtls_md2_init( &operation->ctx.md2 );
2157 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_MD4_C)
2161 case PSA_ALG_MD4:
2162 mbedtls_md4_init( &operation->ctx.md4 );
2163 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_MD5_C)
2167 case PSA_ALG_MD5:
2168 mbedtls_md5_init( &operation->ctx.md5 );
2169 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2170 break;
2171#endif
2172#if defined(MBEDTLS_RIPEMD160_C)
2173 case PSA_ALG_RIPEMD160:
2174 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2175 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2176 break;
2177#endif
2178#if defined(MBEDTLS_SHA1_C)
2179 case PSA_ALG_SHA_1:
2180 mbedtls_sha1_init( &operation->ctx.sha1 );
2181 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2182 break;
2183#endif
2184#if defined(MBEDTLS_SHA256_C)
2185 case PSA_ALG_SHA_224:
2186 mbedtls_sha256_init( &operation->ctx.sha256 );
2187 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2188 break;
2189 case PSA_ALG_SHA_256:
2190 mbedtls_sha256_init( &operation->ctx.sha256 );
2191 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2192 break;
2193#endif
2194#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002195#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 case PSA_ALG_SHA_384:
2197 mbedtls_sha512_init( &operation->ctx.sha512 );
2198 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2199 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002200#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201 case PSA_ALG_SHA_512:
2202 mbedtls_sha512_init( &operation->ctx.sha512 );
2203 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2204 break;
2205#endif
2206 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002207 return( PSA_ALG_IS_HASH( alg ) ?
2208 PSA_ERROR_NOT_SUPPORTED :
2209 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210 }
2211 if( ret == 0 )
2212 operation->alg = alg;
2213 else
2214 psa_hash_abort( operation );
2215 return( mbedtls_to_psa_error( ret ) );
2216}
2217
2218psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2219 const uint8_t *input,
2220 size_t input_length )
2221{
Janos Follath24eed8d2019-11-22 13:21:35 +00002222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002223
2224 /* Don't require hash implementations to behave correctly on a
2225 * zero-length input, which may have an invalid pointer. */
2226 if( input_length == 0 )
2227 return( PSA_SUCCESS );
2228
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 switch( operation->alg )
2230 {
2231#if defined(MBEDTLS_MD2_C)
2232 case PSA_ALG_MD2:
2233 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2234 input, input_length );
2235 break;
2236#endif
2237#if defined(MBEDTLS_MD4_C)
2238 case PSA_ALG_MD4:
2239 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2240 input, input_length );
2241 break;
2242#endif
2243#if defined(MBEDTLS_MD5_C)
2244 case PSA_ALG_MD5:
2245 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2246 input, input_length );
2247 break;
2248#endif
2249#if defined(MBEDTLS_RIPEMD160_C)
2250 case PSA_ALG_RIPEMD160:
2251 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2252 input, input_length );
2253 break;
2254#endif
2255#if defined(MBEDTLS_SHA1_C)
2256 case PSA_ALG_SHA_1:
2257 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2258 input, input_length );
2259 break;
2260#endif
2261#if defined(MBEDTLS_SHA256_C)
2262 case PSA_ALG_SHA_224:
2263 case PSA_ALG_SHA_256:
2264 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2265 input, input_length );
2266 break;
2267#endif
2268#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002269#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002271#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 case PSA_ALG_SHA_512:
2273 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2274 input, input_length );
2275 break;
2276#endif
2277 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002278 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002280
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281 if( ret != 0 )
2282 psa_hash_abort( operation );
2283 return( mbedtls_to_psa_error( ret ) );
2284}
2285
2286psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2287 uint8_t *hash,
2288 size_t hash_size,
2289 size_t *hash_length )
2290{
itayzafrir40835d42018-08-02 13:14:17 +03002291 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002293 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294
2295 /* Fill the output buffer with something that isn't a valid hash
2296 * (barring an attack on the hash and deliberately-crafted input),
2297 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002298 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002299 /* If hash_size is 0 then hash may be NULL and then the
2300 * call to memset would have undefined behavior. */
2301 if( hash_size != 0 )
2302 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303
2304 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002305 {
2306 status = PSA_ERROR_BUFFER_TOO_SMALL;
2307 goto exit;
2308 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309
2310 switch( operation->alg )
2311 {
2312#if defined(MBEDTLS_MD2_C)
2313 case PSA_ALG_MD2:
2314 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2315 break;
2316#endif
2317#if defined(MBEDTLS_MD4_C)
2318 case PSA_ALG_MD4:
2319 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2320 break;
2321#endif
2322#if defined(MBEDTLS_MD5_C)
2323 case PSA_ALG_MD5:
2324 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2325 break;
2326#endif
2327#if defined(MBEDTLS_RIPEMD160_C)
2328 case PSA_ALG_RIPEMD160:
2329 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2330 break;
2331#endif
2332#if defined(MBEDTLS_SHA1_C)
2333 case PSA_ALG_SHA_1:
2334 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2335 break;
2336#endif
2337#if defined(MBEDTLS_SHA256_C)
2338 case PSA_ALG_SHA_224:
2339 case PSA_ALG_SHA_256:
2340 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2341 break;
2342#endif
2343#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002344#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002346#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 case PSA_ALG_SHA_512:
2348 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2349 break;
2350#endif
2351 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002352 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353 }
itayzafrir40835d42018-08-02 13:14:17 +03002354 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355
itayzafrir40835d42018-08-02 13:14:17 +03002356exit:
2357 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002359 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360 return( psa_hash_abort( operation ) );
2361 }
2362 else
2363 {
2364 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002365 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002366 }
2367}
2368
Gilles Peskine2d277862018-06-18 15:41:12 +02002369psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2370 const uint8_t *hash,
2371 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002372{
2373 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2374 size_t actual_hash_length;
2375 psa_status_t status = psa_hash_finish( operation,
2376 actual_hash, sizeof( actual_hash ),
2377 &actual_hash_length );
2378 if( status != PSA_SUCCESS )
2379 return( status );
2380 if( actual_hash_length != hash_length )
2381 return( PSA_ERROR_INVALID_SIGNATURE );
2382 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2383 return( PSA_ERROR_INVALID_SIGNATURE );
2384 return( PSA_SUCCESS );
2385}
2386
Gilles Peskine0a749c82019-11-28 19:33:58 +01002387psa_status_t psa_hash_compute( psa_algorithm_t alg,
2388 const uint8_t *input, size_t input_length,
2389 uint8_t *hash, size_t hash_size,
2390 size_t *hash_length )
2391{
2392 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2393 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2394
2395 *hash_length = hash_size;
2396 status = psa_hash_setup( &operation, alg );
2397 if( status != PSA_SUCCESS )
2398 goto exit;
2399 status = psa_hash_update( &operation, input, input_length );
2400 if( status != PSA_SUCCESS )
2401 goto exit;
2402 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2403 if( status != PSA_SUCCESS )
2404 goto exit;
2405
2406exit:
2407 if( status == PSA_SUCCESS )
2408 status = psa_hash_abort( &operation );
2409 else
2410 psa_hash_abort( &operation );
2411 return( status );
2412}
2413
2414psa_status_t psa_hash_compare( psa_algorithm_t alg,
2415 const uint8_t *input, size_t input_length,
2416 const uint8_t *hash, size_t hash_length )
2417{
2418 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2420
2421 status = psa_hash_setup( &operation, alg );
2422 if( status != PSA_SUCCESS )
2423 goto exit;
2424 status = psa_hash_update( &operation, input, input_length );
2425 if( status != PSA_SUCCESS )
2426 goto exit;
2427 status = psa_hash_verify( &operation, hash, hash_length );
2428 if( status != PSA_SUCCESS )
2429 goto exit;
2430
2431exit:
2432 if( status == PSA_SUCCESS )
2433 status = psa_hash_abort( &operation );
2434 else
2435 psa_hash_abort( &operation );
2436 return( status );
2437}
2438
Gilles Peskineeb35d782019-01-22 17:56:16 +01002439psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2440 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002441{
2442 if( target_operation->alg != 0 )
2443 return( PSA_ERROR_BAD_STATE );
2444
2445 switch( source_operation->alg )
2446 {
2447 case 0:
2448 return( PSA_ERROR_BAD_STATE );
2449#if defined(MBEDTLS_MD2_C)
2450 case PSA_ALG_MD2:
2451 mbedtls_md2_clone( &target_operation->ctx.md2,
2452 &source_operation->ctx.md2 );
2453 break;
2454#endif
2455#if defined(MBEDTLS_MD4_C)
2456 case PSA_ALG_MD4:
2457 mbedtls_md4_clone( &target_operation->ctx.md4,
2458 &source_operation->ctx.md4 );
2459 break;
2460#endif
2461#if defined(MBEDTLS_MD5_C)
2462 case PSA_ALG_MD5:
2463 mbedtls_md5_clone( &target_operation->ctx.md5,
2464 &source_operation->ctx.md5 );
2465 break;
2466#endif
2467#if defined(MBEDTLS_RIPEMD160_C)
2468 case PSA_ALG_RIPEMD160:
2469 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2470 &source_operation->ctx.ripemd160 );
2471 break;
2472#endif
2473#if defined(MBEDTLS_SHA1_C)
2474 case PSA_ALG_SHA_1:
2475 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2476 &source_operation->ctx.sha1 );
2477 break;
2478#endif
2479#if defined(MBEDTLS_SHA256_C)
2480 case PSA_ALG_SHA_224:
2481 case PSA_ALG_SHA_256:
2482 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2483 &source_operation->ctx.sha256 );
2484 break;
2485#endif
2486#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002487#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002488 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002489#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002490 case PSA_ALG_SHA_512:
2491 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2492 &source_operation->ctx.sha512 );
2493 break;
2494#endif
2495 default:
2496 return( PSA_ERROR_NOT_SUPPORTED );
2497 }
2498
2499 target_operation->alg = source_operation->alg;
2500 return( PSA_SUCCESS );
2501}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502
2503
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002504/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505/* MAC */
2506/****************************************************************/
2507
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002508static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 psa_algorithm_t alg,
2510 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002511 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002512 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002515 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002517 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002518 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002519
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2521 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002522 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002524 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002525 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526 mode = MBEDTLS_MODE_STREAM;
2527 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528 case PSA_ALG_CTR:
2529 mode = MBEDTLS_MODE_CTR;
2530 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002531 case PSA_ALG_CFB:
2532 mode = MBEDTLS_MODE_CFB;
2533 break;
2534 case PSA_ALG_OFB:
2535 mode = MBEDTLS_MODE_OFB;
2536 break;
2537 case PSA_ALG_CBC_NO_PADDING:
2538 mode = MBEDTLS_MODE_CBC;
2539 break;
2540 case PSA_ALG_CBC_PKCS7:
2541 mode = MBEDTLS_MODE_CBC;
2542 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002543 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 mode = MBEDTLS_MODE_CCM;
2545 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002546 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547 mode = MBEDTLS_MODE_GCM;
2548 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002549 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2550 mode = MBEDTLS_MODE_CHACHAPOLY;
2551 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 default:
2553 return( NULL );
2554 }
2555 }
2556 else if( alg == PSA_ALG_CMAC )
2557 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 else
2559 return( NULL );
2560
2561 switch( key_type )
2562 {
2563 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002564 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 break;
2566 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002567 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2568 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002570 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002572 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002573 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2574 * but two-key Triple-DES is functionally three-key Triple-DES
2575 * with K1=K3, so that's how we present it to mbedtls. */
2576 if( key_bits == 128 )
2577 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578 break;
2579 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002580 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581 break;
2582 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002583 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002585 case PSA_KEY_TYPE_CHACHA20:
2586 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2587 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 default:
2589 return( NULL );
2590 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002591 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002592 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593
Jaeden Amero23bbb752018-06-26 14:16:54 +01002594 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2595 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596}
2597
Gilles Peskinea05219c2018-11-16 16:02:56 +01002598#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002599static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600{
Gilles Peskine2d277862018-06-18 15:41:12 +02002601 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002602 {
2603 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002604 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002605 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002606 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002607 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002608 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002609 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002610 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002611 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002612 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002613 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002614 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002615 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002616 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002617 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002618 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002619 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002620 return( 128 );
2621 default:
2622 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002623 }
2624}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002625#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002626
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627/* Initialize the MAC operation structure. Once this function has been
2628 * called, psa_mac_abort can run and will do the right thing. */
2629static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2630 psa_algorithm_t alg )
2631{
2632 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2633
2634 operation->alg = alg;
2635 operation->key_set = 0;
2636 operation->iv_set = 0;
2637 operation->iv_required = 0;
2638 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002639 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640
2641#if defined(MBEDTLS_CMAC_C)
2642 if( alg == PSA_ALG_CMAC )
2643 {
2644 operation->iv_required = 0;
2645 mbedtls_cipher_init( &operation->ctx.cmac );
2646 status = PSA_SUCCESS;
2647 }
2648 else
2649#endif /* MBEDTLS_CMAC_C */
2650#if defined(MBEDTLS_MD_C)
2651 if( PSA_ALG_IS_HMAC( operation->alg ) )
2652 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002653 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2654 operation->ctx.hmac.hash_ctx.alg = 0;
2655 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656 }
2657 else
2658#endif /* MBEDTLS_MD_C */
2659 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002660 if( ! PSA_ALG_IS_MAC( alg ) )
2661 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002662 }
2663
2664 if( status != PSA_SUCCESS )
2665 memset( operation, 0, sizeof( *operation ) );
2666 return( status );
2667}
2668
Gilles Peskine01126fa2018-07-12 17:04:55 +02002669#if defined(MBEDTLS_MD_C)
2670static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2671{
Gilles Peskine3f108122018-12-07 18:14:53 +01002672 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002673 return( psa_hash_abort( &hmac->hash_ctx ) );
2674}
2675#endif /* MBEDTLS_MD_C */
2676
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2678{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002679 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002681 /* The object has (apparently) been initialized but it is not
2682 * in use. It's ok to call abort on such an object, and there's
2683 * nothing to do. */
2684 return( PSA_SUCCESS );
2685 }
2686 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002688 if( operation->alg == PSA_ALG_CMAC )
2689 {
2690 mbedtls_cipher_free( &operation->ctx.cmac );
2691 }
2692 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002695 if( PSA_ALG_IS_HMAC( operation->alg ) )
2696 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002697 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002698 }
2699 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002701 {
2702 /* Sanity check (shouldn't happen: operation->alg should
2703 * always have been initialized to a valid value). */
2704 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705 }
Moran Peker41deec42018-04-04 15:43:05 +03002706
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707 operation->alg = 0;
2708 operation->key_set = 0;
2709 operation->iv_set = 0;
2710 operation->iv_required = 0;
2711 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002712 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002713
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002715
2716bad_state:
2717 /* If abort is called on an uninitialized object, we can't trust
2718 * anything. Wipe the object in case it contains confidential data.
2719 * This may result in a memory leak if a pointer gets overwritten,
2720 * but it's too late to do anything about this. */
2721 memset( operation, 0, sizeof( *operation ) );
2722 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723}
2724
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002725#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002726static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002728 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729 const mbedtls_cipher_info_t *cipher_info )
2730{
Janos Follath24eed8d2019-11-22 13:21:35 +00002731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732
2733 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734
2735 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2736 if( ret != 0 )
2737 return( ret );
2738
2739 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2740 slot->data.raw.data,
2741 key_bits );
2742 return( ret );
2743}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002744#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002745
Gilles Peskine248051a2018-06-20 16:09:38 +02002746#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002747static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2748 const uint8_t *key,
2749 size_t key_length,
2750 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002752 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002753 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002754 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002755 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 psa_status_t status;
2757
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002758 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2759 * overflow below. This should never trigger if the hash algorithm
2760 * is implemented correctly. */
2761 /* The size checks against the ipad and opad buffers cannot be written
2762 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2763 * because that triggers -Wlogical-op on GCC 7.3. */
2764 if( block_size > sizeof( ipad ) )
2765 return( PSA_ERROR_NOT_SUPPORTED );
2766 if( block_size > sizeof( hmac->opad ) )
2767 return( PSA_ERROR_NOT_SUPPORTED );
2768 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002769 return( PSA_ERROR_NOT_SUPPORTED );
2770
Gilles Peskined223b522018-06-11 18:12:58 +02002771 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002773 status = psa_hash_compute( hash_alg, key, key_length,
2774 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002775 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002776 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777 }
Gilles Peskine96889972018-07-12 17:07:03 +02002778 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2779 * but it is permitted. It is common when HMAC is used in HKDF, for
2780 * example. Don't call `memcpy` in the 0-length because `key` could be
2781 * an invalid pointer which would make the behavior undefined. */
2782 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002783 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002784
Gilles Peskined223b522018-06-11 18:12:58 +02002785 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2786 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002787 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002788 ipad[i] ^= 0x36;
2789 memset( ipad + key_length, 0x36, block_size - key_length );
2790
2791 /* Copy the key material from ipad to opad, flipping the requisite bits,
2792 * and filling the rest of opad with the requisite constant. */
2793 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002794 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2795 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002796
Gilles Peskine01126fa2018-07-12 17:04:55 +02002797 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002798 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002799 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002800
Gilles Peskine01126fa2018-07-12 17:04:55 +02002801 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002802
2803cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002804 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002805
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002806 return( status );
2807}
Gilles Peskine248051a2018-06-20 16:09:38 +02002808#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002809
Gilles Peskine89167cb2018-07-08 20:12:23 +02002810static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002811 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002812 psa_algorithm_t alg,
2813 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002816 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002818 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002819 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002820 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002821 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002823 /* A context must be freshly initialized before it can be set up. */
2824 if( operation->alg != 0 )
2825 {
2826 return( PSA_ERROR_BAD_STATE );
2827 }
2828
Gilles Peskined911eb72018-08-14 15:18:45 +02002829 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002830 if( status != PSA_SUCCESS )
2831 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002832 if( is_sign )
2833 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834
Gilles Peskine28f8f302019-07-24 13:30:31 +02002835 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002836 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002837 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002838 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002839
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002841 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 {
2843 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002844 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002845 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002847 if( cipher_info == NULL )
2848 {
2849 status = PSA_ERROR_NOT_SUPPORTED;
2850 goto exit;
2851 }
2852 operation->mac_size = cipher_info->block_size;
2853 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2854 status = mbedtls_to_psa_error( ret );
2855 }
2856 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002859 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002860 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002861 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002862 if( hash_alg == 0 )
2863 {
2864 status = PSA_ERROR_NOT_SUPPORTED;
2865 goto exit;
2866 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002867
2868 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2869 /* Sanity check. This shouldn't fail on a valid configuration. */
2870 if( operation->mac_size == 0 ||
2871 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2872 {
2873 status = PSA_ERROR_NOT_SUPPORTED;
2874 goto exit;
2875 }
2876
Gilles Peskine8e338702019-07-30 20:06:31 +02002877 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002878 {
2879 status = PSA_ERROR_INVALID_ARGUMENT;
2880 goto exit;
2881 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002882
Gilles Peskine01126fa2018-07-12 17:04:55 +02002883 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2884 slot->data.raw.data,
2885 slot->data.raw.bytes,
2886 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002887 }
2888 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002889#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002890 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002891 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002892 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002894
Gilles Peskined911eb72018-08-14 15:18:45 +02002895 if( truncated == 0 )
2896 {
2897 /* The "normal" case: untruncated algorithm. Nothing to do. */
2898 }
2899 else if( truncated < 4 )
2900 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002901 /* A very short MAC is too short for security since it can be
2902 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2903 * so we make this our minimum, even though 32 bits is still
2904 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002905 status = PSA_ERROR_NOT_SUPPORTED;
2906 }
2907 else if( truncated > operation->mac_size )
2908 {
2909 /* It's impossible to "truncate" to a larger length. */
2910 status = PSA_ERROR_INVALID_ARGUMENT;
2911 }
2912 else
2913 operation->mac_size = truncated;
2914
Gilles Peskinefbfac682018-07-08 20:51:54 +02002915exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002916 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002918 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002919 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002920 else
2921 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002922 operation->key_set = 1;
2923 }
2924 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925}
2926
Gilles Peskine89167cb2018-07-08 20:12:23 +02002927psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002928 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002929 psa_algorithm_t alg )
2930{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002931 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002932}
2933
2934psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002935 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002936 psa_algorithm_t alg )
2937{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002938 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002939}
2940
Gilles Peskine8c9def32018-02-08 10:02:12 +01002941psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2942 const uint8_t *input,
2943 size_t input_length )
2944{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002945 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002947 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002949 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 operation->has_input = 1;
2951
Gilles Peskine8c9def32018-02-08 10:02:12 +01002952#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002953 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002954 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002955 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2956 input, input_length );
2957 status = mbedtls_to_psa_error( ret );
2958 }
2959 else
2960#endif /* MBEDTLS_CMAC_C */
2961#if defined(MBEDTLS_MD_C)
2962 if( PSA_ALG_IS_HMAC( operation->alg ) )
2963 {
2964 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2965 input_length );
2966 }
2967 else
2968#endif /* MBEDTLS_MD_C */
2969 {
2970 /* This shouldn't happen if `operation` was initialized by
2971 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002972 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002973 }
2974
Gilles Peskinefbfac682018-07-08 20:51:54 +02002975 if( status != PSA_SUCCESS )
2976 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002977 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978}
2979
Gilles Peskine01126fa2018-07-12 17:04:55 +02002980#if defined(MBEDTLS_MD_C)
2981static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2982 uint8_t *mac,
2983 size_t mac_size )
2984{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002985 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002986 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2987 size_t hash_size = 0;
2988 size_t block_size = psa_get_hash_block_size( hash_alg );
2989 psa_status_t status;
2990
Gilles Peskine01126fa2018-07-12 17:04:55 +02002991 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2992 if( status != PSA_SUCCESS )
2993 return( status );
2994 /* From here on, tmp needs to be wiped. */
2995
2996 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2997 if( status != PSA_SUCCESS )
2998 goto exit;
2999
3000 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3001 if( status != PSA_SUCCESS )
3002 goto exit;
3003
3004 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3005 if( status != PSA_SUCCESS )
3006 goto exit;
3007
Gilles Peskined911eb72018-08-14 15:18:45 +02003008 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3009 if( status != PSA_SUCCESS )
3010 goto exit;
3011
3012 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003013
3014exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003015 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003016 return( status );
3017}
3018#endif /* MBEDTLS_MD_C */
3019
mohammad16036df908f2018-04-02 08:34:15 -07003020static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003021 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003022 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003023{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003024 if( ! operation->key_set )
3025 return( PSA_ERROR_BAD_STATE );
3026 if( operation->iv_required && ! operation->iv_set )
3027 return( PSA_ERROR_BAD_STATE );
3028
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029 if( mac_size < operation->mac_size )
3030 return( PSA_ERROR_BUFFER_TOO_SMALL );
3031
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003033 if( operation->alg == PSA_ALG_CMAC )
3034 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003035 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3036 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3037 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003038 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003039 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003040 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003041 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003042 else
3043#endif /* MBEDTLS_CMAC_C */
3044#if defined(MBEDTLS_MD_C)
3045 if( PSA_ALG_IS_HMAC( operation->alg ) )
3046 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003047 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003048 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003049 }
3050 else
3051#endif /* MBEDTLS_MD_C */
3052 {
3053 /* This shouldn't happen if `operation` was initialized by
3054 * a setup function. */
3055 return( PSA_ERROR_BAD_STATE );
3056 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057}
3058
Gilles Peskineacd4be32018-07-08 19:56:25 +02003059psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3060 uint8_t *mac,
3061 size_t mac_size,
3062 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003063{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003064 psa_status_t status;
3065
Jaeden Amero252ef282019-02-15 14:05:35 +00003066 if( operation->alg == 0 )
3067 {
3068 return( PSA_ERROR_BAD_STATE );
3069 }
3070
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003071 /* Fill the output buffer with something that isn't a valid mac
3072 * (barring an attack on the mac and deliberately-crafted input),
3073 * in case the caller doesn't check the return status properly. */
3074 *mac_length = mac_size;
3075 /* If mac_size is 0 then mac may be NULL and then the
3076 * call to memset would have undefined behavior. */
3077 if( mac_size != 0 )
3078 memset( mac, '!', mac_size );
3079
Gilles Peskine89167cb2018-07-08 20:12:23 +02003080 if( ! operation->is_sign )
3081 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003082 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003083 }
mohammad16036df908f2018-04-02 08:34:15 -07003084
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003085 status = psa_mac_finish_internal( operation, mac, mac_size );
3086
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003087 if( status == PSA_SUCCESS )
3088 {
3089 status = psa_mac_abort( operation );
3090 if( status == PSA_SUCCESS )
3091 *mac_length = operation->mac_size;
3092 else
3093 memset( mac, '!', mac_size );
3094 }
3095 else
3096 psa_mac_abort( operation );
3097 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003098}
3099
Gilles Peskineacd4be32018-07-08 19:56:25 +02003100psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3101 const uint8_t *mac,
3102 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003103{
Gilles Peskine828ed142018-06-18 23:25:51 +02003104 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003105 psa_status_t status;
3106
Jaeden Amero252ef282019-02-15 14:05:35 +00003107 if( operation->alg == 0 )
3108 {
3109 return( PSA_ERROR_BAD_STATE );
3110 }
3111
Gilles Peskine89167cb2018-07-08 20:12:23 +02003112 if( operation->is_sign )
3113 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003114 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003115 }
3116 if( operation->mac_size != mac_length )
3117 {
3118 status = PSA_ERROR_INVALID_SIGNATURE;
3119 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003120 }
mohammad16036df908f2018-04-02 08:34:15 -07003121
3122 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003123 actual_mac, sizeof( actual_mac ) );
3124
3125 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3126 status = PSA_ERROR_INVALID_SIGNATURE;
3127
3128cleanup:
3129 if( status == PSA_SUCCESS )
3130 status = psa_mac_abort( operation );
3131 else
3132 psa_mac_abort( operation );
3133
Gilles Peskine3f108122018-12-07 18:14:53 +01003134 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003135
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003136 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003137}
3138
3139
Gilles Peskine20035e32018-02-03 22:44:14 +01003140
Gilles Peskine20035e32018-02-03 22:44:14 +01003141/****************************************************************/
3142/* Asymmetric cryptography */
3143/****************************************************************/
3144
Gilles Peskine2b450e32018-06-27 15:42:46 +02003145#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003146/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003147 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003148static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3149 size_t hash_length,
3150 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003151{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003152 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003153 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003154 *md_alg = mbedtls_md_get_type( md_info );
3155
3156 /* The Mbed TLS RSA module uses an unsigned int for hash length
3157 * parameters. Validate that it fits so that we don't risk an
3158 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003159#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003160 if( hash_length > UINT_MAX )
3161 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003162#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003163
3164#if defined(MBEDTLS_PKCS1_V15)
3165 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3166 * must be correct. */
3167 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3168 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003169 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003170 if( md_info == NULL )
3171 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003172 if( mbedtls_md_get_size( md_info ) != hash_length )
3173 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003174 }
3175#endif /* MBEDTLS_PKCS1_V15 */
3176
3177#if defined(MBEDTLS_PKCS1_V21)
3178 /* PSS requires a hash internally. */
3179 if( PSA_ALG_IS_RSA_PSS( alg ) )
3180 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003181 if( md_info == NULL )
3182 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003183 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003184#endif /* MBEDTLS_PKCS1_V21 */
3185
Gilles Peskine61b91d42018-06-08 16:09:36 +02003186 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003187}
3188
Gilles Peskine2b450e32018-06-27 15:42:46 +02003189static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3190 psa_algorithm_t alg,
3191 const uint8_t *hash,
3192 size_t hash_length,
3193 uint8_t *signature,
3194 size_t signature_size,
3195 size_t *signature_length )
3196{
3197 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003199 mbedtls_md_type_t md_alg;
3200
3201 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3202 if( status != PSA_SUCCESS )
3203 return( status );
3204
Gilles Peskine630a18a2018-06-29 17:49:35 +02003205 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003206 return( PSA_ERROR_BUFFER_TOO_SMALL );
3207
3208#if defined(MBEDTLS_PKCS1_V15)
3209 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3210 {
3211 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3212 MBEDTLS_MD_NONE );
3213 ret = mbedtls_rsa_pkcs1_sign( rsa,
3214 mbedtls_ctr_drbg_random,
3215 &global_data.ctr_drbg,
3216 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003217 md_alg,
3218 (unsigned int) hash_length,
3219 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003220 signature );
3221 }
3222 else
3223#endif /* MBEDTLS_PKCS1_V15 */
3224#if defined(MBEDTLS_PKCS1_V21)
3225 if( PSA_ALG_IS_RSA_PSS( alg ) )
3226 {
3227 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3228 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3229 mbedtls_ctr_drbg_random,
3230 &global_data.ctr_drbg,
3231 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003232 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003233 (unsigned int) hash_length,
3234 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003235 signature );
3236 }
3237 else
3238#endif /* MBEDTLS_PKCS1_V21 */
3239 {
3240 return( PSA_ERROR_INVALID_ARGUMENT );
3241 }
3242
3243 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003244 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003245 return( mbedtls_to_psa_error( ret ) );
3246}
3247
3248static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3249 psa_algorithm_t alg,
3250 const uint8_t *hash,
3251 size_t hash_length,
3252 const uint8_t *signature,
3253 size_t signature_length )
3254{
3255 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003257 mbedtls_md_type_t md_alg;
3258
3259 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3260 if( status != PSA_SUCCESS )
3261 return( status );
3262
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003263 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3264 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003265
3266#if defined(MBEDTLS_PKCS1_V15)
3267 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3268 {
3269 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3270 MBEDTLS_MD_NONE );
3271 ret = mbedtls_rsa_pkcs1_verify( rsa,
3272 mbedtls_ctr_drbg_random,
3273 &global_data.ctr_drbg,
3274 MBEDTLS_RSA_PUBLIC,
3275 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003276 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003277 hash,
3278 signature );
3279 }
3280 else
3281#endif /* MBEDTLS_PKCS1_V15 */
3282#if defined(MBEDTLS_PKCS1_V21)
3283 if( PSA_ALG_IS_RSA_PSS( alg ) )
3284 {
3285 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3286 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3287 mbedtls_ctr_drbg_random,
3288 &global_data.ctr_drbg,
3289 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003290 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003291 (unsigned int) hash_length,
3292 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003293 signature );
3294 }
3295 else
3296#endif /* MBEDTLS_PKCS1_V21 */
3297 {
3298 return( PSA_ERROR_INVALID_ARGUMENT );
3299 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003300
3301 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3302 * the rest of the signature is invalid". This has little use in
3303 * practice and PSA doesn't report this distinction. */
3304 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3305 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003306 return( mbedtls_to_psa_error( ret ) );
3307}
3308#endif /* MBEDTLS_RSA_C */
3309
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003310#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003311/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3312 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3313 * (even though these functions don't modify it). */
3314static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3315 psa_algorithm_t alg,
3316 const uint8_t *hash,
3317 size_t hash_length,
3318 uint8_t *signature,
3319 size_t signature_size,
3320 size_t *signature_length )
3321{
Janos Follath24eed8d2019-11-22 13:21:35 +00003322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003323 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003324 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003325 mbedtls_mpi_init( &r );
3326 mbedtls_mpi_init( &s );
3327
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003328 if( signature_size < 2 * curve_bytes )
3329 {
3330 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3331 goto cleanup;
3332 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003333
Gilles Peskinea05219c2018-11-16 16:02:56 +01003334#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003335 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3336 {
3337 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3338 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3339 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003340 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3341 &ecp->d, hash,
3342 hash_length, md_alg,
3343 mbedtls_ctr_drbg_random,
3344 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003345 }
3346 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003347#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003348 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003349 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003350 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3351 hash, hash_length,
3352 mbedtls_ctr_drbg_random,
3353 &global_data.ctr_drbg ) );
3354 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003355
3356 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3357 signature,
3358 curve_bytes ) );
3359 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3360 signature + curve_bytes,
3361 curve_bytes ) );
3362
3363cleanup:
3364 mbedtls_mpi_free( &r );
3365 mbedtls_mpi_free( &s );
3366 if( ret == 0 )
3367 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003368 return( mbedtls_to_psa_error( ret ) );
3369}
3370
3371static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3372 const uint8_t *hash,
3373 size_t hash_length,
3374 const uint8_t *signature,
3375 size_t signature_length )
3376{
Janos Follath24eed8d2019-11-22 13:21:35 +00003377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003378 mbedtls_mpi r, s;
3379 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3380 mbedtls_mpi_init( &r );
3381 mbedtls_mpi_init( &s );
3382
3383 if( signature_length != 2 * curve_bytes )
3384 return( PSA_ERROR_INVALID_SIGNATURE );
3385
3386 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3387 signature,
3388 curve_bytes ) );
3389 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3390 signature + curve_bytes,
3391 curve_bytes ) );
3392
3393 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3394 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003395
3396cleanup:
3397 mbedtls_mpi_free( &r );
3398 mbedtls_mpi_free( &s );
3399 return( mbedtls_to_psa_error( ret ) );
3400}
3401#endif /* MBEDTLS_ECDSA_C */
3402
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003403psa_status_t psa_sign_hash( psa_key_handle_t handle,
3404 psa_algorithm_t alg,
3405 const uint8_t *hash,
3406 size_t hash_length,
3407 uint8_t *signature,
3408 size_t signature_size,
3409 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003410{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003411 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003412 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003413#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3414 const psa_drv_se_t *drv;
3415 psa_drv_se_context_t *drv_context;
3416#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417
3418 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003419 /* Immediately reject a zero-length signature buffer. This guarantees
3420 * that signature must be a valid pointer. (On the other hand, the hash
3421 * buffer can in principle be empty since it doesn't actually have
3422 * to be a hash.) */
3423 if( signature_size == 0 )
3424 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003425
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003427 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003428 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003429 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003430 {
3431 status = PSA_ERROR_INVALID_ARGUMENT;
3432 goto exit;
3433 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003434
Gilles Peskineedc64242019-08-07 21:05:07 +02003435#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3436 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3437 {
3438 if( drv->asymmetric == NULL ||
3439 drv->asymmetric->p_sign == NULL )
3440 {
3441 status = PSA_ERROR_NOT_SUPPORTED;
3442 goto exit;
3443 }
3444 status = drv->asymmetric->p_sign( drv_context,
3445 slot->data.se.slot_number,
3446 alg,
3447 hash, hash_length,
3448 signature, signature_size,
3449 signature_length );
3450 }
3451 else
3452#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003453#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003454 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003455 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003456 status = psa_rsa_sign( slot->data.rsa,
3457 alg,
3458 hash, hash_length,
3459 signature, signature_size,
3460 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003461 }
3462 else
3463#endif /* defined(MBEDTLS_RSA_C) */
3464#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003465 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003466 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003468 if(
3469#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3470 PSA_ALG_IS_ECDSA( alg )
3471#else
3472 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3473#endif
3474 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003475 status = psa_ecdsa_sign( slot->data.ecp,
3476 alg,
3477 hash, hash_length,
3478 signature, signature_size,
3479 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003480 else
3481#endif /* defined(MBEDTLS_ECDSA_C) */
3482 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003483 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003484 }
itayzafrir5c753392018-05-08 11:18:38 +03003485 }
3486 else
3487#endif /* defined(MBEDTLS_ECP_C) */
3488 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003489 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003490 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003491
3492exit:
3493 /* Fill the unused part of the output buffer (the whole buffer on error,
3494 * the trailing part on success) with something that isn't a valid mac
3495 * (barring an attack on the mac and deliberately-crafted input),
3496 * in case the caller doesn't check the return status properly. */
3497 if( status == PSA_SUCCESS )
3498 memset( signature + *signature_length, '!',
3499 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003500 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003501 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003502 /* If signature_size is 0 then we have nothing to do. We must not call
3503 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003504 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003505}
3506
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003507psa_status_t psa_verify_hash( psa_key_handle_t handle,
3508 psa_algorithm_t alg,
3509 const uint8_t *hash,
3510 size_t hash_length,
3511 const uint8_t *signature,
3512 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003513{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003514 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003515 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003516#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3517 const psa_drv_se_t *drv;
3518 psa_drv_se_context_t *drv_context;
3519#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003520
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003521 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003522 if( status != PSA_SUCCESS )
3523 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003524
Gilles Peskineedc64242019-08-07 21:05:07 +02003525#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3526 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3527 {
3528 if( drv->asymmetric == NULL ||
3529 drv->asymmetric->p_verify == NULL )
3530 return( PSA_ERROR_NOT_SUPPORTED );
3531 return( drv->asymmetric->p_verify( drv_context,
3532 slot->data.se.slot_number,
3533 alg,
3534 hash, hash_length,
3535 signature, signature_length ) );
3536 }
3537 else
3538#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003539#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003540 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003542 return( psa_rsa_verify( slot->data.rsa,
3543 alg,
3544 hash, hash_length,
3545 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546 }
3547 else
3548#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003549#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003550 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003551 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003552#if defined(MBEDTLS_ECDSA_C)
3553 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003554 return( psa_ecdsa_verify( slot->data.ecp,
3555 hash, hash_length,
3556 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003557 else
3558#endif /* defined(MBEDTLS_ECDSA_C) */
3559 {
3560 return( PSA_ERROR_INVALID_ARGUMENT );
3561 }
itayzafrir5c753392018-05-08 11:18:38 +03003562 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003563 else
3564#endif /* defined(MBEDTLS_ECP_C) */
3565 {
3566 return( PSA_ERROR_NOT_SUPPORTED );
3567 }
3568}
3569
Gilles Peskine072ac562018-06-30 00:21:29 +02003570#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3571static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3572 mbedtls_rsa_context *rsa )
3573{
3574 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3575 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3576 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3577 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3578}
3579#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3580
Gilles Peskinec5487a82018-12-03 18:08:14 +01003581psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003582 psa_algorithm_t alg,
3583 const uint8_t *input,
3584 size_t input_length,
3585 const uint8_t *salt,
3586 size_t salt_length,
3587 uint8_t *output,
3588 size_t output_size,
3589 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003591 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003592 psa_status_t status;
3593
Darryl Green5cc689a2018-07-24 15:34:10 +01003594 (void) input;
3595 (void) input_length;
3596 (void) salt;
3597 (void) output;
3598 (void) output_size;
3599
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003600 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003602 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3603 return( PSA_ERROR_INVALID_ARGUMENT );
3604
Gilles Peskine28f8f302019-07-24 13:30:31 +02003605 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003606 if( status != PSA_SUCCESS )
3607 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003608 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3609 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611
3612#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003613 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003614 {
3615 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003617 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003618 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003619#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003620 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003622 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3623 mbedtls_ctr_drbg_random,
3624 &global_data.ctr_drbg,
3625 MBEDTLS_RSA_PUBLIC,
3626 input_length,
3627 input,
3628 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629 }
3630 else
3631#endif /* MBEDTLS_PKCS1_V15 */
3632#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003633 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003635 psa_rsa_oaep_set_padding_mode( alg, rsa );
3636 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3637 mbedtls_ctr_drbg_random,
3638 &global_data.ctr_drbg,
3639 MBEDTLS_RSA_PUBLIC,
3640 salt, salt_length,
3641 input_length,
3642 input,
3643 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 }
3645 else
3646#endif /* MBEDTLS_PKCS1_V21 */
3647 {
3648 return( PSA_ERROR_INVALID_ARGUMENT );
3649 }
3650 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003651 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 return( mbedtls_to_psa_error( ret ) );
3653 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003655#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003656 {
3657 return( PSA_ERROR_NOT_SUPPORTED );
3658 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003659}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660
Gilles Peskinec5487a82018-12-03 18:08:14 +01003661psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003662 psa_algorithm_t alg,
3663 const uint8_t *input,
3664 size_t input_length,
3665 const uint8_t *salt,
3666 size_t salt_length,
3667 uint8_t *output,
3668 size_t output_size,
3669 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003671 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003672 psa_status_t status;
3673
Darryl Green5cc689a2018-07-24 15:34:10 +01003674 (void) input;
3675 (void) input_length;
3676 (void) salt;
3677 (void) output;
3678 (void) output_size;
3679
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003680 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003682 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3683 return( PSA_ERROR_INVALID_ARGUMENT );
3684
Gilles Peskine28f8f302019-07-24 13:30:31 +02003685 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003686 if( status != PSA_SUCCESS )
3687 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003688 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689 return( PSA_ERROR_INVALID_ARGUMENT );
3690
3691#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003692 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003693 {
3694 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003696
Gilles Peskine630a18a2018-06-29 17:49:35 +02003697 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698 return( PSA_ERROR_INVALID_ARGUMENT );
3699
3700#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003701 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003702 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003703 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3704 mbedtls_ctr_drbg_random,
3705 &global_data.ctr_drbg,
3706 MBEDTLS_RSA_PRIVATE,
3707 output_length,
3708 input,
3709 output,
3710 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711 }
3712 else
3713#endif /* MBEDTLS_PKCS1_V15 */
3714#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003715 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003716 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003717 psa_rsa_oaep_set_padding_mode( alg, rsa );
3718 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3719 mbedtls_ctr_drbg_random,
3720 &global_data.ctr_drbg,
3721 MBEDTLS_RSA_PRIVATE,
3722 salt, salt_length,
3723 output_length,
3724 input,
3725 output,
3726 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727 }
3728 else
3729#endif /* MBEDTLS_PKCS1_V21 */
3730 {
3731 return( PSA_ERROR_INVALID_ARGUMENT );
3732 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003733
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003734 return( mbedtls_to_psa_error( ret ) );
3735 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003736 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003737#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003738 {
3739 return( PSA_ERROR_NOT_SUPPORTED );
3740 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003741}
Gilles Peskine20035e32018-02-03 22:44:14 +01003742
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003743
3744
mohammad1603503973b2018-03-12 15:59:30 +02003745/****************************************************************/
3746/* Symmetric cryptography */
3747/****************************************************************/
3748
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003749/* Initialize the cipher operation structure. Once this function has been
3750 * called, psa_cipher_abort can run and will do the right thing. */
3751static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3752 psa_algorithm_t alg )
3753{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003754 if( ! PSA_ALG_IS_CIPHER( alg ) )
3755 {
3756 memset( operation, 0, sizeof( *operation ) );
3757 return( PSA_ERROR_INVALID_ARGUMENT );
3758 }
3759
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003760 operation->alg = alg;
3761 operation->key_set = 0;
3762 operation->iv_set = 0;
3763 operation->iv_required = 1;
3764 operation->iv_size = 0;
3765 operation->block_size = 0;
3766 mbedtls_cipher_init( &operation->ctx.cipher );
3767 return( PSA_SUCCESS );
3768}
3769
Gilles Peskinee553c652018-06-04 16:22:46 +02003770static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003771 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003772 psa_algorithm_t alg,
3773 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003774{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003775 int ret = 0;
3776 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003777 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003778 size_t key_bits;
3779 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003780 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3781 PSA_KEY_USAGE_ENCRYPT :
3782 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003783
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003784 /* A context must be freshly initialized before it can be set up. */
3785 if( operation->alg != 0 )
3786 {
3787 return( PSA_ERROR_BAD_STATE );
3788 }
3789
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003790 status = psa_cipher_init( operation, alg );
3791 if( status != PSA_SUCCESS )
3792 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003793
Gilles Peskine28f8f302019-07-24 13:30:31 +02003794 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003795 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003796 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003797 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003798
Gilles Peskine8e338702019-07-30 20:06:31 +02003799 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003800 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003801 {
3802 status = PSA_ERROR_NOT_SUPPORTED;
3803 goto exit;
3804 }
mohammad1603503973b2018-03-12 15:59:30 +02003805
mohammad1603503973b2018-03-12 15:59:30 +02003806 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003807 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003808 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003809
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003810#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003811 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003812 {
3813 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003814 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003815 memcpy( keys, slot->data.raw.data, 16 );
3816 memcpy( keys + 16, slot->data.raw.data, 8 );
3817 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3818 keys,
3819 192, cipher_operation );
3820 }
3821 else
3822#endif
3823 {
3824 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3825 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003826 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003827 }
Moran Peker41deec42018-04-04 15:43:05 +03003828 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003829 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003830
mohammad16038481e742018-03-18 13:57:31 +02003831#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003832 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003833 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003834 case PSA_ALG_CBC_NO_PADDING:
3835 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3836 MBEDTLS_PADDING_NONE );
3837 break;
3838 case PSA_ALG_CBC_PKCS7:
3839 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3840 MBEDTLS_PADDING_PKCS7 );
3841 break;
3842 default:
3843 /* The algorithm doesn't involve padding. */
3844 ret = 0;
3845 break;
3846 }
3847 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003848 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003849#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3850
mohammad1603503973b2018-03-12 15:59:30 +02003851 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003852 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003853 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003854 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003855 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003856 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003857 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003858#if defined(MBEDTLS_CHACHA20_C)
3859 else
3860 if( alg == PSA_ALG_CHACHA20 )
3861 operation->iv_size = 12;
3862#endif
mohammad1603503973b2018-03-12 15:59:30 +02003863
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003864exit:
3865 if( status == 0 )
3866 status = mbedtls_to_psa_error( ret );
3867 if( status != 0 )
3868 psa_cipher_abort( operation );
3869 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003870}
3871
Gilles Peskinefe119512018-07-08 21:39:34 +02003872psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003873 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003874 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003875{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003876 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003877}
3878
Gilles Peskinefe119512018-07-08 21:39:34 +02003879psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003880 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003881 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003882{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003883 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003884}
3885
Gilles Peskinefe119512018-07-08 21:39:34 +02003886psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003887 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003888 size_t iv_size,
3889 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003890{
itayzafrir534bd7c2018-08-02 13:56:32 +03003891 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003893 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003894 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003895 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 }
Moran Peker41deec42018-04-04 15:43:05 +03003897 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003898 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003900 goto exit;
3901 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003902 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3903 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003904 if( ret != 0 )
3905 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003906 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003907 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003908 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003909
mohammad16038481e742018-03-18 13:57:31 +02003910 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003911 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003912
Moran Peker395db872018-05-31 14:07:14 +03003913exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003914 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003915 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003916 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003917}
3918
Gilles Peskinefe119512018-07-08 21:39:34 +02003919psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003920 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003921 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003922{
itayzafrir534bd7c2018-08-02 13:56:32 +03003923 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003925 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003926 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003927 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003928 }
Moran Pekera28258c2018-05-29 16:25:04 +03003929 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003930 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003931 status = PSA_ERROR_INVALID_ARGUMENT;
3932 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003933 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003934 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3935 status = mbedtls_to_psa_error( ret );
3936exit:
3937 if( status == PSA_SUCCESS )
3938 operation->iv_set = 1;
3939 else
mohammad1603503973b2018-03-12 15:59:30 +02003940 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003941 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003942}
3943
Gilles Peskinee553c652018-06-04 16:22:46 +02003944psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3945 const uint8_t *input,
3946 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003947 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003948 size_t output_size,
3949 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003950{
itayzafrir534bd7c2018-08-02 13:56:32 +03003951 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003953 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003954
3955 if( operation->alg == 0 )
3956 {
3957 return( PSA_ERROR_BAD_STATE );
3958 }
3959
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003960 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003961 {
3962 /* Take the unprocessed partial block left over from previous
3963 * update calls, if any, plus the input to this call. Remove
3964 * the last partial block, if any. You get the data that will be
3965 * output in this call. */
3966 expected_output_size =
3967 ( operation->ctx.cipher.unprocessed_len + input_length )
3968 / operation->block_size * operation->block_size;
3969 }
3970 else
3971 {
3972 expected_output_size = input_length;
3973 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003974
Gilles Peskine89d789c2018-06-04 17:17:16 +02003975 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003976 {
3977 status = PSA_ERROR_BUFFER_TOO_SMALL;
3978 goto exit;
3979 }
mohammad160382759612018-03-12 18:16:40 +02003980
mohammad1603503973b2018-03-12 15:59:30 +02003981 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003982 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003983 status = mbedtls_to_psa_error( ret );
3984exit:
3985 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003986 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003987 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003988}
3989
Gilles Peskinee553c652018-06-04 16:22:46 +02003990psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3991 uint8_t *output,
3992 size_t output_size,
3993 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003994{
David Saadab4ecc272019-02-14 13:48:10 +02003995 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003996 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003997 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003998
mohammad1603503973b2018-03-12 15:59:30 +02003999 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004000 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004001 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004002 }
4003 if( operation->iv_required && ! operation->iv_set )
4004 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004005 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004006 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004007
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004008 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004009 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4010 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004011 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004012 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004013 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004014 }
4015
Janos Follath315b51c2018-07-09 16:04:51 +01004016 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4017 temp_output_buffer,
4018 output_length );
4019 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004020 {
Janos Follath315b51c2018-07-09 16:04:51 +01004021 status = mbedtls_to_psa_error( cipher_ret );
4022 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004023 }
Janos Follath315b51c2018-07-09 16:04:51 +01004024
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004025 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004026 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004027 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004028 memcpy( output, temp_output_buffer, *output_length );
4029 else
4030 {
Janos Follath315b51c2018-07-09 16:04:51 +01004031 status = PSA_ERROR_BUFFER_TOO_SMALL;
4032 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004033 }
mohammad1603503973b2018-03-12 15:59:30 +02004034
Gilles Peskine3f108122018-12-07 18:14:53 +01004035 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004036 status = psa_cipher_abort( operation );
4037
4038 return( status );
4039
4040error:
4041
4042 *output_length = 0;
4043
Gilles Peskine3f108122018-12-07 18:14:53 +01004044 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004045 (void) psa_cipher_abort( operation );
4046
4047 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004048}
4049
Gilles Peskinee553c652018-06-04 16:22:46 +02004050psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4051{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004052 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004053 {
4054 /* The object has (apparently) been initialized but it is not
4055 * in use. It's ok to call abort on such an object, and there's
4056 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004057 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004058 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004059
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004060 /* Sanity check (shouldn't happen: operation->alg should
4061 * always have been initialized to a valid value). */
4062 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4063 return( PSA_ERROR_BAD_STATE );
4064
mohammad1603503973b2018-03-12 15:59:30 +02004065 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004066
Moran Peker41deec42018-04-04 15:43:05 +03004067 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004068 operation->key_set = 0;
4069 operation->iv_set = 0;
4070 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004071 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004072 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004073
Moran Peker395db872018-05-31 14:07:14 +03004074 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004075}
4076
Gilles Peskinea0655c32018-04-30 17:06:50 +02004077
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004078
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004079
mohammad16035955c982018-04-26 00:53:03 +03004080/****************************************************************/
4081/* AEAD */
4082/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004083
Gilles Peskineedf9a652018-08-17 18:11:56 +02004084typedef struct
4085{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004086 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 const mbedtls_cipher_info_t *cipher_info;
4088 union
4089 {
4090#if defined(MBEDTLS_CCM_C)
4091 mbedtls_ccm_context ccm;
4092#endif /* MBEDTLS_CCM_C */
4093#if defined(MBEDTLS_GCM_C)
4094 mbedtls_gcm_context gcm;
4095#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004096#if defined(MBEDTLS_CHACHAPOLY_C)
4097 mbedtls_chachapoly_context chachapoly;
4098#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004099 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004100 psa_algorithm_t core_alg;
4101 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004102 uint8_t tag_length;
4103} aead_operation_t;
4104
Gilles Peskine30a9e412019-01-14 18:36:12 +01004105static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004106{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004107 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004108 {
4109#if defined(MBEDTLS_CCM_C)
4110 case PSA_ALG_CCM:
4111 mbedtls_ccm_free( &operation->ctx.ccm );
4112 break;
4113#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004114#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115 case PSA_ALG_GCM:
4116 mbedtls_gcm_free( &operation->ctx.gcm );
4117 break;
4118#endif /* MBEDTLS_GCM_C */
4119 }
4120}
4121
4122static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004123 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004124 psa_key_usage_t usage,
4125 psa_algorithm_t alg )
4126{
4127 psa_status_t status;
4128 size_t key_bits;
4129 mbedtls_cipher_id_t cipher_id;
4130
Gilles Peskine28f8f302019-07-24 13:30:31 +02004131 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004132 if( status != PSA_SUCCESS )
4133 return( status );
4134
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004135 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004136
4137 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004138 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004139 &cipher_id );
4140 if( operation->cipher_info == NULL )
4141 return( PSA_ERROR_NOT_SUPPORTED );
4142
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004143 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004144 {
4145#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004146 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4147 operation->core_alg = PSA_ALG_CCM;
4148 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004149 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4150 * The call to mbedtls_ccm_encrypt_and_tag or
4151 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004152 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004153 return( PSA_ERROR_INVALID_ARGUMENT );
4154 mbedtls_ccm_init( &operation->ctx.ccm );
4155 status = mbedtls_to_psa_error(
4156 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4157 operation->slot->data.raw.data,
4158 (unsigned int) key_bits ) );
4159 if( status != 0 )
4160 goto cleanup;
4161 break;
4162#endif /* MBEDTLS_CCM_C */
4163
4164#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004165 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4166 operation->core_alg = PSA_ALG_GCM;
4167 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004168 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4169 * The call to mbedtls_gcm_crypt_and_tag or
4170 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004171 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004172 return( PSA_ERROR_INVALID_ARGUMENT );
4173 mbedtls_gcm_init( &operation->ctx.gcm );
4174 status = mbedtls_to_psa_error(
4175 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4176 operation->slot->data.raw.data,
4177 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004178 if( status != 0 )
4179 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004180 break;
4181#endif /* MBEDTLS_GCM_C */
4182
Gilles Peskine26869f22019-05-06 15:25:00 +02004183#if defined(MBEDTLS_CHACHAPOLY_C)
4184 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4185 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4186 operation->full_tag_length = 16;
4187 /* We only support the default tag length. */
4188 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4189 return( PSA_ERROR_NOT_SUPPORTED );
4190 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4191 status = mbedtls_to_psa_error(
4192 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4193 operation->slot->data.raw.data ) );
4194 if( status != 0 )
4195 goto cleanup;
4196 break;
4197#endif /* MBEDTLS_CHACHAPOLY_C */
4198
Gilles Peskineedf9a652018-08-17 18:11:56 +02004199 default:
4200 return( PSA_ERROR_NOT_SUPPORTED );
4201 }
4202
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004203 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4204 {
4205 status = PSA_ERROR_INVALID_ARGUMENT;
4206 goto cleanup;
4207 }
4208 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004209
Gilles Peskineedf9a652018-08-17 18:11:56 +02004210 return( PSA_SUCCESS );
4211
4212cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004213 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004214 return( status );
4215}
4216
Gilles Peskinec5487a82018-12-03 18:08:14 +01004217psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004218 psa_algorithm_t alg,
4219 const uint8_t *nonce,
4220 size_t nonce_length,
4221 const uint8_t *additional_data,
4222 size_t additional_data_length,
4223 const uint8_t *plaintext,
4224 size_t plaintext_length,
4225 uint8_t *ciphertext,
4226 size_t ciphertext_size,
4227 size_t *ciphertext_length )
4228{
mohammad16035955c982018-04-26 00:53:03 +03004229 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004230 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004231 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004232
mohammad1603f08a5502018-06-03 15:05:47 +03004233 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004234
Gilles Peskinec5487a82018-12-03 18:08:14 +01004235 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004236 if( status != PSA_SUCCESS )
4237 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004238
Gilles Peskineedf9a652018-08-17 18:11:56 +02004239 /* For all currently supported modes, the tag is at the end of the
4240 * ciphertext. */
4241 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4242 {
4243 status = PSA_ERROR_BUFFER_TOO_SMALL;
4244 goto exit;
4245 }
4246 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004247
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004248#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004249 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004250 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004251 status = mbedtls_to_psa_error(
4252 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4253 MBEDTLS_GCM_ENCRYPT,
4254 plaintext_length,
4255 nonce, nonce_length,
4256 additional_data, additional_data_length,
4257 plaintext, ciphertext,
4258 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004259 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004260 else
4261#endif /* MBEDTLS_GCM_C */
4262#if defined(MBEDTLS_CCM_C)
4263 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004264 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004265 status = mbedtls_to_psa_error(
4266 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4267 plaintext_length,
4268 nonce, nonce_length,
4269 additional_data,
4270 additional_data_length,
4271 plaintext, ciphertext,
4272 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004273 }
mohammad16035c8845f2018-05-09 05:40:09 -07004274 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004275#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004276#if defined(MBEDTLS_CHACHAPOLY_C)
4277 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4278 {
4279 if( nonce_length != 12 || operation.tag_length != 16 )
4280 {
4281 status = PSA_ERROR_NOT_SUPPORTED;
4282 goto exit;
4283 }
4284 status = mbedtls_to_psa_error(
4285 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4286 plaintext_length,
4287 nonce,
4288 additional_data,
4289 additional_data_length,
4290 plaintext,
4291 ciphertext,
4292 tag ) );
4293 }
4294 else
4295#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004296 {
mohammad1603554faad2018-06-03 15:07:38 +03004297 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004298 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004299
Gilles Peskineedf9a652018-08-17 18:11:56 +02004300 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4301 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004302
Gilles Peskineedf9a652018-08-17 18:11:56 +02004303exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004304 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004305 if( status == PSA_SUCCESS )
4306 *ciphertext_length = plaintext_length + operation.tag_length;
4307 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004308}
4309
Gilles Peskineee652a32018-06-01 19:23:52 +02004310/* Locate the tag in a ciphertext buffer containing the encrypted data
4311 * followed by the tag. Return the length of the part preceding the tag in
4312 * *plaintext_length. This is the size of the plaintext in modes where
4313 * the encrypted data has the same size as the plaintext, such as
4314 * CCM and GCM. */
4315static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4316 const uint8_t *ciphertext,
4317 size_t ciphertext_length,
4318 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004319 const uint8_t **p_tag )
4320{
4321 size_t payload_length;
4322 if( tag_length > ciphertext_length )
4323 return( PSA_ERROR_INVALID_ARGUMENT );
4324 payload_length = ciphertext_length - tag_length;
4325 if( payload_length > plaintext_size )
4326 return( PSA_ERROR_BUFFER_TOO_SMALL );
4327 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004328 return( PSA_SUCCESS );
4329}
4330
Gilles Peskinec5487a82018-12-03 18:08:14 +01004331psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004332 psa_algorithm_t alg,
4333 const uint8_t *nonce,
4334 size_t nonce_length,
4335 const uint8_t *additional_data,
4336 size_t additional_data_length,
4337 const uint8_t *ciphertext,
4338 size_t ciphertext_length,
4339 uint8_t *plaintext,
4340 size_t plaintext_size,
4341 size_t *plaintext_length )
4342{
mohammad16035955c982018-04-26 00:53:03 +03004343 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004344 aead_operation_t operation;
4345 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004346
Gilles Peskineee652a32018-06-01 19:23:52 +02004347 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004348
Gilles Peskinec5487a82018-12-03 18:08:14 +01004349 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004350 if( status != PSA_SUCCESS )
4351 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004352
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004353 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4354 ciphertext, ciphertext_length,
4355 plaintext_size, &tag );
4356 if( status != PSA_SUCCESS )
4357 goto exit;
4358
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004359#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004360 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004361 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004362 status = mbedtls_to_psa_error(
4363 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4364 ciphertext_length - operation.tag_length,
4365 nonce, nonce_length,
4366 additional_data,
4367 additional_data_length,
4368 tag, operation.tag_length,
4369 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004370 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004371 else
4372#endif /* MBEDTLS_GCM_C */
4373#if defined(MBEDTLS_CCM_C)
4374 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004375 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004376 status = mbedtls_to_psa_error(
4377 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4378 ciphertext_length - operation.tag_length,
4379 nonce, nonce_length,
4380 additional_data,
4381 additional_data_length,
4382 ciphertext, plaintext,
4383 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004384 }
mohammad160339574652018-06-01 04:39:53 -07004385 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004386#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004387#if defined(MBEDTLS_CHACHAPOLY_C)
4388 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4389 {
4390 if( nonce_length != 12 || operation.tag_length != 16 )
4391 {
4392 status = PSA_ERROR_NOT_SUPPORTED;
4393 goto exit;
4394 }
4395 status = mbedtls_to_psa_error(
4396 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4397 ciphertext_length - operation.tag_length,
4398 nonce,
4399 additional_data,
4400 additional_data_length,
4401 tag,
4402 ciphertext,
4403 plaintext ) );
4404 }
4405 else
4406#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004407 {
mohammad1603554faad2018-06-03 15:07:38 +03004408 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004409 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004410
Gilles Peskineedf9a652018-08-17 18:11:56 +02004411 if( status != PSA_SUCCESS && plaintext_size != 0 )
4412 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004413
Gilles Peskineedf9a652018-08-17 18:11:56 +02004414exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004415 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004416 if( status == PSA_SUCCESS )
4417 *plaintext_length = ciphertext_length - operation.tag_length;
4418 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004419}
4420
Gilles Peskinea0655c32018-04-30 17:06:50 +02004421
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004422
Gilles Peskine20035e32018-02-03 22:44:14 +01004423/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004424/* Generators */
4425/****************************************************************/
4426
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004427#define HKDF_STATE_INIT 0 /* no input yet */
4428#define HKDF_STATE_STARTED 1 /* got salt */
4429#define HKDF_STATE_KEYED 2 /* got key */
4430#define HKDF_STATE_OUTPUT 3 /* output started */
4431
Gilles Peskinecbe66502019-05-16 16:59:18 +02004432static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4436 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004439}
4440
4441
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004443{
4444 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004446 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004447 {
4448 /* The object has (apparently) been initialized but it is not
4449 * in use. It's ok to call abort on such an object, and there's
4450 * nothing to do. */
4451 }
4452 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004453#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004454 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004455 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004456 mbedtls_free( operation->ctx.hkdf.info );
4457 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004458 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004459 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004460 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004461 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004462 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004463 if( operation->ctx.tls12_prf.seed != NULL )
4464 {
4465 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4466 operation->ctx.tls12_prf.seed_length );
4467 mbedtls_free( operation->ctx.tls12_prf.seed );
4468 }
4469
4470 if( operation->ctx.tls12_prf.label != NULL )
4471 {
4472 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4473 operation->ctx.tls12_prf.label_length );
4474 mbedtls_free( operation->ctx.tls12_prf.label );
4475 }
4476
4477 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4478
4479 /* We leave the fields Ai and output_block to be erased safely by the
4480 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004481 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004482 else
4483#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004484 {
4485 status = PSA_ERROR_BAD_STATE;
4486 }
Janos Follath083036a2019-06-11 10:22:26 +01004487 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004488 return( status );
4489}
4490
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004492 size_t *capacity)
4493{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004495 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004497 return PSA_ERROR_BAD_STATE;
4498 }
4499
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004500 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004501 return( PSA_SUCCESS );
4502}
4503
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004505 size_t capacity )
4506{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004508 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004510 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004512 return( PSA_SUCCESS );
4513}
4514
Gilles Peskinebef7f142018-07-12 17:22:21 +02004515#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004516/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004517 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004518static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004519 psa_algorithm_t hash_alg,
4520 uint8_t *output,
4521 size_t output_length )
4522{
4523 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4524 psa_status_t status;
4525
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004526 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4527 return( PSA_ERROR_BAD_STATE );
4528 hkdf->state = HKDF_STATE_OUTPUT;
4529
Gilles Peskinebef7f142018-07-12 17:22:21 +02004530 while( output_length != 0 )
4531 {
4532 /* Copy what remains of the current block */
4533 uint8_t n = hash_length - hkdf->offset_in_block;
4534 if( n > output_length )
4535 n = (uint8_t) output_length;
4536 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4537 output += n;
4538 output_length -= n;
4539 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004540 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004541 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004542 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004543 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004545 * object was corrupted or if this function is called directly
4546 * inside the library. */
4547 if( hkdf->block_number == 0xff )
4548 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004549
4550 /* We need a new block */
4551 ++hkdf->block_number;
4552 hkdf->offset_in_block = 0;
4553 status = psa_hmac_setup_internal( &hkdf->hmac,
4554 hkdf->prk, hash_length,
4555 hash_alg );
4556 if( status != PSA_SUCCESS )
4557 return( status );
4558 if( hkdf->block_number != 1 )
4559 {
4560 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4561 hkdf->output_block,
4562 hash_length );
4563 if( status != PSA_SUCCESS )
4564 return( status );
4565 }
4566 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4567 hkdf->info,
4568 hkdf->info_length );
4569 if( status != PSA_SUCCESS )
4570 return( status );
4571 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4572 &hkdf->block_number, 1 );
4573 if( status != PSA_SUCCESS )
4574 return( status );
4575 status = psa_hmac_finish_internal( &hkdf->hmac,
4576 hkdf->output_block,
4577 sizeof( hkdf->output_block ) );
4578 if( status != PSA_SUCCESS )
4579 return( status );
4580 }
4581
4582 return( PSA_SUCCESS );
4583}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004584
Janos Follath7742fee2019-06-17 12:58:10 +01004585static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4586 psa_tls12_prf_key_derivation_t *tls12_prf,
4587 psa_algorithm_t alg )
4588{
4589 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4590 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004591 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4592 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004593
Janos Follath7742fee2019-06-17 12:58:10 +01004594 /* We can't be wanting more output after block 0xff, otherwise
4595 * the capacity check in psa_key_derivation_output_bytes() would have
4596 * prevented this call. It could happen only if the operation
4597 * object was corrupted or if this function is called directly
4598 * inside the library. */
4599 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004600 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004601
4602 /* We need a new block */
4603 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004604 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004605
4606 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4607 *
4608 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4609 *
4610 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4611 * HMAC_hash(secret, A(2) + seed) +
4612 * HMAC_hash(secret, A(3) + seed) + ...
4613 *
4614 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004615 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004616 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004617 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004618 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004619 * is currently extracted as `output_block` and where i is
4620 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004621 */
4622
Janos Follathea29bfb2019-06-19 12:21:20 +01004623 /* Save the hash context before using it, to preserve the hash state with
4624 * only the inner padding in it. We need this, because inner padding depends
4625 * on the key (secret in the RFC's terminology). */
4626 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4627 if( status != PSA_SUCCESS )
4628 goto cleanup;
4629
4630 /* Calculate A(i) where i = tls12_prf->block_number. */
4631 if( tls12_prf->block_number == 1 )
4632 {
4633 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4634 * the variable seed and in this instance means it in the context of the
4635 * P_hash function, where seed = label + seed.) */
4636 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4637 tls12_prf->label, tls12_prf->label_length );
4638 if( status != PSA_SUCCESS )
4639 goto cleanup;
4640 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4641 tls12_prf->seed, tls12_prf->seed_length );
4642 if( status != PSA_SUCCESS )
4643 goto cleanup;
4644 }
4645 else
4646 {
4647 /* A(i) = HMAC_hash(secret, A(i-1)) */
4648 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4649 tls12_prf->Ai, hash_length );
4650 if( status != PSA_SUCCESS )
4651 goto cleanup;
4652 }
4653
4654 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4655 tls12_prf->Ai, hash_length );
4656 if( status != PSA_SUCCESS )
4657 goto cleanup;
4658 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4659 if( status != PSA_SUCCESS )
4660 goto cleanup;
4661
4662 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4663 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4664 tls12_prf->Ai, hash_length );
4665 if( status != PSA_SUCCESS )
4666 goto cleanup;
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 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4676 tls12_prf->output_block, hash_length );
4677 if( status != PSA_SUCCESS )
4678 goto cleanup;
4679 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4680 if( status != PSA_SUCCESS )
4681 goto cleanup;
4682
Janos Follath7742fee2019-06-17 12:58:10 +01004683
4684cleanup:
4685
Janos Follathea29bfb2019-06-19 12:21:20 +01004686 cleanup_status = psa_hash_abort( &backup );
4687 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4688 status = cleanup_status;
4689
4690 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004691}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004692
Janos Follath844eb0e2019-06-19 12:10:49 +01004693static psa_status_t psa_key_derivation_tls12_prf_read(
4694 psa_tls12_prf_key_derivation_t *tls12_prf,
4695 psa_algorithm_t alg,
4696 uint8_t *output,
4697 size_t output_length )
4698{
4699 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4700 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4701 psa_status_t status;
4702 uint8_t offset, length;
4703
4704 while( output_length != 0 )
4705 {
4706 /* Check if we have fully processed the current block. */
4707 if( tls12_prf->left_in_block == 0 )
4708 {
4709 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4710 alg );
4711 if( status != PSA_SUCCESS )
4712 return( status );
4713
4714 continue;
4715 }
4716
4717 if( tls12_prf->left_in_block > output_length )
4718 length = (uint8_t) output_length;
4719 else
4720 length = tls12_prf->left_in_block;
4721
4722 offset = hash_length - tls12_prf->left_in_block;
4723 memcpy( output, tls12_prf->output_block + offset, length );
4724 output += length;
4725 output_length -= length;
4726 tls12_prf->left_in_block -= length;
4727 }
4728
4729 return( PSA_SUCCESS );
4730}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004731#endif /* MBEDTLS_MD_C */
4732
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004733psa_status_t psa_key_derivation_output_bytes(
4734 psa_key_derivation_operation_t *operation,
4735 uint8_t *output,
4736 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737{
4738 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004742 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004744 return PSA_ERROR_BAD_STATE;
4745 }
4746
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004752 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753 goto exit;
4754 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004758 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004759 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4760 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004762 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004763 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004766
Gilles Peskinebef7f142018-07-12 17:22:21 +02004767#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004768 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004769 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004770 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004772 output, output_length );
4773 }
Janos Follathadbec812019-06-14 11:05:39 +01004774 else
Janos Follathadbec812019-06-14 11:05:39 +01004775 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004776 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004777 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004779 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004780 output_length );
4781 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004782 else
4783#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004784 {
4785 return( PSA_ERROR_BAD_STATE );
4786 }
4787
4788exit:
4789 if( status != PSA_SUCCESS )
4790 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004791 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 * This allows us to differentiate between exhausted operations and
4793 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4794 * operations. */
4795 psa_algorithm_t alg = operation->alg;
4796 psa_key_derivation_abort( operation );
4797 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004798 memset( output, '!', output_length );
4799 }
4800 return( status );
4801}
4802
Gilles Peskine08542d82018-07-19 17:05:42 +02004803#if defined(MBEDTLS_DES_C)
4804static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4805{
4806 if( data_size >= 8 )
4807 mbedtls_des_key_set_parity( data );
4808 if( data_size >= 16 )
4809 mbedtls_des_key_set_parity( data + 8 );
4810 if( data_size >= 24 )
4811 mbedtls_des_key_set_parity( data + 16 );
4812}
4813#endif /* MBEDTLS_DES_C */
4814
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004815static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004816 psa_key_slot_t *slot,
4817 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004819{
4820 uint8_t *data = NULL;
4821 size_t bytes = PSA_BITS_TO_BYTES( bits );
4822 psa_status_t status;
4823
Gilles Peskine8e338702019-07-30 20:06:31 +02004824 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004825 return( PSA_ERROR_INVALID_ARGUMENT );
4826 if( bits % 8 != 0 )
4827 return( PSA_ERROR_INVALID_ARGUMENT );
4828 data = mbedtls_calloc( 1, bytes );
4829 if( data == NULL )
4830 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004833 if( status != PSA_SUCCESS )
4834 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004835#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004836 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004837 psa_des_set_key_parity( data, bytes );
4838#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004839 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004840
4841exit:
4842 mbedtls_free( data );
4843 return( status );
4844}
4845
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004846psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004848 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004849{
4850 psa_status_t status;
4851 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004852 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004853
4854 /* Reject any attempt to create a zero-length key so that we don't
4855 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4856 if( psa_get_key_bits( attributes ) == 0 )
4857 return( PSA_ERROR_INVALID_ARGUMENT );
4858
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004859 if( ! operation->can_output_key )
4860 return( PSA_ERROR_NOT_PERMITTED );
4861
Gilles Peskinedf179142019-07-15 22:02:14 +02004862 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4863 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004864#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4865 if( driver != NULL )
4866 {
4867 /* Deriving a key in a secure element is not implemented yet. */
4868 status = PSA_ERROR_NOT_SUPPORTED;
4869 }
4870#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004871 if( status == PSA_SUCCESS )
4872 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004873 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004874 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004876 }
4877 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004878 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004879 if( status != PSA_SUCCESS )
4880 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004881 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004882 *handle = 0;
4883 }
4884 return( status );
4885}
4886
Gilles Peskineeab56e42018-07-12 17:12:33 +02004887
4888
4889/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004890/* Key derivation */
4891/****************************************************************/
4892
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004895 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896{
Janos Follath5fe19732019-06-20 15:09:30 +01004897 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4898 * initialisers for this union leave some bytes unspecified.) */
4899 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4900
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4904 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4905 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004906 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4909 if( hash_size == 0 )
4910 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4912 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004913 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914 {
4915 return( PSA_ERROR_NOT_SUPPORTED );
4916 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004918 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004919 }
4920#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004922 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004923}
4924
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 psa_algorithm_t alg )
4927{
4928 psa_status_t status;
4929
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 return( PSA_ERROR_BAD_STATE );
4932
Gilles Peskine6843c292019-01-18 16:44:49 +01004933 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4934 return( PSA_ERROR_INVALID_ARGUMENT );
4935 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 {
4937 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004939 }
4940 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4941 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004943 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944 else
4945 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004946
4947 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004950}
4951
4952#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004953static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004954 psa_algorithm_t hash_alg,
4955 psa_key_derivation_step_t step,
4956 const uint8_t *data,
4957 size_t data_length )
4958{
4959 psa_status_t status;
4960 switch( step )
4961 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004962 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004963 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004964 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004965 status = psa_hmac_setup_internal( &hkdf->hmac,
4966 data, data_length,
4967 hash_alg );
4968 if( status != PSA_SUCCESS )
4969 return( status );
4970 hkdf->state = HKDF_STATE_STARTED;
4971 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004972 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973 /* If no salt was provided, use an empty salt. */
4974 if( hkdf->state == HKDF_STATE_INIT )
4975 {
4976 status = psa_hmac_setup_internal( &hkdf->hmac,
4977 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004978 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004979 if( status != PSA_SUCCESS )
4980 return( status );
4981 hkdf->state = HKDF_STATE_STARTED;
4982 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004983 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004984 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004985 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4986 data, data_length );
4987 if( status != PSA_SUCCESS )
4988 return( status );
4989 status = psa_hmac_finish_internal( &hkdf->hmac,
4990 hkdf->prk,
4991 sizeof( hkdf->prk ) );
4992 if( status != PSA_SUCCESS )
4993 return( status );
4994 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4995 hkdf->block_number = 0;
4996 hkdf->state = HKDF_STATE_KEYED;
4997 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004998 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004999 if( hkdf->state == HKDF_STATE_OUTPUT )
5000 return( PSA_ERROR_BAD_STATE );
5001 if( hkdf->info_set )
5002 return( PSA_ERROR_BAD_STATE );
5003 hkdf->info_length = data_length;
5004 if( data_length != 0 )
5005 {
5006 hkdf->info = mbedtls_calloc( 1, data_length );
5007 if( hkdf->info == NULL )
5008 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5009 memcpy( hkdf->info, data, data_length );
5010 }
5011 hkdf->info_set = 1;
5012 return( PSA_SUCCESS );
5013 default:
5014 return( PSA_ERROR_INVALID_ARGUMENT );
5015 }
5016}
Janos Follathb03233e2019-06-11 15:30:30 +01005017
Janos Follathf08e2652019-06-13 09:05:41 +01005018static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5019 const uint8_t *data,
5020 size_t data_length )
5021{
5022 if( prf->state != TLS12_PRF_STATE_INIT )
5023 return( PSA_ERROR_BAD_STATE );
5024
Janos Follathd6dce9f2019-07-04 09:11:38 +01005025 if( data_length != 0 )
5026 {
5027 prf->seed = mbedtls_calloc( 1, data_length );
5028 if( prf->seed == NULL )
5029 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005030
Janos Follathd6dce9f2019-07-04 09:11:38 +01005031 memcpy( prf->seed, data, data_length );
5032 prf->seed_length = data_length;
5033 }
Janos Follathf08e2652019-06-13 09:05:41 +01005034
5035 prf->state = TLS12_PRF_STATE_SEED_SET;
5036
5037 return( PSA_SUCCESS );
5038}
5039
Janos Follath81550542019-06-13 14:26:34 +01005040static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5041 psa_algorithm_t hash_alg,
5042 const uint8_t *data,
5043 size_t data_length )
5044{
5045 psa_status_t status;
5046 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5047 return( PSA_ERROR_BAD_STATE );
5048
5049 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5050 if( status != PSA_SUCCESS )
5051 return( status );
5052
5053 prf->state = TLS12_PRF_STATE_KEY_SET;
5054
5055 return( PSA_SUCCESS );
5056}
5057
Janos Follath6660f0e2019-06-17 08:44:03 +01005058static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5059 psa_tls12_prf_key_derivation_t *prf,
5060 psa_algorithm_t hash_alg,
5061 const uint8_t *data,
5062 size_t data_length )
5063{
5064 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005065 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5066 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005067
5068 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5069 return( PSA_ERROR_INVALID_ARGUMENT );
5070
5071 /* Quoting RFC 4279, Section 2:
5072 *
5073 * The premaster secret is formed as follows: if the PSK is N octets
5074 * long, concatenate a uint16 with the value N, N zero octets, a second
5075 * uint16 with the value N, and the PSK itself.
5076 */
5077
Janos Follath40e13932019-06-26 13:22:29 +01005078 *cur++ = ( data_length >> 8 ) & 0xff;
5079 *cur++ = ( data_length >> 0 ) & 0xff;
5080 memset( cur, 0, data_length );
5081 cur += data_length;
5082 *cur++ = pms[0];
5083 *cur++ = pms[1];
5084 memcpy( cur, data, data_length );
5085 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005086
Janos Follath40e13932019-06-26 13:22:29 +01005087 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005088
5089 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5090 return( status );
5091}
5092
Janos Follath63028dd2019-06-13 09:15:47 +01005093static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5094 const uint8_t *data,
5095 size_t data_length )
5096{
5097 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5098 return( PSA_ERROR_BAD_STATE );
5099
Janos Follathd6dce9f2019-07-04 09:11:38 +01005100 if( data_length != 0 )
5101 {
5102 prf->label = mbedtls_calloc( 1, data_length );
5103 if( prf->label == NULL )
5104 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005105
Janos Follathd6dce9f2019-07-04 09:11:38 +01005106 memcpy( prf->label, data, data_length );
5107 prf->label_length = data_length;
5108 }
Janos Follath63028dd2019-06-13 09:15:47 +01005109
5110 prf->state = TLS12_PRF_STATE_LABEL_SET;
5111
5112 return( PSA_SUCCESS );
5113}
5114
Janos Follathb03233e2019-06-11 15:30:30 +01005115static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5116 psa_algorithm_t hash_alg,
5117 psa_key_derivation_step_t step,
5118 const uint8_t *data,
5119 size_t data_length )
5120{
Janos Follathb03233e2019-06-11 15:30:30 +01005121 switch( step )
5122 {
Janos Follathf08e2652019-06-13 09:05:41 +01005123 case PSA_KEY_DERIVATION_INPUT_SEED:
5124 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005125 case PSA_KEY_DERIVATION_INPUT_SECRET:
5126 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005127 case PSA_KEY_DERIVATION_INPUT_LABEL:
5128 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005129 default:
5130 return( PSA_ERROR_INVALID_ARGUMENT );
5131 }
5132}
Janos Follath6660f0e2019-06-17 08:44:03 +01005133
5134static psa_status_t psa_tls12_prf_psk_to_ms_input(
5135 psa_tls12_prf_key_derivation_t *prf,
5136 psa_algorithm_t hash_alg,
5137 psa_key_derivation_step_t step,
5138 const uint8_t *data,
5139 size_t data_length )
5140{
5141 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005142 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005143 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5144 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005145 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005146
5147 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5148}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005149#endif /* MBEDTLS_MD_C */
5150
Gilles Peskineb8965192019-09-24 16:21:10 +02005151/** Check whether the given key type is acceptable for the given
5152 * input step of a key derivation.
5153 *
5154 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5155 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5156 * Both secret and non-secret inputs can alternatively have the type
5157 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5158 * that the input was passed as a buffer rather than via a key object.
5159 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005160static int psa_key_derivation_check_input_type(
5161 psa_key_derivation_step_t step,
5162 psa_key_type_t key_type )
5163{
5164 switch( step )
5165 {
5166 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005167 if( key_type == PSA_KEY_TYPE_DERIVE )
5168 return( PSA_SUCCESS );
5169 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005170 return( PSA_SUCCESS );
5171 break;
5172 case PSA_KEY_DERIVATION_INPUT_LABEL:
5173 case PSA_KEY_DERIVATION_INPUT_SALT:
5174 case PSA_KEY_DERIVATION_INPUT_INFO:
5175 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005176 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5177 return( PSA_SUCCESS );
5178 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005179 return( PSA_SUCCESS );
5180 break;
5181 }
5182 return( PSA_ERROR_INVALID_ARGUMENT );
5183}
5184
Janos Follathb80a94e2019-06-12 15:54:46 +01005185static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005187 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005188 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005189 const uint8_t *data,
5190 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005191{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005192 psa_status_t status;
5193 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5194
5195 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005196 if( status != PSA_SUCCESS )
5197 goto exit;
5198
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005200 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005203 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204 step, data, data_length );
5205 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005206 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 {
Janos Follathb03233e2019-06-11 15:30:30 +01005208 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5209 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5210 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005211 }
5212 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5213 {
5214 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5215 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5216 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217 }
5218 else
5219#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005220 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222 return( PSA_ERROR_BAD_STATE );
5223 }
5224
Gilles Peskine224b0d62019-09-23 18:13:17 +02005225exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005226 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005228 return( status );
5229}
5230
Janos Follath51f4a0f2019-06-14 11:35:55 +01005231psa_status_t psa_key_derivation_input_bytes(
5232 psa_key_derivation_operation_t *operation,
5233 psa_key_derivation_step_t step,
5234 const uint8_t *data,
5235 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005236{
Gilles Peskineb8965192019-09-24 16:21:10 +02005237 return( psa_key_derivation_input_internal( operation, step,
5238 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005239 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005240}
5241
Janos Follath51f4a0f2019-06-14 11:35:55 +01005242psa_status_t psa_key_derivation_input_key(
5243 psa_key_derivation_operation_t *operation,
5244 psa_key_derivation_step_t step,
5245 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005246{
5247 psa_key_slot_t *slot;
5248 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005249
Gilles Peskine28f8f302019-07-24 13:30:31 +02005250 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005251 PSA_KEY_USAGE_DERIVE,
5252 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005254 {
5255 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005257 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005258
5259 /* Passing a key object as a SECRET input unlocks the permission
5260 * to output to a key object. */
5261 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5262 operation->can_output_key = 1;
5263
Janos Follathb80a94e2019-06-12 15:54:46 +01005264 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005265 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005266 slot->data.raw.data,
5267 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005268}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005269
5270
5271
5272/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273/* Key agreement */
5274/****************************************************************/
5275
Gilles Peskinea05219c2018-11-16 16:02:56 +01005276#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005277static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5278 size_t peer_key_length,
5279 const mbedtls_ecp_keypair *our_key,
5280 uint8_t *shared_secret,
5281 size_t shared_secret_size,
5282 size_t *shared_secret_length )
5283{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005284 mbedtls_ecp_keypair *their_key = NULL;
5285 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005286 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005287 size_t bits = 0;
5288 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005289 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005291 status = psa_import_ec_public_key( curve,
5292 peer_key, peer_key_length,
5293 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005294 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005295 goto exit;
5296
Jaeden Amero97271b32019-01-10 19:38:51 +00005297 status = mbedtls_to_psa_error(
5298 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5299 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005300 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005301 status = mbedtls_to_psa_error(
5302 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5303 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005304 goto exit;
5305
Jaeden Amero97271b32019-01-10 19:38:51 +00005306 status = mbedtls_to_psa_error(
5307 mbedtls_ecdh_calc_secret( &ecdh,
5308 shared_secret_length,
5309 shared_secret, shared_secret_size,
5310 mbedtls_ctr_drbg_random,
5311 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005312 if( status != PSA_SUCCESS )
5313 goto exit;
5314 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5315 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005316
5317exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005318 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005319 mbedtls_ecp_keypair_free( their_key );
5320 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005321 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005322}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005323#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005324
Gilles Peskine01d718c2018-09-18 12:01:02 +02005325#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5326
Gilles Peskine0216fe12019-04-11 21:23:21 +02005327static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5328 psa_key_slot_t *private_key,
5329 const uint8_t *peer_key,
5330 size_t peer_key_length,
5331 uint8_t *shared_secret,
5332 size_t shared_secret_size,
5333 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005334{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005335 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005336 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005337#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005338 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005339 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005340 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005341 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5342 private_key->data.ecp,
5343 shared_secret, shared_secret_size,
5344 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005345#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005346 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005347 (void) private_key;
5348 (void) peer_key;
5349 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005350 (void) shared_secret;
5351 (void) shared_secret_size;
5352 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005353 return( PSA_ERROR_NOT_SUPPORTED );
5354 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005355}
5356
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005357/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358 * to potentially free embedded data structures and wipe confidential data.
5359 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005360static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005361 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005362 psa_key_slot_t *private_key,
5363 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005364 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005365{
5366 psa_status_t status;
5367 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5368 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005370
5371 /* Step 1: run the secret agreement algorithm to generate the shared
5372 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005373 status = psa_key_agreement_raw_internal( ka_alg,
5374 private_key,
5375 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005376 shared_secret,
5377 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005378 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005379 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005380 goto exit;
5381
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005382 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005383 * the shared secret. A shared secret is permitted wherever a key
5384 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005385 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005386 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005387 shared_secret,
5388 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005389
Gilles Peskine12313cd2018-06-20 00:20:32 +02005390exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005391 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005392 return( status );
5393}
5394
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005395psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005396 psa_key_derivation_step_t step,
5397 psa_key_handle_t private_key,
5398 const uint8_t *peer_key,
5399 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005400{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005401 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005402 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005404 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005405 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005406 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005407 if( status != PSA_SUCCESS )
5408 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005410 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005411 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005412 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005413 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005414 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005415}
5416
Gilles Peskinebe697d82019-05-16 18:00:41 +02005417psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5418 psa_key_handle_t private_key,
5419 const uint8_t *peer_key,
5420 size_t peer_key_length,
5421 uint8_t *output,
5422 size_t output_size,
5423 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005424{
5425 psa_key_slot_t *slot;
5426 psa_status_t status;
5427
5428 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5429 {
5430 status = PSA_ERROR_INVALID_ARGUMENT;
5431 goto exit;
5432 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005433 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005434 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005435 if( status != PSA_SUCCESS )
5436 goto exit;
5437
5438 status = psa_key_agreement_raw_internal( alg, slot,
5439 peer_key, peer_key_length,
5440 output, output_size,
5441 output_length );
5442
5443exit:
5444 if( status != PSA_SUCCESS )
5445 {
5446 /* If an error happens and is not handled properly, the output
5447 * may be used as a key to protect sensitive data. Arrange for such
5448 * a key to be random, which is likely to result in decryption or
5449 * verification errors. This is better than filling the buffer with
5450 * some constant data such as zeros, which would result in the data
5451 * being protected with a reproducible, easily knowable key.
5452 */
5453 psa_generate_random( output, output_size );
5454 *output_length = output_size;
5455 }
5456 return( status );
5457}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005458
5459
5460/****************************************************************/
5461/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005462/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005463
Gilles Peskine4c317f42018-07-12 01:24:09 +02005464psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005465 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005466{
Janos Follath24eed8d2019-11-22 13:21:35 +00005467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005468 GUARD_MODULE_INITIALIZED;
5469
Gilles Peskinef181eca2019-08-07 13:49:00 +02005470 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5471 {
5472 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5473 output,
5474 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5475 if( ret != 0 )
5476 return( mbedtls_to_psa_error( ret ) );
5477 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5478 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5479 }
5480
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5482 return( mbedtls_to_psa_error( ret ) );
5483}
5484
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005485#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5486#include "mbedtls/entropy_poll.h"
5487
Gilles Peskine7228da22019-07-15 11:06:15 +02005488psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005489 size_t seed_size )
5490{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005491 if( global_data.initialized )
5492 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005493
5494 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5495 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5496 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5497 return( PSA_ERROR_INVALID_ARGUMENT );
5498
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005499 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005500}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005501#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005502
Gilles Peskinee56e8782019-04-26 17:34:02 +02005503#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5504static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5505 size_t domain_parameters_size,
5506 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005507{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005508 size_t i;
5509 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005510
Gilles Peskinee56e8782019-04-26 17:34:02 +02005511 if( domain_parameters_size == 0 )
5512 {
5513 *exponent = 65537;
5514 return( PSA_SUCCESS );
5515 }
5516
5517 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5518 * support values that fit in a 32-bit integer, which is larger than
5519 * int on just about every platform anyway. */
5520 if( domain_parameters_size > sizeof( acc ) )
5521 return( PSA_ERROR_NOT_SUPPORTED );
5522 for( i = 0; i < domain_parameters_size; i++ )
5523 acc = ( acc << 8 ) | domain_parameters[i];
5524 if( acc > INT_MAX )
5525 return( PSA_ERROR_NOT_SUPPORTED );
5526 *exponent = acc;
5527 return( PSA_SUCCESS );
5528}
5529#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5530
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005531static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005532 psa_key_slot_t *slot, size_t bits,
5533 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534{
Gilles Peskine8e338702019-07-30 20:06:31 +02005535 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005536
Gilles Peskinee56e8782019-04-26 17:34:02 +02005537 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005538 return( PSA_ERROR_INVALID_ARGUMENT );
5539
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540 if( key_type_is_raw_bytes( type ) )
5541 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005542 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005543 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5544 if( status != PSA_SUCCESS )
5545 return( status );
5546 status = psa_generate_random( slot->data.raw.data,
5547 slot->data.raw.bytes );
5548 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005549 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005550#if defined(MBEDTLS_DES_C)
5551 if( type == PSA_KEY_TYPE_DES )
5552 psa_des_set_key_parity( slot->data.raw.data,
5553 slot->data.raw.bytes );
5554#endif /* MBEDTLS_DES_C */
5555 }
5556 else
5557
5558#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005559 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005560 {
5561 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 int exponent;
5564 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005565 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5566 return( PSA_ERROR_NOT_SUPPORTED );
5567 /* Accept only byte-aligned keys, for the same reasons as
5568 * in psa_import_rsa_key(). */
5569 if( bits % 8 != 0 )
5570 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005571 status = psa_read_rsa_exponent( domain_parameters,
5572 domain_parameters_size,
5573 &exponent );
5574 if( status != PSA_SUCCESS )
5575 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005576 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5577 if( rsa == NULL )
5578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5579 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5580 ret = mbedtls_rsa_gen_key( rsa,
5581 mbedtls_ctr_drbg_random,
5582 &global_data.ctr_drbg,
5583 (unsigned int) bits,
5584 exponent );
5585 if( ret != 0 )
5586 {
5587 mbedtls_rsa_free( rsa );
5588 mbedtls_free( rsa );
5589 return( mbedtls_to_psa_error( ret ) );
5590 }
5591 slot->data.rsa = rsa;
5592 }
5593 else
5594#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5595
5596#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005597 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005598 {
5599 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005600 mbedtls_ecp_group_id grp_id =
5601 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005602 const mbedtls_ecp_curve_info *curve_info =
5603 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5604 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005606 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005607 return( PSA_ERROR_NOT_SUPPORTED );
5608 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5609 return( PSA_ERROR_NOT_SUPPORTED );
5610 if( curve_info->bit_size != bits )
5611 return( PSA_ERROR_INVALID_ARGUMENT );
5612 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5613 if( ecp == NULL )
5614 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5615 mbedtls_ecp_keypair_init( ecp );
5616 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5617 mbedtls_ctr_drbg_random,
5618 &global_data.ctr_drbg );
5619 if( ret != 0 )
5620 {
5621 mbedtls_ecp_keypair_free( ecp );
5622 mbedtls_free( ecp );
5623 return( mbedtls_to_psa_error( ret ) );
5624 }
5625 slot->data.ecp = ecp;
5626 }
5627 else
5628#endif /* MBEDTLS_ECP_C */
5629
5630 return( PSA_ERROR_NOT_SUPPORTED );
5631
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005632 return( PSA_SUCCESS );
5633}
Darryl Green0c6575a2018-11-07 16:05:30 +00005634
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005635psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005636 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637{
5638 psa_status_t status;
5639 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005640 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005641
Gilles Peskine0f84d622019-09-12 19:03:13 +02005642 /* Reject any attempt to create a zero-length key so that we don't
5643 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5644 if( psa_get_key_bits( attributes ) == 0 )
5645 return( PSA_ERROR_INVALID_ARGUMENT );
5646
Gilles Peskinedf179142019-07-15 22:02:14 +02005647 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5648 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005649 if( status != PSA_SUCCESS )
5650 goto exit;
5651
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005652#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5653 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005654 {
Gilles Peskine11792082019-08-06 18:36:36 +02005655 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5656 size_t pubkey_length = 0; /* We don't support this feature yet */
5657 if( drv->key_management == NULL ||
5658 drv->key_management->p_generate == NULL )
5659 {
5660 status = PSA_ERROR_NOT_SUPPORTED;
5661 goto exit;
5662 }
5663 status = drv->key_management->p_generate(
5664 psa_get_se_driver_context( driver ),
5665 slot->data.se.slot_number, attributes,
5666 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005667 }
Gilles Peskine11792082019-08-06 18:36:36 +02005668 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005669#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005671 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005672 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005673 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005674 }
Gilles Peskine11792082019-08-06 18:36:36 +02005675
5676exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005677 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005678 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005679 if( status != PSA_SUCCESS )
5680 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005681 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005682 *handle = 0;
5683 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005684 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005685}
5686
5687
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688
5689/****************************************************************/
5690/* Module setup */
5691/****************************************************************/
5692
Gilles Peskine5e769522018-11-20 21:59:56 +01005693psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5694 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5695 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5696{
5697 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5698 return( PSA_ERROR_BAD_STATE );
5699 global_data.entropy_init = entropy_init;
5700 global_data.entropy_free = entropy_free;
5701 return( PSA_SUCCESS );
5702}
5703
Gilles Peskinee59236f2018-01-27 23:32:46 +01005704void mbedtls_psa_crypto_free( void )
5705{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005706 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005707 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5708 {
5709 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005710 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005711 }
5712 /* Wipe all remaining data, including configuration.
5713 * In particular, this sets all state indicator to the value
5714 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005715 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005716#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005717 /* Unregister all secure element drivers, so that we restart from
5718 * a pristine state. */
5719 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005720#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005721}
5722
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005723#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5724/** Recover a transaction that was interrupted by a power failure.
5725 *
5726 * This function is called during initialization, before psa_crypto_init()
5727 * returns. If this function returns a failure status, the initialization
5728 * fails.
5729 */
5730static psa_status_t psa_crypto_recover_transaction(
5731 const psa_crypto_transaction_t *transaction )
5732{
5733 switch( transaction->unknown.type )
5734 {
5735 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5736 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005737 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005738 * is implemented.
5739 * https://github.com/ARMmbed/mbed-crypto/issues/218
5740 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005741 default:
5742 /* We found an unsupported transaction in the storage.
5743 * We don't know what state the storage is in. Give up. */
5744 return( PSA_ERROR_STORAGE_FAILURE );
5745 }
5746}
5747#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5748
Gilles Peskinee59236f2018-01-27 23:32:46 +01005749psa_status_t psa_crypto_init( void )
5750{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005751 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752 const unsigned char drbg_seed[] = "PSA";
5753
Gilles Peskinec6b69072018-11-20 21:42:52 +01005754 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005755 if( global_data.initialized != 0 )
5756 return( PSA_SUCCESS );
5757
Gilles Peskine5e769522018-11-20 21:59:56 +01005758 /* Set default configuration if
5759 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5760 if( global_data.entropy_init == NULL )
5761 global_data.entropy_init = mbedtls_entropy_init;
5762 if( global_data.entropy_free == NULL )
5763 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005764
Gilles Peskinec6b69072018-11-20 21:42:52 +01005765 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005766 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005767#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5768 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5769 /* The PSA entropy injection feature depends on using NV seed as an entropy
5770 * source. Add NV seed as an entropy source for PSA entropy injection. */
5771 mbedtls_entropy_add_source( &global_data.entropy,
5772 mbedtls_nv_seed_poll, NULL,
5773 MBEDTLS_ENTROPY_BLOCK_SIZE,
5774 MBEDTLS_ENTROPY_SOURCE_STRONG );
5775#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005776 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005777 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005778 status = mbedtls_to_psa_error(
5779 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5780 mbedtls_entropy_func,
5781 &global_data.entropy,
5782 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5783 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005784 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005785 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005786
Gilles Peskine66fb1262018-12-10 16:29:04 +01005787 status = psa_initialize_key_slots( );
5788 if( status != PSA_SUCCESS )
5789 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005790
Gilles Peskined9348f22019-10-01 15:22:29 +02005791#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5792 status = psa_init_all_se_drivers( );
5793 if( status != PSA_SUCCESS )
5794 goto exit;
5795#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5796
Gilles Peskine4b734222019-07-24 15:56:31 +02005797#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005798 status = psa_crypto_load_transaction( );
5799 if( status == PSA_SUCCESS )
5800 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005801 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5802 if( status != PSA_SUCCESS )
5803 goto exit;
5804 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005805 }
5806 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5807 {
5808 /* There's no transaction to complete. It's all good. */
5809 status = PSA_SUCCESS;
5810 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005811#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005812
Gilles Peskinec6b69072018-11-20 21:42:52 +01005813 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005814 global_data.initialized = 1;
5815
5816exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005817 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005819 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005820}
5821
5822#endif /* MBEDTLS_PSA_CRYPTO_C */