blob: 03e56a1c08abb1f3a1f56842ec70095235aa53b9 [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 Peskine4ed0e6f2019-07-30 20:22:33 +02001557 status = psa_save_persistent_key( &attributes.core,
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001558 (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 )
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001575 status = psa_save_persistent_key( &attributes.core,
1576 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001577
Gilles Peskine1df83d42019-07-23 16:13:14 +02001578 if( buffer_size != 0 )
1579 mbedtls_platform_zeroize( buffer, buffer_size );
1580 mbedtls_free( buffer );
1581 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001582 }
1583#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1584
Gilles Peskinecbaff462019-07-12 23:46:04 +02001585#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1586 if( driver != NULL )
1587 {
1588 status = psa_save_se_persistent_data( driver );
1589 if( status != PSA_SUCCESS )
1590 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001591 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001592 return( status );
1593 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001594 status = psa_crypto_stop_transaction( );
1595 if( status != PSA_SUCCESS )
1596 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001597 }
1598#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1599
Gilles Peskine4747d192019-04-17 15:05:45 +02001600 return( status );
1601}
1602
1603/** Abort the creation of a key.
1604 *
1605 * You may call this function after calling psa_start_key_creation(),
1606 * or after psa_finish_key_creation() fails. In other circumstances, this
1607 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001608 * See the documentation of psa_start_key_creation() for the intended use
1609 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001610 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001611 * \param[in,out] slot Pointer to the slot with key material.
1612 * \param[in] driver The secure element driver for the key,
1613 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001614 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001615static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001616 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001617{
Gilles Peskine011e4282019-06-26 18:34:38 +02001618 (void) driver;
1619
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 if( slot == NULL )
1621 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001622
1623#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1624 /* TOnogrepDO: If the key has already been created in the secure
1625 * element, and the failure happened later (when saving metadata
1626 * to internal storage), we need to destroy the key in the secure
1627 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001628
1629 /* Abort the ongoing transaction if any. We already did what it
1630 * takes to undo any partial creation. All that's left is to update
1631 * the transaction data itself. */
1632 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001633#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1634
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 psa_wipe_key_slot( slot );
1636}
1637
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001638static psa_status_t psa_check_key_slot_attributes(
1639 const psa_key_slot_t *slot,
1640 const psa_key_attributes_t *attributes )
1641{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001642 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001643 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001644 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001645 return( PSA_ERROR_INVALID_ARGUMENT );
1646 }
1647
1648 if( attributes->domain_parameters_size != 0 )
1649 {
1650#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001651 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001652 {
1653 mbedtls_mpi actual, required;
1654 int ret;
1655 mbedtls_mpi_init( &actual );
1656 mbedtls_mpi_init( &required );
1657 ret = mbedtls_rsa_export( slot->data.rsa,
1658 NULL, NULL, NULL, NULL, &actual );
1659 if( ret != 0 )
1660 goto rsa_exit;
1661 ret = mbedtls_mpi_read_binary( &required,
1662 attributes->domain_parameters,
1663 attributes->domain_parameters_size );
1664 if( ret != 0 )
1665 goto rsa_exit;
1666 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1667 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1668 rsa_exit:
1669 mbedtls_mpi_free( &actual );
1670 mbedtls_mpi_free( &required );
1671 if( ret != 0)
1672 return( mbedtls_to_psa_error( ret ) );
1673 }
1674 else
1675#endif
1676 {
1677 return( PSA_ERROR_INVALID_ARGUMENT );
1678 }
1679 }
1680
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001681 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001682 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001683 if( attributes->core.bits != psa_get_key_slot_bits( slot ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001684 return( PSA_ERROR_INVALID_ARGUMENT );
1685 }
1686
1687 return( PSA_SUCCESS );
1688}
1689
Gilles Peskine4747d192019-04-17 15:05:45 +02001690psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001691 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001692 size_t data_length,
1693 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001694{
1695 psa_status_t status;
1696 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001697 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001698
Gilles Peskine011e4282019-06-26 18:34:38 +02001699 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001700 if( status != PSA_SUCCESS )
1701 goto exit;
1702
Gilles Peskine5d309672019-07-12 23:47:28 +02001703#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1704 if( driver != NULL )
1705 {
1706 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1707 if( drv->key_management == NULL ||
1708 drv->key_management->p_import == NULL )
1709 {
1710 status = PSA_ERROR_NOT_SUPPORTED;
1711 goto exit;
1712 }
1713 status = drv->key_management->p_import(
1714 psa_get_se_driver_context( driver ),
1715 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001716 slot->attr.lifetime, slot->attr.type,
1717 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001718 data, data_length,
1719 &slot->data.se.bits );
Gilles Peskine5d309672019-07-12 23:47:28 +02001720 }
1721 else
1722#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1723 {
1724 status = psa_import_key_into_slot( slot, data, data_length );
1725 if( status != PSA_SUCCESS )
1726 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001727 }
Gilles Peskine18017402019-07-24 20:25:59 +02001728 status = psa_check_key_slot_attributes( slot, attributes );
1729 if( status != PSA_SUCCESS )
1730 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001731
Gilles Peskine011e4282019-06-26 18:34:38 +02001732 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001733exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 if( status != PSA_SUCCESS )
1735 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 *handle = 0;
1738 }
1739 return( status );
1740}
1741
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001742static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001743 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001744{
1745 psa_status_t status;
1746 uint8_t *buffer = NULL;
1747 size_t buffer_size = 0;
1748 size_t length;
1749
Gilles Peskine8e338702019-07-30 20:06:31 +02001750 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001751 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001752 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001753 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001754 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001755 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1756 if( status != PSA_SUCCESS )
1757 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001758 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001759 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001760
1761exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001762 if( buffer_size != 0 )
1763 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001764 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001765 return( status );
1766}
1767
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001768psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1769 const psa_key_attributes_t *specified_attributes,
1770 psa_key_handle_t *target_handle )
1771{
1772 psa_status_t status;
1773 psa_key_slot_t *source_slot = NULL;
1774 psa_key_slot_t *target_slot = NULL;
1775 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001776 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001777
Gilles Peskine28f8f302019-07-24 13:30:31 +02001778 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001779 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001780 if( status != PSA_SUCCESS )
1781 goto exit;
1782
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1784 if( status != PSA_SUCCESS )
1785 goto exit;
1786
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001787 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001788 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001789 if( status != PSA_SUCCESS )
1790 goto exit;
1791
1792 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001793 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001794 if( status != PSA_SUCCESS )
1795 goto exit;
1796
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001797#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1798 if( driver != NULL )
1799 {
1800 /* Copying to a secure element is not implemented yet. */
1801 status = PSA_ERROR_NOT_SUPPORTED;
1802 goto exit;
1803 }
1804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1805
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001806 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 if( status != PSA_SUCCESS )
1808 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001809
Gilles Peskine011e4282019-06-26 18:34:38 +02001810 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001811exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001812 if( status != PSA_SUCCESS )
1813 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001814 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001815 *target_handle = 0;
1816 }
1817 return( status );
1818}
1819
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001820
1821
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001822/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001823/* Message digests */
1824/****************************************************************/
1825
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001826static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001827{
1828 switch( alg )
1829 {
1830#if defined(MBEDTLS_MD2_C)
1831 case PSA_ALG_MD2:
1832 return( &mbedtls_md2_info );
1833#endif
1834#if defined(MBEDTLS_MD4_C)
1835 case PSA_ALG_MD4:
1836 return( &mbedtls_md4_info );
1837#endif
1838#if defined(MBEDTLS_MD5_C)
1839 case PSA_ALG_MD5:
1840 return( &mbedtls_md5_info );
1841#endif
1842#if defined(MBEDTLS_RIPEMD160_C)
1843 case PSA_ALG_RIPEMD160:
1844 return( &mbedtls_ripemd160_info );
1845#endif
1846#if defined(MBEDTLS_SHA1_C)
1847 case PSA_ALG_SHA_1:
1848 return( &mbedtls_sha1_info );
1849#endif
1850#if defined(MBEDTLS_SHA256_C)
1851 case PSA_ALG_SHA_224:
1852 return( &mbedtls_sha224_info );
1853 case PSA_ALG_SHA_256:
1854 return( &mbedtls_sha256_info );
1855#endif
1856#if defined(MBEDTLS_SHA512_C)
1857 case PSA_ALG_SHA_384:
1858 return( &mbedtls_sha384_info );
1859 case PSA_ALG_SHA_512:
1860 return( &mbedtls_sha512_info );
1861#endif
1862 default:
1863 return( NULL );
1864 }
1865}
1866
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001867psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1868{
1869 switch( operation->alg )
1870 {
Gilles Peskine81736312018-06-26 15:04:31 +02001871 case 0:
1872 /* The object has (apparently) been initialized but it is not
1873 * in use. It's ok to call abort on such an object, and there's
1874 * nothing to do. */
1875 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001876#if defined(MBEDTLS_MD2_C)
1877 case PSA_ALG_MD2:
1878 mbedtls_md2_free( &operation->ctx.md2 );
1879 break;
1880#endif
1881#if defined(MBEDTLS_MD4_C)
1882 case PSA_ALG_MD4:
1883 mbedtls_md4_free( &operation->ctx.md4 );
1884 break;
1885#endif
1886#if defined(MBEDTLS_MD5_C)
1887 case PSA_ALG_MD5:
1888 mbedtls_md5_free( &operation->ctx.md5 );
1889 break;
1890#endif
1891#if defined(MBEDTLS_RIPEMD160_C)
1892 case PSA_ALG_RIPEMD160:
1893 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1894 break;
1895#endif
1896#if defined(MBEDTLS_SHA1_C)
1897 case PSA_ALG_SHA_1:
1898 mbedtls_sha1_free( &operation->ctx.sha1 );
1899 break;
1900#endif
1901#if defined(MBEDTLS_SHA256_C)
1902 case PSA_ALG_SHA_224:
1903 case PSA_ALG_SHA_256:
1904 mbedtls_sha256_free( &operation->ctx.sha256 );
1905 break;
1906#endif
1907#if defined(MBEDTLS_SHA512_C)
1908 case PSA_ALG_SHA_384:
1909 case PSA_ALG_SHA_512:
1910 mbedtls_sha512_free( &operation->ctx.sha512 );
1911 break;
1912#endif
1913 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001914 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001915 }
1916 operation->alg = 0;
1917 return( PSA_SUCCESS );
1918}
1919
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001920psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001921 psa_algorithm_t alg )
1922{
1923 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001924
1925 /* A context must be freshly initialized before it can be set up. */
1926 if( operation->alg != 0 )
1927 {
1928 return( PSA_ERROR_BAD_STATE );
1929 }
1930
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001931 switch( alg )
1932 {
1933#if defined(MBEDTLS_MD2_C)
1934 case PSA_ALG_MD2:
1935 mbedtls_md2_init( &operation->ctx.md2 );
1936 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1937 break;
1938#endif
1939#if defined(MBEDTLS_MD4_C)
1940 case PSA_ALG_MD4:
1941 mbedtls_md4_init( &operation->ctx.md4 );
1942 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1943 break;
1944#endif
1945#if defined(MBEDTLS_MD5_C)
1946 case PSA_ALG_MD5:
1947 mbedtls_md5_init( &operation->ctx.md5 );
1948 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1949 break;
1950#endif
1951#if defined(MBEDTLS_RIPEMD160_C)
1952 case PSA_ALG_RIPEMD160:
1953 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1954 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1955 break;
1956#endif
1957#if defined(MBEDTLS_SHA1_C)
1958 case PSA_ALG_SHA_1:
1959 mbedtls_sha1_init( &operation->ctx.sha1 );
1960 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1961 break;
1962#endif
1963#if defined(MBEDTLS_SHA256_C)
1964 case PSA_ALG_SHA_224:
1965 mbedtls_sha256_init( &operation->ctx.sha256 );
1966 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1967 break;
1968 case PSA_ALG_SHA_256:
1969 mbedtls_sha256_init( &operation->ctx.sha256 );
1970 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1971 break;
1972#endif
1973#if defined(MBEDTLS_SHA512_C)
1974 case PSA_ALG_SHA_384:
1975 mbedtls_sha512_init( &operation->ctx.sha512 );
1976 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1977 break;
1978 case PSA_ALG_SHA_512:
1979 mbedtls_sha512_init( &operation->ctx.sha512 );
1980 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1981 break;
1982#endif
1983 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001984 return( PSA_ALG_IS_HASH( alg ) ?
1985 PSA_ERROR_NOT_SUPPORTED :
1986 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001987 }
1988 if( ret == 0 )
1989 operation->alg = alg;
1990 else
1991 psa_hash_abort( operation );
1992 return( mbedtls_to_psa_error( ret ) );
1993}
1994
1995psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1996 const uint8_t *input,
1997 size_t input_length )
1998{
1999 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002000
2001 /* Don't require hash implementations to behave correctly on a
2002 * zero-length input, which may have an invalid pointer. */
2003 if( input_length == 0 )
2004 return( PSA_SUCCESS );
2005
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002006 switch( operation->alg )
2007 {
2008#if defined(MBEDTLS_MD2_C)
2009 case PSA_ALG_MD2:
2010 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2011 input, input_length );
2012 break;
2013#endif
2014#if defined(MBEDTLS_MD4_C)
2015 case PSA_ALG_MD4:
2016 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2017 input, input_length );
2018 break;
2019#endif
2020#if defined(MBEDTLS_MD5_C)
2021 case PSA_ALG_MD5:
2022 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2023 input, input_length );
2024 break;
2025#endif
2026#if defined(MBEDTLS_RIPEMD160_C)
2027 case PSA_ALG_RIPEMD160:
2028 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2029 input, input_length );
2030 break;
2031#endif
2032#if defined(MBEDTLS_SHA1_C)
2033 case PSA_ALG_SHA_1:
2034 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2035 input, input_length );
2036 break;
2037#endif
2038#if defined(MBEDTLS_SHA256_C)
2039 case PSA_ALG_SHA_224:
2040 case PSA_ALG_SHA_256:
2041 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2042 input, input_length );
2043 break;
2044#endif
2045#if defined(MBEDTLS_SHA512_C)
2046 case PSA_ALG_SHA_384:
2047 case PSA_ALG_SHA_512:
2048 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2049 input, input_length );
2050 break;
2051#endif
2052 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002053 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002054 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002055
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002056 if( ret != 0 )
2057 psa_hash_abort( operation );
2058 return( mbedtls_to_psa_error( ret ) );
2059}
2060
2061psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2062 uint8_t *hash,
2063 size_t hash_size,
2064 size_t *hash_length )
2065{
itayzafrir40835d42018-08-02 13:14:17 +03002066 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002068 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002069
2070 /* Fill the output buffer with something that isn't a valid hash
2071 * (barring an attack on the hash and deliberately-crafted input),
2072 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002073 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002074 /* If hash_size is 0 then hash may be NULL and then the
2075 * call to memset would have undefined behavior. */
2076 if( hash_size != 0 )
2077 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002078
2079 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002080 {
2081 status = PSA_ERROR_BUFFER_TOO_SMALL;
2082 goto exit;
2083 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002084
2085 switch( operation->alg )
2086 {
2087#if defined(MBEDTLS_MD2_C)
2088 case PSA_ALG_MD2:
2089 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2090 break;
2091#endif
2092#if defined(MBEDTLS_MD4_C)
2093 case PSA_ALG_MD4:
2094 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2095 break;
2096#endif
2097#if defined(MBEDTLS_MD5_C)
2098 case PSA_ALG_MD5:
2099 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2100 break;
2101#endif
2102#if defined(MBEDTLS_RIPEMD160_C)
2103 case PSA_ALG_RIPEMD160:
2104 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA1_C)
2108 case PSA_ALG_SHA_1:
2109 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2110 break;
2111#endif
2112#if defined(MBEDTLS_SHA256_C)
2113 case PSA_ALG_SHA_224:
2114 case PSA_ALG_SHA_256:
2115 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2116 break;
2117#endif
2118#if defined(MBEDTLS_SHA512_C)
2119 case PSA_ALG_SHA_384:
2120 case PSA_ALG_SHA_512:
2121 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2122 break;
2123#endif
2124 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002125 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126 }
itayzafrir40835d42018-08-02 13:14:17 +03002127 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128
itayzafrir40835d42018-08-02 13:14:17 +03002129exit:
2130 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002131 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002132 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133 return( psa_hash_abort( operation ) );
2134 }
2135 else
2136 {
2137 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002138 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002139 }
2140}
2141
Gilles Peskine2d277862018-06-18 15:41:12 +02002142psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2143 const uint8_t *hash,
2144 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145{
2146 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2147 size_t actual_hash_length;
2148 psa_status_t status = psa_hash_finish( operation,
2149 actual_hash, sizeof( actual_hash ),
2150 &actual_hash_length );
2151 if( status != PSA_SUCCESS )
2152 return( status );
2153 if( actual_hash_length != hash_length )
2154 return( PSA_ERROR_INVALID_SIGNATURE );
2155 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2156 return( PSA_ERROR_INVALID_SIGNATURE );
2157 return( PSA_SUCCESS );
2158}
2159
Gilles Peskineeb35d782019-01-22 17:56:16 +01002160psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2161 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002162{
2163 if( target_operation->alg != 0 )
2164 return( PSA_ERROR_BAD_STATE );
2165
2166 switch( source_operation->alg )
2167 {
2168 case 0:
2169 return( PSA_ERROR_BAD_STATE );
2170#if defined(MBEDTLS_MD2_C)
2171 case PSA_ALG_MD2:
2172 mbedtls_md2_clone( &target_operation->ctx.md2,
2173 &source_operation->ctx.md2 );
2174 break;
2175#endif
2176#if defined(MBEDTLS_MD4_C)
2177 case PSA_ALG_MD4:
2178 mbedtls_md4_clone( &target_operation->ctx.md4,
2179 &source_operation->ctx.md4 );
2180 break;
2181#endif
2182#if defined(MBEDTLS_MD5_C)
2183 case PSA_ALG_MD5:
2184 mbedtls_md5_clone( &target_operation->ctx.md5,
2185 &source_operation->ctx.md5 );
2186 break;
2187#endif
2188#if defined(MBEDTLS_RIPEMD160_C)
2189 case PSA_ALG_RIPEMD160:
2190 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2191 &source_operation->ctx.ripemd160 );
2192 break;
2193#endif
2194#if defined(MBEDTLS_SHA1_C)
2195 case PSA_ALG_SHA_1:
2196 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2197 &source_operation->ctx.sha1 );
2198 break;
2199#endif
2200#if defined(MBEDTLS_SHA256_C)
2201 case PSA_ALG_SHA_224:
2202 case PSA_ALG_SHA_256:
2203 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2204 &source_operation->ctx.sha256 );
2205 break;
2206#endif
2207#if defined(MBEDTLS_SHA512_C)
2208 case PSA_ALG_SHA_384:
2209 case PSA_ALG_SHA_512:
2210 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2211 &source_operation->ctx.sha512 );
2212 break;
2213#endif
2214 default:
2215 return( PSA_ERROR_NOT_SUPPORTED );
2216 }
2217
2218 target_operation->alg = source_operation->alg;
2219 return( PSA_SUCCESS );
2220}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221
2222
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224/* MAC */
2225/****************************************************************/
2226
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002227static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228 psa_algorithm_t alg,
2229 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002230 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002231 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002232{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002234 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002236 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002237 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002238
Gilles Peskine8c9def32018-02-08 10:02:12 +01002239 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2240 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002241 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002243 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002244 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245 mode = MBEDTLS_MODE_STREAM;
2246 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002247 case PSA_ALG_CTR:
2248 mode = MBEDTLS_MODE_CTR;
2249 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002250 case PSA_ALG_CFB:
2251 mode = MBEDTLS_MODE_CFB;
2252 break;
2253 case PSA_ALG_OFB:
2254 mode = MBEDTLS_MODE_OFB;
2255 break;
2256 case PSA_ALG_CBC_NO_PADDING:
2257 mode = MBEDTLS_MODE_CBC;
2258 break;
2259 case PSA_ALG_CBC_PKCS7:
2260 mode = MBEDTLS_MODE_CBC;
2261 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002262 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002263 mode = MBEDTLS_MODE_CCM;
2264 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002265 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002266 mode = MBEDTLS_MODE_GCM;
2267 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002268 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2269 mode = MBEDTLS_MODE_CHACHAPOLY;
2270 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271 default:
2272 return( NULL );
2273 }
2274 }
2275 else if( alg == PSA_ALG_CMAC )
2276 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277 else
2278 return( NULL );
2279
2280 switch( key_type )
2281 {
2282 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002283 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002284 break;
2285 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002286 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2287 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002289 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002290 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002291 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002292 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2293 * but two-key Triple-DES is functionally three-key Triple-DES
2294 * with K1=K3, so that's how we present it to mbedtls. */
2295 if( key_bits == 128 )
2296 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002297 break;
2298 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002299 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002300 break;
2301 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002302 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002304 case PSA_KEY_TYPE_CHACHA20:
2305 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2306 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002307 default:
2308 return( NULL );
2309 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002310 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002311 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312
Jaeden Amero23bbb752018-06-26 14:16:54 +01002313 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2314 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002315}
2316
Gilles Peskinea05219c2018-11-16 16:02:56 +01002317#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002318static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002319{
Gilles Peskine2d277862018-06-18 15:41:12 +02002320 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002321 {
2322 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002323 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002324 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002325 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002326 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002327 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002328 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002329 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002330 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002331 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002332 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002333 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002334 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002335 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002336 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002337 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002338 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002339 return( 128 );
2340 default:
2341 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002342 }
2343}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002344#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002345
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002346/* Initialize the MAC operation structure. Once this function has been
2347 * called, psa_mac_abort can run and will do the right thing. */
2348static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2349 psa_algorithm_t alg )
2350{
2351 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2352
2353 operation->alg = alg;
2354 operation->key_set = 0;
2355 operation->iv_set = 0;
2356 operation->iv_required = 0;
2357 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002358 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359
2360#if defined(MBEDTLS_CMAC_C)
2361 if( alg == PSA_ALG_CMAC )
2362 {
2363 operation->iv_required = 0;
2364 mbedtls_cipher_init( &operation->ctx.cmac );
2365 status = PSA_SUCCESS;
2366 }
2367 else
2368#endif /* MBEDTLS_CMAC_C */
2369#if defined(MBEDTLS_MD_C)
2370 if( PSA_ALG_IS_HMAC( operation->alg ) )
2371 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002372 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2373 operation->ctx.hmac.hash_ctx.alg = 0;
2374 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002375 }
2376 else
2377#endif /* MBEDTLS_MD_C */
2378 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002379 if( ! PSA_ALG_IS_MAC( alg ) )
2380 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002381 }
2382
2383 if( status != PSA_SUCCESS )
2384 memset( operation, 0, sizeof( *operation ) );
2385 return( status );
2386}
2387
Gilles Peskine01126fa2018-07-12 17:04:55 +02002388#if defined(MBEDTLS_MD_C)
2389static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2390{
Gilles Peskine3f108122018-12-07 18:14:53 +01002391 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002392 return( psa_hash_abort( &hmac->hash_ctx ) );
2393}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002394
Janos Follath999f6482019-06-11 12:04:10 +01002395#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002396static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2397{
2398 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2399 memset( hmac, 0, sizeof( *hmac ) );
2400}
Janos Follath999f6482019-06-11 12:04:10 +01002401#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002402#endif /* MBEDTLS_MD_C */
2403
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2405{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002406 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002408 /* The object has (apparently) been initialized but it is not
2409 * in use. It's ok to call abort on such an object, and there's
2410 * nothing to do. */
2411 return( PSA_SUCCESS );
2412 }
2413 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002415 if( operation->alg == PSA_ALG_CMAC )
2416 {
2417 mbedtls_cipher_free( &operation->ctx.cmac );
2418 }
2419 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002422 if( PSA_ALG_IS_HMAC( operation->alg ) )
2423 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002424 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002425 }
2426 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002428 {
2429 /* Sanity check (shouldn't happen: operation->alg should
2430 * always have been initialized to a valid value). */
2431 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 }
Moran Peker41deec42018-04-04 15:43:05 +03002433
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434 operation->alg = 0;
2435 operation->key_set = 0;
2436 operation->iv_set = 0;
2437 operation->iv_required = 0;
2438 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002439 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002440
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002442
2443bad_state:
2444 /* If abort is called on an uninitialized object, we can't trust
2445 * anything. Wipe the object in case it contains confidential data.
2446 * This may result in a memory leak if a pointer gets overwritten,
2447 * but it's too late to do anything about this. */
2448 memset( operation, 0, sizeof( *operation ) );
2449 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450}
2451
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002452#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002453static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002454 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002455 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002456 const mbedtls_cipher_info_t *cipher_info )
2457{
2458 int ret;
2459
2460 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002461
2462 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2463 if( ret != 0 )
2464 return( ret );
2465
2466 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2467 slot->data.raw.data,
2468 key_bits );
2469 return( ret );
2470}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002471#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002472
Gilles Peskine248051a2018-06-20 16:09:38 +02002473#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002474static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2475 const uint8_t *key,
2476 size_t key_length,
2477 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002478{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002479 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002480 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002481 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002482 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002483 psa_status_t status;
2484
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002485 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2486 * overflow below. This should never trigger if the hash algorithm
2487 * is implemented correctly. */
2488 /* The size checks against the ipad and opad buffers cannot be written
2489 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2490 * because that triggers -Wlogical-op on GCC 7.3. */
2491 if( block_size > sizeof( ipad ) )
2492 return( PSA_ERROR_NOT_SUPPORTED );
2493 if( block_size > sizeof( hmac->opad ) )
2494 return( PSA_ERROR_NOT_SUPPORTED );
2495 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002496 return( PSA_ERROR_NOT_SUPPORTED );
2497
Gilles Peskined223b522018-06-11 18:12:58 +02002498 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002499 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002500 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002501 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002502 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002503 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002505 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002506 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002507 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002508 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002509 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002510 }
Gilles Peskine96889972018-07-12 17:07:03 +02002511 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2512 * but it is permitted. It is common when HMAC is used in HKDF, for
2513 * example. Don't call `memcpy` in the 0-length because `key` could be
2514 * an invalid pointer which would make the behavior undefined. */
2515 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002516 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002517
Gilles Peskined223b522018-06-11 18:12:58 +02002518 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2519 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002520 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002521 ipad[i] ^= 0x36;
2522 memset( ipad + key_length, 0x36, block_size - key_length );
2523
2524 /* Copy the key material from ipad to opad, flipping the requisite bits,
2525 * and filling the rest of opad with the requisite constant. */
2526 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002527 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2528 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002529
Gilles Peskine01126fa2018-07-12 17:04:55 +02002530 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002531 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002532 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002533
Gilles Peskine01126fa2018-07-12 17:04:55 +02002534 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002535
2536cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002537 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002538
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002539 return( status );
2540}
Gilles Peskine248051a2018-06-20 16:09:38 +02002541#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002542
Gilles Peskine89167cb2018-07-08 20:12:23 +02002543static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002544 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002545 psa_algorithm_t alg,
2546 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002549 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002551 psa_key_usage_t usage =
2552 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002553 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002554 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002556 /* A context must be freshly initialized before it can be set up. */
2557 if( operation->alg != 0 )
2558 {
2559 return( PSA_ERROR_BAD_STATE );
2560 }
2561
Gilles Peskined911eb72018-08-14 15:18:45 +02002562 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563 if( status != PSA_SUCCESS )
2564 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002565 if( is_sign )
2566 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567
Gilles Peskine28f8f302019-07-24 13:30:31 +02002568 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002569 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002570 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002571 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002572
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002574 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002575 {
2576 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002577 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002578 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002579 int ret;
2580 if( cipher_info == NULL )
2581 {
2582 status = PSA_ERROR_NOT_SUPPORTED;
2583 goto exit;
2584 }
2585 operation->mac_size = cipher_info->block_size;
2586 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2587 status = mbedtls_to_psa_error( ret );
2588 }
2589 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002591#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002592 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002593 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002594 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002595 if( hash_alg == 0 )
2596 {
2597 status = PSA_ERROR_NOT_SUPPORTED;
2598 goto exit;
2599 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002600
2601 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2602 /* Sanity check. This shouldn't fail on a valid configuration. */
2603 if( operation->mac_size == 0 ||
2604 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2605 {
2606 status = PSA_ERROR_NOT_SUPPORTED;
2607 goto exit;
2608 }
2609
Gilles Peskine8e338702019-07-30 20:06:31 +02002610 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002611 {
2612 status = PSA_ERROR_INVALID_ARGUMENT;
2613 goto exit;
2614 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002615
Gilles Peskine01126fa2018-07-12 17:04:55 +02002616 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2617 slot->data.raw.data,
2618 slot->data.raw.bytes,
2619 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002620 }
2621 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002624 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002626 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002627
Gilles Peskined911eb72018-08-14 15:18:45 +02002628 if( truncated == 0 )
2629 {
2630 /* The "normal" case: untruncated algorithm. Nothing to do. */
2631 }
2632 else if( truncated < 4 )
2633 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002634 /* A very short MAC is too short for security since it can be
2635 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2636 * so we make this our minimum, even though 32 bits is still
2637 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002638 status = PSA_ERROR_NOT_SUPPORTED;
2639 }
2640 else if( truncated > operation->mac_size )
2641 {
2642 /* It's impossible to "truncate" to a larger length. */
2643 status = PSA_ERROR_INVALID_ARGUMENT;
2644 }
2645 else
2646 operation->mac_size = truncated;
2647
Gilles Peskinefbfac682018-07-08 20:51:54 +02002648exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002649 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002651 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002653 else
2654 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002655 operation->key_set = 1;
2656 }
2657 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658}
2659
Gilles Peskine89167cb2018-07-08 20:12:23 +02002660psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002661 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002662 psa_algorithm_t alg )
2663{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002664 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002665}
2666
2667psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002668 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002669 psa_algorithm_t alg )
2670{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002671 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002672}
2673
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2675 const uint8_t *input,
2676 size_t input_length )
2677{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002680 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002682 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683 operation->has_input = 1;
2684
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002686 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002687 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002688 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2689 input, input_length );
2690 status = mbedtls_to_psa_error( ret );
2691 }
2692 else
2693#endif /* MBEDTLS_CMAC_C */
2694#if defined(MBEDTLS_MD_C)
2695 if( PSA_ALG_IS_HMAC( operation->alg ) )
2696 {
2697 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2698 input_length );
2699 }
2700 else
2701#endif /* MBEDTLS_MD_C */
2702 {
2703 /* This shouldn't happen if `operation` was initialized by
2704 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002705 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002706 }
2707
Gilles Peskinefbfac682018-07-08 20:51:54 +02002708 if( status != PSA_SUCCESS )
2709 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002710 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711}
2712
Gilles Peskine01126fa2018-07-12 17:04:55 +02002713#if defined(MBEDTLS_MD_C)
2714static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2715 uint8_t *mac,
2716 size_t mac_size )
2717{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002718 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002719 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2720 size_t hash_size = 0;
2721 size_t block_size = psa_get_hash_block_size( hash_alg );
2722 psa_status_t status;
2723
Gilles Peskine01126fa2018-07-12 17:04:55 +02002724 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2725 if( status != PSA_SUCCESS )
2726 return( status );
2727 /* From here on, tmp needs to be wiped. */
2728
2729 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2730 if( status != PSA_SUCCESS )
2731 goto exit;
2732
2733 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2734 if( status != PSA_SUCCESS )
2735 goto exit;
2736
2737 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2738 if( status != PSA_SUCCESS )
2739 goto exit;
2740
Gilles Peskined911eb72018-08-14 15:18:45 +02002741 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2742 if( status != PSA_SUCCESS )
2743 goto exit;
2744
2745 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002746
2747exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002748 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002749 return( status );
2750}
2751#endif /* MBEDTLS_MD_C */
2752
mohammad16036df908f2018-04-02 08:34:15 -07002753static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002754 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002755 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002757 if( ! operation->key_set )
2758 return( PSA_ERROR_BAD_STATE );
2759 if( operation->iv_required && ! operation->iv_set )
2760 return( PSA_ERROR_BAD_STATE );
2761
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762 if( mac_size < operation->mac_size )
2763 return( PSA_ERROR_BUFFER_TOO_SMALL );
2764
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002766 if( operation->alg == PSA_ALG_CMAC )
2767 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002768 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2769 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2770 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002771 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002772 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002775 else
2776#endif /* MBEDTLS_CMAC_C */
2777#if defined(MBEDTLS_MD_C)
2778 if( PSA_ALG_IS_HMAC( operation->alg ) )
2779 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002780 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002781 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002782 }
2783 else
2784#endif /* MBEDTLS_MD_C */
2785 {
2786 /* This shouldn't happen if `operation` was initialized by
2787 * a setup function. */
2788 return( PSA_ERROR_BAD_STATE );
2789 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790}
2791
Gilles Peskineacd4be32018-07-08 19:56:25 +02002792psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2793 uint8_t *mac,
2794 size_t mac_size,
2795 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002796{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002797 psa_status_t status;
2798
Jaeden Amero252ef282019-02-15 14:05:35 +00002799 if( operation->alg == 0 )
2800 {
2801 return( PSA_ERROR_BAD_STATE );
2802 }
2803
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002804 /* Fill the output buffer with something that isn't a valid mac
2805 * (barring an attack on the mac and deliberately-crafted input),
2806 * in case the caller doesn't check the return status properly. */
2807 *mac_length = mac_size;
2808 /* If mac_size is 0 then mac may be NULL and then the
2809 * call to memset would have undefined behavior. */
2810 if( mac_size != 0 )
2811 memset( mac, '!', mac_size );
2812
Gilles Peskine89167cb2018-07-08 20:12:23 +02002813 if( ! operation->is_sign )
2814 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002815 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002816 }
mohammad16036df908f2018-04-02 08:34:15 -07002817
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002818 status = psa_mac_finish_internal( operation, mac, mac_size );
2819
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002820 if( status == PSA_SUCCESS )
2821 {
2822 status = psa_mac_abort( operation );
2823 if( status == PSA_SUCCESS )
2824 *mac_length = operation->mac_size;
2825 else
2826 memset( mac, '!', mac_size );
2827 }
2828 else
2829 psa_mac_abort( operation );
2830 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002831}
2832
Gilles Peskineacd4be32018-07-08 19:56:25 +02002833psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2834 const uint8_t *mac,
2835 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002836{
Gilles Peskine828ed142018-06-18 23:25:51 +02002837 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002838 psa_status_t status;
2839
Jaeden Amero252ef282019-02-15 14:05:35 +00002840 if( operation->alg == 0 )
2841 {
2842 return( PSA_ERROR_BAD_STATE );
2843 }
2844
Gilles Peskine89167cb2018-07-08 20:12:23 +02002845 if( operation->is_sign )
2846 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002847 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002848 }
2849 if( operation->mac_size != mac_length )
2850 {
2851 status = PSA_ERROR_INVALID_SIGNATURE;
2852 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002853 }
mohammad16036df908f2018-04-02 08:34:15 -07002854
2855 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002856 actual_mac, sizeof( actual_mac ) );
2857
2858 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2859 status = PSA_ERROR_INVALID_SIGNATURE;
2860
2861cleanup:
2862 if( status == PSA_SUCCESS )
2863 status = psa_mac_abort( operation );
2864 else
2865 psa_mac_abort( operation );
2866
Gilles Peskine3f108122018-12-07 18:14:53 +01002867 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002868
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002869 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870}
2871
2872
Gilles Peskine20035e32018-02-03 22:44:14 +01002873
Gilles Peskine20035e32018-02-03 22:44:14 +01002874/****************************************************************/
2875/* Asymmetric cryptography */
2876/****************************************************************/
2877
Gilles Peskine2b450e32018-06-27 15:42:46 +02002878#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002879/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002880 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002881static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2882 size_t hash_length,
2883 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002884{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002885 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002886 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002887 *md_alg = mbedtls_md_get_type( md_info );
2888
2889 /* The Mbed TLS RSA module uses an unsigned int for hash length
2890 * parameters. Validate that it fits so that we don't risk an
2891 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002892#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002893 if( hash_length > UINT_MAX )
2894 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002895#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002896
2897#if defined(MBEDTLS_PKCS1_V15)
2898 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2899 * must be correct. */
2900 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2901 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002902 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002903 if( md_info == NULL )
2904 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002905 if( mbedtls_md_get_size( md_info ) != hash_length )
2906 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002907 }
2908#endif /* MBEDTLS_PKCS1_V15 */
2909
2910#if defined(MBEDTLS_PKCS1_V21)
2911 /* PSS requires a hash internally. */
2912 if( PSA_ALG_IS_RSA_PSS( alg ) )
2913 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002914 if( md_info == NULL )
2915 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002916 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002917#endif /* MBEDTLS_PKCS1_V21 */
2918
Gilles Peskine61b91d42018-06-08 16:09:36 +02002919 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002920}
2921
Gilles Peskine2b450e32018-06-27 15:42:46 +02002922static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2923 psa_algorithm_t alg,
2924 const uint8_t *hash,
2925 size_t hash_length,
2926 uint8_t *signature,
2927 size_t signature_size,
2928 size_t *signature_length )
2929{
2930 psa_status_t status;
2931 int ret;
2932 mbedtls_md_type_t md_alg;
2933
2934 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2935 if( status != PSA_SUCCESS )
2936 return( status );
2937
Gilles Peskine630a18a2018-06-29 17:49:35 +02002938 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002939 return( PSA_ERROR_BUFFER_TOO_SMALL );
2940
2941#if defined(MBEDTLS_PKCS1_V15)
2942 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2943 {
2944 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2945 MBEDTLS_MD_NONE );
2946 ret = mbedtls_rsa_pkcs1_sign( rsa,
2947 mbedtls_ctr_drbg_random,
2948 &global_data.ctr_drbg,
2949 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002950 md_alg,
2951 (unsigned int) hash_length,
2952 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002953 signature );
2954 }
2955 else
2956#endif /* MBEDTLS_PKCS1_V15 */
2957#if defined(MBEDTLS_PKCS1_V21)
2958 if( PSA_ALG_IS_RSA_PSS( alg ) )
2959 {
2960 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2961 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2962 mbedtls_ctr_drbg_random,
2963 &global_data.ctr_drbg,
2964 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002965 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002966 (unsigned int) hash_length,
2967 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002968 signature );
2969 }
2970 else
2971#endif /* MBEDTLS_PKCS1_V21 */
2972 {
2973 return( PSA_ERROR_INVALID_ARGUMENT );
2974 }
2975
2976 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002977 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002978 return( mbedtls_to_psa_error( ret ) );
2979}
2980
2981static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2982 psa_algorithm_t alg,
2983 const uint8_t *hash,
2984 size_t hash_length,
2985 const uint8_t *signature,
2986 size_t signature_length )
2987{
2988 psa_status_t status;
2989 int ret;
2990 mbedtls_md_type_t md_alg;
2991
2992 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2993 if( status != PSA_SUCCESS )
2994 return( status );
2995
Gilles Peskine630a18a2018-06-29 17:49:35 +02002996 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002997 return( PSA_ERROR_BUFFER_TOO_SMALL );
2998
2999#if defined(MBEDTLS_PKCS1_V15)
3000 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3001 {
3002 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3003 MBEDTLS_MD_NONE );
3004 ret = mbedtls_rsa_pkcs1_verify( rsa,
3005 mbedtls_ctr_drbg_random,
3006 &global_data.ctr_drbg,
3007 MBEDTLS_RSA_PUBLIC,
3008 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003009 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003010 hash,
3011 signature );
3012 }
3013 else
3014#endif /* MBEDTLS_PKCS1_V15 */
3015#if defined(MBEDTLS_PKCS1_V21)
3016 if( PSA_ALG_IS_RSA_PSS( alg ) )
3017 {
3018 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3019 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3020 mbedtls_ctr_drbg_random,
3021 &global_data.ctr_drbg,
3022 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003023 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003024 (unsigned int) hash_length,
3025 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003026 signature );
3027 }
3028 else
3029#endif /* MBEDTLS_PKCS1_V21 */
3030 {
3031 return( PSA_ERROR_INVALID_ARGUMENT );
3032 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003033
3034 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3035 * the rest of the signature is invalid". This has little use in
3036 * practice and PSA doesn't report this distinction. */
3037 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3038 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003039 return( mbedtls_to_psa_error( ret ) );
3040}
3041#endif /* MBEDTLS_RSA_C */
3042
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003043#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003044/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3045 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3046 * (even though these functions don't modify it). */
3047static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3048 psa_algorithm_t alg,
3049 const uint8_t *hash,
3050 size_t hash_length,
3051 uint8_t *signature,
3052 size_t signature_size,
3053 size_t *signature_length )
3054{
3055 int ret;
3056 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003057 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003058 mbedtls_mpi_init( &r );
3059 mbedtls_mpi_init( &s );
3060
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003061 if( signature_size < 2 * curve_bytes )
3062 {
3063 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3064 goto cleanup;
3065 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003066
Gilles Peskinea05219c2018-11-16 16:02:56 +01003067#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003068 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3069 {
3070 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3071 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3072 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3073 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3074 hash, hash_length,
3075 md_alg ) );
3076 }
3077 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003078#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003079 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003080 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003081 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3082 hash, hash_length,
3083 mbedtls_ctr_drbg_random,
3084 &global_data.ctr_drbg ) );
3085 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003086
3087 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3088 signature,
3089 curve_bytes ) );
3090 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3091 signature + curve_bytes,
3092 curve_bytes ) );
3093
3094cleanup:
3095 mbedtls_mpi_free( &r );
3096 mbedtls_mpi_free( &s );
3097 if( ret == 0 )
3098 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003099 return( mbedtls_to_psa_error( ret ) );
3100}
3101
3102static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3103 const uint8_t *hash,
3104 size_t hash_length,
3105 const uint8_t *signature,
3106 size_t signature_length )
3107{
3108 int ret;
3109 mbedtls_mpi r, s;
3110 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3111 mbedtls_mpi_init( &r );
3112 mbedtls_mpi_init( &s );
3113
3114 if( signature_length != 2 * curve_bytes )
3115 return( PSA_ERROR_INVALID_SIGNATURE );
3116
3117 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3118 signature,
3119 curve_bytes ) );
3120 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3121 signature + curve_bytes,
3122 curve_bytes ) );
3123
3124 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3125 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003126
3127cleanup:
3128 mbedtls_mpi_free( &r );
3129 mbedtls_mpi_free( &s );
3130 return( mbedtls_to_psa_error( ret ) );
3131}
3132#endif /* MBEDTLS_ECDSA_C */
3133
Gilles Peskinec5487a82018-12-03 18:08:14 +01003134psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003135 psa_algorithm_t alg,
3136 const uint8_t *hash,
3137 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003138 uint8_t *signature,
3139 size_t signature_size,
3140 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003141{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003142 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003143 psa_status_t status;
3144
3145 *signature_length = signature_size;
3146
Gilles Peskine28f8f302019-07-24 13:30:31 +02003147 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003148 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003149 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003150 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003151 {
3152 status = PSA_ERROR_INVALID_ARGUMENT;
3153 goto exit;
3154 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003155
Gilles Peskine20035e32018-02-03 22:44:14 +01003156#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003157 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003158 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003159 status = psa_rsa_sign( slot->data.rsa,
3160 alg,
3161 hash, hash_length,
3162 signature, signature_size,
3163 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003164 }
3165 else
3166#endif /* defined(MBEDTLS_RSA_C) */
3167#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003168 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003169 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003170#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003171 if(
3172#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3173 PSA_ALG_IS_ECDSA( alg )
3174#else
3175 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3176#endif
3177 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003178 status = psa_ecdsa_sign( slot->data.ecp,
3179 alg,
3180 hash, hash_length,
3181 signature, signature_size,
3182 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003183 else
3184#endif /* defined(MBEDTLS_ECDSA_C) */
3185 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003186 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003187 }
itayzafrir5c753392018-05-08 11:18:38 +03003188 }
3189 else
3190#endif /* defined(MBEDTLS_ECP_C) */
3191 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003192 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003193 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003194
3195exit:
3196 /* Fill the unused part of the output buffer (the whole buffer on error,
3197 * the trailing part on success) with something that isn't a valid mac
3198 * (barring an attack on the mac and deliberately-crafted input),
3199 * in case the caller doesn't check the return status properly. */
3200 if( status == PSA_SUCCESS )
3201 memset( signature + *signature_length, '!',
3202 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003203 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003204 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003205 /* If signature_size is 0 then we have nothing to do. We must not call
3206 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003207 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003208}
3209
Gilles Peskinec5487a82018-12-03 18:08:14 +01003210psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003211 psa_algorithm_t alg,
3212 const uint8_t *hash,
3213 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003214 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003215 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003216{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003217 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003218 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003219
Gilles Peskine28f8f302019-07-24 13:30:31 +02003220 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003221 if( status != PSA_SUCCESS )
3222 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003223
Gilles Peskine61b91d42018-06-08 16:09:36 +02003224#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003225 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003227 return( psa_rsa_verify( slot->data.rsa,
3228 alg,
3229 hash, hash_length,
3230 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231 }
3232 else
3233#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003234#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003235 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003236 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003237#if defined(MBEDTLS_ECDSA_C)
3238 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003239 return( psa_ecdsa_verify( slot->data.ecp,
3240 hash, hash_length,
3241 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003242 else
3243#endif /* defined(MBEDTLS_ECDSA_C) */
3244 {
3245 return( PSA_ERROR_INVALID_ARGUMENT );
3246 }
itayzafrir5c753392018-05-08 11:18:38 +03003247 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003248 else
3249#endif /* defined(MBEDTLS_ECP_C) */
3250 {
3251 return( PSA_ERROR_NOT_SUPPORTED );
3252 }
3253}
3254
Gilles Peskine072ac562018-06-30 00:21:29 +02003255#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3256static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3257 mbedtls_rsa_context *rsa )
3258{
3259 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3260 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3261 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3262 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3263}
3264#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3265
Gilles Peskinec5487a82018-12-03 18:08:14 +01003266psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003267 psa_algorithm_t alg,
3268 const uint8_t *input,
3269 size_t input_length,
3270 const uint8_t *salt,
3271 size_t salt_length,
3272 uint8_t *output,
3273 size_t output_size,
3274 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003276 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003277 psa_status_t status;
3278
Darryl Green5cc689a2018-07-24 15:34:10 +01003279 (void) input;
3280 (void) input_length;
3281 (void) salt;
3282 (void) output;
3283 (void) output_size;
3284
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003285 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003287 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3288 return( PSA_ERROR_INVALID_ARGUMENT );
3289
Gilles Peskine28f8f302019-07-24 13:30:31 +02003290 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003291 if( status != PSA_SUCCESS )
3292 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003293 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3294 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296
3297#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003298 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003299 {
3300 mbedtls_rsa_context *rsa = slot->data.rsa;
3301 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003302 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003303 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003305 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003307 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3308 mbedtls_ctr_drbg_random,
3309 &global_data.ctr_drbg,
3310 MBEDTLS_RSA_PUBLIC,
3311 input_length,
3312 input,
3313 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314 }
3315 else
3316#endif /* MBEDTLS_PKCS1_V15 */
3317#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003318 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003320 psa_rsa_oaep_set_padding_mode( alg, rsa );
3321 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3322 mbedtls_ctr_drbg_random,
3323 &global_data.ctr_drbg,
3324 MBEDTLS_RSA_PUBLIC,
3325 salt, salt_length,
3326 input_length,
3327 input,
3328 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 }
3330 else
3331#endif /* MBEDTLS_PKCS1_V21 */
3332 {
3333 return( PSA_ERROR_INVALID_ARGUMENT );
3334 }
3335 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003336 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 return( mbedtls_to_psa_error( ret ) );
3338 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003339 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003340#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341 {
3342 return( PSA_ERROR_NOT_SUPPORTED );
3343 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003344}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345
Gilles Peskinec5487a82018-12-03 18:08:14 +01003346psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003347 psa_algorithm_t alg,
3348 const uint8_t *input,
3349 size_t input_length,
3350 const uint8_t *salt,
3351 size_t salt_length,
3352 uint8_t *output,
3353 size_t output_size,
3354 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003355{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003356 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003357 psa_status_t status;
3358
Darryl Green5cc689a2018-07-24 15:34:10 +01003359 (void) input;
3360 (void) input_length;
3361 (void) salt;
3362 (void) output;
3363 (void) output_size;
3364
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003365 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003366
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003367 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3368 return( PSA_ERROR_INVALID_ARGUMENT );
3369
Gilles Peskine28f8f302019-07-24 13:30:31 +02003370 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003371 if( status != PSA_SUCCESS )
3372 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003373 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003374 return( PSA_ERROR_INVALID_ARGUMENT );
3375
3376#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003377 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378 {
3379 mbedtls_rsa_context *rsa = slot->data.rsa;
3380 int ret;
3381
Gilles Peskine630a18a2018-06-29 17:49:35 +02003382 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003383 return( PSA_ERROR_INVALID_ARGUMENT );
3384
3385#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003386 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003387 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003388 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3389 mbedtls_ctr_drbg_random,
3390 &global_data.ctr_drbg,
3391 MBEDTLS_RSA_PRIVATE,
3392 output_length,
3393 input,
3394 output,
3395 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003396 }
3397 else
3398#endif /* MBEDTLS_PKCS1_V15 */
3399#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003400 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003401 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003402 psa_rsa_oaep_set_padding_mode( alg, rsa );
3403 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3404 mbedtls_ctr_drbg_random,
3405 &global_data.ctr_drbg,
3406 MBEDTLS_RSA_PRIVATE,
3407 salt, salt_length,
3408 output_length,
3409 input,
3410 output,
3411 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003412 }
3413 else
3414#endif /* MBEDTLS_PKCS1_V21 */
3415 {
3416 return( PSA_ERROR_INVALID_ARGUMENT );
3417 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003418
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003419 return( mbedtls_to_psa_error( ret ) );
3420 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003421 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003422#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003423 {
3424 return( PSA_ERROR_NOT_SUPPORTED );
3425 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003426}
Gilles Peskine20035e32018-02-03 22:44:14 +01003427
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003428
3429
mohammad1603503973b2018-03-12 15:59:30 +02003430/****************************************************************/
3431/* Symmetric cryptography */
3432/****************************************************************/
3433
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003434/* Initialize the cipher operation structure. Once this function has been
3435 * called, psa_cipher_abort can run and will do the right thing. */
3436static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3437 psa_algorithm_t alg )
3438{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003439 if( ! PSA_ALG_IS_CIPHER( alg ) )
3440 {
3441 memset( operation, 0, sizeof( *operation ) );
3442 return( PSA_ERROR_INVALID_ARGUMENT );
3443 }
3444
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003445 operation->alg = alg;
3446 operation->key_set = 0;
3447 operation->iv_set = 0;
3448 operation->iv_required = 1;
3449 operation->iv_size = 0;
3450 operation->block_size = 0;
3451 mbedtls_cipher_init( &operation->ctx.cipher );
3452 return( PSA_SUCCESS );
3453}
3454
Gilles Peskinee553c652018-06-04 16:22:46 +02003455static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003456 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003457 psa_algorithm_t alg,
3458 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003459{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003460 int ret = 0;
3461 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003462 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003463 size_t key_bits;
3464 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003465 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3466 PSA_KEY_USAGE_ENCRYPT :
3467 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003468
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003469 /* A context must be freshly initialized before it can be set up. */
3470 if( operation->alg != 0 )
3471 {
3472 return( PSA_ERROR_BAD_STATE );
3473 }
3474
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003475 status = psa_cipher_init( operation, alg );
3476 if( status != PSA_SUCCESS )
3477 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003478
Gilles Peskine28f8f302019-07-24 13:30:31 +02003479 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003480 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003481 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003482 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003483
Gilles Peskine8e338702019-07-30 20:06:31 +02003484 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003485 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003486 {
3487 status = PSA_ERROR_NOT_SUPPORTED;
3488 goto exit;
3489 }
mohammad1603503973b2018-03-12 15:59:30 +02003490
mohammad1603503973b2018-03-12 15:59:30 +02003491 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003492 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003493 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003494
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003495#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003496 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003497 {
3498 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003499 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003500 memcpy( keys, slot->data.raw.data, 16 );
3501 memcpy( keys + 16, slot->data.raw.data, 8 );
3502 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3503 keys,
3504 192, cipher_operation );
3505 }
3506 else
3507#endif
3508 {
3509 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3510 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003511 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003512 }
Moran Peker41deec42018-04-04 15:43:05 +03003513 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003514 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003515
mohammad16038481e742018-03-18 13:57:31 +02003516#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003517 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003518 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003519 case PSA_ALG_CBC_NO_PADDING:
3520 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3521 MBEDTLS_PADDING_NONE );
3522 break;
3523 case PSA_ALG_CBC_PKCS7:
3524 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3525 MBEDTLS_PADDING_PKCS7 );
3526 break;
3527 default:
3528 /* The algorithm doesn't involve padding. */
3529 ret = 0;
3530 break;
3531 }
3532 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003533 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003534#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3535
mohammad1603503973b2018-03-12 15:59:30 +02003536 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003537 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003538 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003539 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003540 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003541 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003542 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003543#if defined(MBEDTLS_CHACHA20_C)
3544 else
3545 if( alg == PSA_ALG_CHACHA20 )
3546 operation->iv_size = 12;
3547#endif
mohammad1603503973b2018-03-12 15:59:30 +02003548
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003549exit:
3550 if( status == 0 )
3551 status = mbedtls_to_psa_error( ret );
3552 if( status != 0 )
3553 psa_cipher_abort( operation );
3554 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003555}
3556
Gilles Peskinefe119512018-07-08 21:39:34 +02003557psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003558 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003559 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003560{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003561 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003562}
3563
Gilles Peskinefe119512018-07-08 21:39:34 +02003564psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003565 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003566 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003567{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003568 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003569}
3570
Gilles Peskinefe119512018-07-08 21:39:34 +02003571psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003572 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003573 size_t iv_size,
3574 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003575{
itayzafrir534bd7c2018-08-02 13:56:32 +03003576 psa_status_t status;
3577 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003578 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003579 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003580 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003581 }
Moran Peker41deec42018-04-04 15:43:05 +03003582 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003583 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003584 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003585 goto exit;
3586 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003587 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3588 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003589 if( ret != 0 )
3590 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003591 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003592 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003593 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003594
mohammad16038481e742018-03-18 13:57:31 +02003595 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003596 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003597
Moran Peker395db872018-05-31 14:07:14 +03003598exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003599 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003600 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003601 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003602}
3603
Gilles Peskinefe119512018-07-08 21:39:34 +02003604psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003605 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003606 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003607{
itayzafrir534bd7c2018-08-02 13:56:32 +03003608 psa_status_t status;
3609 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003610 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003611 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003612 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003613 }
Moran Pekera28258c2018-05-29 16:25:04 +03003614 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003615 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003616 status = PSA_ERROR_INVALID_ARGUMENT;
3617 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003618 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003619 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3620 status = mbedtls_to_psa_error( ret );
3621exit:
3622 if( status == PSA_SUCCESS )
3623 operation->iv_set = 1;
3624 else
mohammad1603503973b2018-03-12 15:59:30 +02003625 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003626 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003627}
3628
Gilles Peskinee553c652018-06-04 16:22:46 +02003629psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3630 const uint8_t *input,
3631 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003632 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003633 size_t output_size,
3634 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003635{
itayzafrir534bd7c2018-08-02 13:56:32 +03003636 psa_status_t status;
3637 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003638 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003639
3640 if( operation->alg == 0 )
3641 {
3642 return( PSA_ERROR_BAD_STATE );
3643 }
3644
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003645 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003646 {
3647 /* Take the unprocessed partial block left over from previous
3648 * update calls, if any, plus the input to this call. Remove
3649 * the last partial block, if any. You get the data that will be
3650 * output in this call. */
3651 expected_output_size =
3652 ( operation->ctx.cipher.unprocessed_len + input_length )
3653 / operation->block_size * operation->block_size;
3654 }
3655 else
3656 {
3657 expected_output_size = input_length;
3658 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003659
Gilles Peskine89d789c2018-06-04 17:17:16 +02003660 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003661 {
3662 status = PSA_ERROR_BUFFER_TOO_SMALL;
3663 goto exit;
3664 }
mohammad160382759612018-03-12 18:16:40 +02003665
mohammad1603503973b2018-03-12 15:59:30 +02003666 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003667 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003668 status = mbedtls_to_psa_error( ret );
3669exit:
3670 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003671 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003672 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003673}
3674
Gilles Peskinee553c652018-06-04 16:22:46 +02003675psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3676 uint8_t *output,
3677 size_t output_size,
3678 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003679{
David Saadab4ecc272019-02-14 13:48:10 +02003680 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003681 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003682 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003683
mohammad1603503973b2018-03-12 15:59:30 +02003684 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003685 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003686 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003687 }
3688 if( operation->iv_required && ! operation->iv_set )
3689 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003690 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003691 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003692
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003693 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003694 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3695 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003696 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003697 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003698 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003699 }
3700
Janos Follath315b51c2018-07-09 16:04:51 +01003701 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3702 temp_output_buffer,
3703 output_length );
3704 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003705 {
Janos Follath315b51c2018-07-09 16:04:51 +01003706 status = mbedtls_to_psa_error( cipher_ret );
3707 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003708 }
Janos Follath315b51c2018-07-09 16:04:51 +01003709
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003710 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003711 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003712 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003713 memcpy( output, temp_output_buffer, *output_length );
3714 else
3715 {
Janos Follath315b51c2018-07-09 16:04:51 +01003716 status = PSA_ERROR_BUFFER_TOO_SMALL;
3717 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003718 }
mohammad1603503973b2018-03-12 15:59:30 +02003719
Gilles Peskine3f108122018-12-07 18:14:53 +01003720 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003721 status = psa_cipher_abort( operation );
3722
3723 return( status );
3724
3725error:
3726
3727 *output_length = 0;
3728
Gilles Peskine3f108122018-12-07 18:14:53 +01003729 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003730 (void) psa_cipher_abort( operation );
3731
3732 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003733}
3734
Gilles Peskinee553c652018-06-04 16:22:46 +02003735psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3736{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003737 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003738 {
3739 /* The object has (apparently) been initialized but it is not
3740 * in use. It's ok to call abort on such an object, and there's
3741 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003742 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003743 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003744
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003745 /* Sanity check (shouldn't happen: operation->alg should
3746 * always have been initialized to a valid value). */
3747 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3748 return( PSA_ERROR_BAD_STATE );
3749
mohammad1603503973b2018-03-12 15:59:30 +02003750 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003751
Moran Peker41deec42018-04-04 15:43:05 +03003752 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003753 operation->key_set = 0;
3754 operation->iv_set = 0;
3755 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003756 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003757 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003758
Moran Peker395db872018-05-31 14:07:14 +03003759 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003760}
3761
Gilles Peskinea0655c32018-04-30 17:06:50 +02003762
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003763
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003764
mohammad16035955c982018-04-26 00:53:03 +03003765/****************************************************************/
3766/* AEAD */
3767/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003768
Gilles Peskineedf9a652018-08-17 18:11:56 +02003769typedef struct
3770{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003771 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003772 const mbedtls_cipher_info_t *cipher_info;
3773 union
3774 {
3775#if defined(MBEDTLS_CCM_C)
3776 mbedtls_ccm_context ccm;
3777#endif /* MBEDTLS_CCM_C */
3778#if defined(MBEDTLS_GCM_C)
3779 mbedtls_gcm_context gcm;
3780#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003781#if defined(MBEDTLS_CHACHAPOLY_C)
3782 mbedtls_chachapoly_context chachapoly;
3783#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003784 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003785 psa_algorithm_t core_alg;
3786 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003787 uint8_t tag_length;
3788} aead_operation_t;
3789
Gilles Peskine30a9e412019-01-14 18:36:12 +01003790static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003791{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003792 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003793 {
3794#if defined(MBEDTLS_CCM_C)
3795 case PSA_ALG_CCM:
3796 mbedtls_ccm_free( &operation->ctx.ccm );
3797 break;
3798#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003799#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003800 case PSA_ALG_GCM:
3801 mbedtls_gcm_free( &operation->ctx.gcm );
3802 break;
3803#endif /* MBEDTLS_GCM_C */
3804 }
3805}
3806
3807static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003808 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003809 psa_key_usage_t usage,
3810 psa_algorithm_t alg )
3811{
3812 psa_status_t status;
3813 size_t key_bits;
3814 mbedtls_cipher_id_t cipher_id;
3815
Gilles Peskine28f8f302019-07-24 13:30:31 +02003816 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003817 if( status != PSA_SUCCESS )
3818 return( status );
3819
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003820 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003821
3822 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003823 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003824 &cipher_id );
3825 if( operation->cipher_info == NULL )
3826 return( PSA_ERROR_NOT_SUPPORTED );
3827
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003828 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003829 {
3830#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003831 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3832 operation->core_alg = PSA_ALG_CCM;
3833 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003834 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3835 * The call to mbedtls_ccm_encrypt_and_tag or
3836 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003837 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003838 return( PSA_ERROR_INVALID_ARGUMENT );
3839 mbedtls_ccm_init( &operation->ctx.ccm );
3840 status = mbedtls_to_psa_error(
3841 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3842 operation->slot->data.raw.data,
3843 (unsigned int) key_bits ) );
3844 if( status != 0 )
3845 goto cleanup;
3846 break;
3847#endif /* MBEDTLS_CCM_C */
3848
3849#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003850 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3851 operation->core_alg = PSA_ALG_GCM;
3852 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003853 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3854 * The call to mbedtls_gcm_crypt_and_tag or
3855 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003856 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003857 return( PSA_ERROR_INVALID_ARGUMENT );
3858 mbedtls_gcm_init( &operation->ctx.gcm );
3859 status = mbedtls_to_psa_error(
3860 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3861 operation->slot->data.raw.data,
3862 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003863 if( status != 0 )
3864 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003865 break;
3866#endif /* MBEDTLS_GCM_C */
3867
Gilles Peskine26869f22019-05-06 15:25:00 +02003868#if defined(MBEDTLS_CHACHAPOLY_C)
3869 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3870 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3871 operation->full_tag_length = 16;
3872 /* We only support the default tag length. */
3873 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3874 return( PSA_ERROR_NOT_SUPPORTED );
3875 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3876 status = mbedtls_to_psa_error(
3877 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3878 operation->slot->data.raw.data ) );
3879 if( status != 0 )
3880 goto cleanup;
3881 break;
3882#endif /* MBEDTLS_CHACHAPOLY_C */
3883
Gilles Peskineedf9a652018-08-17 18:11:56 +02003884 default:
3885 return( PSA_ERROR_NOT_SUPPORTED );
3886 }
3887
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003888 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3889 {
3890 status = PSA_ERROR_INVALID_ARGUMENT;
3891 goto cleanup;
3892 }
3893 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003894
Gilles Peskineedf9a652018-08-17 18:11:56 +02003895 return( PSA_SUCCESS );
3896
3897cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003898 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003899 return( status );
3900}
3901
Gilles Peskinec5487a82018-12-03 18:08:14 +01003902psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003903 psa_algorithm_t alg,
3904 const uint8_t *nonce,
3905 size_t nonce_length,
3906 const uint8_t *additional_data,
3907 size_t additional_data_length,
3908 const uint8_t *plaintext,
3909 size_t plaintext_length,
3910 uint8_t *ciphertext,
3911 size_t ciphertext_size,
3912 size_t *ciphertext_length )
3913{
mohammad16035955c982018-04-26 00:53:03 +03003914 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003915 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003916 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003917
mohammad1603f08a5502018-06-03 15:05:47 +03003918 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003919
Gilles Peskinec5487a82018-12-03 18:08:14 +01003920 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003921 if( status != PSA_SUCCESS )
3922 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003923
Gilles Peskineedf9a652018-08-17 18:11:56 +02003924 /* For all currently supported modes, the tag is at the end of the
3925 * ciphertext. */
3926 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3927 {
3928 status = PSA_ERROR_BUFFER_TOO_SMALL;
3929 goto exit;
3930 }
3931 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003932
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003933#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003934 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003935 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003936 status = mbedtls_to_psa_error(
3937 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3938 MBEDTLS_GCM_ENCRYPT,
3939 plaintext_length,
3940 nonce, nonce_length,
3941 additional_data, additional_data_length,
3942 plaintext, ciphertext,
3943 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003944 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003945 else
3946#endif /* MBEDTLS_GCM_C */
3947#if defined(MBEDTLS_CCM_C)
3948 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003949 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003950 status = mbedtls_to_psa_error(
3951 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3952 plaintext_length,
3953 nonce, nonce_length,
3954 additional_data,
3955 additional_data_length,
3956 plaintext, ciphertext,
3957 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003958 }
mohammad16035c8845f2018-05-09 05:40:09 -07003959 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003960#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003961#if defined(MBEDTLS_CHACHAPOLY_C)
3962 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3963 {
3964 if( nonce_length != 12 || operation.tag_length != 16 )
3965 {
3966 status = PSA_ERROR_NOT_SUPPORTED;
3967 goto exit;
3968 }
3969 status = mbedtls_to_psa_error(
3970 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3971 plaintext_length,
3972 nonce,
3973 additional_data,
3974 additional_data_length,
3975 plaintext,
3976 ciphertext,
3977 tag ) );
3978 }
3979 else
3980#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003981 {
mohammad1603554faad2018-06-03 15:07:38 +03003982 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003983 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003984
Gilles Peskineedf9a652018-08-17 18:11:56 +02003985 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3986 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003987
Gilles Peskineedf9a652018-08-17 18:11:56 +02003988exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003989 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003990 if( status == PSA_SUCCESS )
3991 *ciphertext_length = plaintext_length + operation.tag_length;
3992 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003993}
3994
Gilles Peskineee652a32018-06-01 19:23:52 +02003995/* Locate the tag in a ciphertext buffer containing the encrypted data
3996 * followed by the tag. Return the length of the part preceding the tag in
3997 * *plaintext_length. This is the size of the plaintext in modes where
3998 * the encrypted data has the same size as the plaintext, such as
3999 * CCM and GCM. */
4000static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4001 const uint8_t *ciphertext,
4002 size_t ciphertext_length,
4003 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004004 const uint8_t **p_tag )
4005{
4006 size_t payload_length;
4007 if( tag_length > ciphertext_length )
4008 return( PSA_ERROR_INVALID_ARGUMENT );
4009 payload_length = ciphertext_length - tag_length;
4010 if( payload_length > plaintext_size )
4011 return( PSA_ERROR_BUFFER_TOO_SMALL );
4012 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004013 return( PSA_SUCCESS );
4014}
4015
Gilles Peskinec5487a82018-12-03 18:08:14 +01004016psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004017 psa_algorithm_t alg,
4018 const uint8_t *nonce,
4019 size_t nonce_length,
4020 const uint8_t *additional_data,
4021 size_t additional_data_length,
4022 const uint8_t *ciphertext,
4023 size_t ciphertext_length,
4024 uint8_t *plaintext,
4025 size_t plaintext_size,
4026 size_t *plaintext_length )
4027{
mohammad16035955c982018-04-26 00:53:03 +03004028 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004029 aead_operation_t operation;
4030 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004031
Gilles Peskineee652a32018-06-01 19:23:52 +02004032 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004033
Gilles Peskinec5487a82018-12-03 18:08:14 +01004034 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004035 if( status != PSA_SUCCESS )
4036 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004037
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004038 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4039 ciphertext, ciphertext_length,
4040 plaintext_size, &tag );
4041 if( status != PSA_SUCCESS )
4042 goto exit;
4043
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004044#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004045 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004046 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004047 status = mbedtls_to_psa_error(
4048 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4049 ciphertext_length - operation.tag_length,
4050 nonce, nonce_length,
4051 additional_data,
4052 additional_data_length,
4053 tag, operation.tag_length,
4054 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004055 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004056 else
4057#endif /* MBEDTLS_GCM_C */
4058#if defined(MBEDTLS_CCM_C)
4059 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004060 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004061 status = mbedtls_to_psa_error(
4062 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4063 ciphertext_length - operation.tag_length,
4064 nonce, nonce_length,
4065 additional_data,
4066 additional_data_length,
4067 ciphertext, plaintext,
4068 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004069 }
mohammad160339574652018-06-01 04:39:53 -07004070 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004071#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004072#if defined(MBEDTLS_CHACHAPOLY_C)
4073 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4074 {
4075 if( nonce_length != 12 || operation.tag_length != 16 )
4076 {
4077 status = PSA_ERROR_NOT_SUPPORTED;
4078 goto exit;
4079 }
4080 status = mbedtls_to_psa_error(
4081 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4082 ciphertext_length - operation.tag_length,
4083 nonce,
4084 additional_data,
4085 additional_data_length,
4086 tag,
4087 ciphertext,
4088 plaintext ) );
4089 }
4090 else
4091#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004092 {
mohammad1603554faad2018-06-03 15:07:38 +03004093 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004094 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004095
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 if( status != PSA_SUCCESS && plaintext_size != 0 )
4097 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004098
Gilles Peskineedf9a652018-08-17 18:11:56 +02004099exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004100 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004101 if( status == PSA_SUCCESS )
4102 *plaintext_length = ciphertext_length - operation.tag_length;
4103 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004104}
4105
Gilles Peskinea0655c32018-04-30 17:06:50 +02004106
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004107
Gilles Peskine20035e32018-02-03 22:44:14 +01004108/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004109/* Generators */
4110/****************************************************************/
4111
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004112#define HKDF_STATE_INIT 0 /* no input yet */
4113#define HKDF_STATE_STARTED 1 /* got salt */
4114#define HKDF_STATE_KEYED 2 /* got key */
4115#define HKDF_STATE_OUTPUT 3 /* output started */
4116
Gilles Peskinecbe66502019-05-16 16:59:18 +02004117static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004119{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004120 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4121 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004122 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004123 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004124}
4125
4126
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004127psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004128{
4129 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004131 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004132 {
4133 /* The object has (apparently) been initialized but it is not
4134 * in use. It's ok to call abort on such an object, and there's
4135 * nothing to do. */
4136 }
4137 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004138#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004139 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004140 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004141 mbedtls_free( operation->ctx.hkdf.info );
4142 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004143 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004144 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004145 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004146 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004147 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004148#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004149 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004150 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4152 operation->ctx.tls12_prf.key_len );
4153 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004154 }
Hanno Becker580fba12018-11-13 20:50:45 +00004155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004157 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004158 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4159 operation->ctx.tls12_prf.Ai_with_seed_len );
4160 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004161 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004162#else
4163 if( operation->ctx.tls12_prf.seed != NULL )
4164 {
4165 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4166 operation->ctx.tls12_prf.seed_length );
4167 mbedtls_free( operation->ctx.tls12_prf.seed );
4168 }
4169
4170 if( operation->ctx.tls12_prf.label != NULL )
4171 {
4172 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4173 operation->ctx.tls12_prf.label_length );
4174 mbedtls_free( operation->ctx.tls12_prf.label );
4175 }
4176
4177 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4178
4179 /* We leave the fields Ai and output_block to be erased safely by the
4180 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004181#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004182 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004183 else
4184#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004185 {
4186 status = PSA_ERROR_BAD_STATE;
4187 }
Janos Follath083036a2019-06-11 10:22:26 +01004188 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004189 return( status );
4190}
4191
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004193 size_t *capacity)
4194{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004196 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004198 return PSA_ERROR_BAD_STATE;
4199 }
4200
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004202 return( PSA_SUCCESS );
4203}
4204
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004206 size_t capacity )
4207{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004209 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004210 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004211 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004212 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004213 return( PSA_SUCCESS );
4214}
4215
Gilles Peskinebef7f142018-07-12 17:22:21 +02004216#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004217/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004218 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004219static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004220 psa_algorithm_t hash_alg,
4221 uint8_t *output,
4222 size_t output_length )
4223{
4224 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4225 psa_status_t status;
4226
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004227 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4228 return( PSA_ERROR_BAD_STATE );
4229 hkdf->state = HKDF_STATE_OUTPUT;
4230
Gilles Peskinebef7f142018-07-12 17:22:21 +02004231 while( output_length != 0 )
4232 {
4233 /* Copy what remains of the current block */
4234 uint8_t n = hash_length - hkdf->offset_in_block;
4235 if( n > output_length )
4236 n = (uint8_t) output_length;
4237 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4238 output += n;
4239 output_length -= n;
4240 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004241 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004242 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004243 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004244 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004245 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004246 * object was corrupted or if this function is called directly
4247 * inside the library. */
4248 if( hkdf->block_number == 0xff )
4249 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004250
4251 /* We need a new block */
4252 ++hkdf->block_number;
4253 hkdf->offset_in_block = 0;
4254 status = psa_hmac_setup_internal( &hkdf->hmac,
4255 hkdf->prk, hash_length,
4256 hash_alg );
4257 if( status != PSA_SUCCESS )
4258 return( status );
4259 if( hkdf->block_number != 1 )
4260 {
4261 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4262 hkdf->output_block,
4263 hash_length );
4264 if( status != PSA_SUCCESS )
4265 return( status );
4266 }
4267 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4268 hkdf->info,
4269 hkdf->info_length );
4270 if( status != PSA_SUCCESS )
4271 return( status );
4272 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4273 &hkdf->block_number, 1 );
4274 if( status != PSA_SUCCESS )
4275 return( status );
4276 status = psa_hmac_finish_internal( &hkdf->hmac,
4277 hkdf->output_block,
4278 sizeof( hkdf->output_block ) );
4279 if( status != PSA_SUCCESS )
4280 return( status );
4281 }
4282
4283 return( PSA_SUCCESS );
4284}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004285
Janos Follath999f6482019-06-11 12:04:10 +01004286#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004287static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4288 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004289 psa_algorithm_t alg )
4290{
4291 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4292 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4293 psa_hmac_internal_data hmac;
4294 psa_status_t status, cleanup_status;
4295
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004296 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004297 size_t Ai_len;
4298
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004299 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004300 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004301 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004302 * object was corrupted or if this function is called directly
4303 * inside the library. */
4304 if( tls12_prf->block_number == 0xff )
4305 return( PSA_ERROR_BAD_STATE );
4306
4307 /* We need a new block */
4308 ++tls12_prf->block_number;
4309 tls12_prf->offset_in_block = 0;
4310
4311 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4312 *
4313 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4314 *
4315 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4316 * HMAC_hash(secret, A(2) + seed) +
4317 * HMAC_hash(secret, A(3) + seed) + ...
4318 *
4319 * A(0) = seed
4320 * A(i) = HMAC_hash( secret, A(i-1) )
4321 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004322 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004323 * `HMAC_hash(secret, A(i) + seed)` from which the output
4324 * is currently extracted as `output_block`, while
4325 * `A(i) + seed` is stored in `Ai_with_seed`.
4326 *
4327 * Generating a new block means recalculating `Ai_with_seed`
4328 * from the A(i)-part of it, and afterwards recalculating
4329 * `output_block`.
4330 *
4331 * A(0) is computed at setup time.
4332 *
4333 */
4334
4335 psa_hmac_init_internal( &hmac );
4336
4337 /* We must distinguish the calculation of A(1) from those
4338 * of A(2) and higher, because A(0)=seed has a different
4339 * length than the other A(i). */
4340 if( tls12_prf->block_number == 1 )
4341 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004342 Ai = tls12_prf->Ai_with_seed + hash_length;
4343 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004344 }
4345 else
4346 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004347 Ai = tls12_prf->Ai_with_seed;
4348 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004349 }
4350
Hanno Becker3b339e22018-11-13 20:56:14 +00004351 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4352 status = psa_hmac_setup_internal( &hmac,
4353 tls12_prf->key,
4354 tls12_prf->key_len,
4355 hash_alg );
4356 if( status != PSA_SUCCESS )
4357 goto cleanup;
4358
4359 status = psa_hash_update( &hmac.hash_ctx,
4360 Ai, Ai_len );
4361 if( status != PSA_SUCCESS )
4362 goto cleanup;
4363
4364 status = psa_hmac_finish_internal( &hmac,
4365 tls12_prf->Ai_with_seed,
4366 hash_length );
4367 if( status != PSA_SUCCESS )
4368 goto cleanup;
4369
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4371 status = psa_hmac_setup_internal( &hmac,
4372 tls12_prf->key,
4373 tls12_prf->key_len,
4374 hash_alg );
4375 if( status != PSA_SUCCESS )
4376 goto cleanup;
4377
4378 status = psa_hash_update( &hmac.hash_ctx,
4379 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004380 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004381 if( status != PSA_SUCCESS )
4382 goto cleanup;
4383
4384 status = psa_hmac_finish_internal( &hmac,
4385 tls12_prf->output_block,
4386 hash_length );
4387 if( status != PSA_SUCCESS )
4388 goto cleanup;
4389
4390cleanup:
4391
4392 cleanup_status = psa_hmac_abort_internal( &hmac );
4393 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4394 status = cleanup_status;
4395
4396 return( status );
4397}
Janos Follath7742fee2019-06-17 12:58:10 +01004398#else
4399static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4400 psa_tls12_prf_key_derivation_t *tls12_prf,
4401 psa_algorithm_t alg )
4402{
4403 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4404 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004405 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4406 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004407
Janos Follath7742fee2019-06-17 12:58:10 +01004408 /* We can't be wanting more output after block 0xff, otherwise
4409 * the capacity check in psa_key_derivation_output_bytes() would have
4410 * prevented this call. It could happen only if the operation
4411 * object was corrupted or if this function is called directly
4412 * inside the library. */
4413 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004414 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004415
4416 /* We need a new block */
4417 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004418 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004419
4420 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4421 *
4422 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4423 *
4424 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4425 * HMAC_hash(secret, A(2) + seed) +
4426 * HMAC_hash(secret, A(3) + seed) + ...
4427 *
4428 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004429 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004430 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004431 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004432 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004433 * is currently extracted as `output_block` and where i is
4434 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004435 */
4436
Janos Follathea29bfb2019-06-19 12:21:20 +01004437 /* Save the hash context before using it, to preserve the hash state with
4438 * only the inner padding in it. We need this, because inner padding depends
4439 * on the key (secret in the RFC's terminology). */
4440 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4441 if( status != PSA_SUCCESS )
4442 goto cleanup;
4443
4444 /* Calculate A(i) where i = tls12_prf->block_number. */
4445 if( tls12_prf->block_number == 1 )
4446 {
4447 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4448 * the variable seed and in this instance means it in the context of the
4449 * P_hash function, where seed = label + seed.) */
4450 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4451 tls12_prf->label, tls12_prf->label_length );
4452 if( status != PSA_SUCCESS )
4453 goto cleanup;
4454 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4455 tls12_prf->seed, tls12_prf->seed_length );
4456 if( status != PSA_SUCCESS )
4457 goto cleanup;
4458 }
4459 else
4460 {
4461 /* A(i) = HMAC_hash(secret, A(i-1)) */
4462 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4463 tls12_prf->Ai, hash_length );
4464 if( status != PSA_SUCCESS )
4465 goto cleanup;
4466 }
4467
4468 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4469 tls12_prf->Ai, hash_length );
4470 if( status != PSA_SUCCESS )
4471 goto cleanup;
4472 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4473 if( status != PSA_SUCCESS )
4474 goto cleanup;
4475
4476 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4477 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4478 tls12_prf->Ai, hash_length );
4479 if( status != PSA_SUCCESS )
4480 goto cleanup;
4481 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4482 tls12_prf->label, tls12_prf->label_length );
4483 if( status != PSA_SUCCESS )
4484 goto cleanup;
4485 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4486 tls12_prf->seed, tls12_prf->seed_length );
4487 if( status != PSA_SUCCESS )
4488 goto cleanup;
4489 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4490 tls12_prf->output_block, hash_length );
4491 if( status != PSA_SUCCESS )
4492 goto cleanup;
4493 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4494 if( status != PSA_SUCCESS )
4495 goto cleanup;
4496
Janos Follath7742fee2019-06-17 12:58:10 +01004497
4498cleanup:
4499
Janos Follathea29bfb2019-06-19 12:21:20 +01004500 cleanup_status = psa_hash_abort( &backup );
4501 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4502 status = cleanup_status;
4503
4504 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004505}
Janos Follath999f6482019-06-11 12:04:10 +01004506#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004507
Janos Follath999f6482019-06-11 12:04:10 +01004508#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004510 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004511static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004512 psa_tls12_prf_key_derivation_t *tls12_prf,
4513 psa_algorithm_t alg,
4514 uint8_t *output,
4515 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004516{
4517 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4518 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4519 psa_status_t status;
4520
4521 while( output_length != 0 )
4522 {
4523 /* Copy what remains of the current block */
4524 uint8_t n = hash_length - tls12_prf->offset_in_block;
4525
4526 /* Check if we have fully processed the current block. */
4527 if( n == 0 )
4528 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004529 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004530 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004531 if( status != PSA_SUCCESS )
4532 return( status );
4533
4534 continue;
4535 }
4536
4537 if( n > output_length )
4538 n = (uint8_t) output_length;
4539 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4540 n );
4541 output += n;
4542 output_length -= n;
4543 tls12_prf->offset_in_block += n;
4544 }
4545
4546 return( PSA_SUCCESS );
4547}
Janos Follath844eb0e2019-06-19 12:10:49 +01004548#else
4549static psa_status_t psa_key_derivation_tls12_prf_read(
4550 psa_tls12_prf_key_derivation_t *tls12_prf,
4551 psa_algorithm_t alg,
4552 uint8_t *output,
4553 size_t output_length )
4554{
4555 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4556 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4557 psa_status_t status;
4558 uint8_t offset, length;
4559
4560 while( output_length != 0 )
4561 {
4562 /* Check if we have fully processed the current block. */
4563 if( tls12_prf->left_in_block == 0 )
4564 {
4565 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4566 alg );
4567 if( status != PSA_SUCCESS )
4568 return( status );
4569
4570 continue;
4571 }
4572
4573 if( tls12_prf->left_in_block > output_length )
4574 length = (uint8_t) output_length;
4575 else
4576 length = tls12_prf->left_in_block;
4577
4578 offset = hash_length - tls12_prf->left_in_block;
4579 memcpy( output, tls12_prf->output_block + offset, length );
4580 output += length;
4581 output_length -= length;
4582 tls12_prf->left_in_block -= length;
4583 }
4584
4585 return( PSA_SUCCESS );
4586}
Janos Follath999f6482019-06-11 12:04:10 +01004587#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004588#endif /* MBEDTLS_MD_C */
4589
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004590psa_status_t psa_key_derivation_output_bytes(
4591 psa_key_derivation_operation_t *operation,
4592 uint8_t *output,
4593 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004594{
4595 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004597
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004599 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004600 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004601 return PSA_ERROR_BAD_STATE;
4602 }
4603
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004605 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004607 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004609 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004610 goto exit;
4611 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004613 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004614 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004615 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004616 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4617 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004619 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004620 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004621 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004623
Gilles Peskinebef7f142018-07-12 17:22:21 +02004624#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004625 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004626 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004627 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004629 output, output_length );
4630 }
Janos Follathadbec812019-06-14 11:05:39 +01004631 else
Janos Follathadbec812019-06-14 11:05:39 +01004632 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004633 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004634 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004636 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004637 output_length );
4638 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004639 else
4640#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004641 {
4642 return( PSA_ERROR_BAD_STATE );
4643 }
4644
4645exit:
4646 if( status != PSA_SUCCESS )
4647 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004648 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 * This allows us to differentiate between exhausted operations and
4650 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4651 * operations. */
4652 psa_algorithm_t alg = operation->alg;
4653 psa_key_derivation_abort( operation );
4654 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004655 memset( output, '!', output_length );
4656 }
4657 return( status );
4658}
4659
Gilles Peskine08542d82018-07-19 17:05:42 +02004660#if defined(MBEDTLS_DES_C)
4661static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4662{
4663 if( data_size >= 8 )
4664 mbedtls_des_key_set_parity( data );
4665 if( data_size >= 16 )
4666 mbedtls_des_key_set_parity( data + 8 );
4667 if( data_size >= 24 )
4668 mbedtls_des_key_set_parity( data + 16 );
4669}
4670#endif /* MBEDTLS_DES_C */
4671
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004672static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004673 psa_key_slot_t *slot,
4674 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004676{
4677 uint8_t *data = NULL;
4678 size_t bytes = PSA_BITS_TO_BYTES( bits );
4679 psa_status_t status;
4680
Gilles Peskine8e338702019-07-30 20:06:31 +02004681 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004682 return( PSA_ERROR_INVALID_ARGUMENT );
4683 if( bits % 8 != 0 )
4684 return( PSA_ERROR_INVALID_ARGUMENT );
4685 data = mbedtls_calloc( 1, bytes );
4686 if( data == NULL )
4687 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4688
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004690 if( status != PSA_SUCCESS )
4691 goto exit;
4692#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004693 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004694 psa_des_set_key_parity( data, bytes );
4695#endif /* MBEDTLS_DES_C */
4696 status = psa_import_key_into_slot( slot, data, bytes );
4697
4698exit:
4699 mbedtls_free( data );
4700 return( status );
4701}
4702
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004703psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004704 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004705 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004706{
4707 psa_status_t status;
4708 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004709 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004710 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004711#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4712 if( driver != NULL )
4713 {
4714 /* Deriving a key in a secure element is not implemented yet. */
4715 status = PSA_ERROR_NOT_SUPPORTED;
4716 }
4717#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004718 if( status == PSA_SUCCESS )
4719 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004720 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004721 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004723 }
4724 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004725 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004726 if( status != PSA_SUCCESS )
4727 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004728 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004729 *handle = 0;
4730 }
4731 return( status );
4732}
4733
Gilles Peskineeab56e42018-07-12 17:12:33 +02004734
4735
4736/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004737/* Key derivation */
4738/****************************************************************/
4739
Gilles Peskinea05219c2018-11-16 16:02:56 +01004740#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004741#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004743 * of the HKDF algorithm.
4744 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004745 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004746 * to potentially free embedded data structures and wipe confidential data.
4747 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004748static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004749 const uint8_t *secret,
4750 size_t secret_length,
4751 psa_algorithm_t hash_alg,
4752 const uint8_t *salt,
4753 size_t salt_length,
4754 const uint8_t *label,
4755 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004756{
4757 psa_status_t status;
4758 status = psa_hmac_setup_internal( &hkdf->hmac,
4759 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004760 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004761 if( status != PSA_SUCCESS )
4762 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004763 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764 if( status != PSA_SUCCESS )
4765 return( status );
4766 status = psa_hmac_finish_internal( &hkdf->hmac,
4767 hkdf->prk,
4768 sizeof( hkdf->prk ) );
4769 if( status != PSA_SUCCESS )
4770 return( status );
4771 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4772 hkdf->block_number = 0;
4773 hkdf->info_length = label_length;
4774 if( label_length != 0 )
4775 {
4776 hkdf->info = mbedtls_calloc( 1, label_length );
4777 if( hkdf->info == NULL )
4778 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4779 memcpy( hkdf->info, label, label_length );
4780 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004781 hkdf->state = HKDF_STATE_KEYED;
4782 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004783 return( PSA_SUCCESS );
4784}
Janos Follath71a4c912019-06-11 09:14:47 +01004785#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004786#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004787
Gilles Peskinea05219c2018-11-16 16:02:56 +01004788#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004789#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004791 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004792 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004793 * to potentially free embedded data structures and wipe confidential data.
4794 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004795static psa_status_t psa_key_derivation_tls12_prf_setup(
4796 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004797 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004798 size_t key_len,
4799 psa_algorithm_t hash_alg,
4800 const uint8_t *salt,
4801 size_t salt_length,
4802 const uint8_t *label,
4803 size_t label_length )
4804{
4805 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004806 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4807 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004808
4809 tls12_prf->key = mbedtls_calloc( 1, key_len );
4810 if( tls12_prf->key == NULL )
4811 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4812 tls12_prf->key_len = key_len;
4813 memcpy( tls12_prf->key, key, key_len );
4814
Hanno Becker580fba12018-11-13 20:50:45 +00004815 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004816 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004817 if( overflow )
4818 return( PSA_ERROR_INVALID_ARGUMENT );
4819
4820 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4821 if( tls12_prf->Ai_with_seed == NULL )
4822 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4823 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4824
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004825 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4826 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004827 if( label_length != 0 )
4828 {
4829 memcpy( tls12_prf->Ai_with_seed + hash_length,
4830 label, label_length );
4831 }
4832
4833 if( salt_length != 0 )
4834 {
4835 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4836 salt, salt_length );
4837 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004838
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004839 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004840 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004841 tls12_prf->block_number = 0;
4842 tls12_prf->offset_in_block = hash_length;
4843
4844 return( PSA_SUCCESS );
4845}
Janos Follath71a4c912019-06-11 09:14:47 +01004846#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004847
Janos Follath71a4c912019-06-11 09:14:47 +01004848#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004850static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4851 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004852 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004853 size_t psk_len,
4854 psa_algorithm_t hash_alg,
4855 const uint8_t *salt,
4856 size_t salt_length,
4857 const uint8_t *label,
4858 size_t label_length )
4859{
4860 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004861 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004862
4863 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4864 return( PSA_ERROR_INVALID_ARGUMENT );
4865
4866 /* Quoting RFC 4279, Section 2:
4867 *
4868 * The premaster secret is formed as follows: if the PSK is N octets
4869 * long, concatenate a uint16 with the value N, N zero octets, a second
4870 * uint16 with the value N, and the PSK itself.
4871 */
4872
4873 pms[0] = ( psk_len >> 8 ) & 0xff;
4874 pms[1] = ( psk_len >> 0 ) & 0xff;
4875 memset( pms + 2, 0, psk_len );
4876 pms[2 + psk_len + 0] = pms[0];
4877 pms[2 + psk_len + 1] = pms[1];
4878 memcpy( pms + 4 + psk_len, psk, psk_len );
4879
Gilles Peskinecbe66502019-05-16 16:59:18 +02004880 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004881 pms, 4 + 2 * psk_len,
4882 hash_alg,
4883 salt, salt_length,
4884 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004885
Gilles Peskine3f108122018-12-07 18:14:53 +01004886 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004887 return( status );
4888}
Janos Follath71a4c912019-06-11 09:14:47 +01004889#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004890#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004891
Janos Follath71a4c912019-06-11 09:14:47 +01004892#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004893/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004894 * to potentially free embedded data structures and wipe confidential data.
4895 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004896static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004898 const uint8_t *secret, size_t secret_length,
4899 psa_algorithm_t alg,
4900 const uint8_t *salt, size_t salt_length,
4901 const uint8_t *label, size_t label_length,
4902 size_t capacity )
4903{
4904 psa_status_t status;
4905 size_t max_capacity;
4906
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004907 /* Set operation->alg even on failure so that abort knows what to do. */
4908 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004909
4910#if defined(MBEDTLS_MD_C)
4911 if( PSA_ALG_IS_HKDF( alg ) )
4912 {
4913 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4914 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4915 if( hash_size == 0 )
4916 return( PSA_ERROR_NOT_SUPPORTED );
4917 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004919 secret, secret_length,
4920 hash_alg,
4921 salt, salt_length,
4922 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004923 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004924 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4925 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4926 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004927 {
4928 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4929 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4930
4931 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4932 if( hash_alg != PSA_ALG_SHA_256 &&
4933 hash_alg != PSA_ALG_SHA_384 )
4934 {
4935 return( PSA_ERROR_NOT_SUPPORTED );
4936 }
4937
4938 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004939
4940 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4941 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004943 secret, secret_length,
4944 hash_alg, salt, salt_length,
4945 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004946 }
4947 else
4948 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004949 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004951 secret, secret_length,
4952 hash_alg, salt, salt_length,
4953 label, label_length );
4954 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004955 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004956 else
4957#endif
4958 {
4959 return( PSA_ERROR_NOT_SUPPORTED );
4960 }
4961
4962 if( status != PSA_SUCCESS )
4963 return( status );
4964
4965 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004967 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004969 else
4970 return( PSA_ERROR_INVALID_ARGUMENT );
4971
4972 return( PSA_SUCCESS );
4973}
Janos Follath71a4c912019-06-11 09:14:47 +01004974#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004975
Janos Follath71a4c912019-06-11 09:14:47 +01004976#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004978 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004979 psa_algorithm_t alg,
4980 const uint8_t *salt,
4981 size_t salt_length,
4982 const uint8_t *label,
4983 size_t label_length,
4984 size_t capacity )
4985{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004986 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004987 psa_status_t status;
4988
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004990 return( PSA_ERROR_BAD_STATE );
4991
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004992 /* Make sure that alg is a key derivation algorithm. This prevents
4993 * key selection algorithms, which psa_key_derivation_internal
4994 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004995 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4996 return( PSA_ERROR_INVALID_ARGUMENT );
4997
Gilles Peskine28f8f302019-07-24 13:30:31 +02004998 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004999 if( status != PSA_SUCCESS )
5000 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005001
Gilles Peskine8e338702019-07-30 20:06:31 +02005002 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005003 return( PSA_ERROR_INVALID_ARGUMENT );
5004
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005006 slot->data.raw.data,
5007 slot->data.raw.bytes,
5008 alg,
5009 salt, salt_length,
5010 label, label_length,
5011 capacity );
5012 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005014 return( status );
5015}
Janos Follath71a4c912019-06-11 09:14:47 +01005016#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005017
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005020 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021{
Janos Follath5fe19732019-06-20 15:09:30 +01005022 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5023 * initialisers for this union leave some bytes unspecified.) */
5024 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5025
Gilles Peskine969c5d62019-01-16 15:53:06 +01005026 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005028 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5029 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5030 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005031 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005032 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005033 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5034 if( hash_size == 0 )
5035 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5037 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005038 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005039 {
5040 return( PSA_ERROR_NOT_SUPPORTED );
5041 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005043 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005044 }
5045#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005047 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005048}
5049
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005051 psa_algorithm_t alg )
5052{
5053 psa_status_t status;
5054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005056 return( PSA_ERROR_BAD_STATE );
5057
Gilles Peskine6843c292019-01-18 16:44:49 +01005058 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5059 return( PSA_ERROR_INVALID_ARGUMENT );
5060 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005061 {
5062 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005064 }
5065 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5066 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005068 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005069 else
5070 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005071
5072 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005074 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005075}
5076
5077#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005078static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005079 psa_algorithm_t hash_alg,
5080 psa_key_derivation_step_t step,
5081 const uint8_t *data,
5082 size_t data_length )
5083{
5084 psa_status_t status;
5085 switch( step )
5086 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005087 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005088 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005089 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005090 status = psa_hmac_setup_internal( &hkdf->hmac,
5091 data, data_length,
5092 hash_alg );
5093 if( status != PSA_SUCCESS )
5094 return( status );
5095 hkdf->state = HKDF_STATE_STARTED;
5096 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005097 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005098 /* If no salt was provided, use an empty salt. */
5099 if( hkdf->state == HKDF_STATE_INIT )
5100 {
5101 status = psa_hmac_setup_internal( &hkdf->hmac,
5102 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005103 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005104 if( status != PSA_SUCCESS )
5105 return( status );
5106 hkdf->state = HKDF_STATE_STARTED;
5107 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005108 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005110 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5111 data, data_length );
5112 if( status != PSA_SUCCESS )
5113 return( status );
5114 status = psa_hmac_finish_internal( &hkdf->hmac,
5115 hkdf->prk,
5116 sizeof( hkdf->prk ) );
5117 if( status != PSA_SUCCESS )
5118 return( status );
5119 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5120 hkdf->block_number = 0;
5121 hkdf->state = HKDF_STATE_KEYED;
5122 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005123 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005124 if( hkdf->state == HKDF_STATE_OUTPUT )
5125 return( PSA_ERROR_BAD_STATE );
5126 if( hkdf->info_set )
5127 return( PSA_ERROR_BAD_STATE );
5128 hkdf->info_length = data_length;
5129 if( data_length != 0 )
5130 {
5131 hkdf->info = mbedtls_calloc( 1, data_length );
5132 if( hkdf->info == NULL )
5133 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5134 memcpy( hkdf->info, data, data_length );
5135 }
5136 hkdf->info_set = 1;
5137 return( PSA_SUCCESS );
5138 default:
5139 return( PSA_ERROR_INVALID_ARGUMENT );
5140 }
5141}
Janos Follathb03233e2019-06-11 15:30:30 +01005142
5143#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5144static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5145 psa_algorithm_t hash_alg,
5146 psa_key_derivation_step_t step,
5147 const uint8_t *data,
5148 size_t data_length )
5149{
5150 (void) prf;
5151 (void) hash_alg;
5152 (void) step;
5153 (void) data;
5154 (void) data_length;
5155
5156 return( PSA_ERROR_INVALID_ARGUMENT );
5157}
Janos Follath6660f0e2019-06-17 08:44:03 +01005158
5159static psa_status_t psa_tls12_prf_psk_to_ms_input(
5160 psa_tls12_prf_key_derivation_t *prf,
5161 psa_algorithm_t hash_alg,
5162 psa_key_derivation_step_t step,
5163 const uint8_t *data,
5164 size_t data_length )
5165{
5166 (void) prf;
5167 (void) hash_alg;
5168 (void) step;
5169 (void) data;
5170 (void) data_length;
5171
5172 return( PSA_ERROR_INVALID_ARGUMENT );
5173}
Janos Follathb03233e2019-06-11 15:30:30 +01005174#else
Janos Follathf08e2652019-06-13 09:05:41 +01005175static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5176 const uint8_t *data,
5177 size_t data_length )
5178{
5179 if( prf->state != TLS12_PRF_STATE_INIT )
5180 return( PSA_ERROR_BAD_STATE );
5181
Janos Follathd6dce9f2019-07-04 09:11:38 +01005182 if( data_length != 0 )
5183 {
5184 prf->seed = mbedtls_calloc( 1, data_length );
5185 if( prf->seed == NULL )
5186 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005187
Janos Follathd6dce9f2019-07-04 09:11:38 +01005188 memcpy( prf->seed, data, data_length );
5189 prf->seed_length = data_length;
5190 }
Janos Follathf08e2652019-06-13 09:05:41 +01005191
5192 prf->state = TLS12_PRF_STATE_SEED_SET;
5193
5194 return( PSA_SUCCESS );
5195}
5196
Janos Follath81550542019-06-13 14:26:34 +01005197static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5198 psa_algorithm_t hash_alg,
5199 const uint8_t *data,
5200 size_t data_length )
5201{
5202 psa_status_t status;
5203 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5204 return( PSA_ERROR_BAD_STATE );
5205
5206 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5207 if( status != PSA_SUCCESS )
5208 return( status );
5209
5210 prf->state = TLS12_PRF_STATE_KEY_SET;
5211
5212 return( PSA_SUCCESS );
5213}
5214
Janos Follath6660f0e2019-06-17 08:44:03 +01005215static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5216 psa_tls12_prf_key_derivation_t *prf,
5217 psa_algorithm_t hash_alg,
5218 const uint8_t *data,
5219 size_t data_length )
5220{
5221 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005222 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5223 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005224
5225 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5226 return( PSA_ERROR_INVALID_ARGUMENT );
5227
5228 /* Quoting RFC 4279, Section 2:
5229 *
5230 * The premaster secret is formed as follows: if the PSK is N octets
5231 * long, concatenate a uint16 with the value N, N zero octets, a second
5232 * uint16 with the value N, and the PSK itself.
5233 */
5234
Janos Follath40e13932019-06-26 13:22:29 +01005235 *cur++ = ( data_length >> 8 ) & 0xff;
5236 *cur++ = ( data_length >> 0 ) & 0xff;
5237 memset( cur, 0, data_length );
5238 cur += data_length;
5239 *cur++ = pms[0];
5240 *cur++ = pms[1];
5241 memcpy( cur, data, data_length );
5242 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005243
Janos Follath40e13932019-06-26 13:22:29 +01005244 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005245
5246 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5247 return( status );
5248}
5249
Janos Follath63028dd2019-06-13 09:15:47 +01005250static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5251 const uint8_t *data,
5252 size_t data_length )
5253{
5254 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5255 return( PSA_ERROR_BAD_STATE );
5256
Janos Follathd6dce9f2019-07-04 09:11:38 +01005257 if( data_length != 0 )
5258 {
5259 prf->label = mbedtls_calloc( 1, data_length );
5260 if( prf->label == NULL )
5261 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005262
Janos Follathd6dce9f2019-07-04 09:11:38 +01005263 memcpy( prf->label, data, data_length );
5264 prf->label_length = data_length;
5265 }
Janos Follath63028dd2019-06-13 09:15:47 +01005266
5267 prf->state = TLS12_PRF_STATE_LABEL_SET;
5268
5269 return( PSA_SUCCESS );
5270}
5271
Janos Follathb03233e2019-06-11 15:30:30 +01005272static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5273 psa_algorithm_t hash_alg,
5274 psa_key_derivation_step_t step,
5275 const uint8_t *data,
5276 size_t data_length )
5277{
Janos Follathb03233e2019-06-11 15:30:30 +01005278 switch( step )
5279 {
Janos Follathf08e2652019-06-13 09:05:41 +01005280 case PSA_KEY_DERIVATION_INPUT_SEED:
5281 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005282 case PSA_KEY_DERIVATION_INPUT_SECRET:
5283 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005284 case PSA_KEY_DERIVATION_INPUT_LABEL:
5285 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005286 default:
5287 return( PSA_ERROR_INVALID_ARGUMENT );
5288 }
5289}
Janos Follath6660f0e2019-06-17 08:44:03 +01005290
5291static psa_status_t psa_tls12_prf_psk_to_ms_input(
5292 psa_tls12_prf_key_derivation_t *prf,
5293 psa_algorithm_t hash_alg,
5294 psa_key_derivation_step_t step,
5295 const uint8_t *data,
5296 size_t data_length )
5297{
5298 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005299 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005300 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5301 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005302 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005303
5304 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5305}
Janos Follathb03233e2019-06-11 15:30:30 +01005306#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005307#endif /* MBEDTLS_MD_C */
5308
Janos Follathb80a94e2019-06-12 15:54:46 +01005309static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005311 psa_key_derivation_step_t step,
5312 const uint8_t *data,
5313 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005314{
5315 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005317
5318#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005319 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005320 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005322 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323 step, data, data_length );
5324 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005325 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005326#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005328 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005329 {
Janos Follathb03233e2019-06-11 15:30:30 +01005330 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5331 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5332 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005333 }
5334 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5335 {
5336 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5337 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5338 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005339 }
5340 else
5341#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005342 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005344 return( PSA_ERROR_BAD_STATE );
5345 }
5346
5347 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349 return( status );
5350}
5351
Janos Follath51f4a0f2019-06-14 11:35:55 +01005352psa_status_t psa_key_derivation_input_bytes(
5353 psa_key_derivation_operation_t *operation,
5354 psa_key_derivation_step_t step,
5355 const uint8_t *data,
5356 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005357{
Janos Follathc5621512019-06-14 11:27:57 +01005358 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5359 return( PSA_ERROR_INVALID_ARGUMENT );
5360
5361 return( psa_key_derivation_input_internal( operation, step,
5362 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005363}
5364
Janos Follath51f4a0f2019-06-14 11:35:55 +01005365psa_status_t psa_key_derivation_input_key(
5366 psa_key_derivation_operation_t *operation,
5367 psa_key_derivation_step_t step,
5368 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005369{
5370 psa_key_slot_t *slot;
5371 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005372 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005373 PSA_KEY_USAGE_DERIVE,
5374 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005375 if( status != PSA_SUCCESS )
5376 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005377 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005378 return( PSA_ERROR_INVALID_ARGUMENT );
5379 /* Don't allow a key to be used as an input that is usually public.
5380 * This is debatable. It's ok from a cryptographic perspective to
5381 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005382 * the material should be dedicated to a particular input step,
5383 * otherwise this may allow the key to be used in an unintended way
5384 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005385 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005386 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005387 return( psa_key_derivation_input_internal( operation,
5388 step,
5389 slot->data.raw.data,
5390 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005391}
5392
Gilles Peskineea0fb492018-07-12 17:17:20 +02005393
5394
5395/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005396/* Key agreement */
5397/****************************************************************/
5398
Gilles Peskinea05219c2018-11-16 16:02:56 +01005399#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005400static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5401 size_t peer_key_length,
5402 const mbedtls_ecp_keypair *our_key,
5403 uint8_t *shared_secret,
5404 size_t shared_secret_size,
5405 size_t *shared_secret_length )
5406{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005407 mbedtls_ecp_keypair *their_key = NULL;
5408 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005409 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005410 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005411
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005412 status = psa_import_ec_public_key(
5413 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5414 peer_key, peer_key_length,
5415 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005416 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005417 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005418
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005419 status = mbedtls_to_psa_error(
5420 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5421 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005422 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005423 status = mbedtls_to_psa_error(
5424 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5425 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005426 goto exit;
5427
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005428 status = mbedtls_to_psa_error(
5429 mbedtls_ecdh_calc_secret( &ecdh,
5430 shared_secret_length,
5431 shared_secret, shared_secret_size,
5432 mbedtls_ctr_drbg_random,
5433 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005434
5435exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005436 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005437 mbedtls_ecp_keypair_free( their_key );
5438 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005439 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005440}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005441#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005442
Gilles Peskine01d718c2018-09-18 12:01:02 +02005443#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5444
Gilles Peskine0216fe12019-04-11 21:23:21 +02005445static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5446 psa_key_slot_t *private_key,
5447 const uint8_t *peer_key,
5448 size_t peer_key_length,
5449 uint8_t *shared_secret,
5450 size_t shared_secret_size,
5451 size_t *shared_secret_length )
5452{
5453 switch( alg )
5454 {
5455#if defined(MBEDTLS_ECDH_C)
5456 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005457 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005458 return( PSA_ERROR_INVALID_ARGUMENT );
5459 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5460 private_key->data.ecp,
5461 shared_secret, shared_secret_size,
5462 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005463#endif /* MBEDTLS_ECDH_C */
5464 default:
5465 (void) private_key;
5466 (void) peer_key;
5467 (void) peer_key_length;
5468 (void) shared_secret;
5469 (void) shared_secret_size;
5470 (void) shared_secret_length;
5471 return( PSA_ERROR_NOT_SUPPORTED );
5472 }
5473}
5474
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005475/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005476 * to potentially free embedded data structures and wipe confidential data.
5477 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005479 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005480 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005481 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005482 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005483{
5484 psa_status_t status;
5485 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5486 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005488
5489 /* Step 1: run the secret agreement algorithm to generate the shared
5490 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005491 status = psa_key_agreement_raw_internal( ka_alg,
5492 private_key,
5493 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005494 shared_secret,
5495 sizeof( shared_secret ),
5496 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005497 if( status != PSA_SUCCESS )
5498 goto exit;
5499
5500 /* Step 2: set up the key derivation to generate key material from
5501 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005502 status = psa_key_derivation_input_internal( operation, step,
5503 shared_secret,
5504 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005505
Gilles Peskine01d718c2018-09-18 12:01:02 +02005506exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005507 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005508 return( status );
5509}
5510
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005511psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005512 psa_key_derivation_step_t step,
5513 psa_key_handle_t private_key,
5514 const uint8_t *peer_key,
5515 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005516{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005517 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005518 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005520 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005521 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005522 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005523 if( status != PSA_SUCCESS )
5524 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005526 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005527 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005528 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005530 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005531}
5532
Gilles Peskinebe697d82019-05-16 18:00:41 +02005533psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5534 psa_key_handle_t private_key,
5535 const uint8_t *peer_key,
5536 size_t peer_key_length,
5537 uint8_t *output,
5538 size_t output_size,
5539 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005540{
5541 psa_key_slot_t *slot;
5542 psa_status_t status;
5543
5544 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5545 {
5546 status = PSA_ERROR_INVALID_ARGUMENT;
5547 goto exit;
5548 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005549 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005550 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005551 if( status != PSA_SUCCESS )
5552 goto exit;
5553
5554 status = psa_key_agreement_raw_internal( alg, slot,
5555 peer_key, peer_key_length,
5556 output, output_size,
5557 output_length );
5558
5559exit:
5560 if( status != PSA_SUCCESS )
5561 {
5562 /* If an error happens and is not handled properly, the output
5563 * may be used as a key to protect sensitive data. Arrange for such
5564 * a key to be random, which is likely to result in decryption or
5565 * verification errors. This is better than filling the buffer with
5566 * some constant data such as zeros, which would result in the data
5567 * being protected with a reproducible, easily knowable key.
5568 */
5569 psa_generate_random( output, output_size );
5570 *output_length = output_size;
5571 }
5572 return( status );
5573}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005574
5575
5576/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005577/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005578/****************************************************************/
5579
5580psa_status_t psa_generate_random( uint8_t *output,
5581 size_t output_size )
5582{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005583 int ret;
5584 GUARD_MODULE_INITIALIZED;
5585
5586 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005587 return( mbedtls_to_psa_error( ret ) );
5588}
5589
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005590#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5591#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005592
Gilles Peskine7228da22019-07-15 11:06:15 +02005593psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005594 size_t seed_size )
5595{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005596 if( global_data.initialized )
5597 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005598
5599 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5600 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5601 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5602 return( PSA_ERROR_INVALID_ARGUMENT );
5603
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005604 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005605}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005606#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005607
Gilles Peskinee56e8782019-04-26 17:34:02 +02005608#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5609static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5610 size_t domain_parameters_size,
5611 int *exponent )
5612{
5613 size_t i;
5614 uint32_t acc = 0;
5615
5616 if( domain_parameters_size == 0 )
5617 {
5618 *exponent = 65537;
5619 return( PSA_SUCCESS );
5620 }
5621
5622 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5623 * support values that fit in a 32-bit integer, which is larger than
5624 * int on just about every platform anyway. */
5625 if( domain_parameters_size > sizeof( acc ) )
5626 return( PSA_ERROR_NOT_SUPPORTED );
5627 for( i = 0; i < domain_parameters_size; i++ )
5628 acc = ( acc << 8 ) | domain_parameters[i];
5629 if( acc > INT_MAX )
5630 return( PSA_ERROR_NOT_SUPPORTED );
5631 *exponent = acc;
5632 return( PSA_SUCCESS );
5633}
5634#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5635
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005636static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005637 psa_key_slot_t *slot, size_t bits,
5638 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005639{
Gilles Peskine8e338702019-07-30 20:06:31 +02005640 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005641
Gilles Peskinee56e8782019-04-26 17:34:02 +02005642 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005643 return( PSA_ERROR_INVALID_ARGUMENT );
5644
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005645 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005646 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005647 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005648 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005649 if( status != PSA_SUCCESS )
5650 return( status );
5651 status = psa_generate_random( slot->data.raw.data,
5652 slot->data.raw.bytes );
5653 if( status != PSA_SUCCESS )
5654 {
5655 mbedtls_free( slot->data.raw.data );
5656 return( status );
5657 }
5658#if defined(MBEDTLS_DES_C)
5659 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005660 psa_des_set_key_parity( slot->data.raw.data,
5661 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005662#endif /* MBEDTLS_DES_C */
5663 }
5664 else
5665
5666#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005667 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005668 {
5669 mbedtls_rsa_context *rsa;
5670 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005671 int exponent;
5672 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005673 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5674 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005675 /* Accept only byte-aligned keys, for the same reasons as
5676 * in psa_import_rsa_key(). */
5677 if( bits % 8 != 0 )
5678 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005679 status = psa_read_rsa_exponent( domain_parameters,
5680 domain_parameters_size,
5681 &exponent );
5682 if( status != PSA_SUCCESS )
5683 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005684 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5685 if( rsa == NULL )
5686 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5687 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5688 ret = mbedtls_rsa_gen_key( rsa,
5689 mbedtls_ctr_drbg_random,
5690 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005691 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005692 exponent );
5693 if( ret != 0 )
5694 {
5695 mbedtls_rsa_free( rsa );
5696 mbedtls_free( rsa );
5697 return( mbedtls_to_psa_error( ret ) );
5698 }
5699 slot->data.rsa = rsa;
5700 }
5701 else
5702#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5703
5704#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005705 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005706 {
5707 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5708 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5709 const mbedtls_ecp_curve_info *curve_info =
5710 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5711 mbedtls_ecp_keypair *ecp;
5712 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005713 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005714 return( PSA_ERROR_NOT_SUPPORTED );
5715 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5716 return( PSA_ERROR_NOT_SUPPORTED );
5717 if( curve_info->bit_size != bits )
5718 return( PSA_ERROR_INVALID_ARGUMENT );
5719 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5720 if( ecp == NULL )
5721 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5722 mbedtls_ecp_keypair_init( ecp );
5723 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5724 mbedtls_ctr_drbg_random,
5725 &global_data.ctr_drbg );
5726 if( ret != 0 )
5727 {
5728 mbedtls_ecp_keypair_free( ecp );
5729 mbedtls_free( ecp );
5730 return( mbedtls_to_psa_error( ret ) );
5731 }
5732 slot->data.ecp = ecp;
5733 }
5734 else
5735#endif /* MBEDTLS_ECP_C */
5736
5737 return( PSA_ERROR_NOT_SUPPORTED );
5738
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005739 return( PSA_SUCCESS );
5740}
5741
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005742psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005743 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005744{
5745 psa_status_t status;
5746 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005747 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005748 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5750 if( driver != NULL )
5751 {
5752 /* Generating a key in a secure element is not implemented yet. */
5753 status = PSA_ERROR_NOT_SUPPORTED;
5754 }
5755#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005756 if( status == PSA_SUCCESS )
5757 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005758 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005759 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005760 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005761 }
5762 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005763 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005764 if( status != PSA_SUCCESS )
5765 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005766 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005767 *handle = 0;
5768 }
5769 return( status );
5770}
5771
5772
Gilles Peskine05d69892018-06-19 22:00:52 +02005773
5774/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005775/* Module setup */
5776/****************************************************************/
5777
Gilles Peskine5e769522018-11-20 21:59:56 +01005778psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5779 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5780 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5781{
5782 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5783 return( PSA_ERROR_BAD_STATE );
5784 global_data.entropy_init = entropy_init;
5785 global_data.entropy_free = entropy_free;
5786 return( PSA_SUCCESS );
5787}
5788
Gilles Peskinee59236f2018-01-27 23:32:46 +01005789void mbedtls_psa_crypto_free( void )
5790{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005791 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005792 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5793 {
5794 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005795 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005796 }
5797 /* Wipe all remaining data, including configuration.
5798 * In particular, this sets all state indicator to the value
5799 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005800 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005801#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005802 /* Unregister all secure element drivers, so that we restart from
5803 * a pristine state. */
5804 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005805#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005806}
5807
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005808#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5809/** Recover a transaction that was interrupted by a power failure.
5810 *
5811 * This function is called during initialization, before psa_crypto_init()
5812 * returns. If this function returns a failure status, the initialization
5813 * fails.
5814 */
5815static psa_status_t psa_crypto_recover_transaction(
5816 const psa_crypto_transaction_t *transaction )
5817{
5818 switch( transaction->unknown.type )
5819 {
5820 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5821 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5822 /* TOnogrepDO - fall through to the failure case until this
5823 * is implemented */
5824 default:
5825 /* We found an unsupported transaction in the storage.
5826 * We don't know what state the storage is in. Give up. */
5827 return( PSA_ERROR_STORAGE_FAILURE );
5828 }
5829}
5830#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5831
Gilles Peskinee59236f2018-01-27 23:32:46 +01005832psa_status_t psa_crypto_init( void )
5833{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005834 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005835 const unsigned char drbg_seed[] = "PSA";
5836
Gilles Peskinec6b69072018-11-20 21:42:52 +01005837 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005838 if( global_data.initialized != 0 )
5839 return( PSA_SUCCESS );
5840
Gilles Peskine5e769522018-11-20 21:59:56 +01005841 /* Set default configuration if
5842 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5843 if( global_data.entropy_init == NULL )
5844 global_data.entropy_init = mbedtls_entropy_init;
5845 if( global_data.entropy_free == NULL )
5846 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847
Gilles Peskinec6b69072018-11-20 21:42:52 +01005848 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005849 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005851 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005852 status = mbedtls_to_psa_error(
5853 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5854 mbedtls_entropy_func,
5855 &global_data.entropy,
5856 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5857 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005859 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005860
Gilles Peskine66fb1262018-12-10 16:29:04 +01005861 status = psa_initialize_key_slots( );
5862 if( status != PSA_SUCCESS )
5863 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005864
Gilles Peskine4b734222019-07-24 15:56:31 +02005865#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005866 status = psa_crypto_load_transaction( );
5867 if( status == PSA_SUCCESS )
5868 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005869 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5870 if( status != PSA_SUCCESS )
5871 goto exit;
5872 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005873 }
5874 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5875 {
5876 /* There's no transaction to complete. It's all good. */
5877 status = PSA_SUCCESS;
5878 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005879#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005880
Gilles Peskinec6b69072018-11-20 21:42:52 +01005881 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005882 global_data.initialized = 1;
5883
Gilles Peskinee59236f2018-01-27 23:32:46 +01005884exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005885 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005886 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005887 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005888}
5889
5890#endif /* MBEDTLS_PSA_CRYPTO_C */