blob: b3da8f789cc2a3fb1f7eb8b504b927c983ad86c8 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132static psa_status_t mbedtls_to_psa_error( int ret )
133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
206 return( PSA_ERROR_BAD_STATE );
207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
379 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200380{
381 switch( curve )
382 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 case PSA_ECC_CURVE_SECP_R1:
384 switch( byte_length )
385 {
386 case PSA_BITS_TO_BYTES( 192 ):
387 return( MBEDTLS_ECP_DP_SECP192R1 );
388 case PSA_BITS_TO_BYTES( 224 ):
389 return( MBEDTLS_ECP_DP_SECP224R1 );
390 case PSA_BITS_TO_BYTES( 256 ):
391 return( MBEDTLS_ECP_DP_SECP256R1 );
392 case PSA_BITS_TO_BYTES( 384 ):
393 return( MBEDTLS_ECP_DP_SECP384R1 );
394 case PSA_BITS_TO_BYTES( 521 ):
395 return( MBEDTLS_ECP_DP_SECP521R1 );
396 default:
397 return( MBEDTLS_ECP_DP_NONE );
398 }
399 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100400
401 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
402 switch( byte_length )
403 {
404 case PSA_BITS_TO_BYTES( 256 ):
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_BITS_TO_BYTES( 384 ):
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_BITS_TO_BYTES( 512 ):
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 default:
411 return( MBEDTLS_ECP_DP_NONE );
412 }
413 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100414
415 case PSA_ECC_CURVE_MONTGOMERY:
416 switch( byte_length )
417 {
418 case PSA_BITS_TO_BYTES( 255 ):
419 return( MBEDTLS_ECP_DP_CURVE25519 );
420 case PSA_BITS_TO_BYTES( 448 ):
421 return( MBEDTLS_ECP_DP_CURVE448 );
422 default:
423 return( MBEDTLS_ECP_DP_NONE );
424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
427 case PSA_ECC_CURVE_SECP_K1:
428 switch( byte_length )
429 {
430 case PSA_BITS_TO_BYTES( 192 ):
431 return( MBEDTLS_ECP_DP_SECP192K1 );
432 case PSA_BITS_TO_BYTES( 224 ):
433 return( MBEDTLS_ECP_DP_SECP224K1 );
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_SECP256K1 );
436 default:
437 return( MBEDTLS_ECP_DP_NONE );
438 }
439 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100440
Gilles Peskine12313cd2018-06-20 00:20:32 +0200441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
455#if defined(MBEDTLS_MD_C)
456 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200457#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200458 case PSA_KEY_TYPE_DERIVE:
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_AES_C)
461 case PSA_KEY_TYPE_AES:
462 if( bits != 128 && bits != 192 && bits != 256 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466#if defined(MBEDTLS_CAMELLIA_C)
467 case PSA_KEY_TYPE_CAMELLIA:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_DES_C)
473 case PSA_KEY_TYPE_DES:
474 if( bits != 64 && bits != 128 && bits != 192 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_ARC4_C)
479 case PSA_KEY_TYPE_ARC4:
480 if( bits < 8 || bits > 2048 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200484#if defined(MBEDTLS_CHACHA20_C)
485 case PSA_KEY_TYPE_CHACHA20:
486 if( bits != 256 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 default:
491 return( PSA_ERROR_NOT_SUPPORTED );
492 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200493 if( bits % 8 != 0 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495
496 /* Allocate memory for the key */
497 raw->bytes = PSA_BITS_TO_BYTES( bits );
498 raw->data = mbedtls_calloc( 1, raw->bytes );
499 if( raw->data == NULL )
500 {
501 raw->bytes = 0;
502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
503 }
504 return( PSA_SUCCESS );
505}
506
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100508/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
509 * that are not a multiple of 8) well. For example, there is only
510 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
511 * way to return the exact bit size of a key.
512 * To keep things simple, reject non-byte-aligned key sizes. */
513static psa_status_t psa_check_rsa_key_byte_aligned(
514 const mbedtls_rsa_context *rsa )
515{
516 mbedtls_mpi n;
517 psa_status_t status;
518 mbedtls_mpi_init( &n );
519 status = mbedtls_to_psa_error(
520 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
521 if( status == PSA_SUCCESS )
522 {
523 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
524 status = PSA_ERROR_NOT_SUPPORTED;
525 }
526 mbedtls_mpi_free( &n );
527 return( status );
528}
529
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000530static psa_status_t psa_import_rsa_key( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 mbedtls_rsa_context **p_rsa )
534{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 psa_status_t status;
536 mbedtls_pk_context pk;
537 mbedtls_rsa_context *rsa;
538 size_t bits;
539
540 mbedtls_pk_init( &pk );
541
542 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
548 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
554 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
560 rsa = mbedtls_pk_rsa( pk );
561 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
562 * supports non-byte-aligned key sizes, but not well. For example,
563 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
564 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
565 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
566 {
567 status = PSA_ERROR_NOT_SUPPORTED;
568 goto exit;
569 }
570 status = psa_check_rsa_key_byte_aligned( rsa );
571
572exit:
573 /* Free the content of the pk object only on error. */
574 if( status != PSA_SUCCESS )
575 {
576 mbedtls_pk_free( &pk );
577 return( status );
578 }
579
580 /* On success, store the content of the object in the RSA context. */
581 *p_rsa = rsa;
582
583 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200584}
585#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Jaeden Ameroccdce902019-01-10 11:42:27 +0000587#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100589 size_t data_length,
590 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100591 mbedtls_ecp_keypair **p_ecp )
592{
593 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
594 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
595 if( *p_ecp == NULL )
596 return( PSA_ERROR_INSUFFICIENT_MEMORY );
597 mbedtls_ecp_keypair_init( *p_ecp );
598
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100599 if( is_public )
600 {
601 /* A public key is represented as:
602 * - The byte 0x04;
603 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
604 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
605 * So its data length is 2m+1 where n is the key size in bits.
606 */
607 if( ( data_length & 1 ) == 0 )
608 return( PSA_ERROR_INVALID_ARGUMENT );
609 data_length = data_length / 2;
610 }
611
Gilles Peskine4cd32772019-12-02 20:49:42 +0100612 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100613 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
614 if( grp_id == MBEDTLS_ECP_DP_NONE )
615 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100616 return( mbedtls_to_psa_error(
617 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
618}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619
620/* Import a public key given as the uncompressed representation defined by SEC1
621 * 2.3.3 as the content of an ECPoint. */
622static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
623 const uint8_t *data,
624 size_t data_length,
625 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100630 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 if( status != PSA_SUCCESS )
632 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100633
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634 /* Load the public value. */
635 status = mbedtls_to_psa_error(
636 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
637 data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640
641 /* Check that the point is on the curve. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646
647 *p_ecp = ecp;
648 return( PSA_SUCCESS );
649
650exit:
651 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 mbedtls_ecp_keypair_free( ecp );
654 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000656 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200657}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200658
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659/* Import a private key given as a byte string which is the private value
660 * in big-endian order. */
661static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
662 const uint8_t *data,
663 size_t data_length,
664 mbedtls_ecp_keypair **p_ecp )
665{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100669 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 if( status != PSA_SUCCESS )
671 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100672
Steven Cooremane3fd3922020-06-11 16:50:36 +0200673 /* Load and validate the secret key */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100674 status = mbedtls_to_psa_error(
Steven Cooremane3fd3922020-06-11 16:50:36 +0200675 mbedtls_ecp_read_key( ecp->grp.id, ecp, data, data_length ) );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100676 if( status != PSA_SUCCESS )
677 goto exit;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200678
Gilles Peskinef76aa772018-10-29 19:24:33 +0100679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 }
778 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100779#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Darryl Green940d72c2018-07-13 13:18:51 +0100792
Gilles Peskineb46bef22019-07-30 21:32:04 +0200793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100800 return( status );
801}
802
Gilles Peskinef603c712019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskine549ea862019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
815 * if one is a wildcard. If so the other has the specific algorithm. */
816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskined6f371b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskine549ea862019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
884 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000958 {
959 /* No key material to clean. */
960 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997/** Completely wipe a slot in memory, including its policy.
998 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100999psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000{
1001 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001002 /* Multipart operations may still be using the key. This is safe
1003 * because all multipart operation objects are independent from
1004 * the key slot: if they need to access the key after the setup
1005 * phase, they have a copy of the key. Note that this means that
1006 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007 /* At this point, key material and other type-specific content has
1008 * been wiped. Clear remaining metadata. We can call memset and not
1009 * zeroize because the metadata is not particularly sensitive. */
1010 memset( slot, 0, sizeof( *slot ) );
1011 return( status );
1012}
1013
Gilles Peskinec5487a82018-12-03 18:08:14 +01001014psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001016 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001017 psa_status_t status; /* status of the last operation */
1018 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1020 psa_se_drv_table_entry_t *driver;
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskine1841cf42019-10-08 15:48:25 +02001023 if( handle == 0 )
1024 return( PSA_SUCCESS );
1025
Gilles Peskinec5487a82018-12-03 18:08:14 +01001026 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027 if( status != PSA_SUCCESS )
1028 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029
1030#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001031 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001032 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001034 /* For a key in a secure element, we need to do three things:
1035 * remove the key file in internal storage, destroy the
1036 * key inside the secure element, and update the driver's
1037 * persistent data. Start a transaction that will encompass these
1038 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001039 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001040 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001041 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001042 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 status = psa_crypto_save_transaction( );
1044 if( status != PSA_SUCCESS )
1045 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001046 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001047 /* We should still try to destroy the key in the secure
1048 * element and the key metadata in storage. This is especially
1049 * important if the error is that the storage is full.
1050 * But how to do it exactly without risking an inconsistent
1051 * state after a reset?
1052 * https://github.com/ARMmbed/mbed-crypto/issues/215
1053 */
1054 overall_status = status;
1055 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001056 }
1057
Gilles Peskine354f7672019-07-12 23:46:38 +02001058 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001059 if( overall_status == PSA_SUCCESS )
1060 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001061 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001062#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1063
Darryl Greend49a4992018-06-18 17:27:26 +01001064#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001065 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001066 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001067 status = psa_destroy_persistent_key( slot->attr.id );
1068 if( overall_status == PSA_SUCCESS )
1069 overall_status = status;
1070
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001071 /* TODO: other slots may have a copy of the same key. We should
1072 * invalidate them.
1073 * https://github.com/ARMmbed/mbed-crypto/issues/214
1074 */
Darryl Greend49a4992018-06-18 17:27:26 +01001075 }
1076#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001077
Gilles Peskinefc762652019-07-22 19:30:34 +02001078#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1079 if( driver != NULL )
1080 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001081 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001082 if( overall_status == PSA_SUCCESS )
1083 overall_status = status;
1084 status = psa_crypto_stop_transaction( );
1085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001087 }
1088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1089
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001090#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1091exit:
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001093 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1095 if( overall_status == PSA_SUCCESS )
1096 overall_status = status;
1097 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098}
1099
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001101{
Gilles Peskineb699f072019-04-26 16:06:02 +02001102 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001103 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001104}
1105
Gilles Peskineb699f072019-04-26 16:06:02 +02001106psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1107 psa_key_type_t type,
1108 const uint8_t *data,
1109 size_t data_length )
1110{
1111 uint8_t *copy = NULL;
1112
1113 if( data_length != 0 )
1114 {
1115 copy = mbedtls_calloc( 1, data_length );
1116 if( copy == NULL )
1117 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1118 memcpy( copy, data, data_length );
1119 }
1120 /* After this point, this function is guaranteed to succeed, so it
1121 * can start modifying `*attributes`. */
1122
1123 if( attributes->domain_parameters != NULL )
1124 {
1125 mbedtls_free( attributes->domain_parameters );
1126 attributes->domain_parameters = NULL;
1127 attributes->domain_parameters_size = 0;
1128 }
1129
1130 attributes->domain_parameters = copy;
1131 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001132 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001133 return( PSA_SUCCESS );
1134}
1135
1136psa_status_t psa_get_key_domain_parameters(
1137 const psa_key_attributes_t *attributes,
1138 uint8_t *data, size_t data_size, size_t *data_length )
1139{
1140 if( attributes->domain_parameters_size > data_size )
1141 return( PSA_ERROR_BUFFER_TOO_SMALL );
1142 *data_length = attributes->domain_parameters_size;
1143 if( attributes->domain_parameters_size != 0 )
1144 memcpy( data, attributes->domain_parameters,
1145 attributes->domain_parameters_size );
1146 return( PSA_SUCCESS );
1147}
1148
1149#if defined(MBEDTLS_RSA_C)
1150static psa_status_t psa_get_rsa_public_exponent(
1151 const mbedtls_rsa_context *rsa,
1152 psa_key_attributes_t *attributes )
1153{
1154 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001156 uint8_t *buffer = NULL;
1157 size_t buflen;
1158 mbedtls_mpi_init( &mpi );
1159
1160 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1161 if( ret != 0 )
1162 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001163 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1164 {
1165 /* It's the default value, which is reported as an empty string,
1166 * so there's nothing to do. */
1167 goto exit;
1168 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001169
1170 buflen = mbedtls_mpi_size( &mpi );
1171 buffer = mbedtls_calloc( 1, buflen );
1172 if( buffer == NULL )
1173 {
1174 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1175 goto exit;
1176 }
1177 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1178 if( ret != 0 )
1179 goto exit;
1180 attributes->domain_parameters = buffer;
1181 attributes->domain_parameters_size = buflen;
1182
1183exit:
1184 mbedtls_mpi_free( &mpi );
1185 if( ret != 0 )
1186 mbedtls_free( buffer );
1187 return( mbedtls_to_psa_error( ret ) );
1188}
1189#endif /* MBEDTLS_RSA_C */
1190
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001191/** Retrieve all the publicly-accessible attributes of a key.
1192 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001193psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1194 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001196 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001197 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001199 psa_reset_key_attributes( attributes );
1200
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001201 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001202 if( status != PSA_SUCCESS )
1203 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001204
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001205 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001206 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1207 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1208
1209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1210 if( psa_key_slot_is_external( slot ) )
1211 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1212#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001213
Gilles Peskine8e338702019-07-30 20:06:31 +02001214 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001215 {
1216#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001217 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001219#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001220 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001221 * is not yet implemented.
1222 * https://github.com/ARMmbed/mbed-crypto/issues/216
1223 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001224 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001225 break;
1226#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1228 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001229#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001230 default:
1231 /* Nothing else to do. */
1232 break;
1233 }
1234
1235 if( status != PSA_SUCCESS )
1236 psa_reset_key_attributes( attributes );
1237 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238}
1239
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1241psa_status_t psa_get_key_slot_number(
1242 const psa_key_attributes_t *attributes,
1243 psa_key_slot_number_t *slot_number )
1244{
1245 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1246 {
1247 *slot_number = attributes->slot_number;
1248 return( PSA_SUCCESS );
1249 }
1250 else
1251 return( PSA_ERROR_INVALID_ARGUMENT );
1252}
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1254
Jaeden Ameroccdce902019-01-10 11:42:27 +00001255#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001256static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1257 unsigned char *buf, size_t size )
1258{
Janos Follath24eed8d2019-11-22 13:21:35 +00001259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001260 unsigned char *c;
1261 size_t len = 0;
1262
1263 c = buf + size;
1264
1265 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1266
1267 return( (int) len );
1268}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001269#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001270
Gilles Peskinef603c712019-01-19 13:40:11 +01001271static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1272 uint8_t *data,
1273 size_t data_size,
1274 size_t *data_length,
1275 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001276{
Gilles Peskine5d309672019-07-12 23:47:28 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1278 const psa_drv_se_t *drv;
1279 psa_drv_se_context_t *drv_context;
1280#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1281
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001282 *data_length = 0;
1283
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001285 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001286
Gilles Peskinef9168942019-09-12 19:20:29 +02001287 /* Reject a zero-length output buffer now, since this can never be a
1288 * valid key representation. This way we know that data must be a valid
1289 * pointer and we can do things like memset(data, ..., data_size). */
1290 if( data_size == 0 )
1291 return( PSA_ERROR_BUFFER_TOO_SMALL );
1292
Gilles Peskine5d309672019-07-12 23:47:28 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001294 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001295 {
1296 psa_drv_se_export_key_t method;
1297 if( drv->key_management == NULL )
1298 return( PSA_ERROR_NOT_SUPPORTED );
1299 method = ( export_public_key ?
1300 drv->key_management->p_export_public :
1301 drv->key_management->p_export );
1302 if( method == NULL )
1303 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001304 return( method( drv_context,
1305 slot->data.se.slot_number,
1306 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001307 }
1308#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1309
Gilles Peskine8e338702019-07-30 20:06:31 +02001310 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311 {
1312 if( slot->data.raw.bytes > data_size )
1313 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001314 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1315 memset( data + slot->data.raw.bytes, 0,
1316 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 *data_length = slot->data.raw.bytes;
1318 return( PSA_SUCCESS );
1319 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001320#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001321 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001322 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001323 psa_status_t status;
1324
Gilles Peskineb46bef22019-07-30 21:32:04 +02001325 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001326 if( bytes > data_size )
1327 return( PSA_ERROR_BUFFER_TOO_SMALL );
1328 status = mbedtls_to_psa_error(
Steven Cooreman0024df62020-07-13 10:59:40 +02001329 mbedtls_ecp_write_key( slot->data.ecp,
1330 data, bytes ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001331 if( status != PSA_SUCCESS )
1332 return( status );
1333 memset( data + bytes, 0, data_size - bytes );
1334 *data_length = bytes;
1335 return( PSA_SUCCESS );
1336 }
1337#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 else
Moran Peker17e36e12018-05-02 12:55:20 +03001339 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001340#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001341 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1342 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001343 {
Moran Pekera998bc62018-04-16 18:16:20 +03001344 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001346 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001347 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001348#if defined(MBEDTLS_RSA_C)
1349 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001350 pk.pk_info = &mbedtls_rsa_info;
1351 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001352#else
1353 return( PSA_ERROR_NOT_SUPPORTED );
1354#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001355 }
1356 else
1357 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001358#if defined(MBEDTLS_ECP_C)
1359 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001360 pk.pk_info = &mbedtls_eckey_info;
1361 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001362#else
1363 return( PSA_ERROR_NOT_SUPPORTED );
1364#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001365 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001366 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001367 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001368 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001369 }
Moran Peker17e36e12018-05-02 12:55:20 +03001370 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001371 {
Moran Peker17e36e12018-05-02 12:55:20 +03001372 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001373 }
Moran Peker60364322018-04-29 11:34:58 +03001374 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001375 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001376 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001377 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001379 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1380 * Move the data to the beginning and erase remaining data
1381 * at the original location. */
1382 if( 2 * (size_t) ret <= data_size )
1383 {
1384 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001385 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001386 }
1387 else if( (size_t) ret < data_size )
1388 {
1389 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001390 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001391 }
Moran Pekera998bc62018-04-16 18:16:20 +03001392 *data_length = ret;
1393 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001394 }
1395 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001396#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001397 {
1398 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001399 it is valid for a special-purpose implementation to omit
1400 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001401 return( PSA_ERROR_NOT_SUPPORTED );
1402 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403 }
1404}
1405
Gilles Peskinec5487a82018-12-03 18:08:14 +01001406psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001407 uint8_t *data,
1408 size_t data_size,
1409 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001410{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001411 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001412 psa_status_t status;
1413
1414 /* Set the key to empty now, so that even when there are errors, we always
1415 * set data_length to a value between 0 and data_size. On error, setting
1416 * the key to empty is a good choice because an empty key representation is
1417 * unlikely to be accepted anywhere. */
1418 *data_length = 0;
1419
1420 /* Export requires the EXPORT flag. There is an exception for public keys,
1421 * which don't require any flag, but psa_get_key_from_slot takes
1422 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001423 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001424 if( status != PSA_SUCCESS )
1425 return( status );
1426 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001427 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskinec5487a82018-12-03 18:08:14 +01001430psa_status_t psa_export_public_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 Pekerdd4ea382018-04-03 15:30:03 +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 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001445 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001446 if( status != PSA_SUCCESS )
1447 return( status );
1448 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001449 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001450}
1451
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452#if defined(static_assert)
1453static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1454 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001455static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001456 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001457static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001458 "One or more key attribute flag is listed as both internal-only and external-only" );
1459#endif
1460
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001461/** Validate that a key policy is internally well-formed.
1462 *
1463 * This function only rejects invalid policies. It does not validate the
1464 * consistency of the policy with respect to other attributes of the key
1465 * such as the key type.
1466 */
1467static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001468{
1469 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001470 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001471 PSA_KEY_USAGE_ENCRYPT |
1472 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001473 PSA_KEY_USAGE_SIGN_HASH |
1474 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1476 return( PSA_ERROR_INVALID_ARGUMENT );
1477
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 return( PSA_SUCCESS );
1479}
1480
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001481/** Validate the internal consistency of key attributes.
1482 *
1483 * This function only rejects invalid attribute values. If does not
1484 * validate the consistency of the attributes with any key data that may
1485 * be involved in the creation of the key.
1486 *
1487 * Call this function early in the key creation process.
1488 *
1489 * \param[in] attributes Key attributes for the new key.
1490 * \param[out] p_drv On any return, the driver for the key, if any.
1491 * NULL for a transparent key.
1492 *
1493 */
1494static psa_status_t psa_validate_key_attributes(
1495 const psa_key_attributes_t *attributes,
1496 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001497{
1498 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001499
1500 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001501 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502 status = psa_validate_persistent_key_parameters(
1503 attributes->core.lifetime, attributes->core.id,
1504 p_drv, 1 );
1505 if( status != PSA_SUCCESS )
1506 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001507 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001508
1509 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001510 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001511 return( status );
1512
1513 /* Refuse to create overly large keys.
1514 * Note that this doesn't trigger on import if the attributes don't
1515 * explicitly specify a size (so psa_get_key_bits returns 0), so
1516 * psa_import_key() needs its own checks. */
1517 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1518 return( PSA_ERROR_NOT_SUPPORTED );
1519
Gilles Peskine91e8c332019-08-02 19:19:39 +02001520 /* Reject invalid flags. These should not be reachable through the API. */
1521 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1522 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1523 return( PSA_ERROR_INVALID_ARGUMENT );
1524
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001525 return( PSA_SUCCESS );
1526}
1527
Gilles Peskine4747d192019-04-17 15:05:45 +02001528/** Prepare a key slot to receive key material.
1529 *
1530 * This function allocates a key slot and sets its metadata.
1531 *
1532 * If this function fails, call psa_fail_key_creation().
1533 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001534 * This function is intended to be used as follows:
1535 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1536 * it with the specified attributes, and assign it a handle.
1537 * -# Populate the slot with the key material.
1538 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1539 * In case of failure at any step, stop the sequence and call
1540 * psa_fail_key_creation().
1541 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001542 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001543 * \param[in] attributes Key attributes for the new key.
1544 * \param[out] handle On success, a handle for the allocated slot.
1545 * \param[out] p_slot On success, a pointer to the prepared slot.
1546 * \param[out] p_drv On any return, the driver for the key, if any.
1547 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001548 *
1549 * \retval #PSA_SUCCESS
1550 * The key slot is ready to receive key material.
1551 * \return If this function fails, the key slot is an invalid state.
1552 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001553 */
1554static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001555 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 const psa_key_attributes_t *attributes,
1557 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001558 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001559 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001560{
1561 psa_status_t status;
1562 psa_key_slot_t *slot;
1563
Gilles Peskinedf179142019-07-15 22:02:14 +02001564 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001565 *p_drv = NULL;
1566
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001567 status = psa_validate_key_attributes( attributes, p_drv );
1568 if( status != PSA_SUCCESS )
1569 return( status );
1570
Gilles Peskineedbed562019-08-07 18:19:59 +02001571 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001572 if( status != PSA_SUCCESS )
1573 return( status );
1574 slot = *p_slot;
1575
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001576 /* We're storing the declared bit-size of the key. It's up to each
1577 * creation mechanism to verify that this information is correct.
1578 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001579 * an input (generate, device) but not for those where the bit-size
1580 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001581
1582 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001583
Gilles Peskine91e8c332019-08-02 19:19:39 +02001584 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001585 * external-only flags, query `attributes`. Thanks to the check
1586 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001587 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001588 * may have set. */
1589 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001590
Gilles Peskinecbaff462019-07-12 23:46:04 +02001591#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001592 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001593 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001594 * create the key file in internal storage, create the
1595 * key inside the secure element, and update the driver's
1596 * persistent data. Start a transaction that will encompass these
1597 * three actions. */
1598 /* The first thing to do is to find a slot number for the new key.
1599 * We save the slot number in persistent storage as part of the
1600 * transaction data. It will be needed to recover if the power
1601 * fails during the key creation process, to clean up on the secure
1602 * element side after restarting. Obtaining a slot number from the
1603 * secure element driver updates its persistent state, but we do not yet
1604 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001605 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001606 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001607 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001608 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001609 &slot->data.se.slot_number );
1610 if( status != PSA_SUCCESS )
1611 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001612 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001613 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001614 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001615 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001616 status = psa_crypto_save_transaction( );
1617 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001618 {
1619 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001620 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001621 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001622 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001623
1624 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1625 {
1626 /* Key registration only makes sense with a secure element. */
1627 return( PSA_ERROR_INVALID_ARGUMENT );
1628 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001629#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1630
Darryl Green0c6575a2018-11-07 16:05:30 +00001631 return( status );
1632}
Gilles Peskine4747d192019-04-17 15:05:45 +02001633
1634/** Finalize the creation of a key once its key material has been set.
1635 *
1636 * This entails writing the key to persistent storage.
1637 *
1638 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001639 * See the documentation of psa_start_key_creation() for the intended use
1640 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001642 * \param[in,out] slot Pointer to the slot with key material.
1643 * \param[in] driver The secure element driver for the key,
1644 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001645 *
1646 * \retval #PSA_SUCCESS
1647 * The key was successfully created. The handle is now valid.
1648 * \return If this function fails, the key slot is an invalid state.
1649 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001650 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001651static psa_status_t psa_finish_key_creation(
1652 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001653 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001654{
1655 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001656 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001657 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001658
1659#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001660 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001662#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1663 if( driver != NULL )
1664 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001665 psa_se_key_data_storage_t data;
1666#if defined(static_assert)
1667 static_assert( sizeof( slot->data.se.slot_number ) ==
1668 sizeof( data.slot_number ),
1669 "Slot number size does not match psa_se_key_data_storage_t" );
1670 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1671 "Bit-size size does not match psa_se_key_data_storage_t" );
1672#endif
1673 memcpy( &data.slot_number, &slot->data.se.slot_number,
1674 sizeof( slot->data.se.slot_number ) );
1675 memcpy( &data.bits, &slot->attr.bits,
1676 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001677 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001678 (uint8_t*) &data,
1679 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001680 }
1681 else
1682#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1683 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001684 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001685 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001686 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001687 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1688 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001689 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001690 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1691 status = psa_internal_export_key( slot,
1692 buffer, buffer_size, &length,
1693 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001694 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001695 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001696 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001697
Gilles Peskinef9168942019-09-12 19:20:29 +02001698 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001699 mbedtls_free( buffer );
1700 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001702#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1703
Gilles Peskinecbaff462019-07-12 23:46:04 +02001704#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001705 /* Finish the transaction for a key creation. This does not
1706 * happen when registering an existing key. Detect this case
1707 * by checking whether a transaction is in progress (actual
1708 * creation of a key in a secure element requires a transaction,
1709 * but registration doesn't use one). */
1710 if( driver != NULL &&
1711 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001712 {
1713 status = psa_save_se_persistent_data( driver );
1714 if( status != PSA_SUCCESS )
1715 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001716 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717 return( status );
1718 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001719 status = psa_crypto_stop_transaction( );
1720 if( status != PSA_SUCCESS )
1721 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001722 }
1723#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1724
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 return( status );
1726}
1727
1728/** Abort the creation of a key.
1729 *
1730 * You may call this function after calling psa_start_key_creation(),
1731 * or after psa_finish_key_creation() fails. In other circumstances, this
1732 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001733 * See the documentation of psa_start_key_creation() for the intended use
1734 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001735 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 * \param[in,out] slot Pointer to the slot with key material.
1737 * \param[in] driver The secure element driver for the key,
1738 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001739 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001740static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001741 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001742{
Gilles Peskine011e4282019-06-26 18:34:38 +02001743 (void) driver;
1744
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 if( slot == NULL )
1746 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001747
1748#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001749 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001750 * element, and the failure happened later (when saving metadata
1751 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001752 * element.
1753 * https://github.com/ARMmbed/mbed-crypto/issues/217
1754 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001755
Gilles Peskined7729582019-08-05 15:55:54 +02001756 /* Abort the ongoing transaction if any (there may not be one if
1757 * the creation process failed before starting one, or if the
1758 * key creation is a registration of a key in a secure element).
1759 * Earlier functions must already have done what it takes to undo any
1760 * partial creation. All that's left is to update the transaction data
1761 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001762 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001763#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1764
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 psa_wipe_key_slot( slot );
1766}
1767
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001768/** Validate optional attributes during key creation.
1769 *
1770 * Some key attributes are optional during key creation. If they are
1771 * specified in the attributes structure, check that they are consistent
1772 * with the data in the slot.
1773 *
1774 * This function should be called near the end of key creation, after
1775 * the slot in memory is fully populated but before saving persistent data.
1776 */
1777static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001778 const psa_key_slot_t *slot,
1779 const psa_key_attributes_t *attributes )
1780{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001781 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001782 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001783 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001784 return( PSA_ERROR_INVALID_ARGUMENT );
1785 }
1786
1787 if( attributes->domain_parameters_size != 0 )
1788 {
1789#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001790 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001791 {
1792 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001794 mbedtls_mpi_init( &actual );
1795 mbedtls_mpi_init( &required );
1796 ret = mbedtls_rsa_export( slot->data.rsa,
1797 NULL, NULL, NULL, NULL, &actual );
1798 if( ret != 0 )
1799 goto rsa_exit;
1800 ret = mbedtls_mpi_read_binary( &required,
1801 attributes->domain_parameters,
1802 attributes->domain_parameters_size );
1803 if( ret != 0 )
1804 goto rsa_exit;
1805 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1806 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1807 rsa_exit:
1808 mbedtls_mpi_free( &actual );
1809 mbedtls_mpi_free( &required );
1810 if( ret != 0)
1811 return( mbedtls_to_psa_error( ret ) );
1812 }
1813 else
1814#endif
1815 {
1816 return( PSA_ERROR_INVALID_ARGUMENT );
1817 }
1818 }
1819
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001820 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001821 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001822 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001823 return( PSA_ERROR_INVALID_ARGUMENT );
1824 }
1825
1826 return( PSA_SUCCESS );
1827}
1828
Gilles Peskine4747d192019-04-17 15:05:45 +02001829psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001830 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001831 size_t data_length,
1832 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001833{
1834 psa_status_t status;
1835 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001836 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001837
Gilles Peskine0f84d622019-09-12 19:03:13 +02001838 /* Reject zero-length symmetric keys (including raw data key objects).
1839 * This also rejects any key which might be encoded as an empty string,
1840 * which is never valid. */
1841 if( data_length == 0 )
1842 return( PSA_ERROR_INVALID_ARGUMENT );
1843
Gilles Peskinedf179142019-07-15 22:02:14 +02001844 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1845 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846 if( status != PSA_SUCCESS )
1847 goto exit;
1848
Gilles Peskine5d309672019-07-12 23:47:28 +02001849#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1850 if( driver != NULL )
1851 {
1852 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001853 /* The driver should set the number of key bits, however in
1854 * case it doesn't, we initialize bits to an invalid value. */
1855 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001856 if( drv->key_management == NULL ||
1857 drv->key_management->p_import == NULL )
1858 {
1859 status = PSA_ERROR_NOT_SUPPORTED;
1860 goto exit;
1861 }
1862 status = drv->key_management->p_import(
1863 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001864 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001865 &bits );
1866 if( status != PSA_SUCCESS )
1867 goto exit;
1868 if( bits > PSA_MAX_KEY_BITS )
1869 {
1870 status = PSA_ERROR_NOT_SUPPORTED;
1871 goto exit;
1872 }
1873 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001874 }
1875 else
1876#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1877 {
1878 status = psa_import_key_into_slot( slot, data, data_length );
1879 if( status != PSA_SUCCESS )
1880 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001881 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001882 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001883 if( status != PSA_SUCCESS )
1884 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001885
Gilles Peskine011e4282019-06-26 18:34:38 +02001886 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001887exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001888 if( status != PSA_SUCCESS )
1889 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001890 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 *handle = 0;
1892 }
1893 return( status );
1894}
1895
Gilles Peskined7729582019-08-05 15:55:54 +02001896#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1897psa_status_t mbedtls_psa_register_se_key(
1898 const psa_key_attributes_t *attributes )
1899{
1900 psa_status_t status;
1901 psa_key_slot_t *slot = NULL;
1902 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001903 psa_key_handle_t handle = 0;
1904
1905 /* Leaving attributes unspecified is not currently supported.
1906 * It could make sense to query the key type and size from the
1907 * secure element, but not all secure elements support this
1908 * and the driver HAL doesn't currently support it. */
1909 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1910 return( PSA_ERROR_NOT_SUPPORTED );
1911 if( psa_get_key_bits( attributes ) == 0 )
1912 return( PSA_ERROR_NOT_SUPPORTED );
1913
1914 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1915 &handle, &slot, &driver );
1916 if( status != PSA_SUCCESS )
1917 goto exit;
1918
Gilles Peskined7729582019-08-05 15:55:54 +02001919 status = psa_finish_key_creation( slot, driver );
1920
1921exit:
1922 if( status != PSA_SUCCESS )
1923 {
1924 psa_fail_key_creation( slot, driver );
1925 }
1926 /* Registration doesn't keep the key in RAM. */
1927 psa_close_key( handle );
1928 return( status );
1929}
1930#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1931
Gilles Peskinef603c712019-01-19 13:40:11 +01001932static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001933 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001934{
1935 psa_status_t status;
1936 uint8_t *buffer = NULL;
1937 size_t buffer_size = 0;
1938 size_t length;
1939
Gilles Peskine8e338702019-07-30 20:06:31 +02001940 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001941 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001942 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001943 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001944 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001945 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1946 if( status != PSA_SUCCESS )
1947 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001948 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001949 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950
1951exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001952 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001953 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001954 return( status );
1955}
1956
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001957psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1958 const psa_key_attributes_t *specified_attributes,
1959 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001960{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001961 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001962 psa_key_slot_t *source_slot = NULL;
1963 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001964 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001965 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001966
Gilles Peskine28f8f302019-07-24 13:30:31 +02001967 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001968 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001969 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001970 goto exit;
1971
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001972 status = psa_validate_optional_attributes( source_slot,
1973 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974 if( status != PSA_SUCCESS )
1975 goto exit;
1976
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001977 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001978 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 if( status != PSA_SUCCESS )
1980 goto exit;
1981
Gilles Peskinedf179142019-07-15 22:02:14 +02001982 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1983 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001984 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 if( status != PSA_SUCCESS )
1986 goto exit;
1987
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1989 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001990 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001991 /* Copying to a secure element is not implemented yet. */
1992 status = PSA_ERROR_NOT_SUPPORTED;
1993 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001994 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001995#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001996
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002000
Gilles Peskine011e4282019-06-26 18:34:38 +02002001 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 if( status != PSA_SUCCESS )
2004 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002005 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006 *target_handle = 0;
2007 }
2008 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002009}
2010
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002011
2012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002013/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002014/* Message digests */
2015/****************************************************************/
2016
Gilles Peskineb16841e2019-10-10 20:36:12 +02002017#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002018static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002019{
2020 switch( alg )
2021 {
2022#if defined(MBEDTLS_MD2_C)
2023 case PSA_ALG_MD2:
2024 return( &mbedtls_md2_info );
2025#endif
2026#if defined(MBEDTLS_MD4_C)
2027 case PSA_ALG_MD4:
2028 return( &mbedtls_md4_info );
2029#endif
2030#if defined(MBEDTLS_MD5_C)
2031 case PSA_ALG_MD5:
2032 return( &mbedtls_md5_info );
2033#endif
2034#if defined(MBEDTLS_RIPEMD160_C)
2035 case PSA_ALG_RIPEMD160:
2036 return( &mbedtls_ripemd160_info );
2037#endif
2038#if defined(MBEDTLS_SHA1_C)
2039 case PSA_ALG_SHA_1:
2040 return( &mbedtls_sha1_info );
2041#endif
2042#if defined(MBEDTLS_SHA256_C)
2043 case PSA_ALG_SHA_224:
2044 return( &mbedtls_sha224_info );
2045 case PSA_ALG_SHA_256:
2046 return( &mbedtls_sha256_info );
2047#endif
2048#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002049#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002050 case PSA_ALG_SHA_384:
2051 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002052#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002053 case PSA_ALG_SHA_512:
2054 return( &mbedtls_sha512_info );
2055#endif
2056 default:
2057 return( NULL );
2058 }
2059}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002060#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002061
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002062psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2063{
2064 switch( operation->alg )
2065 {
Gilles Peskine81736312018-06-26 15:04:31 +02002066 case 0:
2067 /* The object has (apparently) been initialized but it is not
2068 * in use. It's ok to call abort on such an object, and there's
2069 * nothing to do. */
2070 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002071#if defined(MBEDTLS_MD2_C)
2072 case PSA_ALG_MD2:
2073 mbedtls_md2_free( &operation->ctx.md2 );
2074 break;
2075#endif
2076#if defined(MBEDTLS_MD4_C)
2077 case PSA_ALG_MD4:
2078 mbedtls_md4_free( &operation->ctx.md4 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_MD5_C)
2082 case PSA_ALG_MD5:
2083 mbedtls_md5_free( &operation->ctx.md5 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_RIPEMD160_C)
2087 case PSA_ALG_RIPEMD160:
2088 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2089 break;
2090#endif
2091#if defined(MBEDTLS_SHA1_C)
2092 case PSA_ALG_SHA_1:
2093 mbedtls_sha1_free( &operation->ctx.sha1 );
2094 break;
2095#endif
2096#if defined(MBEDTLS_SHA256_C)
2097 case PSA_ALG_SHA_224:
2098 case PSA_ALG_SHA_256:
2099 mbedtls_sha256_free( &operation->ctx.sha256 );
2100 break;
2101#endif
2102#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002103#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002104 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002105#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002106 case PSA_ALG_SHA_512:
2107 mbedtls_sha512_free( &operation->ctx.sha512 );
2108 break;
2109#endif
2110 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002111 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 }
2113 operation->alg = 0;
2114 return( PSA_SUCCESS );
2115}
2116
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002117psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002118 psa_algorithm_t alg )
2119{
Janos Follath24eed8d2019-11-22 13:21:35 +00002120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002121
2122 /* A context must be freshly initialized before it can be set up. */
2123 if( operation->alg != 0 )
2124 {
2125 return( PSA_ERROR_BAD_STATE );
2126 }
2127
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128 switch( alg )
2129 {
2130#if defined(MBEDTLS_MD2_C)
2131 case PSA_ALG_MD2:
2132 mbedtls_md2_init( &operation->ctx.md2 );
2133 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2134 break;
2135#endif
2136#if defined(MBEDTLS_MD4_C)
2137 case PSA_ALG_MD4:
2138 mbedtls_md4_init( &operation->ctx.md4 );
2139 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2140 break;
2141#endif
2142#if defined(MBEDTLS_MD5_C)
2143 case PSA_ALG_MD5:
2144 mbedtls_md5_init( &operation->ctx.md5 );
2145 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_RIPEMD160_C)
2149 case PSA_ALG_RIPEMD160:
2150 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2151 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_SHA1_C)
2155 case PSA_ALG_SHA_1:
2156 mbedtls_sha1_init( &operation->ctx.sha1 );
2157 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_SHA256_C)
2161 case PSA_ALG_SHA_224:
2162 mbedtls_sha256_init( &operation->ctx.sha256 );
2163 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2164 break;
2165 case PSA_ALG_SHA_256:
2166 mbedtls_sha256_init( &operation->ctx.sha256 );
2167 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002171#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172 case PSA_ALG_SHA_384:
2173 mbedtls_sha512_init( &operation->ctx.sha512 );
2174 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2175 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002176#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 case PSA_ALG_SHA_512:
2178 mbedtls_sha512_init( &operation->ctx.sha512 );
2179 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2180 break;
2181#endif
2182 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002183 return( PSA_ALG_IS_HASH( alg ) ?
2184 PSA_ERROR_NOT_SUPPORTED :
2185 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186 }
2187 if( ret == 0 )
2188 operation->alg = alg;
2189 else
2190 psa_hash_abort( operation );
2191 return( mbedtls_to_psa_error( ret ) );
2192}
2193
2194psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2195 const uint8_t *input,
2196 size_t input_length )
2197{
Janos Follath24eed8d2019-11-22 13:21:35 +00002198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002199
2200 /* Don't require hash implementations to behave correctly on a
2201 * zero-length input, which may have an invalid pointer. */
2202 if( input_length == 0 )
2203 return( PSA_SUCCESS );
2204
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205 switch( operation->alg )
2206 {
2207#if defined(MBEDTLS_MD2_C)
2208 case PSA_ALG_MD2:
2209 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2210 input, input_length );
2211 break;
2212#endif
2213#if defined(MBEDTLS_MD4_C)
2214 case PSA_ALG_MD4:
2215 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2216 input, input_length );
2217 break;
2218#endif
2219#if defined(MBEDTLS_MD5_C)
2220 case PSA_ALG_MD5:
2221 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2222 input, input_length );
2223 break;
2224#endif
2225#if defined(MBEDTLS_RIPEMD160_C)
2226 case PSA_ALG_RIPEMD160:
2227 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2228 input, input_length );
2229 break;
2230#endif
2231#if defined(MBEDTLS_SHA1_C)
2232 case PSA_ALG_SHA_1:
2233 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2234 input, input_length );
2235 break;
2236#endif
2237#if defined(MBEDTLS_SHA256_C)
2238 case PSA_ALG_SHA_224:
2239 case PSA_ALG_SHA_256:
2240 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2241 input, input_length );
2242 break;
2243#endif
2244#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002245#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002247#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248 case PSA_ALG_SHA_512:
2249 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2250 input, input_length );
2251 break;
2252#endif
2253 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002254 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002256
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 if( ret != 0 )
2258 psa_hash_abort( operation );
2259 return( mbedtls_to_psa_error( ret ) );
2260}
2261
2262psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2263 uint8_t *hash,
2264 size_t hash_size,
2265 size_t *hash_length )
2266{
itayzafrir40835d42018-08-02 13:14:17 +03002267 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002269 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270
2271 /* Fill the output buffer with something that isn't a valid hash
2272 * (barring an attack on the hash and deliberately-crafted input),
2273 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002274 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002275 /* If hash_size is 0 then hash may be NULL and then the
2276 * call to memset would have undefined behavior. */
2277 if( hash_size != 0 )
2278 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279
2280 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002281 {
2282 status = PSA_ERROR_BUFFER_TOO_SMALL;
2283 goto exit;
2284 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285
2286 switch( operation->alg )
2287 {
2288#if defined(MBEDTLS_MD2_C)
2289 case PSA_ALG_MD2:
2290 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2291 break;
2292#endif
2293#if defined(MBEDTLS_MD4_C)
2294 case PSA_ALG_MD4:
2295 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_MD5_C)
2299 case PSA_ALG_MD5:
2300 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_RIPEMD160_C)
2304 case PSA_ALG_RIPEMD160:
2305 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_SHA1_C)
2309 case PSA_ALG_SHA_1:
2310 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2311 break;
2312#endif
2313#if defined(MBEDTLS_SHA256_C)
2314 case PSA_ALG_SHA_224:
2315 case PSA_ALG_SHA_256:
2316 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2317 break;
2318#endif
2319#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002320#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002322#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323 case PSA_ALG_SHA_512:
2324 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2325 break;
2326#endif
2327 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002328 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 }
itayzafrir40835d42018-08-02 13:14:17 +03002330 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331
itayzafrir40835d42018-08-02 13:14:17 +03002332exit:
2333 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002335 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 return( psa_hash_abort( operation ) );
2337 }
2338 else
2339 {
2340 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002341 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 }
2343}
2344
Gilles Peskine2d277862018-06-18 15:41:12 +02002345psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2346 const uint8_t *hash,
2347 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348{
2349 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2350 size_t actual_hash_length;
2351 psa_status_t status = psa_hash_finish( operation,
2352 actual_hash, sizeof( actual_hash ),
2353 &actual_hash_length );
2354 if( status != PSA_SUCCESS )
2355 return( status );
2356 if( actual_hash_length != hash_length )
2357 return( PSA_ERROR_INVALID_SIGNATURE );
2358 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2359 return( PSA_ERROR_INVALID_SIGNATURE );
2360 return( PSA_SUCCESS );
2361}
2362
Gilles Peskine0a749c82019-11-28 19:33:58 +01002363psa_status_t psa_hash_compute( psa_algorithm_t alg,
2364 const uint8_t *input, size_t input_length,
2365 uint8_t *hash, size_t hash_size,
2366 size_t *hash_length )
2367{
2368 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2370
2371 *hash_length = hash_size;
2372 status = psa_hash_setup( &operation, alg );
2373 if( status != PSA_SUCCESS )
2374 goto exit;
2375 status = psa_hash_update( &operation, input, input_length );
2376 if( status != PSA_SUCCESS )
2377 goto exit;
2378 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2379 if( status != PSA_SUCCESS )
2380 goto exit;
2381
2382exit:
2383 if( status == PSA_SUCCESS )
2384 status = psa_hash_abort( &operation );
2385 else
2386 psa_hash_abort( &operation );
2387 return( status );
2388}
2389
2390psa_status_t psa_hash_compare( psa_algorithm_t alg,
2391 const uint8_t *input, size_t input_length,
2392 const uint8_t *hash, size_t hash_length )
2393{
2394 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2396
2397 status = psa_hash_setup( &operation, alg );
2398 if( status != PSA_SUCCESS )
2399 goto exit;
2400 status = psa_hash_update( &operation, input, input_length );
2401 if( status != PSA_SUCCESS )
2402 goto exit;
2403 status = psa_hash_verify( &operation, hash, hash_length );
2404 if( status != PSA_SUCCESS )
2405 goto exit;
2406
2407exit:
2408 if( status == PSA_SUCCESS )
2409 status = psa_hash_abort( &operation );
2410 else
2411 psa_hash_abort( &operation );
2412 return( status );
2413}
2414
Gilles Peskineeb35d782019-01-22 17:56:16 +01002415psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2416 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002417{
2418 if( target_operation->alg != 0 )
2419 return( PSA_ERROR_BAD_STATE );
2420
2421 switch( source_operation->alg )
2422 {
2423 case 0:
2424 return( PSA_ERROR_BAD_STATE );
2425#if defined(MBEDTLS_MD2_C)
2426 case PSA_ALG_MD2:
2427 mbedtls_md2_clone( &target_operation->ctx.md2,
2428 &source_operation->ctx.md2 );
2429 break;
2430#endif
2431#if defined(MBEDTLS_MD4_C)
2432 case PSA_ALG_MD4:
2433 mbedtls_md4_clone( &target_operation->ctx.md4,
2434 &source_operation->ctx.md4 );
2435 break;
2436#endif
2437#if defined(MBEDTLS_MD5_C)
2438 case PSA_ALG_MD5:
2439 mbedtls_md5_clone( &target_operation->ctx.md5,
2440 &source_operation->ctx.md5 );
2441 break;
2442#endif
2443#if defined(MBEDTLS_RIPEMD160_C)
2444 case PSA_ALG_RIPEMD160:
2445 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2446 &source_operation->ctx.ripemd160 );
2447 break;
2448#endif
2449#if defined(MBEDTLS_SHA1_C)
2450 case PSA_ALG_SHA_1:
2451 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2452 &source_operation->ctx.sha1 );
2453 break;
2454#endif
2455#if defined(MBEDTLS_SHA256_C)
2456 case PSA_ALG_SHA_224:
2457 case PSA_ALG_SHA_256:
2458 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2459 &source_operation->ctx.sha256 );
2460 break;
2461#endif
2462#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002463#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002464 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002465#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002466 case PSA_ALG_SHA_512:
2467 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2468 &source_operation->ctx.sha512 );
2469 break;
2470#endif
2471 default:
2472 return( PSA_ERROR_NOT_SUPPORTED );
2473 }
2474
2475 target_operation->alg = source_operation->alg;
2476 return( PSA_SUCCESS );
2477}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478
2479
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481/* MAC */
2482/****************************************************************/
2483
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002484static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 psa_algorithm_t alg,
2486 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002487 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002488 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002491 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002493 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002494 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002495
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2497 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002498 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002500 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002501 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502 mode = MBEDTLS_MODE_STREAM;
2503 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 case PSA_ALG_CTR:
2505 mode = MBEDTLS_MODE_CTR;
2506 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002507 case PSA_ALG_CFB:
2508 mode = MBEDTLS_MODE_CFB;
2509 break;
2510 case PSA_ALG_OFB:
2511 mode = MBEDTLS_MODE_OFB;
2512 break;
2513 case PSA_ALG_CBC_NO_PADDING:
2514 mode = MBEDTLS_MODE_CBC;
2515 break;
2516 case PSA_ALG_CBC_PKCS7:
2517 mode = MBEDTLS_MODE_CBC;
2518 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002519 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 mode = MBEDTLS_MODE_CCM;
2521 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002522 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523 mode = MBEDTLS_MODE_GCM;
2524 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002525 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2526 mode = MBEDTLS_MODE_CHACHAPOLY;
2527 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528 default:
2529 return( NULL );
2530 }
2531 }
2532 else if( alg == PSA_ALG_CMAC )
2533 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534 else
2535 return( NULL );
2536
2537 switch( key_type )
2538 {
2539 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002540 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541 break;
2542 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002543 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2544 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002546 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002548 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002549 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2550 * but two-key Triple-DES is functionally three-key Triple-DES
2551 * with K1=K3, so that's how we present it to mbedtls. */
2552 if( key_bits == 128 )
2553 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554 break;
2555 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002556 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 break;
2558 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002559 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002560 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002561 case PSA_KEY_TYPE_CHACHA20:
2562 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2563 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564 default:
2565 return( NULL );
2566 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002567 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002568 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569
Jaeden Amero23bbb752018-06-26 14:16:54 +01002570 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2571 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572}
2573
Gilles Peskinea05219c2018-11-16 16:02:56 +01002574#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002575static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002576{
Gilles Peskine2d277862018-06-18 15:41:12 +02002577 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002578 {
2579 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002580 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002581 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002582 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002583 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002584 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002585 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002586 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002587 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002588 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002589 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002590 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002591 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002592 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002593 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002594 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002595 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002596 return( 128 );
2597 default:
2598 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002599 }
2600}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002601#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002602
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002603/* Initialize the MAC operation structure. Once this function has been
2604 * called, psa_mac_abort can run and will do the right thing. */
2605static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2606 psa_algorithm_t alg )
2607{
2608 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2609
2610 operation->alg = alg;
2611 operation->key_set = 0;
2612 operation->iv_set = 0;
2613 operation->iv_required = 0;
2614 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002615 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002616
2617#if defined(MBEDTLS_CMAC_C)
2618 if( alg == PSA_ALG_CMAC )
2619 {
2620 operation->iv_required = 0;
2621 mbedtls_cipher_init( &operation->ctx.cmac );
2622 status = PSA_SUCCESS;
2623 }
2624 else
2625#endif /* MBEDTLS_CMAC_C */
2626#if defined(MBEDTLS_MD_C)
2627 if( PSA_ALG_IS_HMAC( operation->alg ) )
2628 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002629 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2630 operation->ctx.hmac.hash_ctx.alg = 0;
2631 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632 }
2633 else
2634#endif /* MBEDTLS_MD_C */
2635 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002636 if( ! PSA_ALG_IS_MAC( alg ) )
2637 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638 }
2639
2640 if( status != PSA_SUCCESS )
2641 memset( operation, 0, sizeof( *operation ) );
2642 return( status );
2643}
2644
Gilles Peskine01126fa2018-07-12 17:04:55 +02002645#if defined(MBEDTLS_MD_C)
2646static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2647{
Gilles Peskine3f108122018-12-07 18:14:53 +01002648 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002649 return( psa_hash_abort( &hmac->hash_ctx ) );
2650}
2651#endif /* MBEDTLS_MD_C */
2652
Gilles Peskine8c9def32018-02-08 10:02:12 +01002653psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2654{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002655 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002657 /* The object has (apparently) been initialized but it is not
2658 * in use. It's ok to call abort on such an object, and there's
2659 * nothing to do. */
2660 return( PSA_SUCCESS );
2661 }
2662 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002663#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002664 if( operation->alg == PSA_ALG_CMAC )
2665 {
2666 mbedtls_cipher_free( &operation->ctx.cmac );
2667 }
2668 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002671 if( PSA_ALG_IS_HMAC( operation->alg ) )
2672 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002673 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674 }
2675 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002677 {
2678 /* Sanity check (shouldn't happen: operation->alg should
2679 * always have been initialized to a valid value). */
2680 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 }
Moran Peker41deec42018-04-04 15:43:05 +03002682
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683 operation->alg = 0;
2684 operation->key_set = 0;
2685 operation->iv_set = 0;
2686 operation->iv_required = 0;
2687 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002688 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002689
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002691
2692bad_state:
2693 /* If abort is called on an uninitialized object, we can't trust
2694 * anything. Wipe the object in case it contains confidential data.
2695 * This may result in a memory leak if a pointer gets overwritten,
2696 * but it's too late to do anything about this. */
2697 memset( operation, 0, sizeof( *operation ) );
2698 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699}
2700
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002701#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002702static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002703 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002704 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002705 const mbedtls_cipher_info_t *cipher_info )
2706{
Janos Follath24eed8d2019-11-22 13:21:35 +00002707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708
2709 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710
2711 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2712 if( ret != 0 )
2713 return( ret );
2714
2715 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2716 slot->data.raw.data,
2717 key_bits );
2718 return( ret );
2719}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002720#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721
Gilles Peskine248051a2018-06-20 16:09:38 +02002722#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002723static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2724 const uint8_t *key,
2725 size_t key_length,
2726 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002728 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002730 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002731 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732 psa_status_t status;
2733
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002734 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2735 * overflow below. This should never trigger if the hash algorithm
2736 * is implemented correctly. */
2737 /* The size checks against the ipad and opad buffers cannot be written
2738 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2739 * because that triggers -Wlogical-op on GCC 7.3. */
2740 if( block_size > sizeof( ipad ) )
2741 return( PSA_ERROR_NOT_SUPPORTED );
2742 if( block_size > sizeof( hmac->opad ) )
2743 return( PSA_ERROR_NOT_SUPPORTED );
2744 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002745 return( PSA_ERROR_NOT_SUPPORTED );
2746
Gilles Peskined223b522018-06-11 18:12:58 +02002747 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002749 status = psa_hash_compute( hash_alg, key, key_length,
2750 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002752 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002753 }
Gilles Peskine96889972018-07-12 17:07:03 +02002754 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2755 * but it is permitted. It is common when HMAC is used in HKDF, for
2756 * example. Don't call `memcpy` in the 0-length because `key` could be
2757 * an invalid pointer which would make the behavior undefined. */
2758 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002759 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760
Gilles Peskined223b522018-06-11 18:12:58 +02002761 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2762 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002764 ipad[i] ^= 0x36;
2765 memset( ipad + key_length, 0x36, block_size - key_length );
2766
2767 /* Copy the key material from ipad to opad, flipping the requisite bits,
2768 * and filling the rest of opad with the requisite constant. */
2769 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2771 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772
Gilles Peskine01126fa2018-07-12 17:04:55 +02002773 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002774 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002775 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002776
Gilles Peskine01126fa2018-07-12 17:04:55 +02002777 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002778
2779cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002780 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002781
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002782 return( status );
2783}
Gilles Peskine248051a2018-06-20 16:09:38 +02002784#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785
Gilles Peskine89167cb2018-07-08 20:12:23 +02002786static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002787 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002788 psa_algorithm_t alg,
2789 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002792 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002794 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002795 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002796 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002797 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002799 /* A context must be freshly initialized before it can be set up. */
2800 if( operation->alg != 0 )
2801 {
2802 return( PSA_ERROR_BAD_STATE );
2803 }
2804
Gilles Peskined911eb72018-08-14 15:18:45 +02002805 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002806 if( status != PSA_SUCCESS )
2807 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002808 if( is_sign )
2809 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810
Gilles Peskine28f8f302019-07-24 13:30:31 +02002811 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002812 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002813 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002814 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002815
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002817 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 {
2819 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002820 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002821 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 if( cipher_info == NULL )
2824 {
2825 status = PSA_ERROR_NOT_SUPPORTED;
2826 goto exit;
2827 }
2828 operation->mac_size = cipher_info->block_size;
2829 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2830 status = mbedtls_to_psa_error( ret );
2831 }
2832 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002835 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002836 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002837 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002838 if( hash_alg == 0 )
2839 {
2840 status = PSA_ERROR_NOT_SUPPORTED;
2841 goto exit;
2842 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002843
2844 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2845 /* Sanity check. This shouldn't fail on a valid configuration. */
2846 if( operation->mac_size == 0 ||
2847 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2848 {
2849 status = PSA_ERROR_NOT_SUPPORTED;
2850 goto exit;
2851 }
2852
Gilles Peskine8e338702019-07-30 20:06:31 +02002853 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002854 {
2855 status = PSA_ERROR_INVALID_ARGUMENT;
2856 goto exit;
2857 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002858
Gilles Peskine01126fa2018-07-12 17:04:55 +02002859 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2860 slot->data.raw.data,
2861 slot->data.raw.bytes,
2862 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 }
2864 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002867 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002870
Gilles Peskined911eb72018-08-14 15:18:45 +02002871 if( truncated == 0 )
2872 {
2873 /* The "normal" case: untruncated algorithm. Nothing to do. */
2874 }
2875 else if( truncated < 4 )
2876 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002877 /* A very short MAC is too short for security since it can be
2878 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2879 * so we make this our minimum, even though 32 bits is still
2880 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002881 status = PSA_ERROR_NOT_SUPPORTED;
2882 }
2883 else if( truncated > operation->mac_size )
2884 {
2885 /* It's impossible to "truncate" to a larger length. */
2886 status = PSA_ERROR_INVALID_ARGUMENT;
2887 }
2888 else
2889 operation->mac_size = truncated;
2890
Gilles Peskinefbfac682018-07-08 20:51:54 +02002891exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002892 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002894 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002895 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002896 else
2897 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002898 operation->key_set = 1;
2899 }
2900 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901}
2902
Gilles Peskine89167cb2018-07-08 20:12:23 +02002903psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002904 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002905 psa_algorithm_t alg )
2906{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002907 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002908}
2909
2910psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002911 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912 psa_algorithm_t alg )
2913{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002914 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002915}
2916
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2918 const uint8_t *input,
2919 size_t input_length )
2920{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002921 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002923 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002924 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002925 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926 operation->has_input = 1;
2927
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002929 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002930 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002931 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2932 input, input_length );
2933 status = mbedtls_to_psa_error( ret );
2934 }
2935 else
2936#endif /* MBEDTLS_CMAC_C */
2937#if defined(MBEDTLS_MD_C)
2938 if( PSA_ALG_IS_HMAC( operation->alg ) )
2939 {
2940 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2941 input_length );
2942 }
2943 else
2944#endif /* MBEDTLS_MD_C */
2945 {
2946 /* This shouldn't happen if `operation` was initialized by
2947 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002948 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002949 }
2950
Gilles Peskinefbfac682018-07-08 20:51:54 +02002951 if( status != PSA_SUCCESS )
2952 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002953 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954}
2955
Gilles Peskine01126fa2018-07-12 17:04:55 +02002956#if defined(MBEDTLS_MD_C)
2957static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2958 uint8_t *mac,
2959 size_t mac_size )
2960{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002961 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002962 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2963 size_t hash_size = 0;
2964 size_t block_size = psa_get_hash_block_size( hash_alg );
2965 psa_status_t status;
2966
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2968 if( status != PSA_SUCCESS )
2969 return( status );
2970 /* From here on, tmp needs to be wiped. */
2971
2972 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2973 if( status != PSA_SUCCESS )
2974 goto exit;
2975
2976 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2977 if( status != PSA_SUCCESS )
2978 goto exit;
2979
2980 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2981 if( status != PSA_SUCCESS )
2982 goto exit;
2983
Gilles Peskined911eb72018-08-14 15:18:45 +02002984 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2985 if( status != PSA_SUCCESS )
2986 goto exit;
2987
2988 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002989
2990exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002991 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002992 return( status );
2993}
2994#endif /* MBEDTLS_MD_C */
2995
mohammad16036df908f2018-04-02 08:34:15 -07002996static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002997 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002998 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003000 if( ! operation->key_set )
3001 return( PSA_ERROR_BAD_STATE );
3002 if( operation->iv_required && ! operation->iv_set )
3003 return( PSA_ERROR_BAD_STATE );
3004
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005 if( mac_size < operation->mac_size )
3006 return( PSA_ERROR_BUFFER_TOO_SMALL );
3007
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003009 if( operation->alg == PSA_ALG_CMAC )
3010 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003011 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3012 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3013 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003014 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003015 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003016 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 else
3019#endif /* MBEDTLS_CMAC_C */
3020#if defined(MBEDTLS_MD_C)
3021 if( PSA_ALG_IS_HMAC( operation->alg ) )
3022 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003023 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003024 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 }
3026 else
3027#endif /* MBEDTLS_MD_C */
3028 {
3029 /* This shouldn't happen if `operation` was initialized by
3030 * a setup function. */
3031 return( PSA_ERROR_BAD_STATE );
3032 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033}
3034
Gilles Peskineacd4be32018-07-08 19:56:25 +02003035psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3036 uint8_t *mac,
3037 size_t mac_size,
3038 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003039{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003040 psa_status_t status;
3041
Jaeden Amero252ef282019-02-15 14:05:35 +00003042 if( operation->alg == 0 )
3043 {
3044 return( PSA_ERROR_BAD_STATE );
3045 }
3046
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003047 /* Fill the output buffer with something that isn't a valid mac
3048 * (barring an attack on the mac and deliberately-crafted input),
3049 * in case the caller doesn't check the return status properly. */
3050 *mac_length = mac_size;
3051 /* If mac_size is 0 then mac may be NULL and then the
3052 * call to memset would have undefined behavior. */
3053 if( mac_size != 0 )
3054 memset( mac, '!', mac_size );
3055
Gilles Peskine89167cb2018-07-08 20:12:23 +02003056 if( ! operation->is_sign )
3057 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003058 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003059 }
mohammad16036df908f2018-04-02 08:34:15 -07003060
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003061 status = psa_mac_finish_internal( operation, mac, mac_size );
3062
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003063 if( status == PSA_SUCCESS )
3064 {
3065 status = psa_mac_abort( operation );
3066 if( status == PSA_SUCCESS )
3067 *mac_length = operation->mac_size;
3068 else
3069 memset( mac, '!', mac_size );
3070 }
3071 else
3072 psa_mac_abort( operation );
3073 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003074}
3075
Gilles Peskineacd4be32018-07-08 19:56:25 +02003076psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3077 const uint8_t *mac,
3078 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003079{
Gilles Peskine828ed142018-06-18 23:25:51 +02003080 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003081 psa_status_t status;
3082
Jaeden Amero252ef282019-02-15 14:05:35 +00003083 if( operation->alg == 0 )
3084 {
3085 return( PSA_ERROR_BAD_STATE );
3086 }
3087
Gilles Peskine89167cb2018-07-08 20:12:23 +02003088 if( operation->is_sign )
3089 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003090 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003091 }
3092 if( operation->mac_size != mac_length )
3093 {
3094 status = PSA_ERROR_INVALID_SIGNATURE;
3095 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003096 }
mohammad16036df908f2018-04-02 08:34:15 -07003097
3098 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003099 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003100 if( status != PSA_SUCCESS )
3101 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003102
3103 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3104 status = PSA_ERROR_INVALID_SIGNATURE;
3105
3106cleanup:
3107 if( status == PSA_SUCCESS )
3108 status = psa_mac_abort( operation );
3109 else
3110 psa_mac_abort( operation );
3111
Gilles Peskine3f108122018-12-07 18:14:53 +01003112 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003113
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003114 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115}
3116
3117
Gilles Peskine20035e32018-02-03 22:44:14 +01003118
Gilles Peskine20035e32018-02-03 22:44:14 +01003119/****************************************************************/
3120/* Asymmetric cryptography */
3121/****************************************************************/
3122
Gilles Peskine2b450e32018-06-27 15:42:46 +02003123#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003124/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003125 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003126static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3127 size_t hash_length,
3128 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003129{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003130 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003131 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003132 *md_alg = mbedtls_md_get_type( md_info );
3133
3134 /* The Mbed TLS RSA module uses an unsigned int for hash length
3135 * parameters. Validate that it fits so that we don't risk an
3136 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003137#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003138 if( hash_length > UINT_MAX )
3139 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003140#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003141
3142#if defined(MBEDTLS_PKCS1_V15)
3143 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3144 * must be correct. */
3145 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3146 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003147 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003148 if( md_info == NULL )
3149 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003150 if( mbedtls_md_get_size( md_info ) != hash_length )
3151 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003152 }
3153#endif /* MBEDTLS_PKCS1_V15 */
3154
3155#if defined(MBEDTLS_PKCS1_V21)
3156 /* PSS requires a hash internally. */
3157 if( PSA_ALG_IS_RSA_PSS( alg ) )
3158 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003159 if( md_info == NULL )
3160 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003161 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003162#endif /* MBEDTLS_PKCS1_V21 */
3163
Gilles Peskine61b91d42018-06-08 16:09:36 +02003164 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003165}
3166
Gilles Peskine2b450e32018-06-27 15:42:46 +02003167static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3168 psa_algorithm_t alg,
3169 const uint8_t *hash,
3170 size_t hash_length,
3171 uint8_t *signature,
3172 size_t signature_size,
3173 size_t *signature_length )
3174{
3175 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003177 mbedtls_md_type_t md_alg;
3178
3179 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3180 if( status != PSA_SUCCESS )
3181 return( status );
3182
Gilles Peskine630a18a2018-06-29 17:49:35 +02003183 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003184 return( PSA_ERROR_BUFFER_TOO_SMALL );
3185
3186#if defined(MBEDTLS_PKCS1_V15)
3187 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3188 {
3189 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3190 MBEDTLS_MD_NONE );
3191 ret = mbedtls_rsa_pkcs1_sign( rsa,
3192 mbedtls_ctr_drbg_random,
3193 &global_data.ctr_drbg,
3194 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003195 md_alg,
3196 (unsigned int) hash_length,
3197 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003198 signature );
3199 }
3200 else
3201#endif /* MBEDTLS_PKCS1_V15 */
3202#if defined(MBEDTLS_PKCS1_V21)
3203 if( PSA_ALG_IS_RSA_PSS( alg ) )
3204 {
3205 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3206 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3207 mbedtls_ctr_drbg_random,
3208 &global_data.ctr_drbg,
3209 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003210 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003211 (unsigned int) hash_length,
3212 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003213 signature );
3214 }
3215 else
3216#endif /* MBEDTLS_PKCS1_V21 */
3217 {
3218 return( PSA_ERROR_INVALID_ARGUMENT );
3219 }
3220
3221 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003222 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003223 return( mbedtls_to_psa_error( ret ) );
3224}
3225
3226static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3227 psa_algorithm_t alg,
3228 const uint8_t *hash,
3229 size_t hash_length,
3230 const uint8_t *signature,
3231 size_t signature_length )
3232{
3233 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003235 mbedtls_md_type_t md_alg;
3236
3237 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3238 if( status != PSA_SUCCESS )
3239 return( status );
3240
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003241 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3242 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003243
3244#if defined(MBEDTLS_PKCS1_V15)
3245 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3246 {
3247 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3248 MBEDTLS_MD_NONE );
3249 ret = mbedtls_rsa_pkcs1_verify( rsa,
3250 mbedtls_ctr_drbg_random,
3251 &global_data.ctr_drbg,
3252 MBEDTLS_RSA_PUBLIC,
3253 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003254 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003255 hash,
3256 signature );
3257 }
3258 else
3259#endif /* MBEDTLS_PKCS1_V15 */
3260#if defined(MBEDTLS_PKCS1_V21)
3261 if( PSA_ALG_IS_RSA_PSS( alg ) )
3262 {
3263 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3264 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3265 mbedtls_ctr_drbg_random,
3266 &global_data.ctr_drbg,
3267 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003268 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003269 (unsigned int) hash_length,
3270 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003271 signature );
3272 }
3273 else
3274#endif /* MBEDTLS_PKCS1_V21 */
3275 {
3276 return( PSA_ERROR_INVALID_ARGUMENT );
3277 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003278
3279 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3280 * the rest of the signature is invalid". This has little use in
3281 * practice and PSA doesn't report this distinction. */
3282 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003284 return( mbedtls_to_psa_error( ret ) );
3285}
3286#endif /* MBEDTLS_RSA_C */
3287
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003288#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003289/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3290 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3291 * (even though these functions don't modify it). */
3292static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3293 psa_algorithm_t alg,
3294 const uint8_t *hash,
3295 size_t hash_length,
3296 uint8_t *signature,
3297 size_t signature_size,
3298 size_t *signature_length )
3299{
Janos Follath24eed8d2019-11-22 13:21:35 +00003300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003301 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003302 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003303 mbedtls_mpi_init( &r );
3304 mbedtls_mpi_init( &s );
3305
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003306 if( signature_size < 2 * curve_bytes )
3307 {
3308 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3309 goto cleanup;
3310 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003311
Gilles Peskinea05219c2018-11-16 16:02:56 +01003312#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003313 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3314 {
3315 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3316 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3317 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003318 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3319 &ecp->d, hash,
3320 hash_length, md_alg,
3321 mbedtls_ctr_drbg_random,
3322 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003323 }
3324 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003325#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003326 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003327 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003328 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3329 hash, hash_length,
3330 mbedtls_ctr_drbg_random,
3331 &global_data.ctr_drbg ) );
3332 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003333
3334 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3335 signature,
3336 curve_bytes ) );
3337 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3338 signature + curve_bytes,
3339 curve_bytes ) );
3340
3341cleanup:
3342 mbedtls_mpi_free( &r );
3343 mbedtls_mpi_free( &s );
3344 if( ret == 0 )
3345 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003346 return( mbedtls_to_psa_error( ret ) );
3347}
3348
3349static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3350 const uint8_t *hash,
3351 size_t hash_length,
3352 const uint8_t *signature,
3353 size_t signature_length )
3354{
Janos Follath24eed8d2019-11-22 13:21:35 +00003355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003356 mbedtls_mpi r, s;
3357 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3358 mbedtls_mpi_init( &r );
3359 mbedtls_mpi_init( &s );
3360
3361 if( signature_length != 2 * curve_bytes )
3362 return( PSA_ERROR_INVALID_SIGNATURE );
3363
3364 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3365 signature,
3366 curve_bytes ) );
3367 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3368 signature + curve_bytes,
3369 curve_bytes ) );
3370
3371 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3372 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003373
3374cleanup:
3375 mbedtls_mpi_free( &r );
3376 mbedtls_mpi_free( &s );
3377 return( mbedtls_to_psa_error( ret ) );
3378}
3379#endif /* MBEDTLS_ECDSA_C */
3380
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003381psa_status_t psa_sign_hash( psa_key_handle_t handle,
3382 psa_algorithm_t alg,
3383 const uint8_t *hash,
3384 size_t hash_length,
3385 uint8_t *signature,
3386 size_t signature_size,
3387 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003388{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003389 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003390 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003391#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3392 const psa_drv_se_t *drv;
3393 psa_drv_se_context_t *drv_context;
3394#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003395
3396 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003397 /* Immediately reject a zero-length signature buffer. This guarantees
3398 * that signature must be a valid pointer. (On the other hand, the hash
3399 * buffer can in principle be empty since it doesn't actually have
3400 * to be a hash.) */
3401 if( signature_size == 0 )
3402 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003403
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003404 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003405 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003407 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003408 {
3409 status = PSA_ERROR_INVALID_ARGUMENT;
3410 goto exit;
3411 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003412
Gilles Peskineedc64242019-08-07 21:05:07 +02003413#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3414 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3415 {
3416 if( drv->asymmetric == NULL ||
3417 drv->asymmetric->p_sign == NULL )
3418 {
3419 status = PSA_ERROR_NOT_SUPPORTED;
3420 goto exit;
3421 }
3422 status = drv->asymmetric->p_sign( drv_context,
3423 slot->data.se.slot_number,
3424 alg,
3425 hash, hash_length,
3426 signature, signature_size,
3427 signature_length );
3428 }
3429 else
3430#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003431#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003432 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003433 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003434 status = psa_rsa_sign( slot->data.rsa,
3435 alg,
3436 hash, hash_length,
3437 signature, signature_size,
3438 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003439 }
3440 else
3441#endif /* defined(MBEDTLS_RSA_C) */
3442#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003443 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003444 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003445#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003446 if(
3447#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3448 PSA_ALG_IS_ECDSA( alg )
3449#else
3450 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3451#endif
3452 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003453 status = psa_ecdsa_sign( slot->data.ecp,
3454 alg,
3455 hash, hash_length,
3456 signature, signature_size,
3457 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003458 else
3459#endif /* defined(MBEDTLS_ECDSA_C) */
3460 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003461 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003462 }
itayzafrir5c753392018-05-08 11:18:38 +03003463 }
3464 else
3465#endif /* defined(MBEDTLS_ECP_C) */
3466 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003467 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003468 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003469
3470exit:
3471 /* Fill the unused part of the output buffer (the whole buffer on error,
3472 * the trailing part on success) with something that isn't a valid mac
3473 * (barring an attack on the mac and deliberately-crafted input),
3474 * in case the caller doesn't check the return status properly. */
3475 if( status == PSA_SUCCESS )
3476 memset( signature + *signature_length, '!',
3477 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003478 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003479 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003480 /* If signature_size is 0 then we have nothing to do. We must not call
3481 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003482 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003483}
3484
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003485psa_status_t psa_verify_hash( psa_key_handle_t handle,
3486 psa_algorithm_t alg,
3487 const uint8_t *hash,
3488 size_t hash_length,
3489 const uint8_t *signature,
3490 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003491{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003492 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003493 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003494#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3495 const psa_drv_se_t *drv;
3496 psa_drv_se_context_t *drv_context;
3497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003498
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003499 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003500 if( status != PSA_SUCCESS )
3501 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003502
Gilles Peskineedc64242019-08-07 21:05:07 +02003503#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3504 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3505 {
3506 if( drv->asymmetric == NULL ||
3507 drv->asymmetric->p_verify == NULL )
3508 return( PSA_ERROR_NOT_SUPPORTED );
3509 return( drv->asymmetric->p_verify( drv_context,
3510 slot->data.se.slot_number,
3511 alg,
3512 hash, hash_length,
3513 signature, signature_length ) );
3514 }
3515 else
3516#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003517#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003518 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003520 return( psa_rsa_verify( slot->data.rsa,
3521 alg,
3522 hash, hash_length,
3523 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524 }
3525 else
3526#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003527#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003528 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003529 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003530#if defined(MBEDTLS_ECDSA_C)
3531 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003532 return( psa_ecdsa_verify( slot->data.ecp,
3533 hash, hash_length,
3534 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003535 else
3536#endif /* defined(MBEDTLS_ECDSA_C) */
3537 {
3538 return( PSA_ERROR_INVALID_ARGUMENT );
3539 }
itayzafrir5c753392018-05-08 11:18:38 +03003540 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003541 else
3542#endif /* defined(MBEDTLS_ECP_C) */
3543 {
3544 return( PSA_ERROR_NOT_SUPPORTED );
3545 }
3546}
3547
Gilles Peskine072ac562018-06-30 00:21:29 +02003548#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3549static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3550 mbedtls_rsa_context *rsa )
3551{
3552 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3553 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3554 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3555 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3556}
3557#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3558
Gilles Peskinec5487a82018-12-03 18:08:14 +01003559psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003560 psa_algorithm_t alg,
3561 const uint8_t *input,
3562 size_t input_length,
3563 const uint8_t *salt,
3564 size_t salt_length,
3565 uint8_t *output,
3566 size_t output_size,
3567 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003569 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003570 psa_status_t status;
3571
Darryl Green5cc689a2018-07-24 15:34:10 +01003572 (void) input;
3573 (void) input_length;
3574 (void) salt;
3575 (void) output;
3576 (void) output_size;
3577
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003578 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003580 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3581 return( PSA_ERROR_INVALID_ARGUMENT );
3582
Gilles Peskine28f8f302019-07-24 13:30:31 +02003583 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003584 if( status != PSA_SUCCESS )
3585 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003586 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3587 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
3590#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003591 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592 {
3593 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003595 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003596 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003598 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003600 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3601 mbedtls_ctr_drbg_random,
3602 &global_data.ctr_drbg,
3603 MBEDTLS_RSA_PUBLIC,
3604 input_length,
3605 input,
3606 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607 }
3608 else
3609#endif /* MBEDTLS_PKCS1_V15 */
3610#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003611 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003613 psa_rsa_oaep_set_padding_mode( alg, rsa );
3614 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3615 mbedtls_ctr_drbg_random,
3616 &global_data.ctr_drbg,
3617 MBEDTLS_RSA_PUBLIC,
3618 salt, salt_length,
3619 input_length,
3620 input,
3621 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003622 }
3623 else
3624#endif /* MBEDTLS_PKCS1_V21 */
3625 {
3626 return( PSA_ERROR_INVALID_ARGUMENT );
3627 }
3628 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003629 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630 return( mbedtls_to_psa_error( ret ) );
3631 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003633#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 {
3635 return( PSA_ERROR_NOT_SUPPORTED );
3636 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003637}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638
Gilles Peskinec5487a82018-12-03 18:08:14 +01003639psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003640 psa_algorithm_t alg,
3641 const uint8_t *input,
3642 size_t input_length,
3643 const uint8_t *salt,
3644 size_t salt_length,
3645 uint8_t *output,
3646 size_t output_size,
3647 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003649 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003650 psa_status_t status;
3651
Darryl Green5cc689a2018-07-24 15:34:10 +01003652 (void) input;
3653 (void) input_length;
3654 (void) salt;
3655 (void) output;
3656 (void) output_size;
3657
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003658 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003660 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3661 return( PSA_ERROR_INVALID_ARGUMENT );
3662
Gilles Peskine28f8f302019-07-24 13:30:31 +02003663 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003664 if( status != PSA_SUCCESS )
3665 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003666 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667 return( PSA_ERROR_INVALID_ARGUMENT );
3668
3669#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003670 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671 {
3672 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674
Gilles Peskine630a18a2018-06-29 17:49:35 +02003675 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676 return( PSA_ERROR_INVALID_ARGUMENT );
3677
3678#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003679 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003681 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3682 mbedtls_ctr_drbg_random,
3683 &global_data.ctr_drbg,
3684 MBEDTLS_RSA_PRIVATE,
3685 output_length,
3686 input,
3687 output,
3688 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689 }
3690 else
3691#endif /* MBEDTLS_PKCS1_V15 */
3692#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003693 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003694 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003695 psa_rsa_oaep_set_padding_mode( alg, rsa );
3696 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3697 mbedtls_ctr_drbg_random,
3698 &global_data.ctr_drbg,
3699 MBEDTLS_RSA_PRIVATE,
3700 salt, salt_length,
3701 output_length,
3702 input,
3703 output,
3704 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705 }
3706 else
3707#endif /* MBEDTLS_PKCS1_V21 */
3708 {
3709 return( PSA_ERROR_INVALID_ARGUMENT );
3710 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003711
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712 return( mbedtls_to_psa_error( ret ) );
3713 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003714 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003715#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003716 {
3717 return( PSA_ERROR_NOT_SUPPORTED );
3718 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003719}
Gilles Peskine20035e32018-02-03 22:44:14 +01003720
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003721
3722
mohammad1603503973b2018-03-12 15:59:30 +02003723/****************************************************************/
3724/* Symmetric cryptography */
3725/****************************************************************/
3726
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003727/* Initialize the cipher operation structure. Once this function has been
3728 * called, psa_cipher_abort can run and will do the right thing. */
3729static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3730 psa_algorithm_t alg )
3731{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003732 if( ! PSA_ALG_IS_CIPHER( alg ) )
3733 {
3734 memset( operation, 0, sizeof( *operation ) );
3735 return( PSA_ERROR_INVALID_ARGUMENT );
3736 }
3737
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003738 operation->alg = alg;
3739 operation->key_set = 0;
3740 operation->iv_set = 0;
3741 operation->iv_required = 1;
3742 operation->iv_size = 0;
3743 operation->block_size = 0;
3744 mbedtls_cipher_init( &operation->ctx.cipher );
3745 return( PSA_SUCCESS );
3746}
3747
Gilles Peskinee553c652018-06-04 16:22:46 +02003748static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003749 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003750 psa_algorithm_t alg,
3751 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003752{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003753 int ret = 0;
3754 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003755 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003756 size_t key_bits;
3757 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003758 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3759 PSA_KEY_USAGE_ENCRYPT :
3760 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003761
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003762 /* A context must be freshly initialized before it can be set up. */
3763 if( operation->alg != 0 )
3764 {
3765 return( PSA_ERROR_BAD_STATE );
3766 }
3767
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003768 status = psa_cipher_init( operation, alg );
3769 if( status != PSA_SUCCESS )
3770 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003771
Gilles Peskine28f8f302019-07-24 13:30:31 +02003772 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003773 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003774 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003775 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003776
Gilles Peskine8e338702019-07-30 20:06:31 +02003777 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003778 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003779 {
3780 status = PSA_ERROR_NOT_SUPPORTED;
3781 goto exit;
3782 }
mohammad1603503973b2018-03-12 15:59:30 +02003783
mohammad1603503973b2018-03-12 15:59:30 +02003784 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003785 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003786 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003787
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003788#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003789 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003790 {
3791 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003792 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003793 memcpy( keys, slot->data.raw.data, 16 );
3794 memcpy( keys + 16, slot->data.raw.data, 8 );
3795 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3796 keys,
3797 192, cipher_operation );
3798 }
3799 else
3800#endif
3801 {
3802 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3803 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003804 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003805 }
Moran Peker41deec42018-04-04 15:43:05 +03003806 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003807 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003808
mohammad16038481e742018-03-18 13:57:31 +02003809#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003810 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003811 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003812 case PSA_ALG_CBC_NO_PADDING:
3813 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3814 MBEDTLS_PADDING_NONE );
3815 break;
3816 case PSA_ALG_CBC_PKCS7:
3817 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3818 MBEDTLS_PADDING_PKCS7 );
3819 break;
3820 default:
3821 /* The algorithm doesn't involve padding. */
3822 ret = 0;
3823 break;
3824 }
3825 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003826 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003827#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3828
mohammad1603503973b2018-03-12 15:59:30 +02003829 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003830 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003831 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003832 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003833 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003834 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003835 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003836#if defined(MBEDTLS_CHACHA20_C)
3837 else
3838 if( alg == PSA_ALG_CHACHA20 )
3839 operation->iv_size = 12;
3840#endif
mohammad1603503973b2018-03-12 15:59:30 +02003841
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003842exit:
3843 if( status == 0 )
3844 status = mbedtls_to_psa_error( ret );
3845 if( status != 0 )
3846 psa_cipher_abort( operation );
3847 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003848}
3849
Gilles Peskinefe119512018-07-08 21:39:34 +02003850psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003851 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003852 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003853{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003854 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003855}
3856
Gilles Peskinefe119512018-07-08 21:39:34 +02003857psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003858 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003859 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003860{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003861 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003862}
3863
Gilles Peskinefe119512018-07-08 21:39:34 +02003864psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003865 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003866 size_t iv_size,
3867 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003868{
itayzafrir534bd7c2018-08-02 13:56:32 +03003869 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003871 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003873 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003874 }
Moran Peker41deec42018-04-04 15:43:05 +03003875 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003876 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003877 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003878 goto exit;
3879 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003880 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3881 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003882 if( ret != 0 )
3883 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003884 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003885 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003886 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003887
mohammad16038481e742018-03-18 13:57:31 +02003888 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003890
Moran Peker395db872018-05-31 14:07:14 +03003891exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003893 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003894 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003895}
3896
Gilles Peskinefe119512018-07-08 21:39:34 +02003897psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003898 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003899 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003900{
itayzafrir534bd7c2018-08-02 13:56:32 +03003901 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003903 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003905 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003906 }
Moran Pekera28258c2018-05-29 16:25:04 +03003907 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003908 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003909 status = PSA_ERROR_INVALID_ARGUMENT;
3910 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003911 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3913 status = mbedtls_to_psa_error( ret );
3914exit:
3915 if( status == PSA_SUCCESS )
3916 operation->iv_set = 1;
3917 else
mohammad1603503973b2018-03-12 15:59:30 +02003918 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003919 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003920}
3921
Gilles Peskinee553c652018-06-04 16:22:46 +02003922psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3923 const uint8_t *input,
3924 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003925 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003926 size_t output_size,
3927 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003928{
itayzafrir534bd7c2018-08-02 13:56:32 +03003929 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003931 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003932
3933 if( operation->alg == 0 )
3934 {
3935 return( PSA_ERROR_BAD_STATE );
3936 }
3937
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003938 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003939 {
3940 /* Take the unprocessed partial block left over from previous
3941 * update calls, if any, plus the input to this call. Remove
3942 * the last partial block, if any. You get the data that will be
3943 * output in this call. */
3944 expected_output_size =
3945 ( operation->ctx.cipher.unprocessed_len + input_length )
3946 / operation->block_size * operation->block_size;
3947 }
3948 else
3949 {
3950 expected_output_size = input_length;
3951 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003952
Gilles Peskine89d789c2018-06-04 17:17:16 +02003953 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003954 {
3955 status = PSA_ERROR_BUFFER_TOO_SMALL;
3956 goto exit;
3957 }
mohammad160382759612018-03-12 18:16:40 +02003958
mohammad1603503973b2018-03-12 15:59:30 +02003959 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003960 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003961 status = mbedtls_to_psa_error( ret );
3962exit:
3963 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003964 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003965 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003966}
3967
Gilles Peskinee553c652018-06-04 16:22:46 +02003968psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3969 uint8_t *output,
3970 size_t output_size,
3971 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003972{
David Saadab4ecc272019-02-14 13:48:10 +02003973 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003974 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003975 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003976
mohammad1603503973b2018-03-12 15:59:30 +02003977 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003978 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003979 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003980 }
3981 if( operation->iv_required && ! operation->iv_set )
3982 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003983 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003984 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003985
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003986 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003987 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3988 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003989 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003990 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003991 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003992 }
3993
Janos Follath315b51c2018-07-09 16:04:51 +01003994 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3995 temp_output_buffer,
3996 output_length );
3997 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003998 {
Janos Follath315b51c2018-07-09 16:04:51 +01003999 status = mbedtls_to_psa_error( cipher_ret );
4000 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004001 }
Janos Follath315b51c2018-07-09 16:04:51 +01004002
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004003 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004004 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004005 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004006 memcpy( output, temp_output_buffer, *output_length );
4007 else
4008 {
Janos Follath315b51c2018-07-09 16:04:51 +01004009 status = PSA_ERROR_BUFFER_TOO_SMALL;
4010 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004011 }
mohammad1603503973b2018-03-12 15:59:30 +02004012
Gilles Peskine3f108122018-12-07 18:14:53 +01004013 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004014 status = psa_cipher_abort( operation );
4015
4016 return( status );
4017
4018error:
4019
4020 *output_length = 0;
4021
Gilles Peskine3f108122018-12-07 18:14:53 +01004022 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004023 (void) psa_cipher_abort( operation );
4024
4025 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004026}
4027
Gilles Peskinee553c652018-06-04 16:22:46 +02004028psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4029{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004030 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004031 {
4032 /* The object has (apparently) been initialized but it is not
4033 * in use. It's ok to call abort on such an object, and there's
4034 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004035 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004036 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004037
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004038 /* Sanity check (shouldn't happen: operation->alg should
4039 * always have been initialized to a valid value). */
4040 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4041 return( PSA_ERROR_BAD_STATE );
4042
mohammad1603503973b2018-03-12 15:59:30 +02004043 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004044
Moran Peker41deec42018-04-04 15:43:05 +03004045 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004046 operation->key_set = 0;
4047 operation->iv_set = 0;
4048 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004049 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004050 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004051
Moran Peker395db872018-05-31 14:07:14 +03004052 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004053}
4054
Gilles Peskinea0655c32018-04-30 17:06:50 +02004055
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004056
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004057
mohammad16035955c982018-04-26 00:53:03 +03004058/****************************************************************/
4059/* AEAD */
4060/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004061
Gilles Peskineedf9a652018-08-17 18:11:56 +02004062typedef struct
4063{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004064 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004065 const mbedtls_cipher_info_t *cipher_info;
4066 union
4067 {
4068#if defined(MBEDTLS_CCM_C)
4069 mbedtls_ccm_context ccm;
4070#endif /* MBEDTLS_CCM_C */
4071#if defined(MBEDTLS_GCM_C)
4072 mbedtls_gcm_context gcm;
4073#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004074#if defined(MBEDTLS_CHACHAPOLY_C)
4075 mbedtls_chachapoly_context chachapoly;
4076#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004077 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004078 psa_algorithm_t core_alg;
4079 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004080 uint8_t tag_length;
4081} aead_operation_t;
4082
Gilles Peskine30a9e412019-01-14 18:36:12 +01004083static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004084{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004085 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004086 {
4087#if defined(MBEDTLS_CCM_C)
4088 case PSA_ALG_CCM:
4089 mbedtls_ccm_free( &operation->ctx.ccm );
4090 break;
4091#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004092#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004093 case PSA_ALG_GCM:
4094 mbedtls_gcm_free( &operation->ctx.gcm );
4095 break;
4096#endif /* MBEDTLS_GCM_C */
4097 }
4098}
4099
4100static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004101 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004102 psa_key_usage_t usage,
4103 psa_algorithm_t alg )
4104{
4105 psa_status_t status;
4106 size_t key_bits;
4107 mbedtls_cipher_id_t cipher_id;
4108
Gilles Peskine28f8f302019-07-24 13:30:31 +02004109 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004110 if( status != PSA_SUCCESS )
4111 return( status );
4112
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004113 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114
4115 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004116 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 &cipher_id );
4118 if( operation->cipher_info == NULL )
4119 return( PSA_ERROR_NOT_SUPPORTED );
4120
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004121 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 {
4123#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004124 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4125 operation->core_alg = PSA_ALG_CCM;
4126 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004127 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4128 * The call to mbedtls_ccm_encrypt_and_tag or
4129 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004130 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004131 return( PSA_ERROR_INVALID_ARGUMENT );
4132 mbedtls_ccm_init( &operation->ctx.ccm );
4133 status = mbedtls_to_psa_error(
4134 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4135 operation->slot->data.raw.data,
4136 (unsigned int) key_bits ) );
4137 if( status != 0 )
4138 goto cleanup;
4139 break;
4140#endif /* MBEDTLS_CCM_C */
4141
4142#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004143 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4144 operation->core_alg = PSA_ALG_GCM;
4145 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004146 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4147 * The call to mbedtls_gcm_crypt_and_tag or
4148 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004149 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004150 return( PSA_ERROR_INVALID_ARGUMENT );
4151 mbedtls_gcm_init( &operation->ctx.gcm );
4152 status = mbedtls_to_psa_error(
4153 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4154 operation->slot->data.raw.data,
4155 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004156 if( status != 0 )
4157 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004158 break;
4159#endif /* MBEDTLS_GCM_C */
4160
Gilles Peskine26869f22019-05-06 15:25:00 +02004161#if defined(MBEDTLS_CHACHAPOLY_C)
4162 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4163 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4164 operation->full_tag_length = 16;
4165 /* We only support the default tag length. */
4166 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4167 return( PSA_ERROR_NOT_SUPPORTED );
4168 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4169 status = mbedtls_to_psa_error(
4170 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4171 operation->slot->data.raw.data ) );
4172 if( status != 0 )
4173 goto cleanup;
4174 break;
4175#endif /* MBEDTLS_CHACHAPOLY_C */
4176
Gilles Peskineedf9a652018-08-17 18:11:56 +02004177 default:
4178 return( PSA_ERROR_NOT_SUPPORTED );
4179 }
4180
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004181 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4182 {
4183 status = PSA_ERROR_INVALID_ARGUMENT;
4184 goto cleanup;
4185 }
4186 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004187
Gilles Peskineedf9a652018-08-17 18:11:56 +02004188 return( PSA_SUCCESS );
4189
4190cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004191 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004192 return( status );
4193}
4194
Gilles Peskinec5487a82018-12-03 18:08:14 +01004195psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004196 psa_algorithm_t alg,
4197 const uint8_t *nonce,
4198 size_t nonce_length,
4199 const uint8_t *additional_data,
4200 size_t additional_data_length,
4201 const uint8_t *plaintext,
4202 size_t plaintext_length,
4203 uint8_t *ciphertext,
4204 size_t ciphertext_size,
4205 size_t *ciphertext_length )
4206{
mohammad16035955c982018-04-26 00:53:03 +03004207 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004209 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004210
mohammad1603f08a5502018-06-03 15:05:47 +03004211 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004212
Gilles Peskinec5487a82018-12-03 18:08:14 +01004213 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004214 if( status != PSA_SUCCESS )
4215 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004216
Gilles Peskineedf9a652018-08-17 18:11:56 +02004217 /* For all currently supported modes, the tag is at the end of the
4218 * ciphertext. */
4219 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4220 {
4221 status = PSA_ERROR_BUFFER_TOO_SMALL;
4222 goto exit;
4223 }
4224 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004225
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004226#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004227 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004228 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004229 status = mbedtls_to_psa_error(
4230 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4231 MBEDTLS_GCM_ENCRYPT,
4232 plaintext_length,
4233 nonce, nonce_length,
4234 additional_data, additional_data_length,
4235 plaintext, ciphertext,
4236 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004237 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004238 else
4239#endif /* MBEDTLS_GCM_C */
4240#if defined(MBEDTLS_CCM_C)
4241 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004242 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004243 status = mbedtls_to_psa_error(
4244 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4245 plaintext_length,
4246 nonce, nonce_length,
4247 additional_data,
4248 additional_data_length,
4249 plaintext, ciphertext,
4250 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004251 }
mohammad16035c8845f2018-05-09 05:40:09 -07004252 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004253#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004254#if defined(MBEDTLS_CHACHAPOLY_C)
4255 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4256 {
4257 if( nonce_length != 12 || operation.tag_length != 16 )
4258 {
4259 status = PSA_ERROR_NOT_SUPPORTED;
4260 goto exit;
4261 }
4262 status = mbedtls_to_psa_error(
4263 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4264 plaintext_length,
4265 nonce,
4266 additional_data,
4267 additional_data_length,
4268 plaintext,
4269 ciphertext,
4270 tag ) );
4271 }
4272 else
4273#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004274 {
mohammad1603554faad2018-06-03 15:07:38 +03004275 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004276 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004277
Gilles Peskineedf9a652018-08-17 18:11:56 +02004278 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4279 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004280
Gilles Peskineedf9a652018-08-17 18:11:56 +02004281exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004282 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004283 if( status == PSA_SUCCESS )
4284 *ciphertext_length = plaintext_length + operation.tag_length;
4285 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004286}
4287
Gilles Peskineee652a32018-06-01 19:23:52 +02004288/* Locate the tag in a ciphertext buffer containing the encrypted data
4289 * followed by the tag. Return the length of the part preceding the tag in
4290 * *plaintext_length. This is the size of the plaintext in modes where
4291 * the encrypted data has the same size as the plaintext, such as
4292 * CCM and GCM. */
4293static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4294 const uint8_t *ciphertext,
4295 size_t ciphertext_length,
4296 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004297 const uint8_t **p_tag )
4298{
4299 size_t payload_length;
4300 if( tag_length > ciphertext_length )
4301 return( PSA_ERROR_INVALID_ARGUMENT );
4302 payload_length = ciphertext_length - tag_length;
4303 if( payload_length > plaintext_size )
4304 return( PSA_ERROR_BUFFER_TOO_SMALL );
4305 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004306 return( PSA_SUCCESS );
4307}
4308
Gilles Peskinec5487a82018-12-03 18:08:14 +01004309psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004310 psa_algorithm_t alg,
4311 const uint8_t *nonce,
4312 size_t nonce_length,
4313 const uint8_t *additional_data,
4314 size_t additional_data_length,
4315 const uint8_t *ciphertext,
4316 size_t ciphertext_length,
4317 uint8_t *plaintext,
4318 size_t plaintext_size,
4319 size_t *plaintext_length )
4320{
mohammad16035955c982018-04-26 00:53:03 +03004321 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004322 aead_operation_t operation;
4323 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004324
Gilles Peskineee652a32018-06-01 19:23:52 +02004325 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004326
Gilles Peskinec5487a82018-12-03 18:08:14 +01004327 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004328 if( status != PSA_SUCCESS )
4329 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004330
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004331 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4332 ciphertext, ciphertext_length,
4333 plaintext_size, &tag );
4334 if( status != PSA_SUCCESS )
4335 goto exit;
4336
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004337#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004338 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004339 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004340 status = mbedtls_to_psa_error(
4341 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4342 ciphertext_length - operation.tag_length,
4343 nonce, nonce_length,
4344 additional_data,
4345 additional_data_length,
4346 tag, operation.tag_length,
4347 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004348 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004349 else
4350#endif /* MBEDTLS_GCM_C */
4351#if defined(MBEDTLS_CCM_C)
4352 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004353 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004354 status = mbedtls_to_psa_error(
4355 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4356 ciphertext_length - operation.tag_length,
4357 nonce, nonce_length,
4358 additional_data,
4359 additional_data_length,
4360 ciphertext, plaintext,
4361 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004362 }
mohammad160339574652018-06-01 04:39:53 -07004363 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004364#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004365#if defined(MBEDTLS_CHACHAPOLY_C)
4366 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4367 {
4368 if( nonce_length != 12 || operation.tag_length != 16 )
4369 {
4370 status = PSA_ERROR_NOT_SUPPORTED;
4371 goto exit;
4372 }
4373 status = mbedtls_to_psa_error(
4374 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4375 ciphertext_length - operation.tag_length,
4376 nonce,
4377 additional_data,
4378 additional_data_length,
4379 tag,
4380 ciphertext,
4381 plaintext ) );
4382 }
4383 else
4384#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004385 {
mohammad1603554faad2018-06-03 15:07:38 +03004386 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004387 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004388
Gilles Peskineedf9a652018-08-17 18:11:56 +02004389 if( status != PSA_SUCCESS && plaintext_size != 0 )
4390 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004391
Gilles Peskineedf9a652018-08-17 18:11:56 +02004392exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004393 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004394 if( status == PSA_SUCCESS )
4395 *plaintext_length = ciphertext_length - operation.tag_length;
4396 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004397}
4398
Gilles Peskinea0655c32018-04-30 17:06:50 +02004399
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004400
Gilles Peskine20035e32018-02-03 22:44:14 +01004401/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004402/* Generators */
4403/****************************************************************/
4404
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004405#define HKDF_STATE_INIT 0 /* no input yet */
4406#define HKDF_STATE_STARTED 1 /* got salt */
4407#define HKDF_STATE_KEYED 2 /* got key */
4408#define HKDF_STATE_OUTPUT 3 /* output started */
4409
Gilles Peskinecbe66502019-05-16 16:59:18 +02004410static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004412{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004413 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4414 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004415 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004417}
4418
4419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004421{
4422 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004424 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004425 {
4426 /* The object has (apparently) been initialized but it is not
4427 * in use. It's ok to call abort on such an object, and there's
4428 * nothing to do. */
4429 }
4430 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004433 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 mbedtls_free( operation->ctx.hkdf.info );
4435 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004436 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004439 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004440 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004441 if( operation->ctx.tls12_prf.seed != NULL )
4442 {
4443 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4444 operation->ctx.tls12_prf.seed_length );
4445 mbedtls_free( operation->ctx.tls12_prf.seed );
4446 }
4447
4448 if( operation->ctx.tls12_prf.label != NULL )
4449 {
4450 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4451 operation->ctx.tls12_prf.label_length );
4452 mbedtls_free( operation->ctx.tls12_prf.label );
4453 }
4454
4455 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4456
4457 /* We leave the fields Ai and output_block to be erased safely by the
4458 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004459 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004460 else
4461#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004462 {
4463 status = PSA_ERROR_BAD_STATE;
4464 }
Janos Follath083036a2019-06-11 10:22:26 +01004465 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004466 return( status );
4467}
4468
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004470 size_t *capacity)
4471{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004473 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004475 return PSA_ERROR_BAD_STATE;
4476 }
4477
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004478 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004479 return( PSA_SUCCESS );
4480}
4481
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004483 size_t capacity )
4484{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004486 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004488 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004490 return( PSA_SUCCESS );
4491}
4492
Gilles Peskinebef7f142018-07-12 17:22:21 +02004493#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004495 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004496static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004497 psa_algorithm_t hash_alg,
4498 uint8_t *output,
4499 size_t output_length )
4500{
4501 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4502 psa_status_t status;
4503
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004504 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4505 return( PSA_ERROR_BAD_STATE );
4506 hkdf->state = HKDF_STATE_OUTPUT;
4507
Gilles Peskinebef7f142018-07-12 17:22:21 +02004508 while( output_length != 0 )
4509 {
4510 /* Copy what remains of the current block */
4511 uint8_t n = hash_length - hkdf->offset_in_block;
4512 if( n > output_length )
4513 n = (uint8_t) output_length;
4514 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4515 output += n;
4516 output_length -= n;
4517 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004518 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004519 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004520 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004521 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004523 * object was corrupted or if this function is called directly
4524 * inside the library. */
4525 if( hkdf->block_number == 0xff )
4526 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004527
4528 /* We need a new block */
4529 ++hkdf->block_number;
4530 hkdf->offset_in_block = 0;
4531 status = psa_hmac_setup_internal( &hkdf->hmac,
4532 hkdf->prk, hash_length,
4533 hash_alg );
4534 if( status != PSA_SUCCESS )
4535 return( status );
4536 if( hkdf->block_number != 1 )
4537 {
4538 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4539 hkdf->output_block,
4540 hash_length );
4541 if( status != PSA_SUCCESS )
4542 return( status );
4543 }
4544 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4545 hkdf->info,
4546 hkdf->info_length );
4547 if( status != PSA_SUCCESS )
4548 return( status );
4549 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4550 &hkdf->block_number, 1 );
4551 if( status != PSA_SUCCESS )
4552 return( status );
4553 status = psa_hmac_finish_internal( &hkdf->hmac,
4554 hkdf->output_block,
4555 sizeof( hkdf->output_block ) );
4556 if( status != PSA_SUCCESS )
4557 return( status );
4558 }
4559
4560 return( PSA_SUCCESS );
4561}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004562
Janos Follath7742fee2019-06-17 12:58:10 +01004563static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4564 psa_tls12_prf_key_derivation_t *tls12_prf,
4565 psa_algorithm_t alg )
4566{
4567 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4568 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004569 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4570 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004571
Janos Follath7742fee2019-06-17 12:58:10 +01004572 /* We can't be wanting more output after block 0xff, otherwise
4573 * the capacity check in psa_key_derivation_output_bytes() would have
4574 * prevented this call. It could happen only if the operation
4575 * object was corrupted or if this function is called directly
4576 * inside the library. */
4577 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004578 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004579
4580 /* We need a new block */
4581 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004582 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004583
4584 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4585 *
4586 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4587 *
4588 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4589 * HMAC_hash(secret, A(2) + seed) +
4590 * HMAC_hash(secret, A(3) + seed) + ...
4591 *
4592 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004593 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004594 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004595 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004596 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004597 * is currently extracted as `output_block` and where i is
4598 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004599 */
4600
Janos Follathea29bfb2019-06-19 12:21:20 +01004601 /* Save the hash context before using it, to preserve the hash state with
4602 * only the inner padding in it. We need this, because inner padding depends
4603 * on the key (secret in the RFC's terminology). */
4604 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4605 if( status != PSA_SUCCESS )
4606 goto cleanup;
4607
4608 /* Calculate A(i) where i = tls12_prf->block_number. */
4609 if( tls12_prf->block_number == 1 )
4610 {
4611 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4612 * the variable seed and in this instance means it in the context of the
4613 * P_hash function, where seed = label + seed.) */
4614 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4615 tls12_prf->label, tls12_prf->label_length );
4616 if( status != PSA_SUCCESS )
4617 goto cleanup;
4618 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4619 tls12_prf->seed, tls12_prf->seed_length );
4620 if( status != PSA_SUCCESS )
4621 goto cleanup;
4622 }
4623 else
4624 {
4625 /* A(i) = HMAC_hash(secret, A(i-1)) */
4626 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4627 tls12_prf->Ai, hash_length );
4628 if( status != PSA_SUCCESS )
4629 goto cleanup;
4630 }
4631
4632 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4633 tls12_prf->Ai, hash_length );
4634 if( status != PSA_SUCCESS )
4635 goto cleanup;
4636 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4637 if( status != PSA_SUCCESS )
4638 goto cleanup;
4639
4640 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4641 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4642 tls12_prf->Ai, hash_length );
4643 if( status != PSA_SUCCESS )
4644 goto cleanup;
4645 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4646 tls12_prf->label, tls12_prf->label_length );
4647 if( status != PSA_SUCCESS )
4648 goto cleanup;
4649 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4650 tls12_prf->seed, tls12_prf->seed_length );
4651 if( status != PSA_SUCCESS )
4652 goto cleanup;
4653 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4654 tls12_prf->output_block, hash_length );
4655 if( status != PSA_SUCCESS )
4656 goto cleanup;
4657 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4658 if( status != PSA_SUCCESS )
4659 goto cleanup;
4660
Janos Follath7742fee2019-06-17 12:58:10 +01004661
4662cleanup:
4663
Janos Follathea29bfb2019-06-19 12:21:20 +01004664 cleanup_status = psa_hash_abort( &backup );
4665 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4666 status = cleanup_status;
4667
4668 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004669}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004670
Janos Follath844eb0e2019-06-19 12:10:49 +01004671static psa_status_t psa_key_derivation_tls12_prf_read(
4672 psa_tls12_prf_key_derivation_t *tls12_prf,
4673 psa_algorithm_t alg,
4674 uint8_t *output,
4675 size_t output_length )
4676{
4677 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4678 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4679 psa_status_t status;
4680 uint8_t offset, length;
4681
4682 while( output_length != 0 )
4683 {
4684 /* Check if we have fully processed the current block. */
4685 if( tls12_prf->left_in_block == 0 )
4686 {
4687 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4688 alg );
4689 if( status != PSA_SUCCESS )
4690 return( status );
4691
4692 continue;
4693 }
4694
4695 if( tls12_prf->left_in_block > output_length )
4696 length = (uint8_t) output_length;
4697 else
4698 length = tls12_prf->left_in_block;
4699
4700 offset = hash_length - tls12_prf->left_in_block;
4701 memcpy( output, tls12_prf->output_block + offset, length );
4702 output += length;
4703 output_length -= length;
4704 tls12_prf->left_in_block -= length;
4705 }
4706
4707 return( PSA_SUCCESS );
4708}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004709#endif /* MBEDTLS_MD_C */
4710
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004711psa_status_t psa_key_derivation_output_bytes(
4712 psa_key_derivation_operation_t *operation,
4713 uint8_t *output,
4714 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004715{
4716 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004717 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004720 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004722 return PSA_ERROR_BAD_STATE;
4723 }
4724
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004728 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004730 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731 goto exit;
4732 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004734 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004736 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4738 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004741 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004742 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004744
Gilles Peskinebef7f142018-07-12 17:22:21 +02004745#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004746 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004747 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004748 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004750 output, output_length );
4751 }
Janos Follathadbec812019-06-14 11:05:39 +01004752 else
Janos Follathadbec812019-06-14 11:05:39 +01004753 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004754 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004755 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004758 output_length );
4759 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 else
4761#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004762 {
4763 return( PSA_ERROR_BAD_STATE );
4764 }
4765
4766exit:
4767 if( status != PSA_SUCCESS )
4768 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004769 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 * This allows us to differentiate between exhausted operations and
4771 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4772 * operations. */
4773 psa_algorithm_t alg = operation->alg;
4774 psa_key_derivation_abort( operation );
4775 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004776 memset( output, '!', output_length );
4777 }
4778 return( status );
4779}
4780
Gilles Peskine08542d82018-07-19 17:05:42 +02004781#if defined(MBEDTLS_DES_C)
4782static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4783{
4784 if( data_size >= 8 )
4785 mbedtls_des_key_set_parity( data );
4786 if( data_size >= 16 )
4787 mbedtls_des_key_set_parity( data + 8 );
4788 if( data_size >= 24 )
4789 mbedtls_des_key_set_parity( data + 16 );
4790}
4791#endif /* MBEDTLS_DES_C */
4792
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004793static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004794 psa_key_slot_t *slot,
4795 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004797{
4798 uint8_t *data = NULL;
4799 size_t bytes = PSA_BITS_TO_BYTES( bits );
4800 psa_status_t status;
4801
Gilles Peskine8e338702019-07-30 20:06:31 +02004802 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004803 return( PSA_ERROR_INVALID_ARGUMENT );
4804 if( bits % 8 != 0 )
4805 return( PSA_ERROR_INVALID_ARGUMENT );
4806 data = mbedtls_calloc( 1, bytes );
4807 if( data == NULL )
4808 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4809
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004811 if( status != PSA_SUCCESS )
4812 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004813#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004814 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004815 psa_des_set_key_parity( data, bytes );
4816#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004817 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004818
4819exit:
4820 mbedtls_free( data );
4821 return( status );
4822}
4823
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004824psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004826 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004827{
4828 psa_status_t status;
4829 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004830 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004831
4832 /* Reject any attempt to create a zero-length key so that we don't
4833 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4834 if( psa_get_key_bits( attributes ) == 0 )
4835 return( PSA_ERROR_INVALID_ARGUMENT );
4836
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004837 if( ! operation->can_output_key )
4838 return( PSA_ERROR_NOT_PERMITTED );
4839
Gilles Peskinedf179142019-07-15 22:02:14 +02004840 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4841 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004842#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4843 if( driver != NULL )
4844 {
4845 /* Deriving a key in a secure element is not implemented yet. */
4846 status = PSA_ERROR_NOT_SUPPORTED;
4847 }
4848#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004849 if( status == PSA_SUCCESS )
4850 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004851 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004852 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004854 }
4855 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004856 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 if( status != PSA_SUCCESS )
4858 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004859 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 *handle = 0;
4861 }
4862 return( status );
4863}
4864
Gilles Peskineeab56e42018-07-12 17:12:33 +02004865
4866
4867/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004868/* Key derivation */
4869/****************************************************************/
4870
Gilles Peskine969c5d62019-01-16 15:53:06 +01004871static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004872 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004873 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004874{
Janos Follath5fe19732019-06-20 15:09:30 +01004875 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4876 * initialisers for this union leave some bytes unspecified.) */
4877 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4878
Gilles Peskine969c5d62019-01-16 15:53:06 +01004879 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004880#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004881 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4882 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4883 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4887 if( hash_size == 0 )
4888 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004889 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4890 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004891 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892 {
4893 return( PSA_ERROR_NOT_SUPPORTED );
4894 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897 }
4898#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901}
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 psa_algorithm_t alg )
4905{
4906 psa_status_t status;
4907
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909 return( PSA_ERROR_BAD_STATE );
4910
Gilles Peskine6843c292019-01-18 16:44:49 +01004911 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4912 return( PSA_ERROR_INVALID_ARGUMENT );
4913 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914 {
4915 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917 }
4918 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4919 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004922 else
4923 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924
4925 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928}
4929
4930#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004931static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004932 psa_algorithm_t hash_alg,
4933 psa_key_derivation_step_t step,
4934 const uint8_t *data,
4935 size_t data_length )
4936{
4937 psa_status_t status;
4938 switch( step )
4939 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004940 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004941 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004942 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004943 status = psa_hmac_setup_internal( &hkdf->hmac,
4944 data, data_length,
4945 hash_alg );
4946 if( status != PSA_SUCCESS )
4947 return( status );
4948 hkdf->state = HKDF_STATE_STARTED;
4949 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004950 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004951 /* If no salt was provided, use an empty salt. */
4952 if( hkdf->state == HKDF_STATE_INIT )
4953 {
4954 status = psa_hmac_setup_internal( &hkdf->hmac,
4955 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004956 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004957 if( status != PSA_SUCCESS )
4958 return( status );
4959 hkdf->state = HKDF_STATE_STARTED;
4960 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004961 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004962 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004963 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4964 data, data_length );
4965 if( status != PSA_SUCCESS )
4966 return( status );
4967 status = psa_hmac_finish_internal( &hkdf->hmac,
4968 hkdf->prk,
4969 sizeof( hkdf->prk ) );
4970 if( status != PSA_SUCCESS )
4971 return( status );
4972 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4973 hkdf->block_number = 0;
4974 hkdf->state = HKDF_STATE_KEYED;
4975 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004976 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004977 if( hkdf->state == HKDF_STATE_OUTPUT )
4978 return( PSA_ERROR_BAD_STATE );
4979 if( hkdf->info_set )
4980 return( PSA_ERROR_BAD_STATE );
4981 hkdf->info_length = data_length;
4982 if( data_length != 0 )
4983 {
4984 hkdf->info = mbedtls_calloc( 1, data_length );
4985 if( hkdf->info == NULL )
4986 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4987 memcpy( hkdf->info, data, data_length );
4988 }
4989 hkdf->info_set = 1;
4990 return( PSA_SUCCESS );
4991 default:
4992 return( PSA_ERROR_INVALID_ARGUMENT );
4993 }
4994}
Janos Follathb03233e2019-06-11 15:30:30 +01004995
Janos Follathf08e2652019-06-13 09:05:41 +01004996static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4997 const uint8_t *data,
4998 size_t data_length )
4999{
5000 if( prf->state != TLS12_PRF_STATE_INIT )
5001 return( PSA_ERROR_BAD_STATE );
5002
Janos Follathd6dce9f2019-07-04 09:11:38 +01005003 if( data_length != 0 )
5004 {
5005 prf->seed = mbedtls_calloc( 1, data_length );
5006 if( prf->seed == NULL )
5007 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005008
Janos Follathd6dce9f2019-07-04 09:11:38 +01005009 memcpy( prf->seed, data, data_length );
5010 prf->seed_length = data_length;
5011 }
Janos Follathf08e2652019-06-13 09:05:41 +01005012
5013 prf->state = TLS12_PRF_STATE_SEED_SET;
5014
5015 return( PSA_SUCCESS );
5016}
5017
Janos Follath81550542019-06-13 14:26:34 +01005018static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5019 psa_algorithm_t hash_alg,
5020 const uint8_t *data,
5021 size_t data_length )
5022{
5023 psa_status_t status;
5024 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5025 return( PSA_ERROR_BAD_STATE );
5026
5027 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5028 if( status != PSA_SUCCESS )
5029 return( status );
5030
5031 prf->state = TLS12_PRF_STATE_KEY_SET;
5032
5033 return( PSA_SUCCESS );
5034}
5035
Janos Follath6660f0e2019-06-17 08:44:03 +01005036static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5037 psa_tls12_prf_key_derivation_t *prf,
5038 psa_algorithm_t hash_alg,
5039 const uint8_t *data,
5040 size_t data_length )
5041{
5042 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005043 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5044 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005045
5046 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5047 return( PSA_ERROR_INVALID_ARGUMENT );
5048
5049 /* Quoting RFC 4279, Section 2:
5050 *
5051 * The premaster secret is formed as follows: if the PSK is N octets
5052 * long, concatenate a uint16 with the value N, N zero octets, a second
5053 * uint16 with the value N, and the PSK itself.
5054 */
5055
Janos Follath40e13932019-06-26 13:22:29 +01005056 *cur++ = ( data_length >> 8 ) & 0xff;
5057 *cur++ = ( data_length >> 0 ) & 0xff;
5058 memset( cur, 0, data_length );
5059 cur += data_length;
5060 *cur++ = pms[0];
5061 *cur++ = pms[1];
5062 memcpy( cur, data, data_length );
5063 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005064
Janos Follath40e13932019-06-26 13:22:29 +01005065 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005066
5067 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5068 return( status );
5069}
5070
Janos Follath63028dd2019-06-13 09:15:47 +01005071static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5072 const uint8_t *data,
5073 size_t data_length )
5074{
5075 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5076 return( PSA_ERROR_BAD_STATE );
5077
Janos Follathd6dce9f2019-07-04 09:11:38 +01005078 if( data_length != 0 )
5079 {
5080 prf->label = mbedtls_calloc( 1, data_length );
5081 if( prf->label == NULL )
5082 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005083
Janos Follathd6dce9f2019-07-04 09:11:38 +01005084 memcpy( prf->label, data, data_length );
5085 prf->label_length = data_length;
5086 }
Janos Follath63028dd2019-06-13 09:15:47 +01005087
5088 prf->state = TLS12_PRF_STATE_LABEL_SET;
5089
5090 return( PSA_SUCCESS );
5091}
5092
Janos Follathb03233e2019-06-11 15:30:30 +01005093static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5094 psa_algorithm_t hash_alg,
5095 psa_key_derivation_step_t step,
5096 const uint8_t *data,
5097 size_t data_length )
5098{
Janos Follathb03233e2019-06-11 15:30:30 +01005099 switch( step )
5100 {
Janos Follathf08e2652019-06-13 09:05:41 +01005101 case PSA_KEY_DERIVATION_INPUT_SEED:
5102 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005103 case PSA_KEY_DERIVATION_INPUT_SECRET:
5104 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005105 case PSA_KEY_DERIVATION_INPUT_LABEL:
5106 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005107 default:
5108 return( PSA_ERROR_INVALID_ARGUMENT );
5109 }
5110}
Janos Follath6660f0e2019-06-17 08:44:03 +01005111
5112static psa_status_t psa_tls12_prf_psk_to_ms_input(
5113 psa_tls12_prf_key_derivation_t *prf,
5114 psa_algorithm_t hash_alg,
5115 psa_key_derivation_step_t step,
5116 const uint8_t *data,
5117 size_t data_length )
5118{
5119 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005120 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005121 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5122 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005123 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005124
5125 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5126}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005127#endif /* MBEDTLS_MD_C */
5128
Gilles Peskineb8965192019-09-24 16:21:10 +02005129/** Check whether the given key type is acceptable for the given
5130 * input step of a key derivation.
5131 *
5132 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5133 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5134 * Both secret and non-secret inputs can alternatively have the type
5135 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5136 * that the input was passed as a buffer rather than via a key object.
5137 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005138static int psa_key_derivation_check_input_type(
5139 psa_key_derivation_step_t step,
5140 psa_key_type_t key_type )
5141{
5142 switch( step )
5143 {
5144 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005145 if( key_type == PSA_KEY_TYPE_DERIVE )
5146 return( PSA_SUCCESS );
5147 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005148 return( PSA_SUCCESS );
5149 break;
5150 case PSA_KEY_DERIVATION_INPUT_LABEL:
5151 case PSA_KEY_DERIVATION_INPUT_SALT:
5152 case PSA_KEY_DERIVATION_INPUT_INFO:
5153 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005154 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5155 return( PSA_SUCCESS );
5156 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005157 return( PSA_SUCCESS );
5158 break;
5159 }
5160 return( PSA_ERROR_INVALID_ARGUMENT );
5161}
5162
Janos Follathb80a94e2019-06-12 15:54:46 +01005163static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005165 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005166 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005167 const uint8_t *data,
5168 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005169{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005170 psa_status_t status;
5171 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5172
5173 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005174 if( status != PSA_SUCCESS )
5175 goto exit;
5176
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005177#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005178 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005180 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005181 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182 step, data, data_length );
5183 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005184 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 {
Janos Follathb03233e2019-06-11 15:30:30 +01005186 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5187 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5188 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005189 }
5190 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5191 {
5192 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5193 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5194 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195 }
5196 else
5197#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005200 return( PSA_ERROR_BAD_STATE );
5201 }
5202
Gilles Peskine224b0d62019-09-23 18:13:17 +02005203exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005206 return( status );
5207}
5208
Janos Follath51f4a0f2019-06-14 11:35:55 +01005209psa_status_t psa_key_derivation_input_bytes(
5210 psa_key_derivation_operation_t *operation,
5211 psa_key_derivation_step_t step,
5212 const uint8_t *data,
5213 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005214{
Gilles Peskineb8965192019-09-24 16:21:10 +02005215 return( psa_key_derivation_input_internal( operation, step,
5216 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005217 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005218}
5219
Janos Follath51f4a0f2019-06-14 11:35:55 +01005220psa_status_t psa_key_derivation_input_key(
5221 psa_key_derivation_operation_t *operation,
5222 psa_key_derivation_step_t step,
5223 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005224{
5225 psa_key_slot_t *slot;
5226 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005227
Gilles Peskine28f8f302019-07-24 13:30:31 +02005228 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005229 PSA_KEY_USAGE_DERIVE,
5230 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005231 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005232 {
5233 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005235 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005236
5237 /* Passing a key object as a SECRET input unlocks the permission
5238 * to output to a key object. */
5239 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5240 operation->can_output_key = 1;
5241
Janos Follathb80a94e2019-06-12 15:54:46 +01005242 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005243 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005244 slot->data.raw.data,
5245 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005246}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005247
5248
5249
5250/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005251/* Key agreement */
5252/****************************************************************/
5253
Gilles Peskinea05219c2018-11-16 16:02:56 +01005254#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005255static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5256 size_t peer_key_length,
5257 const mbedtls_ecp_keypair *our_key,
5258 uint8_t *shared_secret,
5259 size_t shared_secret_size,
5260 size_t *shared_secret_length )
5261{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005262 mbedtls_ecp_keypair *their_key = NULL;
5263 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005264 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005265 size_t bits = 0;
5266 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005267 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005268
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005269 status = psa_import_ec_public_key( curve,
5270 peer_key, peer_key_length,
5271 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005272 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005273 goto exit;
5274
Jaeden Amero97271b32019-01-10 19:38:51 +00005275 status = mbedtls_to_psa_error(
5276 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5277 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005278 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005279 status = mbedtls_to_psa_error(
5280 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5281 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005282 goto exit;
5283
Jaeden Amero97271b32019-01-10 19:38:51 +00005284 status = mbedtls_to_psa_error(
5285 mbedtls_ecdh_calc_secret( &ecdh,
5286 shared_secret_length,
5287 shared_secret, shared_secret_size,
5288 mbedtls_ctr_drbg_random,
5289 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005290 if( status != PSA_SUCCESS )
5291 goto exit;
5292 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5293 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005294
5295exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005296 if( status != PSA_SUCCESS )
5297 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005298 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005299 mbedtls_ecp_keypair_free( their_key );
5300 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005301 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005302}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005303#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005304
Gilles Peskine01d718c2018-09-18 12:01:02 +02005305#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5306
Gilles Peskine0216fe12019-04-11 21:23:21 +02005307static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5308 psa_key_slot_t *private_key,
5309 const uint8_t *peer_key,
5310 size_t peer_key_length,
5311 uint8_t *shared_secret,
5312 size_t shared_secret_size,
5313 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005314{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005315 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005316 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005317#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005318 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005319 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005321 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5322 private_key->data.ecp,
5323 shared_secret, shared_secret_size,
5324 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005325#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005327 (void) private_key;
5328 (void) peer_key;
5329 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005330 (void) shared_secret;
5331 (void) shared_secret_size;
5332 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005333 return( PSA_ERROR_NOT_SUPPORTED );
5334 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005335}
5336
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005337/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005338 * to potentially free embedded data structures and wipe confidential data.
5339 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005341 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342 psa_key_slot_t *private_key,
5343 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005344 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005345{
5346 psa_status_t status;
5347 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5348 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005350
5351 /* Step 1: run the secret agreement algorithm to generate the shared
5352 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005353 status = psa_key_agreement_raw_internal( ka_alg,
5354 private_key,
5355 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005356 shared_secret,
5357 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005358 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005359 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005360 goto exit;
5361
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005362 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005363 * the shared secret. A shared secret is permitted wherever a key
5364 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005365 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005366 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005367 shared_secret,
5368 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005369
Gilles Peskine12313cd2018-06-20 00:20:32 +02005370exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005371 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005372 return( status );
5373}
5374
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005375psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005376 psa_key_derivation_step_t step,
5377 psa_key_handle_t private_key,
5378 const uint8_t *peer_key,
5379 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005380{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005381 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005382 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005384 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005385 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005386 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005387 if( status != PSA_SUCCESS )
5388 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005390 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005391 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005392 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005394 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005395}
5396
Gilles Peskinebe697d82019-05-16 18:00:41 +02005397psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5398 psa_key_handle_t private_key,
5399 const uint8_t *peer_key,
5400 size_t peer_key_length,
5401 uint8_t *output,
5402 size_t output_size,
5403 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005404{
5405 psa_key_slot_t *slot;
5406 psa_status_t status;
5407
5408 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5409 {
5410 status = PSA_ERROR_INVALID_ARGUMENT;
5411 goto exit;
5412 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005413 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005414 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005415 if( status != PSA_SUCCESS )
5416 goto exit;
5417
5418 status = psa_key_agreement_raw_internal( alg, slot,
5419 peer_key, peer_key_length,
5420 output, output_size,
5421 output_length );
5422
5423exit:
5424 if( status != PSA_SUCCESS )
5425 {
5426 /* If an error happens and is not handled properly, the output
5427 * may be used as a key to protect sensitive data. Arrange for such
5428 * a key to be random, which is likely to result in decryption or
5429 * verification errors. This is better than filling the buffer with
5430 * some constant data such as zeros, which would result in the data
5431 * being protected with a reproducible, easily knowable key.
5432 */
5433 psa_generate_random( output, output_size );
5434 *output_length = output_size;
5435 }
5436 return( status );
5437}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005438
5439
5440/****************************************************************/
5441/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005442/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005443
Gilles Peskine4c317f42018-07-12 01:24:09 +02005444psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005445 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005446{
Janos Follath24eed8d2019-11-22 13:21:35 +00005447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448 GUARD_MODULE_INITIALIZED;
5449
Gilles Peskinef181eca2019-08-07 13:49:00 +02005450 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5451 {
5452 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5453 output,
5454 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5455 if( ret != 0 )
5456 return( mbedtls_to_psa_error( ret ) );
5457 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5458 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5459 }
5460
Gilles Peskinee59236f2018-01-27 23:32:46 +01005461 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5462 return( mbedtls_to_psa_error( ret ) );
5463}
5464
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005465#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5466#include "mbedtls/entropy_poll.h"
5467
Gilles Peskine7228da22019-07-15 11:06:15 +02005468psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005469 size_t seed_size )
5470{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005471 if( global_data.initialized )
5472 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005473
5474 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5475 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5476 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5477 return( PSA_ERROR_INVALID_ARGUMENT );
5478
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005479 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005480}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005481#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005482
Gilles Peskinee56e8782019-04-26 17:34:02 +02005483#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5484static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5485 size_t domain_parameters_size,
5486 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005487{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005488 size_t i;
5489 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005490
Gilles Peskinee56e8782019-04-26 17:34:02 +02005491 if( domain_parameters_size == 0 )
5492 {
5493 *exponent = 65537;
5494 return( PSA_SUCCESS );
5495 }
5496
5497 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5498 * support values that fit in a 32-bit integer, which is larger than
5499 * int on just about every platform anyway. */
5500 if( domain_parameters_size > sizeof( acc ) )
5501 return( PSA_ERROR_NOT_SUPPORTED );
5502 for( i = 0; i < domain_parameters_size; i++ )
5503 acc = ( acc << 8 ) | domain_parameters[i];
5504 if( acc > INT_MAX )
5505 return( PSA_ERROR_NOT_SUPPORTED );
5506 *exponent = acc;
5507 return( PSA_SUCCESS );
5508}
5509#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5510
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005511static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005512 psa_key_slot_t *slot, size_t bits,
5513 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005514{
Gilles Peskine8e338702019-07-30 20:06:31 +02005515 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005516
Gilles Peskinee56e8782019-04-26 17:34:02 +02005517 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005518 return( PSA_ERROR_INVALID_ARGUMENT );
5519
Gilles Peskinee59236f2018-01-27 23:32:46 +01005520 if( key_type_is_raw_bytes( type ) )
5521 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005522 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005523 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5524 if( status != PSA_SUCCESS )
5525 return( status );
5526 status = psa_generate_random( slot->data.raw.data,
5527 slot->data.raw.bytes );
5528 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005529 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530#if defined(MBEDTLS_DES_C)
5531 if( type == PSA_KEY_TYPE_DES )
5532 psa_des_set_key_parity( slot->data.raw.data,
5533 slot->data.raw.bytes );
5534#endif /* MBEDTLS_DES_C */
5535 }
5536 else
5537
5538#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005539 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540 {
5541 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005543 int exponent;
5544 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005545 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5546 return( PSA_ERROR_NOT_SUPPORTED );
5547 /* Accept only byte-aligned keys, for the same reasons as
5548 * in psa_import_rsa_key(). */
5549 if( bits % 8 != 0 )
5550 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005551 status = psa_read_rsa_exponent( domain_parameters,
5552 domain_parameters_size,
5553 &exponent );
5554 if( status != PSA_SUCCESS )
5555 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5557 if( rsa == NULL )
5558 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5559 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5560 ret = mbedtls_rsa_gen_key( rsa,
5561 mbedtls_ctr_drbg_random,
5562 &global_data.ctr_drbg,
5563 (unsigned int) bits,
5564 exponent );
5565 if( ret != 0 )
5566 {
5567 mbedtls_rsa_free( rsa );
5568 mbedtls_free( rsa );
5569 return( mbedtls_to_psa_error( ret ) );
5570 }
5571 slot->data.rsa = rsa;
5572 }
5573 else
5574#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5575
5576#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005577 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005578 {
5579 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005580 mbedtls_ecp_group_id grp_id =
5581 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005582 const mbedtls_ecp_curve_info *curve_info =
5583 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5584 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005586 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587 return( PSA_ERROR_NOT_SUPPORTED );
5588 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5589 return( PSA_ERROR_NOT_SUPPORTED );
5590 if( curve_info->bit_size != bits )
5591 return( PSA_ERROR_INVALID_ARGUMENT );
5592 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5593 if( ecp == NULL )
5594 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5595 mbedtls_ecp_keypair_init( ecp );
5596 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5597 mbedtls_ctr_drbg_random,
5598 &global_data.ctr_drbg );
5599 if( ret != 0 )
5600 {
5601 mbedtls_ecp_keypair_free( ecp );
5602 mbedtls_free( ecp );
5603 return( mbedtls_to_psa_error( ret ) );
5604 }
5605 slot->data.ecp = ecp;
5606 }
5607 else
5608#endif /* MBEDTLS_ECP_C */
5609
5610 return( PSA_ERROR_NOT_SUPPORTED );
5611
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005612 return( PSA_SUCCESS );
5613}
Darryl Green0c6575a2018-11-07 16:05:30 +00005614
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005615psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005616 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005617{
5618 psa_status_t status;
5619 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005620 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005621
Gilles Peskine0f84d622019-09-12 19:03:13 +02005622 /* Reject any attempt to create a zero-length key so that we don't
5623 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5624 if( psa_get_key_bits( attributes ) == 0 )
5625 return( PSA_ERROR_INVALID_ARGUMENT );
5626
Gilles Peskinedf179142019-07-15 22:02:14 +02005627 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5628 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005629 if( status != PSA_SUCCESS )
5630 goto exit;
5631
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005632#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5633 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005634 {
Gilles Peskine11792082019-08-06 18:36:36 +02005635 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5636 size_t pubkey_length = 0; /* We don't support this feature yet */
5637 if( drv->key_management == NULL ||
5638 drv->key_management->p_generate == NULL )
5639 {
5640 status = PSA_ERROR_NOT_SUPPORTED;
5641 goto exit;
5642 }
5643 status = drv->key_management->p_generate(
5644 psa_get_se_driver_context( driver ),
5645 slot->data.se.slot_number, attributes,
5646 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005647 }
Gilles Peskine11792082019-08-06 18:36:36 +02005648 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005649#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005650 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005651 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005652 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005653 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005654 }
Gilles Peskine11792082019-08-06 18:36:36 +02005655
5656exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005657 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005658 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005659 if( status != PSA_SUCCESS )
5660 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005661 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 *handle = 0;
5663 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005664 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005665}
5666
5667
Gilles Peskinee59236f2018-01-27 23:32:46 +01005668
5669/****************************************************************/
5670/* Module setup */
5671/****************************************************************/
5672
Gilles Peskine5e769522018-11-20 21:59:56 +01005673psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5674 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5675 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5676{
5677 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5678 return( PSA_ERROR_BAD_STATE );
5679 global_data.entropy_init = entropy_init;
5680 global_data.entropy_free = entropy_free;
5681 return( PSA_SUCCESS );
5682}
5683
Gilles Peskinee59236f2018-01-27 23:32:46 +01005684void mbedtls_psa_crypto_free( void )
5685{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005686 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005687 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5688 {
5689 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005690 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005691 }
5692 /* Wipe all remaining data, including configuration.
5693 * In particular, this sets all state indicator to the value
5694 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005695 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005696#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005697 /* Unregister all secure element drivers, so that we restart from
5698 * a pristine state. */
5699 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005700#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005701}
5702
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005703#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5704/** Recover a transaction that was interrupted by a power failure.
5705 *
5706 * This function is called during initialization, before psa_crypto_init()
5707 * returns. If this function returns a failure status, the initialization
5708 * fails.
5709 */
5710static psa_status_t psa_crypto_recover_transaction(
5711 const psa_crypto_transaction_t *transaction )
5712{
5713 switch( transaction->unknown.type )
5714 {
5715 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5716 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005717 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005718 * is implemented.
5719 * https://github.com/ARMmbed/mbed-crypto/issues/218
5720 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005721 default:
5722 /* We found an unsupported transaction in the storage.
5723 * We don't know what state the storage is in. Give up. */
5724 return( PSA_ERROR_STORAGE_FAILURE );
5725 }
5726}
5727#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5728
Gilles Peskinee59236f2018-01-27 23:32:46 +01005729psa_status_t psa_crypto_init( void )
5730{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005731 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005732 const unsigned char drbg_seed[] = "PSA";
5733
Gilles Peskinec6b69072018-11-20 21:42:52 +01005734 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005735 if( global_data.initialized != 0 )
5736 return( PSA_SUCCESS );
5737
Gilles Peskine5e769522018-11-20 21:59:56 +01005738 /* Set default configuration if
5739 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5740 if( global_data.entropy_init == NULL )
5741 global_data.entropy_init = mbedtls_entropy_init;
5742 if( global_data.entropy_free == NULL )
5743 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744
Gilles Peskinec6b69072018-11-20 21:42:52 +01005745 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005746 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005747#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5748 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5749 /* The PSA entropy injection feature depends on using NV seed as an entropy
5750 * source. Add NV seed as an entropy source for PSA entropy injection. */
5751 mbedtls_entropy_add_source( &global_data.entropy,
5752 mbedtls_nv_seed_poll, NULL,
5753 MBEDTLS_ENTROPY_BLOCK_SIZE,
5754 MBEDTLS_ENTROPY_SOURCE_STRONG );
5755#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005756 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005757 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005758 status = mbedtls_to_psa_error(
5759 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5760 mbedtls_entropy_func,
5761 &global_data.entropy,
5762 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5763 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005764 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005765 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005766
Gilles Peskine66fb1262018-12-10 16:29:04 +01005767 status = psa_initialize_key_slots( );
5768 if( status != PSA_SUCCESS )
5769 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005770
Gilles Peskined9348f22019-10-01 15:22:29 +02005771#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5772 status = psa_init_all_se_drivers( );
5773 if( status != PSA_SUCCESS )
5774 goto exit;
5775#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5776
Gilles Peskine4b734222019-07-24 15:56:31 +02005777#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005778 status = psa_crypto_load_transaction( );
5779 if( status == PSA_SUCCESS )
5780 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005781 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5782 if( status != PSA_SUCCESS )
5783 goto exit;
5784 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005785 }
5786 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5787 {
5788 /* There's no transaction to complete. It's all good. */
5789 status = PSA_SUCCESS;
5790 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005791#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005792
Gilles Peskinec6b69072018-11-20 21:42:52 +01005793 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005794 global_data.initialized = 1;
5795
5796exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005797 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005798 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005799 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005800}
5801
5802#endif /* MBEDTLS_PSA_CRYPTO_C */