blob: 1646ae584e694ee7e4928c16d8844a4d7c31407b [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 Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskine8e338702019-07-30 20:06:31 +0200698/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Gilles Peskine8e338702019-07-30 20:06:31 +0200707 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200709 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
710 /* Ensure that the bytes-to-bit conversion doesn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 if( data_length > SIZE_MAX / 8 )
712 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200713 /* Ensure that the bit size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200714 if( bit_size > PSA_MAX_KEY_BITS )
715 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200716 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200717 &slot->data.raw );
718 if( status != PSA_SUCCESS )
719 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200720 if( data_length != 0 )
721 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100722 }
723 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100724#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200725 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100726 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200727 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100728 data, data_length,
729 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100730 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200731 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000732 {
733 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200734 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000735 data, data_length,
736 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100738 else
739#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000740#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200741 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200743 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000744 data, data_length,
745 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 }
747 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000748#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749 {
750 return( PSA_ERROR_NOT_SUPPORTED );
751 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000752 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100753}
754
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100755/** Calculate the intersection of two algorithm usage policies.
756 *
757 * Return 0 (which allows no operation) on incompatibility.
758 */
759static psa_algorithm_t psa_key_policy_algorithm_intersection(
760 psa_algorithm_t alg1,
761 psa_algorithm_t alg2 )
762{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200763 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100764 if( alg1 == alg2 )
765 return( alg1 );
766 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100767 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100768 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
769 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
770 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
771 {
772 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
773 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100774 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100775 return( alg1 );
776 }
777 /* If the policies are incompatible, allow nothing. */
778 return( 0 );
779}
780
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200781static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
782 psa_algorithm_t requested_alg )
783{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200784 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200785 if( requested_alg == policy_alg )
786 return( 1 );
787 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200788 * and requested_alg is the same hash-and-sign family with any hash,
789 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200790 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
791 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
792 {
793 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
794 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
795 }
796 /* If it isn't permitted, it's forbidden. */
797 return( 0 );
798}
799
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100800/** Test whether a policy permits an algorithm.
801 *
802 * The caller must test usage flags separately.
803 */
804static int psa_key_policy_permits( const psa_key_policy_t *policy,
805 psa_algorithm_t alg )
806{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200807 return( psa_key_algorithm_permits( policy->alg, alg ) ||
808 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100809}
810
Gilles Peskinef603c712019-01-19 13:40:11 +0100811/** Restrict a key policy based on a constraint.
812 *
813 * \param[in,out] policy The policy to restrict.
814 * \param[in] constraint The policy constraint to apply.
815 *
816 * \retval #PSA_SUCCESS
817 * \c *policy contains the intersection of the original value of
818 * \c *policy and \c *constraint.
819 * \retval #PSA_ERROR_INVALID_ARGUMENT
820 * \c *policy and \c *constraint are incompatible.
821 * \c *policy is unchanged.
822 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823static psa_status_t psa_restrict_key_policy(
824 psa_key_policy_t *policy,
825 const psa_key_policy_t *constraint )
826{
827 psa_algorithm_t intersection_alg =
828 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829 psa_algorithm_t intersection_alg2 =
830 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100831 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
832 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
834 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100836 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200837 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100838 return( PSA_SUCCESS );
839}
840
Darryl Green06fd18d2018-07-16 11:21:11 +0100841/** Retrieve a slot which must contain a key. The key must have allow all the
842 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
843 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100844static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100845 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100846 psa_key_usage_t usage,
847 psa_algorithm_t alg )
848{
849 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100850 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100851
852 *p_slot = NULL;
853
Gilles Peskinec5487a82018-12-03 18:08:14 +0100854 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100855 if( status != PSA_SUCCESS )
856 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +0200857 if( slot->attr.type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200858 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100859
860 /* Enforce that usage policy for the key slot contains all the flags
861 * required by the usage parameter. There is one exception: public
862 * keys can always be exported, so we treat public key objects as
863 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200864 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100865 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200866 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100867 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100868
869 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200870 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100871 return( PSA_ERROR_NOT_PERMITTED );
872
873 *p_slot = slot;
874 return( PSA_SUCCESS );
875}
Darryl Green940d72c2018-07-13 13:18:51 +0100876
Gilles Peskine28f8f302019-07-24 13:30:31 +0200877/** Retrieve a slot which must contain a transparent key.
878 *
879 * A transparent key is a key for which the key material is directly
880 * available, as opposed to a key in a secure element.
881 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200882 * This is a temporary function to use instead of psa_get_key_from_slot()
883 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200884 */
885#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
886static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
887 psa_key_slot_t **p_slot,
888 psa_key_usage_t usage,
889 psa_algorithm_t alg )
890{
891 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
892 if( status != PSA_SUCCESS )
893 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200894 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200895 {
896 *p_slot = NULL;
897 return( PSA_ERROR_NOT_SUPPORTED );
898 }
899 return( PSA_SUCCESS );
900}
901#else /* MBEDTLS_PSA_CRYPTO_SE_C */
902/* With no secure element support, all keys are transparent. */
903#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
904 psa_get_key_from_slot( handle, p_slot, usage, alg )
905#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
906
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100907/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100908static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000909{
Gilles Peskine73167e12019-07-12 23:44:37 +0200910#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
911 if( psa_key_slot_is_external( slot ) )
912 {
913 /* No key material to clean. */
914 }
915 else
916#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200917 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000918 {
919 /* No key material to clean. */
920 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200921 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000922 {
923 mbedtls_free( slot->data.raw.data );
924 }
925 else
926#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200927 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000928 {
929 mbedtls_rsa_free( slot->data.rsa );
930 mbedtls_free( slot->data.rsa );
931 }
932 else
933#endif /* defined(MBEDTLS_RSA_C) */
934#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200935 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000936 {
937 mbedtls_ecp_keypair_free( slot->data.ecp );
938 mbedtls_free( slot->data.ecp );
939 }
940 else
941#endif /* defined(MBEDTLS_ECP_C) */
942 {
943 /* Shouldn't happen: the key type is not any type that we
944 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200945 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000946 }
947
948 return( PSA_SUCCESS );
949}
950
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951static void psa_abort_operations_using_key( psa_key_slot_t *slot )
952{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200953 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100954 (void) slot;
955}
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Completely wipe a slot in memory, including its policy.
958 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100959psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960{
961 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100962 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100963 /* At this point, key material and other type-specific content has
964 * been wiped. Clear remaining metadata. We can call memset and not
965 * zeroize because the metadata is not particularly sensitive. */
966 memset( slot, 0, sizeof( *slot ) );
967 return( status );
968}
969
Gilles Peskinec5487a82018-12-03 18:08:14 +0100970psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100972 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100973 psa_status_t status = PSA_SUCCESS;
974 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200975#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
976 psa_se_drv_table_entry_t *driver;
977#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978
Gilles Peskinec5487a82018-12-03 18:08:14 +0100979 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200980 if( status != PSA_SUCCESS )
981 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200982
983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200984 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +0200985 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200986 {
Gilles Peskine60450a42019-07-25 11:32:45 +0200987 /* For a key in a secure element, we need to do three things:
988 * remove the key file in internal storage, destroy the
989 * key inside the secure element, and update the driver's
990 * persistent data. Start a transaction that will encompass these
991 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +0200992 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +0200993 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +0200994 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +0200995 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +0200996 status = psa_crypto_save_transaction( );
997 if( status != PSA_SUCCESS )
998 {
Gilles Peskine66be51c2019-07-25 18:02:52 +0200999 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001000 /* TOnogrepDO: destroy what can be destroyed anyway */
1001 return( status );
1002 }
1003
Gilles Peskine354f7672019-07-12 23:46:38 +02001004 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001005 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001006#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1007
Darryl Greend49a4992018-06-18 17:27:26 +01001008#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001009 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001010 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001011 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001012 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001013 }
1014#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001015
Gilles Peskinefc762652019-07-22 19:30:34 +02001016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1017 if( driver != NULL )
1018 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001019 psa_status_t status2;
1020 status = psa_save_se_persistent_data( driver );
1021 status2 = psa_crypto_stop_transaction( );
1022 if( status == PSA_SUCCESS )
1023 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001024 if( status != PSA_SUCCESS )
1025 {
1026 /* TOnogrepDO: destroy what can be destroyed anyway */
1027 return( status );
1028 }
1029 }
1030#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1031
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001032 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001033 if( status != PSA_SUCCESS )
1034 return( status );
1035 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036}
1037
Gilles Peskineb870b182018-07-06 16:02:09 +02001038/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001039static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001040{
Gilles Peskine424f8942019-07-15 21:59:53 +02001041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001042 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskine424f8942019-07-15 21:59:53 +02001043 return( slot->data.se.bits );
1044#endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
1045
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +02001047 return( slot->data.raw.bytes * 8 );
1048#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001049 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001050 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001051#endif /* defined(MBEDTLS_RSA_C) */
1052#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001053 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +02001054 return( slot->data.ecp->grp.pbits );
1055#endif /* defined(MBEDTLS_ECP_C) */
1056 /* Shouldn't happen except on an empty slot. */
1057 return( 0 );
1058}
1059
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001060void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1061{
Gilles Peskineb699f072019-04-26 16:06:02 +02001062 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001063 memset( attributes, 0, sizeof( *attributes ) );
1064}
1065
Gilles Peskineb699f072019-04-26 16:06:02 +02001066psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1067 psa_key_type_t type,
1068 const uint8_t *data,
1069 size_t data_length )
1070{
1071 uint8_t *copy = NULL;
1072
1073 if( data_length != 0 )
1074 {
1075 copy = mbedtls_calloc( 1, data_length );
1076 if( copy == NULL )
1077 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1078 memcpy( copy, data, data_length );
1079 }
1080 /* After this point, this function is guaranteed to succeed, so it
1081 * can start modifying `*attributes`. */
1082
1083 if( attributes->domain_parameters != NULL )
1084 {
1085 mbedtls_free( attributes->domain_parameters );
1086 attributes->domain_parameters = NULL;
1087 attributes->domain_parameters_size = 0;
1088 }
1089
1090 attributes->domain_parameters = copy;
1091 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001092 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001093 return( PSA_SUCCESS );
1094}
1095
1096psa_status_t psa_get_key_domain_parameters(
1097 const psa_key_attributes_t *attributes,
1098 uint8_t *data, size_t data_size, size_t *data_length )
1099{
1100 if( attributes->domain_parameters_size > data_size )
1101 return( PSA_ERROR_BUFFER_TOO_SMALL );
1102 *data_length = attributes->domain_parameters_size;
1103 if( attributes->domain_parameters_size != 0 )
1104 memcpy( data, attributes->domain_parameters,
1105 attributes->domain_parameters_size );
1106 return( PSA_SUCCESS );
1107}
1108
1109#if defined(MBEDTLS_RSA_C)
1110static psa_status_t psa_get_rsa_public_exponent(
1111 const mbedtls_rsa_context *rsa,
1112 psa_key_attributes_t *attributes )
1113{
1114 mbedtls_mpi mpi;
1115 int ret;
1116 uint8_t *buffer = NULL;
1117 size_t buflen;
1118 mbedtls_mpi_init( &mpi );
1119
1120 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1121 if( ret != 0 )
1122 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001123 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1124 {
1125 /* It's the default value, which is reported as an empty string,
1126 * so there's nothing to do. */
1127 goto exit;
1128 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001129
1130 buflen = mbedtls_mpi_size( &mpi );
1131 buffer = mbedtls_calloc( 1, buflen );
1132 if( buffer == NULL )
1133 {
1134 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1135 goto exit;
1136 }
1137 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1138 if( ret != 0 )
1139 goto exit;
1140 attributes->domain_parameters = buffer;
1141 attributes->domain_parameters_size = buflen;
1142
1143exit:
1144 mbedtls_mpi_free( &mpi );
1145 if( ret != 0 )
1146 mbedtls_free( buffer );
1147 return( mbedtls_to_psa_error( ret ) );
1148}
1149#endif /* MBEDTLS_RSA_C */
1150
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001151/** Retrieve the generic attributes of a key in a slot.
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001152 *
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001153 * This function does not retrieve domain parameters, which require
1154 * additional memory management.
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001155 */
1156static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1157 psa_key_attributes_t *attributes )
1158{
Gilles Peskine8e338702019-07-30 20:06:31 +02001159 attributes->core.id = slot->attr.id;
1160 attributes->core.lifetime = slot->attr.lifetime;
1161 attributes->core.policy = slot->attr.policy;
1162 attributes->core.type = slot->attr.type;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001163 attributes->core.bits = psa_get_key_slot_bits( slot );
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001164}
1165
1166/** Retrieve all the publicly-accessible attributes of a key.
1167 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1169 psa_key_attributes_t *attributes )
1170{
1171 psa_key_slot_t *slot;
1172 psa_status_t status;
1173
1174 psa_reset_key_attributes( attributes );
1175
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001176 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177 if( status != PSA_SUCCESS )
1178 return( status );
1179
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001180 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskineb699f072019-04-26 16:06:02 +02001181
Gilles Peskine8e338702019-07-30 20:06:31 +02001182 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001183 {
1184#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001185 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001187#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1188 /* TOnogrepDO: reporting the public exponent for opaque keys
1189 * is not yet implemented. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001190 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191 break;
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1194 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001195#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001196 default:
1197 /* Nothing else to do. */
1198 break;
1199 }
1200
1201 if( status != PSA_SUCCESS )
1202 psa_reset_key_attributes( attributes );
1203 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204}
1205
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001206#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001207static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1208 unsigned char *buf, size_t size )
1209{
1210 int ret;
1211 unsigned char *c;
1212 size_t len = 0;
1213
1214 c = buf + size;
1215
1216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1217
1218 return( (int) len );
1219}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001220#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001221
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001222static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1223 uint8_t *data,
1224 size_t data_size,
1225 size_t *data_length,
1226 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001227{
Gilles Peskine5d309672019-07-12 23:47:28 +02001228#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1229 const psa_drv_se_t *drv;
1230 psa_drv_se_context_t *drv_context;
1231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1232
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001233 *data_length = 0;
1234
Gilles Peskine8e338702019-07-30 20:06:31 +02001235 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001236 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001237
Gilles Peskine5d309672019-07-12 23:47:28 +02001238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001239 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001240 {
1241 psa_drv_se_export_key_t method;
1242 if( drv->key_management == NULL )
1243 return( PSA_ERROR_NOT_SUPPORTED );
1244 method = ( export_public_key ?
1245 drv->key_management->p_export_public :
1246 drv->key_management->p_export );
1247 if( method == NULL )
1248 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001249 return( method( drv_context,
1250 slot->data.se.slot_number,
1251 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001252 }
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1254
Gilles Peskine8e338702019-07-30 20:06:31 +02001255 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256 {
1257 if( slot->data.raw.bytes > data_size )
1258 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001259 if( data_size != 0 )
1260 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001261 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001262 memset( data + slot->data.raw.bytes, 0,
1263 data_size - slot->data.raw.bytes );
1264 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 *data_length = slot->data.raw.bytes;
1266 return( PSA_SUCCESS );
1267 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001268#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001269 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001270 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001271 psa_status_t status;
1272
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001273 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001274 if( bytes > data_size )
1275 return( PSA_ERROR_BUFFER_TOO_SMALL );
1276 status = mbedtls_to_psa_error(
1277 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1278 if( status != PSA_SUCCESS )
1279 return( status );
1280 memset( data + bytes, 0, data_size - bytes );
1281 *data_length = bytes;
1282 return( PSA_SUCCESS );
1283 }
1284#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 else
Moran Peker17e36e12018-05-02 12:55:20 +03001286 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001287#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001288 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1289 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001290 {
Moran Pekera998bc62018-04-16 18:16:20 +03001291 mbedtls_pk_context pk;
1292 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001293 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001294 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001295#if defined(MBEDTLS_RSA_C)
1296 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001297 pk.pk_info = &mbedtls_rsa_info;
1298 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001299#else
1300 return( PSA_ERROR_NOT_SUPPORTED );
1301#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001302 }
1303 else
1304 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001305#if defined(MBEDTLS_ECP_C)
1306 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001307 pk.pk_info = &mbedtls_eckey_info;
1308 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001309#else
1310 return( PSA_ERROR_NOT_SUPPORTED );
1311#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001312 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001313 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001314 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001315 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001316 }
Moran Peker17e36e12018-05-02 12:55:20 +03001317 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001318 {
Moran Peker17e36e12018-05-02 12:55:20 +03001319 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001320 }
Moran Peker60364322018-04-29 11:34:58 +03001321 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001322 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001323 /* If data_size is 0 then data may be NULL and then the
1324 * call to memset would have undefined behavior. */
1325 if( data_size != 0 )
1326 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001327 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001328 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001329 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1330 * Move the data to the beginning and erase remaining data
1331 * at the original location. */
1332 if( 2 * (size_t) ret <= data_size )
1333 {
1334 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001335 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001336 }
1337 else if( (size_t) ret < data_size )
1338 {
1339 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001340 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001341 }
Moran Pekera998bc62018-04-16 18:16:20 +03001342 *data_length = ret;
1343 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001344 }
1345 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001346#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001347 {
1348 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001349 it is valid for a special-purpose implementation to omit
1350 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001351 return( PSA_ERROR_NOT_SUPPORTED );
1352 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353 }
1354}
1355
Gilles Peskinec5487a82018-12-03 18:08:14 +01001356psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001357 uint8_t *data,
1358 size_t data_size,
1359 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001360{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001361 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001362 psa_status_t status;
1363
1364 /* Set the key to empty now, so that even when there are errors, we always
1365 * set data_length to a value between 0 and data_size. On error, setting
1366 * the key to empty is a good choice because an empty key representation is
1367 * unlikely to be accepted anywhere. */
1368 *data_length = 0;
1369
1370 /* Export requires the EXPORT flag. There is an exception for public keys,
1371 * which don't require any flag, but psa_get_key_from_slot takes
1372 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001373 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001374 if( status != PSA_SUCCESS )
1375 return( status );
1376 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001377 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001378}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379
Gilles Peskinec5487a82018-12-03 18:08:14 +01001380psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001381 uint8_t *data,
1382 size_t data_size,
1383 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001384{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001385 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001386 psa_status_t status;
1387
1388 /* Set the key to empty now, so that even when there are errors, we always
1389 * set data_length to a value between 0 and data_size. On error, setting
1390 * the key to empty is a good choice because an empty key representation is
1391 * unlikely to be accepted anywhere. */
1392 *data_length = 0;
1393
1394 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001395 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001396 if( status != PSA_SUCCESS )
1397 return( status );
1398 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001399 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001400}
1401
Gilles Peskine4747d192019-04-17 15:05:45 +02001402static psa_status_t psa_set_key_policy_internal(
1403 psa_key_slot_t *slot,
1404 const psa_key_policy_t *policy )
1405{
1406 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001407 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001408 PSA_KEY_USAGE_ENCRYPT |
1409 PSA_KEY_USAGE_DECRYPT |
1410 PSA_KEY_USAGE_SIGN |
1411 PSA_KEY_USAGE_VERIFY |
1412 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1413 return( PSA_ERROR_INVALID_ARGUMENT );
1414
Gilles Peskine8e338702019-07-30 20:06:31 +02001415 slot->attr.policy = *policy;
Gilles Peskine4747d192019-04-17 15:05:45 +02001416 return( PSA_SUCCESS );
1417}
1418
1419/** Prepare a key slot to receive key material.
1420 *
1421 * This function allocates a key slot and sets its metadata.
1422 *
1423 * If this function fails, call psa_fail_key_creation().
1424 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001425 * This function is intended to be used as follows:
1426 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1427 * it with the specified attributes, and assign it a handle.
1428 * -# Populate the slot with the key material.
1429 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1430 * In case of failure at any step, stop the sequence and call
1431 * psa_fail_key_creation().
1432 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001433 * \param[in] attributes Key attributes for the new key.
1434 * \param[out] handle On success, a handle for the allocated slot.
1435 * \param[out] p_slot On success, a pointer to the prepared slot.
1436 * \param[out] p_drv On any return, the driver for the key, if any.
1437 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001438 *
1439 * \retval #PSA_SUCCESS
1440 * The key slot is ready to receive key material.
1441 * \return If this function fails, the key slot is an invalid state.
1442 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 */
1444static psa_status_t psa_start_key_creation(
1445 const psa_key_attributes_t *attributes,
1446 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001447 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001448 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001449{
1450 psa_status_t status;
1451 psa_key_slot_t *slot;
1452
Gilles Peskine011e4282019-06-26 18:34:38 +02001453 *p_drv = NULL;
1454
Gilles Peskine267c6562019-05-27 19:01:54 +02001455 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001456 if( status != PSA_SUCCESS )
1457 return( status );
1458 slot = *p_slot;
1459
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001460 status = psa_set_key_policy_internal( slot, &attributes->core.policy );
Gilles Peskine4747d192019-04-17 15:05:45 +02001461 if( status != PSA_SUCCESS )
1462 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02001463 slot->attr.lifetime = attributes->core.lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001464
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001465 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001466 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001467 status = psa_validate_persistent_key_parameters( attributes->core.lifetime,
1468 attributes->core.id,
Gilles Peskine011e4282019-06-26 18:34:38 +02001469 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001470 if( status != PSA_SUCCESS )
1471 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02001472 slot->attr.id = attributes->core.id;
Gilles Peskined167b942019-04-19 18:19:40 +02001473 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001474 slot->attr.type = attributes->core.type;
Gilles Peskine4747d192019-04-17 15:05:45 +02001475
Gilles Peskinec744d992019-07-30 17:26:54 +02001476 /* Refuse to create overly large keys.
1477 * Note that this doesn't trigger on import if the attributes don't
1478 * explicitly specify a size (so psa_get_key_bits returns 0), so
1479 * psa_import_key() needs its own checks. */
1480 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1481 return( PSA_ERROR_NOT_SUPPORTED );
1482
Gilles Peskinecbaff462019-07-12 23:46:04 +02001483#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001484 /* For a key in a secure element, we need to do three things:
1485 * create the key file in internal storage, create the
1486 * key inside the secure element, and update the driver's
1487 * persistent data. Start a transaction that will encompass these
1488 * three actions. */
1489 /* The first thing to do is to find a slot number for the new key.
1490 * We save the slot number in persistent storage as part of the
1491 * transaction data. It will be needed to recover if the power
1492 * fails during the key creation process, to clean up on the secure
1493 * element side after restarting. Obtaining a slot number from the
1494 * secure element driver updates its persistent state, but we do not yet
1495 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001496 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001497 if( *p_drv != NULL )
1498 {
1499 status = psa_find_se_slot_for_key( attributes, *p_drv,
1500 &slot->data.se.slot_number );
1501 if( status != PSA_SUCCESS )
1502 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001503 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001504 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001505 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001506 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001507 status = psa_crypto_save_transaction( );
1508 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001509 {
1510 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001511 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001512 }
Gilles Peskine424f8942019-07-15 21:59:53 +02001513
1514 /* TOnogrepDO: validate bits. How to do this depends on the key
1515 * creation method, so setting bits might not belong here. */
1516 slot->data.se.bits = psa_get_key_bits( attributes );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001517 }
1518#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1519
Gilles Peskine4747d192019-04-17 15:05:45 +02001520 return( status );
1521}
1522
1523/** Finalize the creation of a key once its key material has been set.
1524 *
1525 * This entails writing the key to persistent storage.
1526 *
1527 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001528 * See the documentation of psa_start_key_creation() for the intended use
1529 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001530 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001531 * \param[in,out] slot Pointer to the slot with key material.
1532 * \param[in] driver The secure element driver for the key,
1533 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001534 *
1535 * \retval #PSA_SUCCESS
1536 * The key was successfully created. The handle is now valid.
1537 * \return If this function fails, the key slot is an invalid state.
1538 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001539 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001540static psa_status_t psa_finish_key_creation(
1541 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001542 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001543{
1544 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001545 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001546 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001547
1548#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001549 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1552 psa_get_key_slot_attributes( slot, &attributes );
Gilles Peskine4747d192019-04-17 15:05:45 +02001553
Gilles Peskine1df83d42019-07-23 16:13:14 +02001554#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1555 if( driver != NULL )
1556 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001557 status = psa_save_persistent_key( &attributes,
1558 (uint8_t*) &slot->data.se,
1559 sizeof( slot->data.se ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001560 }
1561 else
1562#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1563 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001564 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001565 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001566 psa_get_key_bits( &attributes ) );
1567 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1568 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001569 if( buffer == NULL && buffer_size != 0 )
1570 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1571 status = psa_internal_export_key( slot,
1572 buffer, buffer_size, &length,
1573 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001574 if( status == PSA_SUCCESS )
1575 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001576
Gilles Peskine1df83d42019-07-23 16:13:14 +02001577 if( buffer_size != 0 )
1578 mbedtls_platform_zeroize( buffer, buffer_size );
1579 mbedtls_free( buffer );
1580 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 }
1582#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1583
Gilles Peskinecbaff462019-07-12 23:46:04 +02001584#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1585 if( driver != NULL )
1586 {
1587 status = psa_save_se_persistent_data( driver );
1588 if( status != PSA_SUCCESS )
1589 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001590 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001591 return( status );
1592 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001593 status = psa_crypto_stop_transaction( );
1594 if( status != PSA_SUCCESS )
1595 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001596 }
1597#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1598
Gilles Peskine4747d192019-04-17 15:05:45 +02001599 return( status );
1600}
1601
1602/** Abort the creation of a key.
1603 *
1604 * You may call this function after calling psa_start_key_creation(),
1605 * or after psa_finish_key_creation() fails. In other circumstances, this
1606 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001607 * See the documentation of psa_start_key_creation() for the intended use
1608 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 * \param[in,out] slot Pointer to the slot with key material.
1611 * \param[in] driver The secure element driver for the key,
1612 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001613 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001614static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001615 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001616{
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 (void) driver;
1618
Gilles Peskine4747d192019-04-17 15:05:45 +02001619 if( slot == NULL )
1620 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001621
1622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1623 /* TOnogrepDO: If the key has already been created in the secure
1624 * element, and the failure happened later (when saving metadata
1625 * to internal storage), we need to destroy the key in the secure
1626 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001627
1628 /* Abort the ongoing transaction if any. We already did what it
1629 * takes to undo any partial creation. All that's left is to update
1630 * the transaction data itself. */
1631 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001632#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1633
Gilles Peskine4747d192019-04-17 15:05:45 +02001634 psa_wipe_key_slot( slot );
1635}
1636
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001637static psa_status_t psa_check_key_slot_attributes(
1638 const psa_key_slot_t *slot,
1639 const psa_key_attributes_t *attributes )
1640{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001641 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001642 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001643 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001644 return( PSA_ERROR_INVALID_ARGUMENT );
1645 }
1646
1647 if( attributes->domain_parameters_size != 0 )
1648 {
1649#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001650 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001651 {
1652 mbedtls_mpi actual, required;
1653 int ret;
1654 mbedtls_mpi_init( &actual );
1655 mbedtls_mpi_init( &required );
1656 ret = mbedtls_rsa_export( slot->data.rsa,
1657 NULL, NULL, NULL, NULL, &actual );
1658 if( ret != 0 )
1659 goto rsa_exit;
1660 ret = mbedtls_mpi_read_binary( &required,
1661 attributes->domain_parameters,
1662 attributes->domain_parameters_size );
1663 if( ret != 0 )
1664 goto rsa_exit;
1665 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1666 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1667 rsa_exit:
1668 mbedtls_mpi_free( &actual );
1669 mbedtls_mpi_free( &required );
1670 if( ret != 0)
1671 return( mbedtls_to_psa_error( ret ) );
1672 }
1673 else
1674#endif
1675 {
1676 return( PSA_ERROR_INVALID_ARGUMENT );
1677 }
1678 }
1679
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001680 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001681 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001682 if( attributes->core.bits != psa_get_key_slot_bits( slot ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001683 return( PSA_ERROR_INVALID_ARGUMENT );
1684 }
1685
1686 return( PSA_SUCCESS );
1687}
1688
Gilles Peskine4747d192019-04-17 15:05:45 +02001689psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001691 size_t data_length,
1692 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001693{
1694 psa_status_t status;
1695 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001696 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001697
Gilles Peskine011e4282019-06-26 18:34:38 +02001698 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001699 if( status != PSA_SUCCESS )
1700 goto exit;
1701
Gilles Peskine5d309672019-07-12 23:47:28 +02001702#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1703 if( driver != NULL )
1704 {
1705 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1706 if( drv->key_management == NULL ||
1707 drv->key_management->p_import == NULL )
1708 {
1709 status = PSA_ERROR_NOT_SUPPORTED;
1710 goto exit;
1711 }
1712 status = drv->key_management->p_import(
1713 psa_get_se_driver_context( driver ),
1714 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001715 slot->attr.lifetime, slot->attr.type,
1716 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001717 data, data_length,
1718 &slot->data.se.bits );
Gilles Peskine5d309672019-07-12 23:47:28 +02001719 }
1720 else
1721#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1722 {
1723 status = psa_import_key_into_slot( slot, data, data_length );
1724 if( status != PSA_SUCCESS )
1725 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001726 }
Gilles Peskine18017402019-07-24 20:25:59 +02001727 status = psa_check_key_slot_attributes( slot, attributes );
1728 if( status != PSA_SUCCESS )
1729 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001730
Gilles Peskine011e4282019-06-26 18:34:38 +02001731 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001732exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 if( status != PSA_SUCCESS )
1734 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 *handle = 0;
1737 }
1738 return( status );
1739}
1740
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001741static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001742 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001743{
1744 psa_status_t status;
1745 uint8_t *buffer = NULL;
1746 size_t buffer_size = 0;
1747 size_t length;
1748
Gilles Peskine8e338702019-07-30 20:06:31 +02001749 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001750 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001751 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001752 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001753 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001754 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1755 if( status != PSA_SUCCESS )
1756 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001757 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001758 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001759
1760exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001761 if( buffer_size != 0 )
1762 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001763 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001764 return( status );
1765}
1766
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001767psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1768 const psa_key_attributes_t *specified_attributes,
1769 psa_key_handle_t *target_handle )
1770{
1771 psa_status_t status;
1772 psa_key_slot_t *source_slot = NULL;
1773 psa_key_slot_t *target_slot = NULL;
1774 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001775 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001776
Gilles Peskine28f8f302019-07-24 13:30:31 +02001777 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001778 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001779 if( status != PSA_SUCCESS )
1780 goto exit;
1781
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001782 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1783 if( status != PSA_SUCCESS )
1784 goto exit;
1785
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001786 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001787 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001788 if( status != PSA_SUCCESS )
1789 goto exit;
1790
1791 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001792 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001793 if( status != PSA_SUCCESS )
1794 goto exit;
1795
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1797 if( driver != NULL )
1798 {
1799 /* Copying to a secure element is not implemented yet. */
1800 status = PSA_ERROR_NOT_SUPPORTED;
1801 goto exit;
1802 }
1803#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1804
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001805 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001806 if( status != PSA_SUCCESS )
1807 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001808
Gilles Peskine011e4282019-06-26 18:34:38 +02001809 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001810exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001811 if( status != PSA_SUCCESS )
1812 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001813 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001814 *target_handle = 0;
1815 }
1816 return( status );
1817}
1818
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001819
1820
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001821/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001822/* Message digests */
1823/****************************************************************/
1824
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001825static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001826{
1827 switch( alg )
1828 {
1829#if defined(MBEDTLS_MD2_C)
1830 case PSA_ALG_MD2:
1831 return( &mbedtls_md2_info );
1832#endif
1833#if defined(MBEDTLS_MD4_C)
1834 case PSA_ALG_MD4:
1835 return( &mbedtls_md4_info );
1836#endif
1837#if defined(MBEDTLS_MD5_C)
1838 case PSA_ALG_MD5:
1839 return( &mbedtls_md5_info );
1840#endif
1841#if defined(MBEDTLS_RIPEMD160_C)
1842 case PSA_ALG_RIPEMD160:
1843 return( &mbedtls_ripemd160_info );
1844#endif
1845#if defined(MBEDTLS_SHA1_C)
1846 case PSA_ALG_SHA_1:
1847 return( &mbedtls_sha1_info );
1848#endif
1849#if defined(MBEDTLS_SHA256_C)
1850 case PSA_ALG_SHA_224:
1851 return( &mbedtls_sha224_info );
1852 case PSA_ALG_SHA_256:
1853 return( &mbedtls_sha256_info );
1854#endif
1855#if defined(MBEDTLS_SHA512_C)
1856 case PSA_ALG_SHA_384:
1857 return( &mbedtls_sha384_info );
1858 case PSA_ALG_SHA_512:
1859 return( &mbedtls_sha512_info );
1860#endif
1861 default:
1862 return( NULL );
1863 }
1864}
1865
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001866psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1867{
1868 switch( operation->alg )
1869 {
Gilles Peskine81736312018-06-26 15:04:31 +02001870 case 0:
1871 /* The object has (apparently) been initialized but it is not
1872 * in use. It's ok to call abort on such an object, and there's
1873 * nothing to do. */
1874 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001875#if defined(MBEDTLS_MD2_C)
1876 case PSA_ALG_MD2:
1877 mbedtls_md2_free( &operation->ctx.md2 );
1878 break;
1879#endif
1880#if defined(MBEDTLS_MD4_C)
1881 case PSA_ALG_MD4:
1882 mbedtls_md4_free( &operation->ctx.md4 );
1883 break;
1884#endif
1885#if defined(MBEDTLS_MD5_C)
1886 case PSA_ALG_MD5:
1887 mbedtls_md5_free( &operation->ctx.md5 );
1888 break;
1889#endif
1890#if defined(MBEDTLS_RIPEMD160_C)
1891 case PSA_ALG_RIPEMD160:
1892 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1893 break;
1894#endif
1895#if defined(MBEDTLS_SHA1_C)
1896 case PSA_ALG_SHA_1:
1897 mbedtls_sha1_free( &operation->ctx.sha1 );
1898 break;
1899#endif
1900#if defined(MBEDTLS_SHA256_C)
1901 case PSA_ALG_SHA_224:
1902 case PSA_ALG_SHA_256:
1903 mbedtls_sha256_free( &operation->ctx.sha256 );
1904 break;
1905#endif
1906#if defined(MBEDTLS_SHA512_C)
1907 case PSA_ALG_SHA_384:
1908 case PSA_ALG_SHA_512:
1909 mbedtls_sha512_free( &operation->ctx.sha512 );
1910 break;
1911#endif
1912 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001913 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001914 }
1915 operation->alg = 0;
1916 return( PSA_SUCCESS );
1917}
1918
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001919psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001920 psa_algorithm_t alg )
1921{
1922 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001923
1924 /* A context must be freshly initialized before it can be set up. */
1925 if( operation->alg != 0 )
1926 {
1927 return( PSA_ERROR_BAD_STATE );
1928 }
1929
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001930 switch( alg )
1931 {
1932#if defined(MBEDTLS_MD2_C)
1933 case PSA_ALG_MD2:
1934 mbedtls_md2_init( &operation->ctx.md2 );
1935 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1936 break;
1937#endif
1938#if defined(MBEDTLS_MD4_C)
1939 case PSA_ALG_MD4:
1940 mbedtls_md4_init( &operation->ctx.md4 );
1941 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1942 break;
1943#endif
1944#if defined(MBEDTLS_MD5_C)
1945 case PSA_ALG_MD5:
1946 mbedtls_md5_init( &operation->ctx.md5 );
1947 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1948 break;
1949#endif
1950#if defined(MBEDTLS_RIPEMD160_C)
1951 case PSA_ALG_RIPEMD160:
1952 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1953 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1954 break;
1955#endif
1956#if defined(MBEDTLS_SHA1_C)
1957 case PSA_ALG_SHA_1:
1958 mbedtls_sha1_init( &operation->ctx.sha1 );
1959 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1960 break;
1961#endif
1962#if defined(MBEDTLS_SHA256_C)
1963 case PSA_ALG_SHA_224:
1964 mbedtls_sha256_init( &operation->ctx.sha256 );
1965 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1966 break;
1967 case PSA_ALG_SHA_256:
1968 mbedtls_sha256_init( &operation->ctx.sha256 );
1969 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1970 break;
1971#endif
1972#if defined(MBEDTLS_SHA512_C)
1973 case PSA_ALG_SHA_384:
1974 mbedtls_sha512_init( &operation->ctx.sha512 );
1975 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1976 break;
1977 case PSA_ALG_SHA_512:
1978 mbedtls_sha512_init( &operation->ctx.sha512 );
1979 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1980 break;
1981#endif
1982 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001983 return( PSA_ALG_IS_HASH( alg ) ?
1984 PSA_ERROR_NOT_SUPPORTED :
1985 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001986 }
1987 if( ret == 0 )
1988 operation->alg = alg;
1989 else
1990 psa_hash_abort( operation );
1991 return( mbedtls_to_psa_error( ret ) );
1992}
1993
1994psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1995 const uint8_t *input,
1996 size_t input_length )
1997{
1998 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001999
2000 /* Don't require hash implementations to behave correctly on a
2001 * zero-length input, which may have an invalid pointer. */
2002 if( input_length == 0 )
2003 return( PSA_SUCCESS );
2004
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002005 switch( operation->alg )
2006 {
2007#if defined(MBEDTLS_MD2_C)
2008 case PSA_ALG_MD2:
2009 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2010 input, input_length );
2011 break;
2012#endif
2013#if defined(MBEDTLS_MD4_C)
2014 case PSA_ALG_MD4:
2015 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2016 input, input_length );
2017 break;
2018#endif
2019#if defined(MBEDTLS_MD5_C)
2020 case PSA_ALG_MD5:
2021 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2022 input, input_length );
2023 break;
2024#endif
2025#if defined(MBEDTLS_RIPEMD160_C)
2026 case PSA_ALG_RIPEMD160:
2027 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2028 input, input_length );
2029 break;
2030#endif
2031#if defined(MBEDTLS_SHA1_C)
2032 case PSA_ALG_SHA_1:
2033 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2034 input, input_length );
2035 break;
2036#endif
2037#if defined(MBEDTLS_SHA256_C)
2038 case PSA_ALG_SHA_224:
2039 case PSA_ALG_SHA_256:
2040 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2041 input, input_length );
2042 break;
2043#endif
2044#if defined(MBEDTLS_SHA512_C)
2045 case PSA_ALG_SHA_384:
2046 case PSA_ALG_SHA_512:
2047 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2048 input, input_length );
2049 break;
2050#endif
2051 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002052 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002053 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002054
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002055 if( ret != 0 )
2056 psa_hash_abort( operation );
2057 return( mbedtls_to_psa_error( ret ) );
2058}
2059
2060psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2061 uint8_t *hash,
2062 size_t hash_size,
2063 size_t *hash_length )
2064{
itayzafrir40835d42018-08-02 13:14:17 +03002065 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002066 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002067 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002068
2069 /* Fill the output buffer with something that isn't a valid hash
2070 * (barring an attack on the hash and deliberately-crafted input),
2071 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002072 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002073 /* If hash_size is 0 then hash may be NULL and then the
2074 * call to memset would have undefined behavior. */
2075 if( hash_size != 0 )
2076 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002077
2078 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002079 {
2080 status = PSA_ERROR_BUFFER_TOO_SMALL;
2081 goto exit;
2082 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002083
2084 switch( operation->alg )
2085 {
2086#if defined(MBEDTLS_MD2_C)
2087 case PSA_ALG_MD2:
2088 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2089 break;
2090#endif
2091#if defined(MBEDTLS_MD4_C)
2092 case PSA_ALG_MD4:
2093 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2094 break;
2095#endif
2096#if defined(MBEDTLS_MD5_C)
2097 case PSA_ALG_MD5:
2098 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2099 break;
2100#endif
2101#if defined(MBEDTLS_RIPEMD160_C)
2102 case PSA_ALG_RIPEMD160:
2103 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2104 break;
2105#endif
2106#if defined(MBEDTLS_SHA1_C)
2107 case PSA_ALG_SHA_1:
2108 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2109 break;
2110#endif
2111#if defined(MBEDTLS_SHA256_C)
2112 case PSA_ALG_SHA_224:
2113 case PSA_ALG_SHA_256:
2114 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2115 break;
2116#endif
2117#if defined(MBEDTLS_SHA512_C)
2118 case PSA_ALG_SHA_384:
2119 case PSA_ALG_SHA_512:
2120 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2121 break;
2122#endif
2123 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002124 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002125 }
itayzafrir40835d42018-08-02 13:14:17 +03002126 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127
itayzafrir40835d42018-08-02 13:14:17 +03002128exit:
2129 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002130 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002131 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002132 return( psa_hash_abort( operation ) );
2133 }
2134 else
2135 {
2136 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002137 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002138 }
2139}
2140
Gilles Peskine2d277862018-06-18 15:41:12 +02002141psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2142 const uint8_t *hash,
2143 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144{
2145 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2146 size_t actual_hash_length;
2147 psa_status_t status = psa_hash_finish( operation,
2148 actual_hash, sizeof( actual_hash ),
2149 &actual_hash_length );
2150 if( status != PSA_SUCCESS )
2151 return( status );
2152 if( actual_hash_length != hash_length )
2153 return( PSA_ERROR_INVALID_SIGNATURE );
2154 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2155 return( PSA_ERROR_INVALID_SIGNATURE );
2156 return( PSA_SUCCESS );
2157}
2158
Gilles Peskineeb35d782019-01-22 17:56:16 +01002159psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2160 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002161{
2162 if( target_operation->alg != 0 )
2163 return( PSA_ERROR_BAD_STATE );
2164
2165 switch( source_operation->alg )
2166 {
2167 case 0:
2168 return( PSA_ERROR_BAD_STATE );
2169#if defined(MBEDTLS_MD2_C)
2170 case PSA_ALG_MD2:
2171 mbedtls_md2_clone( &target_operation->ctx.md2,
2172 &source_operation->ctx.md2 );
2173 break;
2174#endif
2175#if defined(MBEDTLS_MD4_C)
2176 case PSA_ALG_MD4:
2177 mbedtls_md4_clone( &target_operation->ctx.md4,
2178 &source_operation->ctx.md4 );
2179 break;
2180#endif
2181#if defined(MBEDTLS_MD5_C)
2182 case PSA_ALG_MD5:
2183 mbedtls_md5_clone( &target_operation->ctx.md5,
2184 &source_operation->ctx.md5 );
2185 break;
2186#endif
2187#if defined(MBEDTLS_RIPEMD160_C)
2188 case PSA_ALG_RIPEMD160:
2189 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2190 &source_operation->ctx.ripemd160 );
2191 break;
2192#endif
2193#if defined(MBEDTLS_SHA1_C)
2194 case PSA_ALG_SHA_1:
2195 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2196 &source_operation->ctx.sha1 );
2197 break;
2198#endif
2199#if defined(MBEDTLS_SHA256_C)
2200 case PSA_ALG_SHA_224:
2201 case PSA_ALG_SHA_256:
2202 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2203 &source_operation->ctx.sha256 );
2204 break;
2205#endif
2206#if defined(MBEDTLS_SHA512_C)
2207 case PSA_ALG_SHA_384:
2208 case PSA_ALG_SHA_512:
2209 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2210 &source_operation->ctx.sha512 );
2211 break;
2212#endif
2213 default:
2214 return( PSA_ERROR_NOT_SUPPORTED );
2215 }
2216
2217 target_operation->alg = source_operation->alg;
2218 return( PSA_SUCCESS );
2219}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220
2221
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223/* MAC */
2224/****************************************************************/
2225
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002226static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002227 psa_algorithm_t alg,
2228 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002229 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002230 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002231{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002232 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002233 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002234
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002235 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002236 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002237
Gilles Peskine8c9def32018-02-08 10:02:12 +01002238 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2239 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002240 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002242 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002243 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002244 mode = MBEDTLS_MODE_STREAM;
2245 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002246 case PSA_ALG_CTR:
2247 mode = MBEDTLS_MODE_CTR;
2248 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002249 case PSA_ALG_CFB:
2250 mode = MBEDTLS_MODE_CFB;
2251 break;
2252 case PSA_ALG_OFB:
2253 mode = MBEDTLS_MODE_OFB;
2254 break;
2255 case PSA_ALG_CBC_NO_PADDING:
2256 mode = MBEDTLS_MODE_CBC;
2257 break;
2258 case PSA_ALG_CBC_PKCS7:
2259 mode = MBEDTLS_MODE_CBC;
2260 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002261 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002262 mode = MBEDTLS_MODE_CCM;
2263 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002264 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265 mode = MBEDTLS_MODE_GCM;
2266 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002267 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2268 mode = MBEDTLS_MODE_CHACHAPOLY;
2269 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002270 default:
2271 return( NULL );
2272 }
2273 }
2274 else if( alg == PSA_ALG_CMAC )
2275 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276 else
2277 return( NULL );
2278
2279 switch( key_type )
2280 {
2281 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002282 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002283 break;
2284 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002285 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2286 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002287 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002288 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002290 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002291 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2292 * but two-key Triple-DES is functionally three-key Triple-DES
2293 * with K1=K3, so that's how we present it to mbedtls. */
2294 if( key_bits == 128 )
2295 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296 break;
2297 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002298 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299 break;
2300 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002301 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002302 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002303 case PSA_KEY_TYPE_CHACHA20:
2304 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2305 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002306 default:
2307 return( NULL );
2308 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002309 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002310 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311
Jaeden Amero23bbb752018-06-26 14:16:54 +01002312 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2313 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002314}
2315
Gilles Peskinea05219c2018-11-16 16:02:56 +01002316#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002317static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002318{
Gilles Peskine2d277862018-06-18 15:41:12 +02002319 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002320 {
2321 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002322 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002323 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002324 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002325 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002326 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002327 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002328 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002329 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002330 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002331 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002332 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002333 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002334 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002335 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002336 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002337 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002338 return( 128 );
2339 default:
2340 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002341 }
2342}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002343#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002344
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345/* Initialize the MAC operation structure. Once this function has been
2346 * called, psa_mac_abort can run and will do the right thing. */
2347static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2348 psa_algorithm_t alg )
2349{
2350 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2351
2352 operation->alg = alg;
2353 operation->key_set = 0;
2354 operation->iv_set = 0;
2355 operation->iv_required = 0;
2356 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002357 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358
2359#if defined(MBEDTLS_CMAC_C)
2360 if( alg == PSA_ALG_CMAC )
2361 {
2362 operation->iv_required = 0;
2363 mbedtls_cipher_init( &operation->ctx.cmac );
2364 status = PSA_SUCCESS;
2365 }
2366 else
2367#endif /* MBEDTLS_CMAC_C */
2368#if defined(MBEDTLS_MD_C)
2369 if( PSA_ALG_IS_HMAC( operation->alg ) )
2370 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002371 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2372 operation->ctx.hmac.hash_ctx.alg = 0;
2373 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002374 }
2375 else
2376#endif /* MBEDTLS_MD_C */
2377 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002378 if( ! PSA_ALG_IS_MAC( alg ) )
2379 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380 }
2381
2382 if( status != PSA_SUCCESS )
2383 memset( operation, 0, sizeof( *operation ) );
2384 return( status );
2385}
2386
Gilles Peskine01126fa2018-07-12 17:04:55 +02002387#if defined(MBEDTLS_MD_C)
2388static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2389{
Gilles Peskine3f108122018-12-07 18:14:53 +01002390 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002391 return( psa_hash_abort( &hmac->hash_ctx ) );
2392}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002393
Janos Follath999f6482019-06-11 12:04:10 +01002394#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002395static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2396{
2397 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2398 memset( hmac, 0, sizeof( *hmac ) );
2399}
Janos Follath999f6482019-06-11 12:04:10 +01002400#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002401#endif /* MBEDTLS_MD_C */
2402
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2404{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002405 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002407 /* The object has (apparently) been initialized but it is not
2408 * in use. It's ok to call abort on such an object, and there's
2409 * nothing to do. */
2410 return( PSA_SUCCESS );
2411 }
2412 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002414 if( operation->alg == PSA_ALG_CMAC )
2415 {
2416 mbedtls_cipher_free( &operation->ctx.cmac );
2417 }
2418 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002421 if( PSA_ALG_IS_HMAC( operation->alg ) )
2422 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002423 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 }
2425 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002427 {
2428 /* Sanity check (shouldn't happen: operation->alg should
2429 * always have been initialized to a valid value). */
2430 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431 }
Moran Peker41deec42018-04-04 15:43:05 +03002432
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 operation->alg = 0;
2434 operation->key_set = 0;
2435 operation->iv_set = 0;
2436 operation->iv_required = 0;
2437 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002438 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002439
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002441
2442bad_state:
2443 /* If abort is called on an uninitialized object, we can't trust
2444 * anything. Wipe the object in case it contains confidential data.
2445 * This may result in a memory leak if a pointer gets overwritten,
2446 * but it's too late to do anything about this. */
2447 memset( operation, 0, sizeof( *operation ) );
2448 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449}
2450
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002451#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002452static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002453 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002454 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002455 const mbedtls_cipher_info_t *cipher_info )
2456{
2457 int ret;
2458
2459 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002460
2461 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2462 if( ret != 0 )
2463 return( ret );
2464
2465 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2466 slot->data.raw.data,
2467 key_bits );
2468 return( ret );
2469}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002470#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002471
Gilles Peskine248051a2018-06-20 16:09:38 +02002472#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002473static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2474 const uint8_t *key,
2475 size_t key_length,
2476 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002477{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002478 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002479 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002480 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002481 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002482 psa_status_t status;
2483
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002484 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2485 * overflow below. This should never trigger if the hash algorithm
2486 * is implemented correctly. */
2487 /* The size checks against the ipad and opad buffers cannot be written
2488 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2489 * because that triggers -Wlogical-op on GCC 7.3. */
2490 if( block_size > sizeof( ipad ) )
2491 return( PSA_ERROR_NOT_SUPPORTED );
2492 if( block_size > sizeof( hmac->opad ) )
2493 return( PSA_ERROR_NOT_SUPPORTED );
2494 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002495 return( PSA_ERROR_NOT_SUPPORTED );
2496
Gilles Peskined223b522018-06-11 18:12:58 +02002497 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002499 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002500 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002501 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002502 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002503 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002504 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002505 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002506 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002507 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002508 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002509 }
Gilles Peskine96889972018-07-12 17:07:03 +02002510 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2511 * but it is permitted. It is common when HMAC is used in HKDF, for
2512 * example. Don't call `memcpy` in the 0-length because `key` could be
2513 * an invalid pointer which would make the behavior undefined. */
2514 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002515 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002516
Gilles Peskined223b522018-06-11 18:12:58 +02002517 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2518 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002519 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002520 ipad[i] ^= 0x36;
2521 memset( ipad + key_length, 0x36, block_size - key_length );
2522
2523 /* Copy the key material from ipad to opad, flipping the requisite bits,
2524 * and filling the rest of opad with the requisite constant. */
2525 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002526 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2527 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002528
Gilles Peskine01126fa2018-07-12 17:04:55 +02002529 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002530 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002531 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002532
Gilles Peskine01126fa2018-07-12 17:04:55 +02002533 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002534
2535cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002536 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002537
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002538 return( status );
2539}
Gilles Peskine248051a2018-06-20 16:09:38 +02002540#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002541
Gilles Peskine89167cb2018-07-08 20:12:23 +02002542static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002543 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002544 psa_algorithm_t alg,
2545 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002548 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002550 psa_key_usage_t usage =
2551 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002552 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002553 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002555 /* A context must be freshly initialized before it can be set up. */
2556 if( operation->alg != 0 )
2557 {
2558 return( PSA_ERROR_BAD_STATE );
2559 }
2560
Gilles Peskined911eb72018-08-14 15:18:45 +02002561 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562 if( status != PSA_SUCCESS )
2563 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002564 if( is_sign )
2565 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566
Gilles Peskine28f8f302019-07-24 13:30:31 +02002567 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002568 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002569 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002570 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002571
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002573 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002574 {
2575 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002576 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002577 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002578 int ret;
2579 if( cipher_info == NULL )
2580 {
2581 status = PSA_ERROR_NOT_SUPPORTED;
2582 goto exit;
2583 }
2584 operation->mac_size = cipher_info->block_size;
2585 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2586 status = mbedtls_to_psa_error( ret );
2587 }
2588 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002591 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002592 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002593 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002594 if( hash_alg == 0 )
2595 {
2596 status = PSA_ERROR_NOT_SUPPORTED;
2597 goto exit;
2598 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002599
2600 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2601 /* Sanity check. This shouldn't fail on a valid configuration. */
2602 if( operation->mac_size == 0 ||
2603 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2604 {
2605 status = PSA_ERROR_NOT_SUPPORTED;
2606 goto exit;
2607 }
2608
Gilles Peskine8e338702019-07-30 20:06:31 +02002609 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002610 {
2611 status = PSA_ERROR_INVALID_ARGUMENT;
2612 goto exit;
2613 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002614
Gilles Peskine01126fa2018-07-12 17:04:55 +02002615 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2616 slot->data.raw.data,
2617 slot->data.raw.bytes,
2618 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002619 }
2620 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002622 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002623 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002624 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002626
Gilles Peskined911eb72018-08-14 15:18:45 +02002627 if( truncated == 0 )
2628 {
2629 /* The "normal" case: untruncated algorithm. Nothing to do. */
2630 }
2631 else if( truncated < 4 )
2632 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002633 /* A very short MAC is too short for security since it can be
2634 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2635 * so we make this our minimum, even though 32 bits is still
2636 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002637 status = PSA_ERROR_NOT_SUPPORTED;
2638 }
2639 else if( truncated > operation->mac_size )
2640 {
2641 /* It's impossible to "truncate" to a larger length. */
2642 status = PSA_ERROR_INVALID_ARGUMENT;
2643 }
2644 else
2645 operation->mac_size = truncated;
2646
Gilles Peskinefbfac682018-07-08 20:51:54 +02002647exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002648 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002650 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652 else
2653 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002654 operation->key_set = 1;
2655 }
2656 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657}
2658
Gilles Peskine89167cb2018-07-08 20:12:23 +02002659psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002660 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002661 psa_algorithm_t alg )
2662{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002663 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002664}
2665
2666psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002667 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002668 psa_algorithm_t alg )
2669{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002670 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002671}
2672
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2674 const uint8_t *input,
2675 size_t input_length )
2676{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002677 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002679 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002681 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682 operation->has_input = 1;
2683
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002685 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002686 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002687 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2688 input, input_length );
2689 status = mbedtls_to_psa_error( ret );
2690 }
2691 else
2692#endif /* MBEDTLS_CMAC_C */
2693#if defined(MBEDTLS_MD_C)
2694 if( PSA_ALG_IS_HMAC( operation->alg ) )
2695 {
2696 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2697 input_length );
2698 }
2699 else
2700#endif /* MBEDTLS_MD_C */
2701 {
2702 /* This shouldn't happen if `operation` was initialized by
2703 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002704 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002705 }
2706
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707 if( status != PSA_SUCCESS )
2708 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002709 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710}
2711
Gilles Peskine01126fa2018-07-12 17:04:55 +02002712#if defined(MBEDTLS_MD_C)
2713static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2714 uint8_t *mac,
2715 size_t mac_size )
2716{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002717 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002718 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2719 size_t hash_size = 0;
2720 size_t block_size = psa_get_hash_block_size( hash_alg );
2721 psa_status_t status;
2722
Gilles Peskine01126fa2018-07-12 17:04:55 +02002723 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2724 if( status != PSA_SUCCESS )
2725 return( status );
2726 /* From here on, tmp needs to be wiped. */
2727
2728 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2729 if( status != PSA_SUCCESS )
2730 goto exit;
2731
2732 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2733 if( status != PSA_SUCCESS )
2734 goto exit;
2735
2736 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2737 if( status != PSA_SUCCESS )
2738 goto exit;
2739
Gilles Peskined911eb72018-08-14 15:18:45 +02002740 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2741 if( status != PSA_SUCCESS )
2742 goto exit;
2743
2744 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002745
2746exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002747 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002748 return( status );
2749}
2750#endif /* MBEDTLS_MD_C */
2751
mohammad16036df908f2018-04-02 08:34:15 -07002752static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002753 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002754 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002756 if( ! operation->key_set )
2757 return( PSA_ERROR_BAD_STATE );
2758 if( operation->iv_required && ! operation->iv_set )
2759 return( PSA_ERROR_BAD_STATE );
2760
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761 if( mac_size < operation->mac_size )
2762 return( PSA_ERROR_BUFFER_TOO_SMALL );
2763
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002765 if( operation->alg == PSA_ALG_CMAC )
2766 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002767 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2768 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2769 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002770 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002771 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002772 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002773 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002774 else
2775#endif /* MBEDTLS_CMAC_C */
2776#if defined(MBEDTLS_MD_C)
2777 if( PSA_ALG_IS_HMAC( operation->alg ) )
2778 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002779 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002780 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002781 }
2782 else
2783#endif /* MBEDTLS_MD_C */
2784 {
2785 /* This shouldn't happen if `operation` was initialized by
2786 * a setup function. */
2787 return( PSA_ERROR_BAD_STATE );
2788 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789}
2790
Gilles Peskineacd4be32018-07-08 19:56:25 +02002791psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2792 uint8_t *mac,
2793 size_t mac_size,
2794 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002795{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002796 psa_status_t status;
2797
Jaeden Amero252ef282019-02-15 14:05:35 +00002798 if( operation->alg == 0 )
2799 {
2800 return( PSA_ERROR_BAD_STATE );
2801 }
2802
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002803 /* Fill the output buffer with something that isn't a valid mac
2804 * (barring an attack on the mac and deliberately-crafted input),
2805 * in case the caller doesn't check the return status properly. */
2806 *mac_length = mac_size;
2807 /* If mac_size is 0 then mac may be NULL and then the
2808 * call to memset would have undefined behavior. */
2809 if( mac_size != 0 )
2810 memset( mac, '!', mac_size );
2811
Gilles Peskine89167cb2018-07-08 20:12:23 +02002812 if( ! operation->is_sign )
2813 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002814 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002815 }
mohammad16036df908f2018-04-02 08:34:15 -07002816
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002817 status = psa_mac_finish_internal( operation, mac, mac_size );
2818
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002819 if( status == PSA_SUCCESS )
2820 {
2821 status = psa_mac_abort( operation );
2822 if( status == PSA_SUCCESS )
2823 *mac_length = operation->mac_size;
2824 else
2825 memset( mac, '!', mac_size );
2826 }
2827 else
2828 psa_mac_abort( operation );
2829 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002830}
2831
Gilles Peskineacd4be32018-07-08 19:56:25 +02002832psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2833 const uint8_t *mac,
2834 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835{
Gilles Peskine828ed142018-06-18 23:25:51 +02002836 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002837 psa_status_t status;
2838
Jaeden Amero252ef282019-02-15 14:05:35 +00002839 if( operation->alg == 0 )
2840 {
2841 return( PSA_ERROR_BAD_STATE );
2842 }
2843
Gilles Peskine89167cb2018-07-08 20:12:23 +02002844 if( operation->is_sign )
2845 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002846 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002847 }
2848 if( operation->mac_size != mac_length )
2849 {
2850 status = PSA_ERROR_INVALID_SIGNATURE;
2851 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002852 }
mohammad16036df908f2018-04-02 08:34:15 -07002853
2854 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002855 actual_mac, sizeof( actual_mac ) );
2856
2857 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2858 status = PSA_ERROR_INVALID_SIGNATURE;
2859
2860cleanup:
2861 if( status == PSA_SUCCESS )
2862 status = psa_mac_abort( operation );
2863 else
2864 psa_mac_abort( operation );
2865
Gilles Peskine3f108122018-12-07 18:14:53 +01002866 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002867
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002868 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869}
2870
2871
Gilles Peskine20035e32018-02-03 22:44:14 +01002872
Gilles Peskine20035e32018-02-03 22:44:14 +01002873/****************************************************************/
2874/* Asymmetric cryptography */
2875/****************************************************************/
2876
Gilles Peskine2b450e32018-06-27 15:42:46 +02002877#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002878/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002879 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002880static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2881 size_t hash_length,
2882 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002883{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002884 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002885 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002886 *md_alg = mbedtls_md_get_type( md_info );
2887
2888 /* The Mbed TLS RSA module uses an unsigned int for hash length
2889 * parameters. Validate that it fits so that we don't risk an
2890 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002891#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002892 if( hash_length > UINT_MAX )
2893 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002894#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002895
2896#if defined(MBEDTLS_PKCS1_V15)
2897 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2898 * must be correct. */
2899 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2900 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002901 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002902 if( md_info == NULL )
2903 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002904 if( mbedtls_md_get_size( md_info ) != hash_length )
2905 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002906 }
2907#endif /* MBEDTLS_PKCS1_V15 */
2908
2909#if defined(MBEDTLS_PKCS1_V21)
2910 /* PSS requires a hash internally. */
2911 if( PSA_ALG_IS_RSA_PSS( alg ) )
2912 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002913 if( md_info == NULL )
2914 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002915 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002916#endif /* MBEDTLS_PKCS1_V21 */
2917
Gilles Peskine61b91d42018-06-08 16:09:36 +02002918 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002919}
2920
Gilles Peskine2b450e32018-06-27 15:42:46 +02002921static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2922 psa_algorithm_t alg,
2923 const uint8_t *hash,
2924 size_t hash_length,
2925 uint8_t *signature,
2926 size_t signature_size,
2927 size_t *signature_length )
2928{
2929 psa_status_t status;
2930 int ret;
2931 mbedtls_md_type_t md_alg;
2932
2933 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2934 if( status != PSA_SUCCESS )
2935 return( status );
2936
Gilles Peskine630a18a2018-06-29 17:49:35 +02002937 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002938 return( PSA_ERROR_BUFFER_TOO_SMALL );
2939
2940#if defined(MBEDTLS_PKCS1_V15)
2941 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2942 {
2943 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2944 MBEDTLS_MD_NONE );
2945 ret = mbedtls_rsa_pkcs1_sign( rsa,
2946 mbedtls_ctr_drbg_random,
2947 &global_data.ctr_drbg,
2948 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002949 md_alg,
2950 (unsigned int) hash_length,
2951 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002952 signature );
2953 }
2954 else
2955#endif /* MBEDTLS_PKCS1_V15 */
2956#if defined(MBEDTLS_PKCS1_V21)
2957 if( PSA_ALG_IS_RSA_PSS( alg ) )
2958 {
2959 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2960 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2961 mbedtls_ctr_drbg_random,
2962 &global_data.ctr_drbg,
2963 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002964 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002965 (unsigned int) hash_length,
2966 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002967 signature );
2968 }
2969 else
2970#endif /* MBEDTLS_PKCS1_V21 */
2971 {
2972 return( PSA_ERROR_INVALID_ARGUMENT );
2973 }
2974
2975 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002976 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002977 return( mbedtls_to_psa_error( ret ) );
2978}
2979
2980static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2981 psa_algorithm_t alg,
2982 const uint8_t *hash,
2983 size_t hash_length,
2984 const uint8_t *signature,
2985 size_t signature_length )
2986{
2987 psa_status_t status;
2988 int ret;
2989 mbedtls_md_type_t md_alg;
2990
2991 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2992 if( status != PSA_SUCCESS )
2993 return( status );
2994
Gilles Peskine630a18a2018-06-29 17:49:35 +02002995 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002996 return( PSA_ERROR_BUFFER_TOO_SMALL );
2997
2998#if defined(MBEDTLS_PKCS1_V15)
2999 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3000 {
3001 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3002 MBEDTLS_MD_NONE );
3003 ret = mbedtls_rsa_pkcs1_verify( rsa,
3004 mbedtls_ctr_drbg_random,
3005 &global_data.ctr_drbg,
3006 MBEDTLS_RSA_PUBLIC,
3007 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003008 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003009 hash,
3010 signature );
3011 }
3012 else
3013#endif /* MBEDTLS_PKCS1_V15 */
3014#if defined(MBEDTLS_PKCS1_V21)
3015 if( PSA_ALG_IS_RSA_PSS( alg ) )
3016 {
3017 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3018 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3019 mbedtls_ctr_drbg_random,
3020 &global_data.ctr_drbg,
3021 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003022 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003023 (unsigned int) hash_length,
3024 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003025 signature );
3026 }
3027 else
3028#endif /* MBEDTLS_PKCS1_V21 */
3029 {
3030 return( PSA_ERROR_INVALID_ARGUMENT );
3031 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003032
3033 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3034 * the rest of the signature is invalid". This has little use in
3035 * practice and PSA doesn't report this distinction. */
3036 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3037 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003038 return( mbedtls_to_psa_error( ret ) );
3039}
3040#endif /* MBEDTLS_RSA_C */
3041
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003042#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003043/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3044 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3045 * (even though these functions don't modify it). */
3046static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3047 psa_algorithm_t alg,
3048 const uint8_t *hash,
3049 size_t hash_length,
3050 uint8_t *signature,
3051 size_t signature_size,
3052 size_t *signature_length )
3053{
3054 int ret;
3055 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003056 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003057 mbedtls_mpi_init( &r );
3058 mbedtls_mpi_init( &s );
3059
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003060 if( signature_size < 2 * curve_bytes )
3061 {
3062 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3063 goto cleanup;
3064 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003065
Gilles Peskinea05219c2018-11-16 16:02:56 +01003066#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003067 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3068 {
3069 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3070 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3071 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3072 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3073 hash, hash_length,
3074 md_alg ) );
3075 }
3076 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003077#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003078 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003079 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003080 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3081 hash, hash_length,
3082 mbedtls_ctr_drbg_random,
3083 &global_data.ctr_drbg ) );
3084 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003085
3086 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3087 signature,
3088 curve_bytes ) );
3089 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3090 signature + curve_bytes,
3091 curve_bytes ) );
3092
3093cleanup:
3094 mbedtls_mpi_free( &r );
3095 mbedtls_mpi_free( &s );
3096 if( ret == 0 )
3097 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003098 return( mbedtls_to_psa_error( ret ) );
3099}
3100
3101static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3102 const uint8_t *hash,
3103 size_t hash_length,
3104 const uint8_t *signature,
3105 size_t signature_length )
3106{
3107 int ret;
3108 mbedtls_mpi r, s;
3109 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3110 mbedtls_mpi_init( &r );
3111 mbedtls_mpi_init( &s );
3112
3113 if( signature_length != 2 * curve_bytes )
3114 return( PSA_ERROR_INVALID_SIGNATURE );
3115
3116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3117 signature,
3118 curve_bytes ) );
3119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3120 signature + curve_bytes,
3121 curve_bytes ) );
3122
3123 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3124 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003125
3126cleanup:
3127 mbedtls_mpi_free( &r );
3128 mbedtls_mpi_free( &s );
3129 return( mbedtls_to_psa_error( ret ) );
3130}
3131#endif /* MBEDTLS_ECDSA_C */
3132
Gilles Peskinec5487a82018-12-03 18:08:14 +01003133psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003134 psa_algorithm_t alg,
3135 const uint8_t *hash,
3136 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003137 uint8_t *signature,
3138 size_t signature_size,
3139 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003140{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003141 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003142 psa_status_t status;
3143
3144 *signature_length = signature_size;
3145
Gilles Peskine28f8f302019-07-24 13:30:31 +02003146 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003147 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003148 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003149 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003150 {
3151 status = PSA_ERROR_INVALID_ARGUMENT;
3152 goto exit;
3153 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003154
Gilles Peskine20035e32018-02-03 22:44:14 +01003155#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003156 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003157 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003158 status = psa_rsa_sign( slot->data.rsa,
3159 alg,
3160 hash, hash_length,
3161 signature, signature_size,
3162 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003163 }
3164 else
3165#endif /* defined(MBEDTLS_RSA_C) */
3166#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003167 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003168 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003169#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003170 if(
3171#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3172 PSA_ALG_IS_ECDSA( alg )
3173#else
3174 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3175#endif
3176 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003177 status = psa_ecdsa_sign( slot->data.ecp,
3178 alg,
3179 hash, hash_length,
3180 signature, signature_size,
3181 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003182 else
3183#endif /* defined(MBEDTLS_ECDSA_C) */
3184 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003185 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003186 }
itayzafrir5c753392018-05-08 11:18:38 +03003187 }
3188 else
3189#endif /* defined(MBEDTLS_ECP_C) */
3190 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003191 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003192 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003193
3194exit:
3195 /* Fill the unused part of the output buffer (the whole buffer on error,
3196 * the trailing part on success) with something that isn't a valid mac
3197 * (barring an attack on the mac and deliberately-crafted input),
3198 * in case the caller doesn't check the return status properly. */
3199 if( status == PSA_SUCCESS )
3200 memset( signature + *signature_length, '!',
3201 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003202 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003203 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003204 /* If signature_size is 0 then we have nothing to do. We must not call
3205 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003206 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003207}
3208
Gilles Peskinec5487a82018-12-03 18:08:14 +01003209psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003210 psa_algorithm_t alg,
3211 const uint8_t *hash,
3212 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003213 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003214 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003215{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003216 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003217 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003218
Gilles Peskine28f8f302019-07-24 13:30:31 +02003219 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003220 if( status != PSA_SUCCESS )
3221 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003222
Gilles Peskine61b91d42018-06-08 16:09:36 +02003223#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003224 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003226 return( psa_rsa_verify( slot->data.rsa,
3227 alg,
3228 hash, hash_length,
3229 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230 }
3231 else
3232#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003233#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003234 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003235 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003236#if defined(MBEDTLS_ECDSA_C)
3237 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003238 return( psa_ecdsa_verify( slot->data.ecp,
3239 hash, hash_length,
3240 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003241 else
3242#endif /* defined(MBEDTLS_ECDSA_C) */
3243 {
3244 return( PSA_ERROR_INVALID_ARGUMENT );
3245 }
itayzafrir5c753392018-05-08 11:18:38 +03003246 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003247 else
3248#endif /* defined(MBEDTLS_ECP_C) */
3249 {
3250 return( PSA_ERROR_NOT_SUPPORTED );
3251 }
3252}
3253
Gilles Peskine072ac562018-06-30 00:21:29 +02003254#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3255static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3256 mbedtls_rsa_context *rsa )
3257{
3258 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3259 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3260 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3261 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3262}
3263#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3264
Gilles Peskinec5487a82018-12-03 18:08:14 +01003265psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003266 psa_algorithm_t alg,
3267 const uint8_t *input,
3268 size_t input_length,
3269 const uint8_t *salt,
3270 size_t salt_length,
3271 uint8_t *output,
3272 size_t output_size,
3273 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003275 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003276 psa_status_t status;
3277
Darryl Green5cc689a2018-07-24 15:34:10 +01003278 (void) input;
3279 (void) input_length;
3280 (void) salt;
3281 (void) output;
3282 (void) output_size;
3283
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003284 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003286 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3287 return( PSA_ERROR_INVALID_ARGUMENT );
3288
Gilles Peskine28f8f302019-07-24 13:30:31 +02003289 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003290 if( status != PSA_SUCCESS )
3291 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003292 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3293 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295
3296#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003297 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 {
3299 mbedtls_rsa_context *rsa = slot->data.rsa;
3300 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003301 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003302 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003304 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003306 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3307 mbedtls_ctr_drbg_random,
3308 &global_data.ctr_drbg,
3309 MBEDTLS_RSA_PUBLIC,
3310 input_length,
3311 input,
3312 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313 }
3314 else
3315#endif /* MBEDTLS_PKCS1_V15 */
3316#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003317 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003319 psa_rsa_oaep_set_padding_mode( alg, rsa );
3320 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3321 mbedtls_ctr_drbg_random,
3322 &global_data.ctr_drbg,
3323 MBEDTLS_RSA_PUBLIC,
3324 salt, salt_length,
3325 input_length,
3326 input,
3327 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003328 }
3329 else
3330#endif /* MBEDTLS_PKCS1_V21 */
3331 {
3332 return( PSA_ERROR_INVALID_ARGUMENT );
3333 }
3334 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003335 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336 return( mbedtls_to_psa_error( ret ) );
3337 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003338 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003339#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003340 {
3341 return( PSA_ERROR_NOT_SUPPORTED );
3342 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003343}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003344
Gilles Peskinec5487a82018-12-03 18:08:14 +01003345psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003346 psa_algorithm_t alg,
3347 const uint8_t *input,
3348 size_t input_length,
3349 const uint8_t *salt,
3350 size_t salt_length,
3351 uint8_t *output,
3352 size_t output_size,
3353 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003355 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003356 psa_status_t status;
3357
Darryl Green5cc689a2018-07-24 15:34:10 +01003358 (void) input;
3359 (void) input_length;
3360 (void) salt;
3361 (void) output;
3362 (void) output_size;
3363
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003364 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003366 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3367 return( PSA_ERROR_INVALID_ARGUMENT );
3368
Gilles Peskine28f8f302019-07-24 13:30:31 +02003369 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003370 if( status != PSA_SUCCESS )
3371 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003372 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003373 return( PSA_ERROR_INVALID_ARGUMENT );
3374
3375#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003376 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003377 {
3378 mbedtls_rsa_context *rsa = slot->data.rsa;
3379 int ret;
3380
Gilles Peskine630a18a2018-06-29 17:49:35 +02003381 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382 return( PSA_ERROR_INVALID_ARGUMENT );
3383
3384#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003385 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003386 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003387 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3388 mbedtls_ctr_drbg_random,
3389 &global_data.ctr_drbg,
3390 MBEDTLS_RSA_PRIVATE,
3391 output_length,
3392 input,
3393 output,
3394 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003395 }
3396 else
3397#endif /* MBEDTLS_PKCS1_V15 */
3398#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003399 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003400 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003401 psa_rsa_oaep_set_padding_mode( alg, rsa );
3402 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3403 mbedtls_ctr_drbg_random,
3404 &global_data.ctr_drbg,
3405 MBEDTLS_RSA_PRIVATE,
3406 salt, salt_length,
3407 output_length,
3408 input,
3409 output,
3410 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003411 }
3412 else
3413#endif /* MBEDTLS_PKCS1_V21 */
3414 {
3415 return( PSA_ERROR_INVALID_ARGUMENT );
3416 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003417
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003418 return( mbedtls_to_psa_error( ret ) );
3419 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003420 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003421#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003422 {
3423 return( PSA_ERROR_NOT_SUPPORTED );
3424 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003425}
Gilles Peskine20035e32018-02-03 22:44:14 +01003426
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003427
3428
mohammad1603503973b2018-03-12 15:59:30 +02003429/****************************************************************/
3430/* Symmetric cryptography */
3431/****************************************************************/
3432
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003433/* Initialize the cipher operation structure. Once this function has been
3434 * called, psa_cipher_abort can run and will do the right thing. */
3435static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3436 psa_algorithm_t alg )
3437{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003438 if( ! PSA_ALG_IS_CIPHER( alg ) )
3439 {
3440 memset( operation, 0, sizeof( *operation ) );
3441 return( PSA_ERROR_INVALID_ARGUMENT );
3442 }
3443
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003444 operation->alg = alg;
3445 operation->key_set = 0;
3446 operation->iv_set = 0;
3447 operation->iv_required = 1;
3448 operation->iv_size = 0;
3449 operation->block_size = 0;
3450 mbedtls_cipher_init( &operation->ctx.cipher );
3451 return( PSA_SUCCESS );
3452}
3453
Gilles Peskinee553c652018-06-04 16:22:46 +02003454static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003455 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003456 psa_algorithm_t alg,
3457 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003458{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003459 int ret = 0;
3460 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003461 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003462 size_t key_bits;
3463 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003464 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3465 PSA_KEY_USAGE_ENCRYPT :
3466 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003467
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003468 /* A context must be freshly initialized before it can be set up. */
3469 if( operation->alg != 0 )
3470 {
3471 return( PSA_ERROR_BAD_STATE );
3472 }
3473
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003474 status = psa_cipher_init( operation, alg );
3475 if( status != PSA_SUCCESS )
3476 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003477
Gilles Peskine28f8f302019-07-24 13:30:31 +02003478 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003479 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003480 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003481 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003482
Gilles Peskine8e338702019-07-30 20:06:31 +02003483 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003484 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003485 {
3486 status = PSA_ERROR_NOT_SUPPORTED;
3487 goto exit;
3488 }
mohammad1603503973b2018-03-12 15:59:30 +02003489
mohammad1603503973b2018-03-12 15:59:30 +02003490 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003491 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003492 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003493
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003494#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003495 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003496 {
3497 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003498 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003499 memcpy( keys, slot->data.raw.data, 16 );
3500 memcpy( keys + 16, slot->data.raw.data, 8 );
3501 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3502 keys,
3503 192, cipher_operation );
3504 }
3505 else
3506#endif
3507 {
3508 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3509 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003510 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003511 }
Moran Peker41deec42018-04-04 15:43:05 +03003512 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003513 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003514
mohammad16038481e742018-03-18 13:57:31 +02003515#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003516 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003517 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003518 case PSA_ALG_CBC_NO_PADDING:
3519 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3520 MBEDTLS_PADDING_NONE );
3521 break;
3522 case PSA_ALG_CBC_PKCS7:
3523 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3524 MBEDTLS_PADDING_PKCS7 );
3525 break;
3526 default:
3527 /* The algorithm doesn't involve padding. */
3528 ret = 0;
3529 break;
3530 }
3531 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003532 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003533#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3534
mohammad1603503973b2018-03-12 15:59:30 +02003535 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003536 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003537 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003538 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003539 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003540 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003541 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003542#if defined(MBEDTLS_CHACHA20_C)
3543 else
3544 if( alg == PSA_ALG_CHACHA20 )
3545 operation->iv_size = 12;
3546#endif
mohammad1603503973b2018-03-12 15:59:30 +02003547
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003548exit:
3549 if( status == 0 )
3550 status = mbedtls_to_psa_error( ret );
3551 if( status != 0 )
3552 psa_cipher_abort( operation );
3553 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003554}
3555
Gilles Peskinefe119512018-07-08 21:39:34 +02003556psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003557 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003558 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003559{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003560 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003561}
3562
Gilles Peskinefe119512018-07-08 21:39:34 +02003563psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003564 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003565 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003566{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003567 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003568}
3569
Gilles Peskinefe119512018-07-08 21:39:34 +02003570psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003571 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003572 size_t iv_size,
3573 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003574{
itayzafrir534bd7c2018-08-02 13:56:32 +03003575 psa_status_t status;
3576 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003577 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003578 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003579 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003580 }
Moran Peker41deec42018-04-04 15:43:05 +03003581 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003582 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003583 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003584 goto exit;
3585 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003586 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3587 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003588 if( ret != 0 )
3589 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003590 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003591 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003592 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003593
mohammad16038481e742018-03-18 13:57:31 +02003594 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003595 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003596
Moran Peker395db872018-05-31 14:07:14 +03003597exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003598 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003599 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003600 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003601}
3602
Gilles Peskinefe119512018-07-08 21:39:34 +02003603psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003604 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003605 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003606{
itayzafrir534bd7c2018-08-02 13:56:32 +03003607 psa_status_t status;
3608 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003609 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003610 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003611 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003612 }
Moran Pekera28258c2018-05-29 16:25:04 +03003613 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003614 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003615 status = PSA_ERROR_INVALID_ARGUMENT;
3616 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003617 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003618 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3619 status = mbedtls_to_psa_error( ret );
3620exit:
3621 if( status == PSA_SUCCESS )
3622 operation->iv_set = 1;
3623 else
mohammad1603503973b2018-03-12 15:59:30 +02003624 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003625 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003626}
3627
Gilles Peskinee553c652018-06-04 16:22:46 +02003628psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3629 const uint8_t *input,
3630 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003631 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003632 size_t output_size,
3633 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003634{
itayzafrir534bd7c2018-08-02 13:56:32 +03003635 psa_status_t status;
3636 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003637 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003638
3639 if( operation->alg == 0 )
3640 {
3641 return( PSA_ERROR_BAD_STATE );
3642 }
3643
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003644 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003645 {
3646 /* Take the unprocessed partial block left over from previous
3647 * update calls, if any, plus the input to this call. Remove
3648 * the last partial block, if any. You get the data that will be
3649 * output in this call. */
3650 expected_output_size =
3651 ( operation->ctx.cipher.unprocessed_len + input_length )
3652 / operation->block_size * operation->block_size;
3653 }
3654 else
3655 {
3656 expected_output_size = input_length;
3657 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003658
Gilles Peskine89d789c2018-06-04 17:17:16 +02003659 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003660 {
3661 status = PSA_ERROR_BUFFER_TOO_SMALL;
3662 goto exit;
3663 }
mohammad160382759612018-03-12 18:16:40 +02003664
mohammad1603503973b2018-03-12 15:59:30 +02003665 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003666 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003667 status = mbedtls_to_psa_error( ret );
3668exit:
3669 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003670 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003671 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003672}
3673
Gilles Peskinee553c652018-06-04 16:22:46 +02003674psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3675 uint8_t *output,
3676 size_t output_size,
3677 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003678{
David Saadab4ecc272019-02-14 13:48:10 +02003679 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003680 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003681 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003682
mohammad1603503973b2018-03-12 15:59:30 +02003683 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003684 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003685 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003686 }
3687 if( operation->iv_required && ! operation->iv_set )
3688 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003689 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003690 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003691
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003692 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003693 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3694 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003695 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003696 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003697 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003698 }
3699
Janos Follath315b51c2018-07-09 16:04:51 +01003700 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3701 temp_output_buffer,
3702 output_length );
3703 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003704 {
Janos Follath315b51c2018-07-09 16:04:51 +01003705 status = mbedtls_to_psa_error( cipher_ret );
3706 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003707 }
Janos Follath315b51c2018-07-09 16:04:51 +01003708
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003709 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003710 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003711 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003712 memcpy( output, temp_output_buffer, *output_length );
3713 else
3714 {
Janos Follath315b51c2018-07-09 16:04:51 +01003715 status = PSA_ERROR_BUFFER_TOO_SMALL;
3716 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003717 }
mohammad1603503973b2018-03-12 15:59:30 +02003718
Gilles Peskine3f108122018-12-07 18:14:53 +01003719 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003720 status = psa_cipher_abort( operation );
3721
3722 return( status );
3723
3724error:
3725
3726 *output_length = 0;
3727
Gilles Peskine3f108122018-12-07 18:14:53 +01003728 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003729 (void) psa_cipher_abort( operation );
3730
3731 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003732}
3733
Gilles Peskinee553c652018-06-04 16:22:46 +02003734psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3735{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003736 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003737 {
3738 /* The object has (apparently) been initialized but it is not
3739 * in use. It's ok to call abort on such an object, and there's
3740 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003741 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003742 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003743
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003744 /* Sanity check (shouldn't happen: operation->alg should
3745 * always have been initialized to a valid value). */
3746 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3747 return( PSA_ERROR_BAD_STATE );
3748
mohammad1603503973b2018-03-12 15:59:30 +02003749 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003750
Moran Peker41deec42018-04-04 15:43:05 +03003751 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003752 operation->key_set = 0;
3753 operation->iv_set = 0;
3754 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003755 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003756 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003757
Moran Peker395db872018-05-31 14:07:14 +03003758 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003759}
3760
Gilles Peskinea0655c32018-04-30 17:06:50 +02003761
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003762
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003763
mohammad16035955c982018-04-26 00:53:03 +03003764/****************************************************************/
3765/* AEAD */
3766/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003767
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768typedef struct
3769{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003770 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003771 const mbedtls_cipher_info_t *cipher_info;
3772 union
3773 {
3774#if defined(MBEDTLS_CCM_C)
3775 mbedtls_ccm_context ccm;
3776#endif /* MBEDTLS_CCM_C */
3777#if defined(MBEDTLS_GCM_C)
3778 mbedtls_gcm_context gcm;
3779#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003780#if defined(MBEDTLS_CHACHAPOLY_C)
3781 mbedtls_chachapoly_context chachapoly;
3782#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003783 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003784 psa_algorithm_t core_alg;
3785 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003786 uint8_t tag_length;
3787} aead_operation_t;
3788
Gilles Peskine30a9e412019-01-14 18:36:12 +01003789static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003791 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003792 {
3793#if defined(MBEDTLS_CCM_C)
3794 case PSA_ALG_CCM:
3795 mbedtls_ccm_free( &operation->ctx.ccm );
3796 break;
3797#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003798#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003799 case PSA_ALG_GCM:
3800 mbedtls_gcm_free( &operation->ctx.gcm );
3801 break;
3802#endif /* MBEDTLS_GCM_C */
3803 }
3804}
3805
3806static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003807 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003808 psa_key_usage_t usage,
3809 psa_algorithm_t alg )
3810{
3811 psa_status_t status;
3812 size_t key_bits;
3813 mbedtls_cipher_id_t cipher_id;
3814
Gilles Peskine28f8f302019-07-24 13:30:31 +02003815 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003816 if( status != PSA_SUCCESS )
3817 return( status );
3818
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003819 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003820
3821 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003822 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003823 &cipher_id );
3824 if( operation->cipher_info == NULL )
3825 return( PSA_ERROR_NOT_SUPPORTED );
3826
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003827 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828 {
3829#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003830 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3831 operation->core_alg = PSA_ALG_CCM;
3832 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003833 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3834 * The call to mbedtls_ccm_encrypt_and_tag or
3835 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003836 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003837 return( PSA_ERROR_INVALID_ARGUMENT );
3838 mbedtls_ccm_init( &operation->ctx.ccm );
3839 status = mbedtls_to_psa_error(
3840 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3841 operation->slot->data.raw.data,
3842 (unsigned int) key_bits ) );
3843 if( status != 0 )
3844 goto cleanup;
3845 break;
3846#endif /* MBEDTLS_CCM_C */
3847
3848#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003849 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3850 operation->core_alg = PSA_ALG_GCM;
3851 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003852 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3853 * The call to mbedtls_gcm_crypt_and_tag or
3854 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003855 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003856 return( PSA_ERROR_INVALID_ARGUMENT );
3857 mbedtls_gcm_init( &operation->ctx.gcm );
3858 status = mbedtls_to_psa_error(
3859 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3860 operation->slot->data.raw.data,
3861 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003862 if( status != 0 )
3863 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003864 break;
3865#endif /* MBEDTLS_GCM_C */
3866
Gilles Peskine26869f22019-05-06 15:25:00 +02003867#if defined(MBEDTLS_CHACHAPOLY_C)
3868 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3869 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3870 operation->full_tag_length = 16;
3871 /* We only support the default tag length. */
3872 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3873 return( PSA_ERROR_NOT_SUPPORTED );
3874 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3875 status = mbedtls_to_psa_error(
3876 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3877 operation->slot->data.raw.data ) );
3878 if( status != 0 )
3879 goto cleanup;
3880 break;
3881#endif /* MBEDTLS_CHACHAPOLY_C */
3882
Gilles Peskineedf9a652018-08-17 18:11:56 +02003883 default:
3884 return( PSA_ERROR_NOT_SUPPORTED );
3885 }
3886
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003887 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3888 {
3889 status = PSA_ERROR_INVALID_ARGUMENT;
3890 goto cleanup;
3891 }
3892 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003893
Gilles Peskineedf9a652018-08-17 18:11:56 +02003894 return( PSA_SUCCESS );
3895
3896cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003897 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003898 return( status );
3899}
3900
Gilles Peskinec5487a82018-12-03 18:08:14 +01003901psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003902 psa_algorithm_t alg,
3903 const uint8_t *nonce,
3904 size_t nonce_length,
3905 const uint8_t *additional_data,
3906 size_t additional_data_length,
3907 const uint8_t *plaintext,
3908 size_t plaintext_length,
3909 uint8_t *ciphertext,
3910 size_t ciphertext_size,
3911 size_t *ciphertext_length )
3912{
mohammad16035955c982018-04-26 00:53:03 +03003913 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003914 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003915 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003916
mohammad1603f08a5502018-06-03 15:05:47 +03003917 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003918
Gilles Peskinec5487a82018-12-03 18:08:14 +01003919 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003920 if( status != PSA_SUCCESS )
3921 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003922
Gilles Peskineedf9a652018-08-17 18:11:56 +02003923 /* For all currently supported modes, the tag is at the end of the
3924 * ciphertext. */
3925 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3926 {
3927 status = PSA_ERROR_BUFFER_TOO_SMALL;
3928 goto exit;
3929 }
3930 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003931
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003932#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003933 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003934 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003935 status = mbedtls_to_psa_error(
3936 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3937 MBEDTLS_GCM_ENCRYPT,
3938 plaintext_length,
3939 nonce, nonce_length,
3940 additional_data, additional_data_length,
3941 plaintext, ciphertext,
3942 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003943 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003944 else
3945#endif /* MBEDTLS_GCM_C */
3946#if defined(MBEDTLS_CCM_C)
3947 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003948 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003949 status = mbedtls_to_psa_error(
3950 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3951 plaintext_length,
3952 nonce, nonce_length,
3953 additional_data,
3954 additional_data_length,
3955 plaintext, ciphertext,
3956 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003957 }
mohammad16035c8845f2018-05-09 05:40:09 -07003958 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003959#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003960#if defined(MBEDTLS_CHACHAPOLY_C)
3961 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3962 {
3963 if( nonce_length != 12 || operation.tag_length != 16 )
3964 {
3965 status = PSA_ERROR_NOT_SUPPORTED;
3966 goto exit;
3967 }
3968 status = mbedtls_to_psa_error(
3969 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3970 plaintext_length,
3971 nonce,
3972 additional_data,
3973 additional_data_length,
3974 plaintext,
3975 ciphertext,
3976 tag ) );
3977 }
3978 else
3979#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003980 {
mohammad1603554faad2018-06-03 15:07:38 +03003981 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003982 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003983
Gilles Peskineedf9a652018-08-17 18:11:56 +02003984 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3985 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003986
Gilles Peskineedf9a652018-08-17 18:11:56 +02003987exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003988 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003989 if( status == PSA_SUCCESS )
3990 *ciphertext_length = plaintext_length + operation.tag_length;
3991 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003992}
3993
Gilles Peskineee652a32018-06-01 19:23:52 +02003994/* Locate the tag in a ciphertext buffer containing the encrypted data
3995 * followed by the tag. Return the length of the part preceding the tag in
3996 * *plaintext_length. This is the size of the plaintext in modes where
3997 * the encrypted data has the same size as the plaintext, such as
3998 * CCM and GCM. */
3999static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4000 const uint8_t *ciphertext,
4001 size_t ciphertext_length,
4002 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004003 const uint8_t **p_tag )
4004{
4005 size_t payload_length;
4006 if( tag_length > ciphertext_length )
4007 return( PSA_ERROR_INVALID_ARGUMENT );
4008 payload_length = ciphertext_length - tag_length;
4009 if( payload_length > plaintext_size )
4010 return( PSA_ERROR_BUFFER_TOO_SMALL );
4011 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004012 return( PSA_SUCCESS );
4013}
4014
Gilles Peskinec5487a82018-12-03 18:08:14 +01004015psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004016 psa_algorithm_t alg,
4017 const uint8_t *nonce,
4018 size_t nonce_length,
4019 const uint8_t *additional_data,
4020 size_t additional_data_length,
4021 const uint8_t *ciphertext,
4022 size_t ciphertext_length,
4023 uint8_t *plaintext,
4024 size_t plaintext_size,
4025 size_t *plaintext_length )
4026{
mohammad16035955c982018-04-26 00:53:03 +03004027 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004028 aead_operation_t operation;
4029 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004030
Gilles Peskineee652a32018-06-01 19:23:52 +02004031 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004032
Gilles Peskinec5487a82018-12-03 18:08:14 +01004033 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004034 if( status != PSA_SUCCESS )
4035 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004036
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004037 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4038 ciphertext, ciphertext_length,
4039 plaintext_size, &tag );
4040 if( status != PSA_SUCCESS )
4041 goto exit;
4042
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004043#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004044 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004045 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004046 status = mbedtls_to_psa_error(
4047 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4048 ciphertext_length - operation.tag_length,
4049 nonce, nonce_length,
4050 additional_data,
4051 additional_data_length,
4052 tag, operation.tag_length,
4053 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004054 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004055 else
4056#endif /* MBEDTLS_GCM_C */
4057#if defined(MBEDTLS_CCM_C)
4058 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004059 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 status = mbedtls_to_psa_error(
4061 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4062 ciphertext_length - operation.tag_length,
4063 nonce, nonce_length,
4064 additional_data,
4065 additional_data_length,
4066 ciphertext, plaintext,
4067 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004068 }
mohammad160339574652018-06-01 04:39:53 -07004069 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004070#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004071#if defined(MBEDTLS_CHACHAPOLY_C)
4072 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4073 {
4074 if( nonce_length != 12 || operation.tag_length != 16 )
4075 {
4076 status = PSA_ERROR_NOT_SUPPORTED;
4077 goto exit;
4078 }
4079 status = mbedtls_to_psa_error(
4080 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4081 ciphertext_length - operation.tag_length,
4082 nonce,
4083 additional_data,
4084 additional_data_length,
4085 tag,
4086 ciphertext,
4087 plaintext ) );
4088 }
4089 else
4090#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004091 {
mohammad1603554faad2018-06-03 15:07:38 +03004092 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004093 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004094
Gilles Peskineedf9a652018-08-17 18:11:56 +02004095 if( status != PSA_SUCCESS && plaintext_size != 0 )
4096 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004097
Gilles Peskineedf9a652018-08-17 18:11:56 +02004098exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004099 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004100 if( status == PSA_SUCCESS )
4101 *plaintext_length = ciphertext_length - operation.tag_length;
4102 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004103}
4104
Gilles Peskinea0655c32018-04-30 17:06:50 +02004105
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004106
Gilles Peskine20035e32018-02-03 22:44:14 +01004107/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004108/* Generators */
4109/****************************************************************/
4110
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004111#define HKDF_STATE_INIT 0 /* no input yet */
4112#define HKDF_STATE_STARTED 1 /* got salt */
4113#define HKDF_STATE_KEYED 2 /* got key */
4114#define HKDF_STATE_OUTPUT 3 /* output started */
4115
Gilles Peskinecbe66502019-05-16 16:59:18 +02004116static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004117 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004118{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004119 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4120 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004121 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004122 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004123}
4124
4125
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004126psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004127{
4128 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004129 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004130 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004131 {
4132 /* The object has (apparently) been initialized but it is not
4133 * in use. It's ok to call abort on such an object, and there's
4134 * nothing to do. */
4135 }
4136 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004137#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004138 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004139 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004140 mbedtls_free( operation->ctx.hkdf.info );
4141 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004142 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004143 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004145 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004146 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004147#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004148 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004149 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004150 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4151 operation->ctx.tls12_prf.key_len );
4152 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004153 }
Hanno Becker580fba12018-11-13 20:50:45 +00004154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004155 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004156 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004157 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4158 operation->ctx.tls12_prf.Ai_with_seed_len );
4159 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004160 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004161#else
4162 if( operation->ctx.tls12_prf.seed != NULL )
4163 {
4164 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4165 operation->ctx.tls12_prf.seed_length );
4166 mbedtls_free( operation->ctx.tls12_prf.seed );
4167 }
4168
4169 if( operation->ctx.tls12_prf.label != NULL )
4170 {
4171 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4172 operation->ctx.tls12_prf.label_length );
4173 mbedtls_free( operation->ctx.tls12_prf.label );
4174 }
4175
4176 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4177
4178 /* We leave the fields Ai and output_block to be erased safely by the
4179 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004180#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004181 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004182 else
4183#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004184 {
4185 status = PSA_ERROR_BAD_STATE;
4186 }
Janos Follath083036a2019-06-11 10:22:26 +01004187 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004188 return( status );
4189}
4190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004192 size_t *capacity)
4193{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004194 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004195 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004197 return PSA_ERROR_BAD_STATE;
4198 }
4199
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004200 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004201 return( PSA_SUCCESS );
4202}
4203
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004204psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004205 size_t capacity )
4206{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004207 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004208 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004209 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004210 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004211 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004212 return( PSA_SUCCESS );
4213}
4214
Gilles Peskinebef7f142018-07-12 17:22:21 +02004215#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004216/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004217 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004218static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004219 psa_algorithm_t hash_alg,
4220 uint8_t *output,
4221 size_t output_length )
4222{
4223 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4224 psa_status_t status;
4225
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004226 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4227 return( PSA_ERROR_BAD_STATE );
4228 hkdf->state = HKDF_STATE_OUTPUT;
4229
Gilles Peskinebef7f142018-07-12 17:22:21 +02004230 while( output_length != 0 )
4231 {
4232 /* Copy what remains of the current block */
4233 uint8_t n = hash_length - hkdf->offset_in_block;
4234 if( n > output_length )
4235 n = (uint8_t) output_length;
4236 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4237 output += n;
4238 output_length -= n;
4239 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004240 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004241 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004242 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004243 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004244 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004245 * object was corrupted or if this function is called directly
4246 * inside the library. */
4247 if( hkdf->block_number == 0xff )
4248 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004249
4250 /* We need a new block */
4251 ++hkdf->block_number;
4252 hkdf->offset_in_block = 0;
4253 status = psa_hmac_setup_internal( &hkdf->hmac,
4254 hkdf->prk, hash_length,
4255 hash_alg );
4256 if( status != PSA_SUCCESS )
4257 return( status );
4258 if( hkdf->block_number != 1 )
4259 {
4260 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4261 hkdf->output_block,
4262 hash_length );
4263 if( status != PSA_SUCCESS )
4264 return( status );
4265 }
4266 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4267 hkdf->info,
4268 hkdf->info_length );
4269 if( status != PSA_SUCCESS )
4270 return( status );
4271 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4272 &hkdf->block_number, 1 );
4273 if( status != PSA_SUCCESS )
4274 return( status );
4275 status = psa_hmac_finish_internal( &hkdf->hmac,
4276 hkdf->output_block,
4277 sizeof( hkdf->output_block ) );
4278 if( status != PSA_SUCCESS )
4279 return( status );
4280 }
4281
4282 return( PSA_SUCCESS );
4283}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004284
Janos Follath999f6482019-06-11 12:04:10 +01004285#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004286static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4287 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004288 psa_algorithm_t alg )
4289{
4290 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4291 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4292 psa_hmac_internal_data hmac;
4293 psa_status_t status, cleanup_status;
4294
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004295 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004296 size_t Ai_len;
4297
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004298 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004299 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004300 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004301 * object was corrupted or if this function is called directly
4302 * inside the library. */
4303 if( tls12_prf->block_number == 0xff )
4304 return( PSA_ERROR_BAD_STATE );
4305
4306 /* We need a new block */
4307 ++tls12_prf->block_number;
4308 tls12_prf->offset_in_block = 0;
4309
4310 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4311 *
4312 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4313 *
4314 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4315 * HMAC_hash(secret, A(2) + seed) +
4316 * HMAC_hash(secret, A(3) + seed) + ...
4317 *
4318 * A(0) = seed
4319 * A(i) = HMAC_hash( secret, A(i-1) )
4320 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004321 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004322 * `HMAC_hash(secret, A(i) + seed)` from which the output
4323 * is currently extracted as `output_block`, while
4324 * `A(i) + seed` is stored in `Ai_with_seed`.
4325 *
4326 * Generating a new block means recalculating `Ai_with_seed`
4327 * from the A(i)-part of it, and afterwards recalculating
4328 * `output_block`.
4329 *
4330 * A(0) is computed at setup time.
4331 *
4332 */
4333
4334 psa_hmac_init_internal( &hmac );
4335
4336 /* We must distinguish the calculation of A(1) from those
4337 * of A(2) and higher, because A(0)=seed has a different
4338 * length than the other A(i). */
4339 if( tls12_prf->block_number == 1 )
4340 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004341 Ai = tls12_prf->Ai_with_seed + hash_length;
4342 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004343 }
4344 else
4345 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004346 Ai = tls12_prf->Ai_with_seed;
4347 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004348 }
4349
Hanno Becker3b339e22018-11-13 20:56:14 +00004350 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4351 status = psa_hmac_setup_internal( &hmac,
4352 tls12_prf->key,
4353 tls12_prf->key_len,
4354 hash_alg );
4355 if( status != PSA_SUCCESS )
4356 goto cleanup;
4357
4358 status = psa_hash_update( &hmac.hash_ctx,
4359 Ai, Ai_len );
4360 if( status != PSA_SUCCESS )
4361 goto cleanup;
4362
4363 status = psa_hmac_finish_internal( &hmac,
4364 tls12_prf->Ai_with_seed,
4365 hash_length );
4366 if( status != PSA_SUCCESS )
4367 goto cleanup;
4368
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004369 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4370 status = psa_hmac_setup_internal( &hmac,
4371 tls12_prf->key,
4372 tls12_prf->key_len,
4373 hash_alg );
4374 if( status != PSA_SUCCESS )
4375 goto cleanup;
4376
4377 status = psa_hash_update( &hmac.hash_ctx,
4378 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004379 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004380 if( status != PSA_SUCCESS )
4381 goto cleanup;
4382
4383 status = psa_hmac_finish_internal( &hmac,
4384 tls12_prf->output_block,
4385 hash_length );
4386 if( status != PSA_SUCCESS )
4387 goto cleanup;
4388
4389cleanup:
4390
4391 cleanup_status = psa_hmac_abort_internal( &hmac );
4392 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4393 status = cleanup_status;
4394
4395 return( status );
4396}
Janos Follath7742fee2019-06-17 12:58:10 +01004397#else
4398static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4399 psa_tls12_prf_key_derivation_t *tls12_prf,
4400 psa_algorithm_t alg )
4401{
4402 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4403 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004404 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4405 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004406
Janos Follath7742fee2019-06-17 12:58:10 +01004407 /* We can't be wanting more output after block 0xff, otherwise
4408 * the capacity check in psa_key_derivation_output_bytes() would have
4409 * prevented this call. It could happen only if the operation
4410 * object was corrupted or if this function is called directly
4411 * inside the library. */
4412 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004413 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004414
4415 /* We need a new block */
4416 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004417 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004418
4419 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4420 *
4421 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4422 *
4423 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4424 * HMAC_hash(secret, A(2) + seed) +
4425 * HMAC_hash(secret, A(3) + seed) + ...
4426 *
4427 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004428 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004429 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004430 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004431 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004432 * is currently extracted as `output_block` and where i is
4433 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004434 */
4435
Janos Follathea29bfb2019-06-19 12:21:20 +01004436 /* Save the hash context before using it, to preserve the hash state with
4437 * only the inner padding in it. We need this, because inner padding depends
4438 * on the key (secret in the RFC's terminology). */
4439 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4440 if( status != PSA_SUCCESS )
4441 goto cleanup;
4442
4443 /* Calculate A(i) where i = tls12_prf->block_number. */
4444 if( tls12_prf->block_number == 1 )
4445 {
4446 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4447 * the variable seed and in this instance means it in the context of the
4448 * P_hash function, where seed = label + seed.) */
4449 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4450 tls12_prf->label, tls12_prf->label_length );
4451 if( status != PSA_SUCCESS )
4452 goto cleanup;
4453 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4454 tls12_prf->seed, tls12_prf->seed_length );
4455 if( status != PSA_SUCCESS )
4456 goto cleanup;
4457 }
4458 else
4459 {
4460 /* A(i) = HMAC_hash(secret, A(i-1)) */
4461 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4462 tls12_prf->Ai, hash_length );
4463 if( status != PSA_SUCCESS )
4464 goto cleanup;
4465 }
4466
4467 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4468 tls12_prf->Ai, hash_length );
4469 if( status != PSA_SUCCESS )
4470 goto cleanup;
4471 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4472 if( status != PSA_SUCCESS )
4473 goto cleanup;
4474
4475 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4476 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4477 tls12_prf->Ai, hash_length );
4478 if( status != PSA_SUCCESS )
4479 goto cleanup;
4480 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4481 tls12_prf->label, tls12_prf->label_length );
4482 if( status != PSA_SUCCESS )
4483 goto cleanup;
4484 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4485 tls12_prf->seed, tls12_prf->seed_length );
4486 if( status != PSA_SUCCESS )
4487 goto cleanup;
4488 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4489 tls12_prf->output_block, hash_length );
4490 if( status != PSA_SUCCESS )
4491 goto cleanup;
4492 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4493 if( status != PSA_SUCCESS )
4494 goto cleanup;
4495
Janos Follath7742fee2019-06-17 12:58:10 +01004496
4497cleanup:
4498
Janos Follathea29bfb2019-06-19 12:21:20 +01004499 cleanup_status = psa_hash_abort( &backup );
4500 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4501 status = cleanup_status;
4502
4503 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004504}
Janos Follath999f6482019-06-11 12:04:10 +01004505#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004506
Janos Follath999f6482019-06-11 12:04:10 +01004507#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004509 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004510static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004511 psa_tls12_prf_key_derivation_t *tls12_prf,
4512 psa_algorithm_t alg,
4513 uint8_t *output,
4514 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004515{
4516 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4517 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4518 psa_status_t status;
4519
4520 while( output_length != 0 )
4521 {
4522 /* Copy what remains of the current block */
4523 uint8_t n = hash_length - tls12_prf->offset_in_block;
4524
4525 /* Check if we have fully processed the current block. */
4526 if( n == 0 )
4527 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004528 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004529 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004530 if( status != PSA_SUCCESS )
4531 return( status );
4532
4533 continue;
4534 }
4535
4536 if( n > output_length )
4537 n = (uint8_t) output_length;
4538 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4539 n );
4540 output += n;
4541 output_length -= n;
4542 tls12_prf->offset_in_block += n;
4543 }
4544
4545 return( PSA_SUCCESS );
4546}
Janos Follath844eb0e2019-06-19 12:10:49 +01004547#else
4548static psa_status_t psa_key_derivation_tls12_prf_read(
4549 psa_tls12_prf_key_derivation_t *tls12_prf,
4550 psa_algorithm_t alg,
4551 uint8_t *output,
4552 size_t output_length )
4553{
4554 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4555 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4556 psa_status_t status;
4557 uint8_t offset, length;
4558
4559 while( output_length != 0 )
4560 {
4561 /* Check if we have fully processed the current block. */
4562 if( tls12_prf->left_in_block == 0 )
4563 {
4564 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4565 alg );
4566 if( status != PSA_SUCCESS )
4567 return( status );
4568
4569 continue;
4570 }
4571
4572 if( tls12_prf->left_in_block > output_length )
4573 length = (uint8_t) output_length;
4574 else
4575 length = tls12_prf->left_in_block;
4576
4577 offset = hash_length - tls12_prf->left_in_block;
4578 memcpy( output, tls12_prf->output_block + offset, length );
4579 output += length;
4580 output_length -= length;
4581 tls12_prf->left_in_block -= length;
4582 }
4583
4584 return( PSA_SUCCESS );
4585}
Janos Follath999f6482019-06-11 12:04:10 +01004586#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004587#endif /* MBEDTLS_MD_C */
4588
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004589psa_status_t psa_key_derivation_output_bytes(
4590 psa_key_derivation_operation_t *operation,
4591 uint8_t *output,
4592 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004593{
4594 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004596
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004598 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004600 return PSA_ERROR_BAD_STATE;
4601 }
4602
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004604 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004606 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004608 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004609 goto exit;
4610 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004611 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004612 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004613 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004614 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004615 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4616 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004618 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004619 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004620 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004621 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004622
Gilles Peskinebef7f142018-07-12 17:22:21 +02004623#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004624 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004625 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004626 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004628 output, output_length );
4629 }
Janos Follathadbec812019-06-14 11:05:39 +01004630 else
Janos Follathadbec812019-06-14 11:05:39 +01004631 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004632 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004633 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004635 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004636 output_length );
4637 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004638 else
4639#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004640 {
4641 return( PSA_ERROR_BAD_STATE );
4642 }
4643
4644exit:
4645 if( status != PSA_SUCCESS )
4646 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004647 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 * This allows us to differentiate between exhausted operations and
4649 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4650 * operations. */
4651 psa_algorithm_t alg = operation->alg;
4652 psa_key_derivation_abort( operation );
4653 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004654 memset( output, '!', output_length );
4655 }
4656 return( status );
4657}
4658
Gilles Peskine08542d82018-07-19 17:05:42 +02004659#if defined(MBEDTLS_DES_C)
4660static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4661{
4662 if( data_size >= 8 )
4663 mbedtls_des_key_set_parity( data );
4664 if( data_size >= 16 )
4665 mbedtls_des_key_set_parity( data + 8 );
4666 if( data_size >= 24 )
4667 mbedtls_des_key_set_parity( data + 16 );
4668}
4669#endif /* MBEDTLS_DES_C */
4670
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004671static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004672 psa_key_slot_t *slot,
4673 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004675{
4676 uint8_t *data = NULL;
4677 size_t bytes = PSA_BITS_TO_BYTES( bits );
4678 psa_status_t status;
4679
Gilles Peskine8e338702019-07-30 20:06:31 +02004680 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 return( PSA_ERROR_INVALID_ARGUMENT );
4682 if( bits % 8 != 0 )
4683 return( PSA_ERROR_INVALID_ARGUMENT );
4684 data = mbedtls_calloc( 1, bytes );
4685 if( data == NULL )
4686 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4687
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004689 if( status != PSA_SUCCESS )
4690 goto exit;
4691#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004692 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004693 psa_des_set_key_parity( data, bytes );
4694#endif /* MBEDTLS_DES_C */
4695 status = psa_import_key_into_slot( slot, data, bytes );
4696
4697exit:
4698 mbedtls_free( data );
4699 return( status );
4700}
4701
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004702psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004704 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004705{
4706 psa_status_t status;
4707 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004708 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004709 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004710#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4711 if( driver != NULL )
4712 {
4713 /* Deriving a key in a secure element is not implemented yet. */
4714 status = PSA_ERROR_NOT_SUPPORTED;
4715 }
4716#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004717 if( status == PSA_SUCCESS )
4718 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004719 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004720 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004722 }
4723 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004724 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004725 if( status != PSA_SUCCESS )
4726 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004727 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004728 *handle = 0;
4729 }
4730 return( status );
4731}
4732
Gilles Peskineeab56e42018-07-12 17:12:33 +02004733
4734
4735/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004736/* Key derivation */
4737/****************************************************************/
4738
Gilles Peskinea05219c2018-11-16 16:02:56 +01004739#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004740#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004742 * of the HKDF algorithm.
4743 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004744 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004745 * to potentially free embedded data structures and wipe confidential data.
4746 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004747static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004748 const uint8_t *secret,
4749 size_t secret_length,
4750 psa_algorithm_t hash_alg,
4751 const uint8_t *salt,
4752 size_t salt_length,
4753 const uint8_t *label,
4754 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755{
4756 psa_status_t status;
4757 status = psa_hmac_setup_internal( &hkdf->hmac,
4758 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004759 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 if( status != PSA_SUCCESS )
4761 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004762 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004763 if( status != PSA_SUCCESS )
4764 return( status );
4765 status = psa_hmac_finish_internal( &hkdf->hmac,
4766 hkdf->prk,
4767 sizeof( hkdf->prk ) );
4768 if( status != PSA_SUCCESS )
4769 return( status );
4770 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4771 hkdf->block_number = 0;
4772 hkdf->info_length = label_length;
4773 if( label_length != 0 )
4774 {
4775 hkdf->info = mbedtls_calloc( 1, label_length );
4776 if( hkdf->info == NULL )
4777 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4778 memcpy( hkdf->info, label, label_length );
4779 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004780 hkdf->state = HKDF_STATE_KEYED;
4781 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004782 return( PSA_SUCCESS );
4783}
Janos Follath71a4c912019-06-11 09:14:47 +01004784#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004785#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004786
Gilles Peskinea05219c2018-11-16 16:02:56 +01004787#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004788#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004790 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004791 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004792 * to potentially free embedded data structures and wipe confidential data.
4793 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004794static psa_status_t psa_key_derivation_tls12_prf_setup(
4795 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004796 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004797 size_t key_len,
4798 psa_algorithm_t hash_alg,
4799 const uint8_t *salt,
4800 size_t salt_length,
4801 const uint8_t *label,
4802 size_t label_length )
4803{
4804 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004805 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4806 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004807
4808 tls12_prf->key = mbedtls_calloc( 1, key_len );
4809 if( tls12_prf->key == NULL )
4810 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4811 tls12_prf->key_len = key_len;
4812 memcpy( tls12_prf->key, key, key_len );
4813
Hanno Becker580fba12018-11-13 20:50:45 +00004814 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004815 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004816 if( overflow )
4817 return( PSA_ERROR_INVALID_ARGUMENT );
4818
4819 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4820 if( tls12_prf->Ai_with_seed == NULL )
4821 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4822 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4823
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004824 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4825 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004826 if( label_length != 0 )
4827 {
4828 memcpy( tls12_prf->Ai_with_seed + hash_length,
4829 label, label_length );
4830 }
4831
4832 if( salt_length != 0 )
4833 {
4834 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4835 salt, salt_length );
4836 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004837
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004838 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004839 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004840 tls12_prf->block_number = 0;
4841 tls12_prf->offset_in_block = hash_length;
4842
4843 return( PSA_SUCCESS );
4844}
Janos Follath71a4c912019-06-11 09:14:47 +01004845#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004846
Janos Follath71a4c912019-06-11 09:14:47 +01004847#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004849static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4850 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004851 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004852 size_t psk_len,
4853 psa_algorithm_t hash_alg,
4854 const uint8_t *salt,
4855 size_t salt_length,
4856 const uint8_t *label,
4857 size_t label_length )
4858{
4859 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004860 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004861
4862 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4863 return( PSA_ERROR_INVALID_ARGUMENT );
4864
4865 /* Quoting RFC 4279, Section 2:
4866 *
4867 * The premaster secret is formed as follows: if the PSK is N octets
4868 * long, concatenate a uint16 with the value N, N zero octets, a second
4869 * uint16 with the value N, and the PSK itself.
4870 */
4871
4872 pms[0] = ( psk_len >> 8 ) & 0xff;
4873 pms[1] = ( psk_len >> 0 ) & 0xff;
4874 memset( pms + 2, 0, psk_len );
4875 pms[2 + psk_len + 0] = pms[0];
4876 pms[2 + psk_len + 1] = pms[1];
4877 memcpy( pms + 4 + psk_len, psk, psk_len );
4878
Gilles Peskinecbe66502019-05-16 16:59:18 +02004879 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004880 pms, 4 + 2 * psk_len,
4881 hash_alg,
4882 salt, salt_length,
4883 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004884
Gilles Peskine3f108122018-12-07 18:14:53 +01004885 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004886 return( status );
4887}
Janos Follath71a4c912019-06-11 09:14:47 +01004888#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004889#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004890
Janos Follath71a4c912019-06-11 09:14:47 +01004891#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004892/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004893 * to potentially free embedded data structures and wipe confidential data.
4894 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004895static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004897 const uint8_t *secret, size_t secret_length,
4898 psa_algorithm_t alg,
4899 const uint8_t *salt, size_t salt_length,
4900 const uint8_t *label, size_t label_length,
4901 size_t capacity )
4902{
4903 psa_status_t status;
4904 size_t max_capacity;
4905
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 /* Set operation->alg even on failure so that abort knows what to do. */
4907 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004908
4909#if defined(MBEDTLS_MD_C)
4910 if( PSA_ALG_IS_HKDF( alg ) )
4911 {
4912 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4913 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4914 if( hash_size == 0 )
4915 return( PSA_ERROR_NOT_SUPPORTED );
4916 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004918 secret, secret_length,
4919 hash_alg,
4920 salt, salt_length,
4921 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004922 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004923 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4924 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4925 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004926 {
4927 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4928 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4929
4930 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4931 if( hash_alg != PSA_ALG_SHA_256 &&
4932 hash_alg != PSA_ALG_SHA_384 )
4933 {
4934 return( PSA_ERROR_NOT_SUPPORTED );
4935 }
4936
4937 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004938
4939 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4940 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004942 secret, secret_length,
4943 hash_alg, salt, salt_length,
4944 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004945 }
4946 else
4947 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004948 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004950 secret, secret_length,
4951 hash_alg, salt, salt_length,
4952 label, label_length );
4953 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004954 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004955 else
4956#endif
4957 {
4958 return( PSA_ERROR_NOT_SUPPORTED );
4959 }
4960
4961 if( status != PSA_SUCCESS )
4962 return( status );
4963
4964 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004966 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004968 else
4969 return( PSA_ERROR_INVALID_ARGUMENT );
4970
4971 return( PSA_SUCCESS );
4972}
Janos Follath71a4c912019-06-11 09:14:47 +01004973#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004974
Janos Follath71a4c912019-06-11 09:14:47 +01004975#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004977 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004978 psa_algorithm_t alg,
4979 const uint8_t *salt,
4980 size_t salt_length,
4981 const uint8_t *label,
4982 size_t label_length,
4983 size_t capacity )
4984{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004985 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004986 psa_status_t status;
4987
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004989 return( PSA_ERROR_BAD_STATE );
4990
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004991 /* Make sure that alg is a key derivation algorithm. This prevents
4992 * key selection algorithms, which psa_key_derivation_internal
4993 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004994 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4995 return( PSA_ERROR_INVALID_ARGUMENT );
4996
Gilles Peskine28f8f302019-07-24 13:30:31 +02004997 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004998 if( status != PSA_SUCCESS )
4999 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005000
Gilles Peskine8e338702019-07-30 20:06:31 +02005001 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005002 return( PSA_ERROR_INVALID_ARGUMENT );
5003
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005005 slot->data.raw.data,
5006 slot->data.raw.bytes,
5007 alg,
5008 salt, salt_length,
5009 label, label_length,
5010 capacity );
5011 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005013 return( status );
5014}
Janos Follath71a4c912019-06-11 09:14:47 +01005015#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005016
Gilles Peskine969c5d62019-01-16 15:53:06 +01005017static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005019 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005020{
Janos Follath5fe19732019-06-20 15:09:30 +01005021 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5022 * initialisers for this union leave some bytes unspecified.) */
5023 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5024
Gilles Peskine969c5d62019-01-16 15:53:06 +01005025 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005026#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005027 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5028 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5029 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005030 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005031 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5033 if( hash_size == 0 )
5034 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005035 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5036 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005037 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038 {
5039 return( PSA_ERROR_NOT_SUPPORTED );
5040 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005043 }
5044#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005045 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005046 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005047}
5048
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005049psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005050 psa_algorithm_t alg )
5051{
5052 psa_status_t status;
5053
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005055 return( PSA_ERROR_BAD_STATE );
5056
Gilles Peskine6843c292019-01-18 16:44:49 +01005057 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5058 return( PSA_ERROR_INVALID_ARGUMENT );
5059 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005060 {
5061 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005063 }
5064 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5065 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005067 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005068 else
5069 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005070
5071 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005073 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005074}
5075
5076#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005077static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005078 psa_algorithm_t hash_alg,
5079 psa_key_derivation_step_t step,
5080 const uint8_t *data,
5081 size_t data_length )
5082{
5083 psa_status_t status;
5084 switch( step )
5085 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005086 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005087 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005088 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005089 status = psa_hmac_setup_internal( &hkdf->hmac,
5090 data, data_length,
5091 hash_alg );
5092 if( status != PSA_SUCCESS )
5093 return( status );
5094 hkdf->state = HKDF_STATE_STARTED;
5095 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005096 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005097 /* If no salt was provided, use an empty salt. */
5098 if( hkdf->state == HKDF_STATE_INIT )
5099 {
5100 status = psa_hmac_setup_internal( &hkdf->hmac,
5101 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005102 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005103 if( status != PSA_SUCCESS )
5104 return( status );
5105 hkdf->state = HKDF_STATE_STARTED;
5106 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005107 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005108 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005109 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5110 data, data_length );
5111 if( status != PSA_SUCCESS )
5112 return( status );
5113 status = psa_hmac_finish_internal( &hkdf->hmac,
5114 hkdf->prk,
5115 sizeof( hkdf->prk ) );
5116 if( status != PSA_SUCCESS )
5117 return( status );
5118 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5119 hkdf->block_number = 0;
5120 hkdf->state = HKDF_STATE_KEYED;
5121 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005122 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005123 if( hkdf->state == HKDF_STATE_OUTPUT )
5124 return( PSA_ERROR_BAD_STATE );
5125 if( hkdf->info_set )
5126 return( PSA_ERROR_BAD_STATE );
5127 hkdf->info_length = data_length;
5128 if( data_length != 0 )
5129 {
5130 hkdf->info = mbedtls_calloc( 1, data_length );
5131 if( hkdf->info == NULL )
5132 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5133 memcpy( hkdf->info, data, data_length );
5134 }
5135 hkdf->info_set = 1;
5136 return( PSA_SUCCESS );
5137 default:
5138 return( PSA_ERROR_INVALID_ARGUMENT );
5139 }
5140}
Janos Follathb03233e2019-06-11 15:30:30 +01005141
5142#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5143static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5144 psa_algorithm_t hash_alg,
5145 psa_key_derivation_step_t step,
5146 const uint8_t *data,
5147 size_t data_length )
5148{
5149 (void) prf;
5150 (void) hash_alg;
5151 (void) step;
5152 (void) data;
5153 (void) data_length;
5154
5155 return( PSA_ERROR_INVALID_ARGUMENT );
5156}
Janos Follath6660f0e2019-06-17 08:44:03 +01005157
5158static psa_status_t psa_tls12_prf_psk_to_ms_input(
5159 psa_tls12_prf_key_derivation_t *prf,
5160 psa_algorithm_t hash_alg,
5161 psa_key_derivation_step_t step,
5162 const uint8_t *data,
5163 size_t data_length )
5164{
5165 (void) prf;
5166 (void) hash_alg;
5167 (void) step;
5168 (void) data;
5169 (void) data_length;
5170
5171 return( PSA_ERROR_INVALID_ARGUMENT );
5172}
Janos Follathb03233e2019-06-11 15:30:30 +01005173#else
Janos Follathf08e2652019-06-13 09:05:41 +01005174static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5175 const uint8_t *data,
5176 size_t data_length )
5177{
5178 if( prf->state != TLS12_PRF_STATE_INIT )
5179 return( PSA_ERROR_BAD_STATE );
5180
Janos Follathd6dce9f2019-07-04 09:11:38 +01005181 if( data_length != 0 )
5182 {
5183 prf->seed = mbedtls_calloc( 1, data_length );
5184 if( prf->seed == NULL )
5185 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005186
Janos Follathd6dce9f2019-07-04 09:11:38 +01005187 memcpy( prf->seed, data, data_length );
5188 prf->seed_length = data_length;
5189 }
Janos Follathf08e2652019-06-13 09:05:41 +01005190
5191 prf->state = TLS12_PRF_STATE_SEED_SET;
5192
5193 return( PSA_SUCCESS );
5194}
5195
Janos Follath81550542019-06-13 14:26:34 +01005196static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5197 psa_algorithm_t hash_alg,
5198 const uint8_t *data,
5199 size_t data_length )
5200{
5201 psa_status_t status;
5202 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5203 return( PSA_ERROR_BAD_STATE );
5204
5205 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5206 if( status != PSA_SUCCESS )
5207 return( status );
5208
5209 prf->state = TLS12_PRF_STATE_KEY_SET;
5210
5211 return( PSA_SUCCESS );
5212}
5213
Janos Follath6660f0e2019-06-17 08:44:03 +01005214static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5215 psa_tls12_prf_key_derivation_t *prf,
5216 psa_algorithm_t hash_alg,
5217 const uint8_t *data,
5218 size_t data_length )
5219{
5220 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005221 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5222 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005223
5224 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5225 return( PSA_ERROR_INVALID_ARGUMENT );
5226
5227 /* Quoting RFC 4279, Section 2:
5228 *
5229 * The premaster secret is formed as follows: if the PSK is N octets
5230 * long, concatenate a uint16 with the value N, N zero octets, a second
5231 * uint16 with the value N, and the PSK itself.
5232 */
5233
Janos Follath40e13932019-06-26 13:22:29 +01005234 *cur++ = ( data_length >> 8 ) & 0xff;
5235 *cur++ = ( data_length >> 0 ) & 0xff;
5236 memset( cur, 0, data_length );
5237 cur += data_length;
5238 *cur++ = pms[0];
5239 *cur++ = pms[1];
5240 memcpy( cur, data, data_length );
5241 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005242
Janos Follath40e13932019-06-26 13:22:29 +01005243 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005244
5245 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5246 return( status );
5247}
5248
Janos Follath63028dd2019-06-13 09:15:47 +01005249static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5250 const uint8_t *data,
5251 size_t data_length )
5252{
5253 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5254 return( PSA_ERROR_BAD_STATE );
5255
Janos Follathd6dce9f2019-07-04 09:11:38 +01005256 if( data_length != 0 )
5257 {
5258 prf->label = mbedtls_calloc( 1, data_length );
5259 if( prf->label == NULL )
5260 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005261
Janos Follathd6dce9f2019-07-04 09:11:38 +01005262 memcpy( prf->label, data, data_length );
5263 prf->label_length = data_length;
5264 }
Janos Follath63028dd2019-06-13 09:15:47 +01005265
5266 prf->state = TLS12_PRF_STATE_LABEL_SET;
5267
5268 return( PSA_SUCCESS );
5269}
5270
Janos Follathb03233e2019-06-11 15:30:30 +01005271static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5272 psa_algorithm_t hash_alg,
5273 psa_key_derivation_step_t step,
5274 const uint8_t *data,
5275 size_t data_length )
5276{
Janos Follathb03233e2019-06-11 15:30:30 +01005277 switch( step )
5278 {
Janos Follathf08e2652019-06-13 09:05:41 +01005279 case PSA_KEY_DERIVATION_INPUT_SEED:
5280 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005281 case PSA_KEY_DERIVATION_INPUT_SECRET:
5282 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005283 case PSA_KEY_DERIVATION_INPUT_LABEL:
5284 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005285 default:
5286 return( PSA_ERROR_INVALID_ARGUMENT );
5287 }
5288}
Janos Follath6660f0e2019-06-17 08:44:03 +01005289
5290static psa_status_t psa_tls12_prf_psk_to_ms_input(
5291 psa_tls12_prf_key_derivation_t *prf,
5292 psa_algorithm_t hash_alg,
5293 psa_key_derivation_step_t step,
5294 const uint8_t *data,
5295 size_t data_length )
5296{
5297 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005298 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005299 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5300 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005301 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005302
5303 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5304}
Janos Follathb03233e2019-06-11 15:30:30 +01005305#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005306#endif /* MBEDTLS_MD_C */
5307
Janos Follathb80a94e2019-06-12 15:54:46 +01005308static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005310 psa_key_derivation_step_t step,
5311 const uint8_t *data,
5312 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313{
5314 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316
5317#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005321 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005322 step, data, data_length );
5323 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005324 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005326#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005327 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005328 {
Janos Follathb03233e2019-06-11 15:30:30 +01005329 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5330 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5331 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005332 }
5333 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5334 {
5335 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5336 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5337 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 }
5339 else
5340#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005341 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005343 return( PSA_ERROR_BAD_STATE );
5344 }
5345
5346 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005348 return( status );
5349}
5350
Janos Follath51f4a0f2019-06-14 11:35:55 +01005351psa_status_t psa_key_derivation_input_bytes(
5352 psa_key_derivation_operation_t *operation,
5353 psa_key_derivation_step_t step,
5354 const uint8_t *data,
5355 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005356{
Janos Follathc5621512019-06-14 11:27:57 +01005357 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5358 return( PSA_ERROR_INVALID_ARGUMENT );
5359
5360 return( psa_key_derivation_input_internal( operation, step,
5361 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005362}
5363
Janos Follath51f4a0f2019-06-14 11:35:55 +01005364psa_status_t psa_key_derivation_input_key(
5365 psa_key_derivation_operation_t *operation,
5366 psa_key_derivation_step_t step,
5367 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005368{
5369 psa_key_slot_t *slot;
5370 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005371 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005372 PSA_KEY_USAGE_DERIVE,
5373 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005374 if( status != PSA_SUCCESS )
5375 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005376 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005377 return( PSA_ERROR_INVALID_ARGUMENT );
5378 /* Don't allow a key to be used as an input that is usually public.
5379 * This is debatable. It's ok from a cryptographic perspective to
5380 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005381 * the material should be dedicated to a particular input step,
5382 * otherwise this may allow the key to be used in an unintended way
5383 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005384 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005385 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005386 return( psa_key_derivation_input_internal( operation,
5387 step,
5388 slot->data.raw.data,
5389 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005390}
5391
Gilles Peskineea0fb492018-07-12 17:17:20 +02005392
5393
5394/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395/* Key agreement */
5396/****************************************************************/
5397
Gilles Peskinea05219c2018-11-16 16:02:56 +01005398#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005399static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5400 size_t peer_key_length,
5401 const mbedtls_ecp_keypair *our_key,
5402 uint8_t *shared_secret,
5403 size_t shared_secret_size,
5404 size_t *shared_secret_length )
5405{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005406 mbedtls_ecp_keypair *their_key = NULL;
5407 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005408 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005409 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005410
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005411 status = psa_import_ec_public_key(
5412 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5413 peer_key, peer_key_length,
5414 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005415 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005416 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005417
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005418 status = mbedtls_to_psa_error(
5419 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5420 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005421 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005422 status = mbedtls_to_psa_error(
5423 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5424 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005425 goto exit;
5426
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005427 status = mbedtls_to_psa_error(
5428 mbedtls_ecdh_calc_secret( &ecdh,
5429 shared_secret_length,
5430 shared_secret, shared_secret_size,
5431 mbedtls_ctr_drbg_random,
5432 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005433
5434exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005435 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005436 mbedtls_ecp_keypair_free( their_key );
5437 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005438 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005439}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005440#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005441
Gilles Peskine01d718c2018-09-18 12:01:02 +02005442#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5443
Gilles Peskine0216fe12019-04-11 21:23:21 +02005444static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5445 psa_key_slot_t *private_key,
5446 const uint8_t *peer_key,
5447 size_t peer_key_length,
5448 uint8_t *shared_secret,
5449 size_t shared_secret_size,
5450 size_t *shared_secret_length )
5451{
5452 switch( alg )
5453 {
5454#if defined(MBEDTLS_ECDH_C)
5455 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005456 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005457 return( PSA_ERROR_INVALID_ARGUMENT );
5458 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5459 private_key->data.ecp,
5460 shared_secret, shared_secret_size,
5461 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005462#endif /* MBEDTLS_ECDH_C */
5463 default:
5464 (void) private_key;
5465 (void) peer_key;
5466 (void) peer_key_length;
5467 (void) shared_secret;
5468 (void) shared_secret_size;
5469 (void) shared_secret_length;
5470 return( PSA_ERROR_NOT_SUPPORTED );
5471 }
5472}
5473
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005474/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005475 * to potentially free embedded data structures and wipe confidential data.
5476 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005478 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005479 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005480 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005481 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005482{
5483 psa_status_t status;
5484 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5485 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005487
5488 /* Step 1: run the secret agreement algorithm to generate the shared
5489 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005490 status = psa_key_agreement_raw_internal( ka_alg,
5491 private_key,
5492 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005493 shared_secret,
5494 sizeof( shared_secret ),
5495 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005496 if( status != PSA_SUCCESS )
5497 goto exit;
5498
5499 /* Step 2: set up the key derivation to generate key material from
5500 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005501 status = psa_key_derivation_input_internal( operation, step,
5502 shared_secret,
5503 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005504
Gilles Peskine01d718c2018-09-18 12:01:02 +02005505exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005506 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005507 return( status );
5508}
5509
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005511 psa_key_derivation_step_t step,
5512 psa_key_handle_t private_key,
5513 const uint8_t *peer_key,
5514 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005515{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005516 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005517 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005519 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005520 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005521 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005522 if( status != PSA_SUCCESS )
5523 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005525 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005526 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005527 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005529 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005530}
5531
Gilles Peskinebe697d82019-05-16 18:00:41 +02005532psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5533 psa_key_handle_t private_key,
5534 const uint8_t *peer_key,
5535 size_t peer_key_length,
5536 uint8_t *output,
5537 size_t output_size,
5538 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005539{
5540 psa_key_slot_t *slot;
5541 psa_status_t status;
5542
5543 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5544 {
5545 status = PSA_ERROR_INVALID_ARGUMENT;
5546 goto exit;
5547 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005548 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005549 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005550 if( status != PSA_SUCCESS )
5551 goto exit;
5552
5553 status = psa_key_agreement_raw_internal( alg, slot,
5554 peer_key, peer_key_length,
5555 output, output_size,
5556 output_length );
5557
5558exit:
5559 if( status != PSA_SUCCESS )
5560 {
5561 /* If an error happens and is not handled properly, the output
5562 * may be used as a key to protect sensitive data. Arrange for such
5563 * a key to be random, which is likely to result in decryption or
5564 * verification errors. This is better than filling the buffer with
5565 * some constant data such as zeros, which would result in the data
5566 * being protected with a reproducible, easily knowable key.
5567 */
5568 psa_generate_random( output, output_size );
5569 *output_length = output_size;
5570 }
5571 return( status );
5572}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005573
5574
5575/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005576/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005577/****************************************************************/
5578
5579psa_status_t psa_generate_random( uint8_t *output,
5580 size_t output_size )
5581{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005582 int ret;
5583 GUARD_MODULE_INITIALIZED;
5584
5585 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005586 return( mbedtls_to_psa_error( ret ) );
5587}
5588
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005589#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5590#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005591
Gilles Peskine7228da22019-07-15 11:06:15 +02005592psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005593 size_t seed_size )
5594{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005595 if( global_data.initialized )
5596 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005597
5598 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5599 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5600 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5601 return( PSA_ERROR_INVALID_ARGUMENT );
5602
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005603 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005604}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005605#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005606
Gilles Peskinee56e8782019-04-26 17:34:02 +02005607#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5608static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5609 size_t domain_parameters_size,
5610 int *exponent )
5611{
5612 size_t i;
5613 uint32_t acc = 0;
5614
5615 if( domain_parameters_size == 0 )
5616 {
5617 *exponent = 65537;
5618 return( PSA_SUCCESS );
5619 }
5620
5621 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5622 * support values that fit in a 32-bit integer, which is larger than
5623 * int on just about every platform anyway. */
5624 if( domain_parameters_size > sizeof( acc ) )
5625 return( PSA_ERROR_NOT_SUPPORTED );
5626 for( i = 0; i < domain_parameters_size; i++ )
5627 acc = ( acc << 8 ) | domain_parameters[i];
5628 if( acc > INT_MAX )
5629 return( PSA_ERROR_NOT_SUPPORTED );
5630 *exponent = acc;
5631 return( PSA_SUCCESS );
5632}
5633#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5634
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005635static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005636 psa_key_slot_t *slot, size_t bits,
5637 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005638{
Gilles Peskine8e338702019-07-30 20:06:31 +02005639 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005640
Gilles Peskinee56e8782019-04-26 17:34:02 +02005641 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005642 return( PSA_ERROR_INVALID_ARGUMENT );
5643
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005644 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005645 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005646 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005647 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005648 if( status != PSA_SUCCESS )
5649 return( status );
5650 status = psa_generate_random( slot->data.raw.data,
5651 slot->data.raw.bytes );
5652 if( status != PSA_SUCCESS )
5653 {
5654 mbedtls_free( slot->data.raw.data );
5655 return( status );
5656 }
5657#if defined(MBEDTLS_DES_C)
5658 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005659 psa_des_set_key_parity( slot->data.raw.data,
5660 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005661#endif /* MBEDTLS_DES_C */
5662 }
5663 else
5664
5665#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005666 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005667 {
5668 mbedtls_rsa_context *rsa;
5669 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005670 int exponent;
5671 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005672 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5673 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005674 /* Accept only byte-aligned keys, for the same reasons as
5675 * in psa_import_rsa_key(). */
5676 if( bits % 8 != 0 )
5677 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005678 status = psa_read_rsa_exponent( domain_parameters,
5679 domain_parameters_size,
5680 &exponent );
5681 if( status != PSA_SUCCESS )
5682 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005683 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5684 if( rsa == NULL )
5685 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5686 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5687 ret = mbedtls_rsa_gen_key( rsa,
5688 mbedtls_ctr_drbg_random,
5689 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005690 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005691 exponent );
5692 if( ret != 0 )
5693 {
5694 mbedtls_rsa_free( rsa );
5695 mbedtls_free( rsa );
5696 return( mbedtls_to_psa_error( ret ) );
5697 }
5698 slot->data.rsa = rsa;
5699 }
5700 else
5701#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5702
5703#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005704 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005705 {
5706 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5707 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5708 const mbedtls_ecp_curve_info *curve_info =
5709 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5710 mbedtls_ecp_keypair *ecp;
5711 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005712 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005713 return( PSA_ERROR_NOT_SUPPORTED );
5714 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5715 return( PSA_ERROR_NOT_SUPPORTED );
5716 if( curve_info->bit_size != bits )
5717 return( PSA_ERROR_INVALID_ARGUMENT );
5718 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5719 if( ecp == NULL )
5720 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5721 mbedtls_ecp_keypair_init( ecp );
5722 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5723 mbedtls_ctr_drbg_random,
5724 &global_data.ctr_drbg );
5725 if( ret != 0 )
5726 {
5727 mbedtls_ecp_keypair_free( ecp );
5728 mbedtls_free( ecp );
5729 return( mbedtls_to_psa_error( ret ) );
5730 }
5731 slot->data.ecp = ecp;
5732 }
5733 else
5734#endif /* MBEDTLS_ECP_C */
5735
5736 return( PSA_ERROR_NOT_SUPPORTED );
5737
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005738 return( PSA_SUCCESS );
5739}
5740
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005741psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005742 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005743{
5744 psa_status_t status;
5745 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005746 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005747 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005748#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5749 if( driver != NULL )
5750 {
5751 /* Generating a key in a secure element is not implemented yet. */
5752 status = PSA_ERROR_NOT_SUPPORTED;
5753 }
5754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005755 if( status == PSA_SUCCESS )
5756 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005757 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005758 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005759 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005760 }
5761 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005762 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005763 if( status != PSA_SUCCESS )
5764 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005765 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005766 *handle = 0;
5767 }
5768 return( status );
5769}
5770
5771
Gilles Peskine05d69892018-06-19 22:00:52 +02005772
5773/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005774/* Module setup */
5775/****************************************************************/
5776
Gilles Peskine5e769522018-11-20 21:59:56 +01005777psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5778 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5779 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5780{
5781 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5782 return( PSA_ERROR_BAD_STATE );
5783 global_data.entropy_init = entropy_init;
5784 global_data.entropy_free = entropy_free;
5785 return( PSA_SUCCESS );
5786}
5787
Gilles Peskinee59236f2018-01-27 23:32:46 +01005788void mbedtls_psa_crypto_free( void )
5789{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005790 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005791 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5792 {
5793 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005794 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005795 }
5796 /* Wipe all remaining data, including configuration.
5797 * In particular, this sets all state indicator to the value
5798 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005799 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005800#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005801 /* Unregister all secure element drivers, so that we restart from
5802 * a pristine state. */
5803 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005805}
5806
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005807#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5808/** Recover a transaction that was interrupted by a power failure.
5809 *
5810 * This function is called during initialization, before psa_crypto_init()
5811 * returns. If this function returns a failure status, the initialization
5812 * fails.
5813 */
5814static psa_status_t psa_crypto_recover_transaction(
5815 const psa_crypto_transaction_t *transaction )
5816{
5817 switch( transaction->unknown.type )
5818 {
5819 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5820 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5821 /* TOnogrepDO - fall through to the failure case until this
5822 * is implemented */
5823 default:
5824 /* We found an unsupported transaction in the storage.
5825 * We don't know what state the storage is in. Give up. */
5826 return( PSA_ERROR_STORAGE_FAILURE );
5827 }
5828}
5829#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5830
Gilles Peskinee59236f2018-01-27 23:32:46 +01005831psa_status_t psa_crypto_init( void )
5832{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005833 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005834 const unsigned char drbg_seed[] = "PSA";
5835
Gilles Peskinec6b69072018-11-20 21:42:52 +01005836 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005837 if( global_data.initialized != 0 )
5838 return( PSA_SUCCESS );
5839
Gilles Peskine5e769522018-11-20 21:59:56 +01005840 /* Set default configuration if
5841 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5842 if( global_data.entropy_init == NULL )
5843 global_data.entropy_init = mbedtls_entropy_init;
5844 if( global_data.entropy_free == NULL )
5845 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005846
Gilles Peskinec6b69072018-11-20 21:42:52 +01005847 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005848 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005849 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005850 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005851 status = mbedtls_to_psa_error(
5852 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5853 mbedtls_entropy_func,
5854 &global_data.entropy,
5855 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5856 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005857 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005858 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005859
Gilles Peskine66fb1262018-12-10 16:29:04 +01005860 status = psa_initialize_key_slots( );
5861 if( status != PSA_SUCCESS )
5862 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005863
Gilles Peskine4b734222019-07-24 15:56:31 +02005864#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005865 status = psa_crypto_load_transaction( );
5866 if( status == PSA_SUCCESS )
5867 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005868 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5869 if( status != PSA_SUCCESS )
5870 goto exit;
5871 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005872 }
5873 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5874 {
5875 /* There's no transaction to complete. It's all good. */
5876 status = PSA_SUCCESS;
5877 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005878#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005879
Gilles Peskinec6b69072018-11-20 21:42:52 +01005880 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005881 global_data.initialized = 1;
5882
Gilles Peskinee59236f2018-01-27 23:32:46 +01005883exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005884 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005885 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005886 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005887}
5888
5889#endif /* MBEDTLS_PSA_CRYPTO_C */