blob: cb805468135aefbded75ef7a1a4ad7fc3ac34069 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
597static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
717static size_t psa_calculate_key_bits( const psa_key_slot_t *slot )
718{
719 if( key_type_is_raw_bytes( slot->attr.type ) )
720 return( slot->data.raw.bytes * 8 );
721#if defined(MBEDTLS_RSA_C)
722 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
723 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
724#endif /* defined(MBEDTLS_RSA_C) */
725#if defined(MBEDTLS_ECP_C)
726 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
727 return( slot->data.ecp->grp.pbits );
728#endif /* defined(MBEDTLS_ECP_C) */
729 /* Shouldn't happen except on an empty slot. */
730 return( 0 );
731}
732
Gilles Peskine8e338702019-07-30 20:06:31 +0200733/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100734 * previously. This function assumes that the slot does not contain
735 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100736psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
737 const uint8_t *data,
738 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200740 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741
Gilles Peskine8e338702019-07-30 20:06:31 +0200742 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
745 /* Ensure that the bytes-to-bit conversion doesn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 if( data_length > SIZE_MAX / 8 )
747 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200748 /* Ensure that the bit size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 if( bit_size > PSA_MAX_KEY_BITS )
750 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200751 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200752 &slot->data.raw );
753 if( status != PSA_SUCCESS )
754 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200755 if( data_length != 0 )
756 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100757 }
758 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200762 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 data, data_length,
764 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100765 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200766 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000767 {
768 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200769 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000770 data, data_length,
771 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000772 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100773 else
774#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000775#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000779 data, data_length,
780 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 }
782 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000783#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784 {
785 return( PSA_ERROR_NOT_SUPPORTED );
786 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200787
788 if( status == PSA_SUCCESS )
789 {
790 /* Write the actual key size to the slot.
791 * psa_start_key_creation() wrote the size declared by the
792 * caller, which may be 0 (meaning unspecified) or wrong. */
793 slot->attr.bits = psa_calculate_key_bits( slot );
794 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000795 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100796}
797
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100798/** Calculate the intersection of two algorithm usage policies.
799 *
800 * Return 0 (which allows no operation) on incompatibility.
801 */
802static psa_algorithm_t psa_key_policy_algorithm_intersection(
803 psa_algorithm_t alg1,
804 psa_algorithm_t alg2 )
805{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200806 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100807 if( alg1 == alg2 )
808 return( alg1 );
809 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100810 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
812 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
813 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
814 {
815 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
816 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100817 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100818 return( alg1 );
819 }
820 /* If the policies are incompatible, allow nothing. */
821 return( 0 );
822}
823
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200824static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
825 psa_algorithm_t requested_alg )
826{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200827 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200828 if( requested_alg == policy_alg )
829 return( 1 );
830 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200831 * and requested_alg is the same hash-and-sign family with any hash,
832 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
834 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
835 {
836 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
837 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
838 }
839 /* If it isn't permitted, it's forbidden. */
840 return( 0 );
841}
842
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100843/** Test whether a policy permits an algorithm.
844 *
845 * The caller must test usage flags separately.
846 */
847static int psa_key_policy_permits( const psa_key_policy_t *policy,
848 psa_algorithm_t alg )
849{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200850 return( psa_key_algorithm_permits( policy->alg, alg ) ||
851 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852}
853
Gilles Peskinef603c712019-01-19 13:40:11 +0100854/** Restrict a key policy based on a constraint.
855 *
856 * \param[in,out] policy The policy to restrict.
857 * \param[in] constraint The policy constraint to apply.
858 *
859 * \retval #PSA_SUCCESS
860 * \c *policy contains the intersection of the original value of
861 * \c *policy and \c *constraint.
862 * \retval #PSA_ERROR_INVALID_ARGUMENT
863 * \c *policy and \c *constraint are incompatible.
864 * \c *policy is unchanged.
865 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100866static psa_status_t psa_restrict_key_policy(
867 psa_key_policy_t *policy,
868 const psa_key_policy_t *constraint )
869{
870 psa_algorithm_t intersection_alg =
871 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200872 psa_algorithm_t intersection_alg2 =
873 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100874 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200876 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
877 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100878 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200880 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100881 return( PSA_SUCCESS );
882}
883
Darryl Green06fd18d2018-07-16 11:21:11 +0100884/** Retrieve a slot which must contain a key. The key must have allow all the
885 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
886 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100887static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100888 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100889 psa_key_usage_t usage,
890 psa_algorithm_t alg )
891{
892 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100894
895 *p_slot = NULL;
896
Gilles Peskinec5487a82018-12-03 18:08:14 +0100897 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898 if( status != PSA_SUCCESS )
899 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +0200900 if( slot->attr.type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200901 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100902
903 /* Enforce that usage policy for the key slot contains all the flags
904 * required by the usage parameter. There is one exception: public
905 * keys can always be exported, so we treat public key objects as
906 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200907 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100908 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100911
912 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200913 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100914 return( PSA_ERROR_NOT_PERMITTED );
915
916 *p_slot = slot;
917 return( PSA_SUCCESS );
918}
Darryl Green940d72c2018-07-13 13:18:51 +0100919
Gilles Peskine28f8f302019-07-24 13:30:31 +0200920/** Retrieve a slot which must contain a transparent key.
921 *
922 * A transparent key is a key for which the key material is directly
923 * available, as opposed to a key in a secure element.
924 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200925 * This is a temporary function to use instead of psa_get_key_from_slot()
926 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927 */
928#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
929static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
930 psa_key_slot_t **p_slot,
931 psa_key_usage_t usage,
932 psa_algorithm_t alg )
933{
934 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
935 if( status != PSA_SUCCESS )
936 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200937 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200938 {
939 *p_slot = NULL;
940 return( PSA_ERROR_NOT_SUPPORTED );
941 }
942 return( PSA_SUCCESS );
943}
944#else /* MBEDTLS_PSA_CRYPTO_SE_C */
945/* With no secure element support, all keys are transparent. */
946#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
947 psa_get_key_from_slot( handle, p_slot, usage, alg )
948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
949
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100950/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000952{
Gilles Peskine73167e12019-07-12 23:44:37 +0200953#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
954 if( psa_key_slot_is_external( slot ) )
955 {
956 /* No key material to clean. */
957 }
958 else
959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 /* No key material to clean. */
963 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200964 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000965 {
966 mbedtls_free( slot->data.raw.data );
967 }
968 else
969#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200970 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000971 {
972 mbedtls_rsa_free( slot->data.rsa );
973 mbedtls_free( slot->data.rsa );
974 }
975 else
976#endif /* defined(MBEDTLS_RSA_C) */
977#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200978 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000979 {
980 mbedtls_ecp_keypair_free( slot->data.ecp );
981 mbedtls_free( slot->data.ecp );
982 }
983 else
984#endif /* defined(MBEDTLS_ECP_C) */
985 {
986 /* Shouldn't happen: the key type is not any type that we
987 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200988 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000989 }
990
991 return( PSA_SUCCESS );
992}
993
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100994static void psa_abort_operations_using_key( psa_key_slot_t *slot )
995{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200996 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100997 (void) slot;
998}
999
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000/** Completely wipe a slot in memory, including its policy.
1001 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001002psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003{
1004 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001005 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006 /* At this point, key material and other type-specific content has
1007 * been wiped. Clear remaining metadata. We can call memset and not
1008 * zeroize because the metadata is not particularly sensitive. */
1009 memset( slot, 0, sizeof( *slot ) );
1010 return( status );
1011}
1012
Gilles Peskinec5487a82018-12-03 18:08:14 +01001013psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001015 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001016 psa_status_t status = PSA_SUCCESS;
1017 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001018#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1019 psa_se_drv_table_entry_t *driver;
1020#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021
Gilles Peskinec5487a82018-12-03 18:08:14 +01001022 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001023 if( status != PSA_SUCCESS )
1024 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001025
1026#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001027 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001030 /* For a key in a secure element, we need to do three things:
1031 * remove the key file in internal storage, destroy the
1032 * key inside the secure element, and update the driver's
1033 * persistent data. Start a transaction that will encompass these
1034 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001035 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001036 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001038 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001039 status = psa_crypto_save_transaction( );
1040 if( status != PSA_SUCCESS )
1041 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001042 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 /* TOnogrepDO: destroy what can be destroyed anyway */
1044 return( status );
1045 }
1046
Gilles Peskine354f7672019-07-12 23:46:38 +02001047 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001048 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001049#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1050
Darryl Greend49a4992018-06-18 17:27:26 +01001051#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001052 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001053 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001054 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001055 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001056 }
1057#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001058
Gilles Peskinefc762652019-07-22 19:30:34 +02001059#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1060 if( driver != NULL )
1061 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001062 psa_status_t status2;
1063 status = psa_save_se_persistent_data( driver );
1064 status2 = psa_crypto_stop_transaction( );
1065 if( status == PSA_SUCCESS )
1066 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001067 if( status != PSA_SUCCESS )
1068 {
1069 /* TOnogrepDO: destroy what can be destroyed anyway */
1070 return( status );
1071 }
1072 }
1073#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1074
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001076 if( status != PSA_SUCCESS )
1077 return( status );
1078 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079}
1080
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001081void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1082{
Gilles Peskineb699f072019-04-26 16:06:02 +02001083 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001084 memset( attributes, 0, sizeof( *attributes ) );
1085}
1086
Gilles Peskineb699f072019-04-26 16:06:02 +02001087psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1088 psa_key_type_t type,
1089 const uint8_t *data,
1090 size_t data_length )
1091{
1092 uint8_t *copy = NULL;
1093
1094 if( data_length != 0 )
1095 {
1096 copy = mbedtls_calloc( 1, data_length );
1097 if( copy == NULL )
1098 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1099 memcpy( copy, data, data_length );
1100 }
1101 /* After this point, this function is guaranteed to succeed, so it
1102 * can start modifying `*attributes`. */
1103
1104 if( attributes->domain_parameters != NULL )
1105 {
1106 mbedtls_free( attributes->domain_parameters );
1107 attributes->domain_parameters = NULL;
1108 attributes->domain_parameters_size = 0;
1109 }
1110
1111 attributes->domain_parameters = copy;
1112 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001113 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001114 return( PSA_SUCCESS );
1115}
1116
1117psa_status_t psa_get_key_domain_parameters(
1118 const psa_key_attributes_t *attributes,
1119 uint8_t *data, size_t data_size, size_t *data_length )
1120{
1121 if( attributes->domain_parameters_size > data_size )
1122 return( PSA_ERROR_BUFFER_TOO_SMALL );
1123 *data_length = attributes->domain_parameters_size;
1124 if( attributes->domain_parameters_size != 0 )
1125 memcpy( data, attributes->domain_parameters,
1126 attributes->domain_parameters_size );
1127 return( PSA_SUCCESS );
1128}
1129
1130#if defined(MBEDTLS_RSA_C)
1131static psa_status_t psa_get_rsa_public_exponent(
1132 const mbedtls_rsa_context *rsa,
1133 psa_key_attributes_t *attributes )
1134{
1135 mbedtls_mpi mpi;
1136 int ret;
1137 uint8_t *buffer = NULL;
1138 size_t buflen;
1139 mbedtls_mpi_init( &mpi );
1140
1141 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1142 if( ret != 0 )
1143 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001144 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1145 {
1146 /* It's the default value, which is reported as an empty string,
1147 * so there's nothing to do. */
1148 goto exit;
1149 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001150
1151 buflen = mbedtls_mpi_size( &mpi );
1152 buffer = mbedtls_calloc( 1, buflen );
1153 if( buffer == NULL )
1154 {
1155 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1156 goto exit;
1157 }
1158 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1159 if( ret != 0 )
1160 goto exit;
1161 attributes->domain_parameters = buffer;
1162 attributes->domain_parameters_size = buflen;
1163
1164exit:
1165 mbedtls_mpi_free( &mpi );
1166 if( ret != 0 )
1167 mbedtls_free( buffer );
1168 return( mbedtls_to_psa_error( ret ) );
1169}
1170#endif /* MBEDTLS_RSA_C */
1171
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001172/** Retrieve all the publicly-accessible attributes of a key.
1173 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1175 psa_key_attributes_t *attributes )
1176{
1177 psa_key_slot_t *slot;
1178 psa_status_t status;
1179
1180 psa_reset_key_attributes( attributes );
1181
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001182 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 if( status != PSA_SUCCESS )
1184 return( status );
1185
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001186 attributes->core = slot->attr;
Gilles Peskineb699f072019-04-26 16:06:02 +02001187
Gilles Peskine8e338702019-07-30 20:06:31 +02001188 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001189 {
1190#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001191 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1194 /* TOnogrepDO: reporting the public exponent for opaque keys
1195 * is not yet implemented. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001196 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001197 break;
1198#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001199 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1200 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001201#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001202 default:
1203 /* Nothing else to do. */
1204 break;
1205 }
1206
1207 if( status != PSA_SUCCESS )
1208 psa_reset_key_attributes( attributes );
1209 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001210}
1211
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001212#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001213static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1214 unsigned char *buf, size_t size )
1215{
1216 int ret;
1217 unsigned char *c;
1218 size_t len = 0;
1219
1220 c = buf + size;
1221
1222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1223
1224 return( (int) len );
1225}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001226#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001227
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001228static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1229 uint8_t *data,
1230 size_t data_size,
1231 size_t *data_length,
1232 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233{
Gilles Peskine5d309672019-07-12 23:47:28 +02001234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1235 const psa_drv_se_t *drv;
1236 psa_drv_se_context_t *drv_context;
1237#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1238
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001239 *data_length = 0;
1240
Gilles Peskine8e338702019-07-30 20:06:31 +02001241 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001242 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001243
Gilles Peskine5d309672019-07-12 23:47:28 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001245 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001246 {
1247 psa_drv_se_export_key_t method;
1248 if( drv->key_management == NULL )
1249 return( PSA_ERROR_NOT_SUPPORTED );
1250 method = ( export_public_key ?
1251 drv->key_management->p_export_public :
1252 drv->key_management->p_export );
1253 if( method == NULL )
1254 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001255 return( method( drv_context,
1256 slot->data.se.slot_number,
1257 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001258 }
1259#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1260
Gilles Peskine8e338702019-07-30 20:06:31 +02001261 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001262 {
1263 if( slot->data.raw.bytes > data_size )
1264 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001265 if( data_size != 0 )
1266 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001267 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001268 memset( data + slot->data.raw.bytes, 0,
1269 data_size - slot->data.raw.bytes );
1270 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001271 *data_length = slot->data.raw.bytes;
1272 return( PSA_SUCCESS );
1273 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001274#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001275 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001276 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001277 psa_status_t status;
1278
Gilles Peskineb46bef22019-07-30 21:32:04 +02001279 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001280 if( bytes > data_size )
1281 return( PSA_ERROR_BUFFER_TOO_SMALL );
1282 status = mbedtls_to_psa_error(
1283 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1284 if( status != PSA_SUCCESS )
1285 return( status );
1286 memset( data + bytes, 0, data_size - bytes );
1287 *data_length = bytes;
1288 return( PSA_SUCCESS );
1289 }
1290#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291 else
Moran Peker17e36e12018-05-02 12:55:20 +03001292 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001293#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001294 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1295 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001296 {
Moran Pekera998bc62018-04-16 18:16:20 +03001297 mbedtls_pk_context pk;
1298 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001299 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001300 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001301#if defined(MBEDTLS_RSA_C)
1302 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001303 pk.pk_info = &mbedtls_rsa_info;
1304 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001305#else
1306 return( PSA_ERROR_NOT_SUPPORTED );
1307#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001308 }
1309 else
1310 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001311#if defined(MBEDTLS_ECP_C)
1312 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001313 pk.pk_info = &mbedtls_eckey_info;
1314 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001315#else
1316 return( PSA_ERROR_NOT_SUPPORTED );
1317#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001318 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001319 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001320 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001321 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001322 }
Moran Peker17e36e12018-05-02 12:55:20 +03001323 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001324 {
Moran Peker17e36e12018-05-02 12:55:20 +03001325 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001326 }
Moran Peker60364322018-04-29 11:34:58 +03001327 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001328 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001329 /* If data_size is 0 then data may be NULL and then the
1330 * call to memset would have undefined behavior. */
1331 if( data_size != 0 )
1332 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001333 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001334 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001335 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1336 * Move the data to the beginning and erase remaining data
1337 * at the original location. */
1338 if( 2 * (size_t) ret <= data_size )
1339 {
1340 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001341 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001342 }
1343 else if( (size_t) ret < data_size )
1344 {
1345 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001346 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001347 }
Moran Pekera998bc62018-04-16 18:16:20 +03001348 *data_length = ret;
1349 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001350 }
1351 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001352#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001353 {
1354 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001355 it is valid for a special-purpose implementation to omit
1356 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001357 return( PSA_ERROR_NOT_SUPPORTED );
1358 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359 }
1360}
1361
Gilles Peskinec5487a82018-12-03 18:08:14 +01001362psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001363 uint8_t *data,
1364 size_t data_size,
1365 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001366{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001367 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001368 psa_status_t status;
1369
1370 /* Set the key to empty now, so that even when there are errors, we always
1371 * set data_length to a value between 0 and data_size. On error, setting
1372 * the key to empty is a good choice because an empty key representation is
1373 * unlikely to be accepted anywhere. */
1374 *data_length = 0;
1375
1376 /* Export requires the EXPORT flag. There is an exception for public keys,
1377 * which don't require any flag, but psa_get_key_from_slot takes
1378 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001379 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001380 if( status != PSA_SUCCESS )
1381 return( status );
1382 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001383 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001385
Gilles Peskinec5487a82018-12-03 18:08:14 +01001386psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001390{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001391 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001392 psa_status_t status;
1393
1394 /* Set the key to empty now, so that even when there are errors, we always
1395 * set data_length to a value between 0 and data_size. On error, setting
1396 * the key to empty is a good choice because an empty key representation is
1397 * unlikely to be accepted anywhere. */
1398 *data_length = 0;
1399
1400 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001401 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001402 if( status != PSA_SUCCESS )
1403 return( status );
1404 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001405 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001406}
1407
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001408static psa_status_t psa_check_key_slot_policy(
1409 const psa_key_slot_t *slot )
Gilles Peskine4747d192019-04-17 15:05:45 +02001410{
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001411 const psa_key_policy_t *policy = &slot->attr.policy;
Gilles Peskine4747d192019-04-17 15:05:45 +02001412 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001413 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001414 PSA_KEY_USAGE_ENCRYPT |
1415 PSA_KEY_USAGE_DECRYPT |
1416 PSA_KEY_USAGE_SIGN |
1417 PSA_KEY_USAGE_VERIFY |
1418 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1419 return( PSA_ERROR_INVALID_ARGUMENT );
1420
Gilles Peskine4747d192019-04-17 15:05:45 +02001421 return( PSA_SUCCESS );
1422}
1423
1424/** Prepare a key slot to receive key material.
1425 *
1426 * This function allocates a key slot and sets its metadata.
1427 *
1428 * If this function fails, call psa_fail_key_creation().
1429 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001430 * This function is intended to be used as follows:
1431 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1432 * it with the specified attributes, and assign it a handle.
1433 * -# Populate the slot with the key material.
1434 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1435 * In case of failure at any step, stop the sequence and call
1436 * psa_fail_key_creation().
1437 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001438 * \param[in] attributes Key attributes for the new key.
1439 * \param[out] handle On success, a handle for the allocated slot.
1440 * \param[out] p_slot On success, a pointer to the prepared slot.
1441 * \param[out] p_drv On any return, the driver for the key, if any.
1442 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001443 *
1444 * \retval #PSA_SUCCESS
1445 * The key slot is ready to receive key material.
1446 * \return If this function fails, the key slot is an invalid state.
1447 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001448 */
1449static psa_status_t psa_start_key_creation(
1450 const psa_key_attributes_t *attributes,
1451 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001452 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001453 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001454{
1455 psa_status_t status;
1456 psa_key_slot_t *slot;
1457
Gilles Peskine011e4282019-06-26 18:34:38 +02001458 *p_drv = NULL;
1459
Gilles Peskine267c6562019-05-27 19:01:54 +02001460 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001461 if( status != PSA_SUCCESS )
1462 return( status );
1463 slot = *p_slot;
1464
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 Peskined167b942019-04-19 18:19:40 +02001472 }
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001473
1474 status = psa_check_key_slot_policy( slot );
1475 if( status != PSA_SUCCESS )
1476 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001477
Gilles Peskinec744d992019-07-30 17:26:54 +02001478 /* Refuse to create overly large keys.
1479 * Note that this doesn't trigger on import if the attributes don't
1480 * explicitly specify a size (so psa_get_key_bits returns 0), so
1481 * psa_import_key() needs its own checks. */
1482 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1483 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001484 /* We're storing the declared bit-size of the key. It's up to each
1485 * creation mechanism to verify that this information is correct.
1486 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001487 * an input (generate, device) but not for those where the bit-size
1488 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001489
1490 slot->attr = attributes->core;
1491 /* This is awkward... Copying the attributes has overwritten the
1492 * flag that marks this slot as used. Restore it. */
1493 psa_key_slot_set_bits_in_flags( slot, PSA_KEY_SLOT_FLAG_ALLOCATED );
Gilles Peskinec744d992019-07-30 17:26:54 +02001494
Gilles Peskinecbaff462019-07-12 23:46:04 +02001495#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001496 /* For a key in a secure element, we need to do three things:
1497 * create the key file in internal storage, create the
1498 * key inside the secure element, and update the driver's
1499 * persistent data. Start a transaction that will encompass these
1500 * three actions. */
1501 /* The first thing to do is to find a slot number for the new key.
1502 * We save the slot number in persistent storage as part of the
1503 * transaction data. It will be needed to recover if the power
1504 * fails during the key creation process, to clean up on the secure
1505 * element side after restarting. Obtaining a slot number from the
1506 * secure element driver updates its persistent state, but we do not yet
1507 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001508 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001509 if( *p_drv != NULL )
1510 {
1511 status = psa_find_se_slot_for_key( attributes, *p_drv,
1512 &slot->data.se.slot_number );
1513 if( status != PSA_SUCCESS )
1514 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001515 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001516 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001517 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001518 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001519 status = psa_crypto_save_transaction( );
1520 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001521 {
1522 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001523 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001524 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001525 }
1526#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1527
Gilles Peskine4747d192019-04-17 15:05:45 +02001528 return( status );
1529}
1530
1531/** Finalize the creation of a key once its key material has been set.
1532 *
1533 * This entails writing the key to persistent storage.
1534 *
1535 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001536 * See the documentation of psa_start_key_creation() for the intended use
1537 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001538 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001539 * \param[in,out] slot Pointer to the slot with key material.
1540 * \param[in] driver The secure element driver for the key,
1541 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * The key was successfully created. The handle is now valid.
1545 * \return If this function fails, the key slot is an invalid state.
1546 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001547 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001548static psa_status_t psa_finish_key_creation(
1549 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001550 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001551{
1552 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001553 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001555
1556#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001557 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001559#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1560 if( driver != NULL )
1561 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001562 psa_se_key_data_storage_t data;
1563#if defined(static_assert)
1564 static_assert( sizeof( slot->data.se.slot_number ) ==
1565 sizeof( data.slot_number ),
1566 "Slot number size does not match psa_se_key_data_storage_t" );
1567 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1568 "Bit-size size does not match psa_se_key_data_storage_t" );
1569#endif
1570 memcpy( &data.slot_number, &slot->data.se.slot_number,
1571 sizeof( slot->data.se.slot_number ) );
1572 memcpy( &data.bits, &slot->attr.bits,
1573 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001574 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001575 (uint8_t*) &data,
1576 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001577 }
1578 else
1579#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1580 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001581 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001582 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001583 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001584 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1585 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001586 if( buffer == NULL && buffer_size != 0 )
1587 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1588 status = psa_internal_export_key( slot,
1589 buffer, buffer_size, &length,
1590 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001591 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001592 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001593 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001594
Gilles Peskine1df83d42019-07-23 16:13:14 +02001595 if( buffer_size != 0 )
1596 mbedtls_platform_zeroize( buffer, buffer_size );
1597 mbedtls_free( buffer );
1598 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001599 }
1600#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1601
Gilles Peskinecbaff462019-07-12 23:46:04 +02001602#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1603 if( driver != NULL )
1604 {
1605 status = psa_save_se_persistent_data( driver );
1606 if( status != PSA_SUCCESS )
1607 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001608 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001609 return( status );
1610 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001611 status = psa_crypto_stop_transaction( );
1612 if( status != PSA_SUCCESS )
1613 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001614 }
1615#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1616
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 return( status );
1618}
1619
1620/** Abort the creation of a key.
1621 *
1622 * You may call this function after calling psa_start_key_creation(),
1623 * or after psa_finish_key_creation() fails. In other circumstances, this
1624 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001625 * See the documentation of psa_start_key_creation() for the intended use
1626 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001627 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001628 * \param[in,out] slot Pointer to the slot with key material.
1629 * \param[in] driver The secure element driver for the key,
1630 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001632static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001633 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001634{
Gilles Peskine011e4282019-06-26 18:34:38 +02001635 (void) driver;
1636
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 if( slot == NULL )
1638 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001639
1640#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1641 /* TOnogrepDO: If the key has already been created in the secure
1642 * element, and the failure happened later (when saving metadata
1643 * to internal storage), we need to destroy the key in the secure
1644 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001645
1646 /* Abort the ongoing transaction if any. We already did what it
1647 * takes to undo any partial creation. All that's left is to update
1648 * the transaction data itself. */
1649 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001650#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1651
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 psa_wipe_key_slot( slot );
1653}
1654
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001655static psa_status_t psa_check_key_slot_attributes(
1656 const psa_key_slot_t *slot,
1657 const psa_key_attributes_t *attributes )
1658{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001659 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001660 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001661 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001662 return( PSA_ERROR_INVALID_ARGUMENT );
1663 }
1664
1665 if( attributes->domain_parameters_size != 0 )
1666 {
1667#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001668 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001669 {
1670 mbedtls_mpi actual, required;
1671 int ret;
1672 mbedtls_mpi_init( &actual );
1673 mbedtls_mpi_init( &required );
1674 ret = mbedtls_rsa_export( slot->data.rsa,
1675 NULL, NULL, NULL, NULL, &actual );
1676 if( ret != 0 )
1677 goto rsa_exit;
1678 ret = mbedtls_mpi_read_binary( &required,
1679 attributes->domain_parameters,
1680 attributes->domain_parameters_size );
1681 if( ret != 0 )
1682 goto rsa_exit;
1683 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1684 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1685 rsa_exit:
1686 mbedtls_mpi_free( &actual );
1687 mbedtls_mpi_free( &required );
1688 if( ret != 0)
1689 return( mbedtls_to_psa_error( ret ) );
1690 }
1691 else
1692#endif
1693 {
1694 return( PSA_ERROR_INVALID_ARGUMENT );
1695 }
1696 }
1697
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001698 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001699 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001700 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001701 return( PSA_ERROR_INVALID_ARGUMENT );
1702 }
1703
1704 return( PSA_SUCCESS );
1705}
1706
Gilles Peskine4747d192019-04-17 15:05:45 +02001707psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001709 size_t data_length,
1710 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001711{
1712 psa_status_t status;
1713 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001714 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001715
Gilles Peskine011e4282019-06-26 18:34:38 +02001716 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001717 if( status != PSA_SUCCESS )
1718 goto exit;
1719
Gilles Peskine5d309672019-07-12 23:47:28 +02001720#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1721 if( driver != NULL )
1722 {
1723 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001724 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001725 if( drv->key_management == NULL ||
1726 drv->key_management->p_import == NULL )
1727 {
1728 status = PSA_ERROR_NOT_SUPPORTED;
1729 goto exit;
1730 }
1731 status = drv->key_management->p_import(
1732 psa_get_se_driver_context( driver ),
1733 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001734 slot->attr.lifetime, slot->attr.type,
1735 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001736 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001737 &bits );
1738 if( status != PSA_SUCCESS )
1739 goto exit;
1740 if( bits > PSA_MAX_KEY_BITS )
1741 {
1742 status = PSA_ERROR_NOT_SUPPORTED;
1743 goto exit;
1744 }
1745 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001746 }
1747 else
1748#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1749 {
1750 status = psa_import_key_into_slot( slot, data, data_length );
1751 if( status != PSA_SUCCESS )
1752 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001753 }
Gilles Peskine18017402019-07-24 20:25:59 +02001754 status = psa_check_key_slot_attributes( slot, attributes );
1755 if( status != PSA_SUCCESS )
1756 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001757
Gilles Peskine011e4282019-06-26 18:34:38 +02001758 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001759exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 if( status != PSA_SUCCESS )
1761 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001762 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 *handle = 0;
1764 }
1765 return( status );
1766}
1767
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001768static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001769 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001770{
1771 psa_status_t status;
1772 uint8_t *buffer = NULL;
1773 size_t buffer_size = 0;
1774 size_t length;
1775
Gilles Peskine8e338702019-07-30 20:06:31 +02001776 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001777 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001778 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001779 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001780 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001781 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1782 if( status != PSA_SUCCESS )
1783 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001784 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001785 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001786
1787exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001788 if( buffer_size != 0 )
1789 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001790 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001791 return( status );
1792}
1793
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001794psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1795 const psa_key_attributes_t *specified_attributes,
1796 psa_key_handle_t *target_handle )
1797{
1798 psa_status_t status;
1799 psa_key_slot_t *source_slot = NULL;
1800 psa_key_slot_t *target_slot = NULL;
1801 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001802 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001803
Gilles Peskine28f8f302019-07-24 13:30:31 +02001804 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001805 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001806 if( status != PSA_SUCCESS )
1807 goto exit;
1808
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1810 if( status != PSA_SUCCESS )
1811 goto exit;
1812
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001813 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001814 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001815 if( status != PSA_SUCCESS )
1816 goto exit;
1817
1818 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001819 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001820 if( status != PSA_SUCCESS )
1821 goto exit;
1822
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001823#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1824 if( driver != NULL )
1825 {
1826 /* Copying to a secure element is not implemented yet. */
1827 status = PSA_ERROR_NOT_SUPPORTED;
1828 goto exit;
1829 }
1830#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1831
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001832 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001833 if( status != PSA_SUCCESS )
1834 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001835
Gilles Peskine011e4282019-06-26 18:34:38 +02001836 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001837exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001838 if( status != PSA_SUCCESS )
1839 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001840 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001841 *target_handle = 0;
1842 }
1843 return( status );
1844}
1845
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001846
1847
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001848/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001849/* Message digests */
1850/****************************************************************/
1851
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001852static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001853{
1854 switch( alg )
1855 {
1856#if defined(MBEDTLS_MD2_C)
1857 case PSA_ALG_MD2:
1858 return( &mbedtls_md2_info );
1859#endif
1860#if defined(MBEDTLS_MD4_C)
1861 case PSA_ALG_MD4:
1862 return( &mbedtls_md4_info );
1863#endif
1864#if defined(MBEDTLS_MD5_C)
1865 case PSA_ALG_MD5:
1866 return( &mbedtls_md5_info );
1867#endif
1868#if defined(MBEDTLS_RIPEMD160_C)
1869 case PSA_ALG_RIPEMD160:
1870 return( &mbedtls_ripemd160_info );
1871#endif
1872#if defined(MBEDTLS_SHA1_C)
1873 case PSA_ALG_SHA_1:
1874 return( &mbedtls_sha1_info );
1875#endif
1876#if defined(MBEDTLS_SHA256_C)
1877 case PSA_ALG_SHA_224:
1878 return( &mbedtls_sha224_info );
1879 case PSA_ALG_SHA_256:
1880 return( &mbedtls_sha256_info );
1881#endif
1882#if defined(MBEDTLS_SHA512_C)
1883 case PSA_ALG_SHA_384:
1884 return( &mbedtls_sha384_info );
1885 case PSA_ALG_SHA_512:
1886 return( &mbedtls_sha512_info );
1887#endif
1888 default:
1889 return( NULL );
1890 }
1891}
1892
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001893psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1894{
1895 switch( operation->alg )
1896 {
Gilles Peskine81736312018-06-26 15:04:31 +02001897 case 0:
1898 /* The object has (apparently) been initialized but it is not
1899 * in use. It's ok to call abort on such an object, and there's
1900 * nothing to do. */
1901 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001902#if defined(MBEDTLS_MD2_C)
1903 case PSA_ALG_MD2:
1904 mbedtls_md2_free( &operation->ctx.md2 );
1905 break;
1906#endif
1907#if defined(MBEDTLS_MD4_C)
1908 case PSA_ALG_MD4:
1909 mbedtls_md4_free( &operation->ctx.md4 );
1910 break;
1911#endif
1912#if defined(MBEDTLS_MD5_C)
1913 case PSA_ALG_MD5:
1914 mbedtls_md5_free( &operation->ctx.md5 );
1915 break;
1916#endif
1917#if defined(MBEDTLS_RIPEMD160_C)
1918 case PSA_ALG_RIPEMD160:
1919 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1920 break;
1921#endif
1922#if defined(MBEDTLS_SHA1_C)
1923 case PSA_ALG_SHA_1:
1924 mbedtls_sha1_free( &operation->ctx.sha1 );
1925 break;
1926#endif
1927#if defined(MBEDTLS_SHA256_C)
1928 case PSA_ALG_SHA_224:
1929 case PSA_ALG_SHA_256:
1930 mbedtls_sha256_free( &operation->ctx.sha256 );
1931 break;
1932#endif
1933#if defined(MBEDTLS_SHA512_C)
1934 case PSA_ALG_SHA_384:
1935 case PSA_ALG_SHA_512:
1936 mbedtls_sha512_free( &operation->ctx.sha512 );
1937 break;
1938#endif
1939 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001940 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001941 }
1942 operation->alg = 0;
1943 return( PSA_SUCCESS );
1944}
1945
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001946psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001947 psa_algorithm_t alg )
1948{
1949 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001950
1951 /* A context must be freshly initialized before it can be set up. */
1952 if( operation->alg != 0 )
1953 {
1954 return( PSA_ERROR_BAD_STATE );
1955 }
1956
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001957 switch( alg )
1958 {
1959#if defined(MBEDTLS_MD2_C)
1960 case PSA_ALG_MD2:
1961 mbedtls_md2_init( &operation->ctx.md2 );
1962 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1963 break;
1964#endif
1965#if defined(MBEDTLS_MD4_C)
1966 case PSA_ALG_MD4:
1967 mbedtls_md4_init( &operation->ctx.md4 );
1968 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1969 break;
1970#endif
1971#if defined(MBEDTLS_MD5_C)
1972 case PSA_ALG_MD5:
1973 mbedtls_md5_init( &operation->ctx.md5 );
1974 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1975 break;
1976#endif
1977#if defined(MBEDTLS_RIPEMD160_C)
1978 case PSA_ALG_RIPEMD160:
1979 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1980 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1981 break;
1982#endif
1983#if defined(MBEDTLS_SHA1_C)
1984 case PSA_ALG_SHA_1:
1985 mbedtls_sha1_init( &operation->ctx.sha1 );
1986 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1987 break;
1988#endif
1989#if defined(MBEDTLS_SHA256_C)
1990 case PSA_ALG_SHA_224:
1991 mbedtls_sha256_init( &operation->ctx.sha256 );
1992 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1993 break;
1994 case PSA_ALG_SHA_256:
1995 mbedtls_sha256_init( &operation->ctx.sha256 );
1996 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1997 break;
1998#endif
1999#if defined(MBEDTLS_SHA512_C)
2000 case PSA_ALG_SHA_384:
2001 mbedtls_sha512_init( &operation->ctx.sha512 );
2002 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2003 break;
2004 case PSA_ALG_SHA_512:
2005 mbedtls_sha512_init( &operation->ctx.sha512 );
2006 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2007 break;
2008#endif
2009 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002010 return( PSA_ALG_IS_HASH( alg ) ?
2011 PSA_ERROR_NOT_SUPPORTED :
2012 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002013 }
2014 if( ret == 0 )
2015 operation->alg = alg;
2016 else
2017 psa_hash_abort( operation );
2018 return( mbedtls_to_psa_error( ret ) );
2019}
2020
2021psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2022 const uint8_t *input,
2023 size_t input_length )
2024{
2025 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002026
2027 /* Don't require hash implementations to behave correctly on a
2028 * zero-length input, which may have an invalid pointer. */
2029 if( input_length == 0 )
2030 return( PSA_SUCCESS );
2031
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002032 switch( operation->alg )
2033 {
2034#if defined(MBEDTLS_MD2_C)
2035 case PSA_ALG_MD2:
2036 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2037 input, input_length );
2038 break;
2039#endif
2040#if defined(MBEDTLS_MD4_C)
2041 case PSA_ALG_MD4:
2042 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2043 input, input_length );
2044 break;
2045#endif
2046#if defined(MBEDTLS_MD5_C)
2047 case PSA_ALG_MD5:
2048 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2049 input, input_length );
2050 break;
2051#endif
2052#if defined(MBEDTLS_RIPEMD160_C)
2053 case PSA_ALG_RIPEMD160:
2054 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2055 input, input_length );
2056 break;
2057#endif
2058#if defined(MBEDTLS_SHA1_C)
2059 case PSA_ALG_SHA_1:
2060 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2061 input, input_length );
2062 break;
2063#endif
2064#if defined(MBEDTLS_SHA256_C)
2065 case PSA_ALG_SHA_224:
2066 case PSA_ALG_SHA_256:
2067 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2068 input, input_length );
2069 break;
2070#endif
2071#if defined(MBEDTLS_SHA512_C)
2072 case PSA_ALG_SHA_384:
2073 case PSA_ALG_SHA_512:
2074 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2075 input, input_length );
2076 break;
2077#endif
2078 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002079 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002080 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002081
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082 if( ret != 0 )
2083 psa_hash_abort( operation );
2084 return( mbedtls_to_psa_error( ret ) );
2085}
2086
2087psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2088 uint8_t *hash,
2089 size_t hash_size,
2090 size_t *hash_length )
2091{
itayzafrir40835d42018-08-02 13:14:17 +03002092 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002093 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002094 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002095
2096 /* Fill the output buffer with something that isn't a valid hash
2097 * (barring an attack on the hash and deliberately-crafted input),
2098 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002099 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002100 /* If hash_size is 0 then hash may be NULL and then the
2101 * call to memset would have undefined behavior. */
2102 if( hash_size != 0 )
2103 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002104
2105 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002106 {
2107 status = PSA_ERROR_BUFFER_TOO_SMALL;
2108 goto exit;
2109 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110
2111 switch( operation->alg )
2112 {
2113#if defined(MBEDTLS_MD2_C)
2114 case PSA_ALG_MD2:
2115 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2116 break;
2117#endif
2118#if defined(MBEDTLS_MD4_C)
2119 case PSA_ALG_MD4:
2120 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2121 break;
2122#endif
2123#if defined(MBEDTLS_MD5_C)
2124 case PSA_ALG_MD5:
2125 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2126 break;
2127#endif
2128#if defined(MBEDTLS_RIPEMD160_C)
2129 case PSA_ALG_RIPEMD160:
2130 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2131 break;
2132#endif
2133#if defined(MBEDTLS_SHA1_C)
2134 case PSA_ALG_SHA_1:
2135 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2136 break;
2137#endif
2138#if defined(MBEDTLS_SHA256_C)
2139 case PSA_ALG_SHA_224:
2140 case PSA_ALG_SHA_256:
2141 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2142 break;
2143#endif
2144#if defined(MBEDTLS_SHA512_C)
2145 case PSA_ALG_SHA_384:
2146 case PSA_ALG_SHA_512:
2147 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2148 break;
2149#endif
2150 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002151 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152 }
itayzafrir40835d42018-08-02 13:14:17 +03002153 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154
itayzafrir40835d42018-08-02 13:14:17 +03002155exit:
2156 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002158 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159 return( psa_hash_abort( operation ) );
2160 }
2161 else
2162 {
2163 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002164 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165 }
2166}
2167
Gilles Peskine2d277862018-06-18 15:41:12 +02002168psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2169 const uint8_t *hash,
2170 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171{
2172 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2173 size_t actual_hash_length;
2174 psa_status_t status = psa_hash_finish( operation,
2175 actual_hash, sizeof( actual_hash ),
2176 &actual_hash_length );
2177 if( status != PSA_SUCCESS )
2178 return( status );
2179 if( actual_hash_length != hash_length )
2180 return( PSA_ERROR_INVALID_SIGNATURE );
2181 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2182 return( PSA_ERROR_INVALID_SIGNATURE );
2183 return( PSA_SUCCESS );
2184}
2185
Gilles Peskineeb35d782019-01-22 17:56:16 +01002186psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2187 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002188{
2189 if( target_operation->alg != 0 )
2190 return( PSA_ERROR_BAD_STATE );
2191
2192 switch( source_operation->alg )
2193 {
2194 case 0:
2195 return( PSA_ERROR_BAD_STATE );
2196#if defined(MBEDTLS_MD2_C)
2197 case PSA_ALG_MD2:
2198 mbedtls_md2_clone( &target_operation->ctx.md2,
2199 &source_operation->ctx.md2 );
2200 break;
2201#endif
2202#if defined(MBEDTLS_MD4_C)
2203 case PSA_ALG_MD4:
2204 mbedtls_md4_clone( &target_operation->ctx.md4,
2205 &source_operation->ctx.md4 );
2206 break;
2207#endif
2208#if defined(MBEDTLS_MD5_C)
2209 case PSA_ALG_MD5:
2210 mbedtls_md5_clone( &target_operation->ctx.md5,
2211 &source_operation->ctx.md5 );
2212 break;
2213#endif
2214#if defined(MBEDTLS_RIPEMD160_C)
2215 case PSA_ALG_RIPEMD160:
2216 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2217 &source_operation->ctx.ripemd160 );
2218 break;
2219#endif
2220#if defined(MBEDTLS_SHA1_C)
2221 case PSA_ALG_SHA_1:
2222 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2223 &source_operation->ctx.sha1 );
2224 break;
2225#endif
2226#if defined(MBEDTLS_SHA256_C)
2227 case PSA_ALG_SHA_224:
2228 case PSA_ALG_SHA_256:
2229 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2230 &source_operation->ctx.sha256 );
2231 break;
2232#endif
2233#if defined(MBEDTLS_SHA512_C)
2234 case PSA_ALG_SHA_384:
2235 case PSA_ALG_SHA_512:
2236 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2237 &source_operation->ctx.sha512 );
2238 break;
2239#endif
2240 default:
2241 return( PSA_ERROR_NOT_SUPPORTED );
2242 }
2243
2244 target_operation->alg = source_operation->alg;
2245 return( PSA_SUCCESS );
2246}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247
2248
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002250/* MAC */
2251/****************************************************************/
2252
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002253static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002254 psa_algorithm_t alg,
2255 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002256 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002257 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002258{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002260 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002261
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002262 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002263 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002264
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2266 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002267 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002268 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002269 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002270 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271 mode = MBEDTLS_MODE_STREAM;
2272 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002273 case PSA_ALG_CTR:
2274 mode = MBEDTLS_MODE_CTR;
2275 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002276 case PSA_ALG_CFB:
2277 mode = MBEDTLS_MODE_CFB;
2278 break;
2279 case PSA_ALG_OFB:
2280 mode = MBEDTLS_MODE_OFB;
2281 break;
2282 case PSA_ALG_CBC_NO_PADDING:
2283 mode = MBEDTLS_MODE_CBC;
2284 break;
2285 case PSA_ALG_CBC_PKCS7:
2286 mode = MBEDTLS_MODE_CBC;
2287 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002288 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289 mode = MBEDTLS_MODE_CCM;
2290 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002291 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292 mode = MBEDTLS_MODE_GCM;
2293 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002294 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2295 mode = MBEDTLS_MODE_CHACHAPOLY;
2296 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002297 default:
2298 return( NULL );
2299 }
2300 }
2301 else if( alg == PSA_ALG_CMAC )
2302 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303 else
2304 return( NULL );
2305
2306 switch( key_type )
2307 {
2308 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002309 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002310 break;
2311 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002312 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2313 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002314 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002315 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002316 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002317 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002318 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2319 * but two-key Triple-DES is functionally three-key Triple-DES
2320 * with K1=K3, so that's how we present it to mbedtls. */
2321 if( key_bits == 128 )
2322 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323 break;
2324 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002325 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002326 break;
2327 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002328 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002330 case PSA_KEY_TYPE_CHACHA20:
2331 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2332 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333 default:
2334 return( NULL );
2335 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002336 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002337 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338
Jaeden Amero23bbb752018-06-26 14:16:54 +01002339 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2340 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341}
2342
Gilles Peskinea05219c2018-11-16 16:02:56 +01002343#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002344static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002345{
Gilles Peskine2d277862018-06-18 15:41:12 +02002346 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002347 {
2348 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002349 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002350 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002351 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002352 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002353 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002354 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002355 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002356 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002357 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002358 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002359 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002360 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002361 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002362 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002363 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002364 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002365 return( 128 );
2366 default:
2367 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002368 }
2369}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002370#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002371
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002372/* Initialize the MAC operation structure. Once this function has been
2373 * called, psa_mac_abort can run and will do the right thing. */
2374static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2375 psa_algorithm_t alg )
2376{
2377 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2378
2379 operation->alg = alg;
2380 operation->key_set = 0;
2381 operation->iv_set = 0;
2382 operation->iv_required = 0;
2383 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002384 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002385
2386#if defined(MBEDTLS_CMAC_C)
2387 if( alg == PSA_ALG_CMAC )
2388 {
2389 operation->iv_required = 0;
2390 mbedtls_cipher_init( &operation->ctx.cmac );
2391 status = PSA_SUCCESS;
2392 }
2393 else
2394#endif /* MBEDTLS_CMAC_C */
2395#if defined(MBEDTLS_MD_C)
2396 if( PSA_ALG_IS_HMAC( operation->alg ) )
2397 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002398 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2399 operation->ctx.hmac.hash_ctx.alg = 0;
2400 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401 }
2402 else
2403#endif /* MBEDTLS_MD_C */
2404 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002405 if( ! PSA_ALG_IS_MAC( alg ) )
2406 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002407 }
2408
2409 if( status != PSA_SUCCESS )
2410 memset( operation, 0, sizeof( *operation ) );
2411 return( status );
2412}
2413
Gilles Peskine01126fa2018-07-12 17:04:55 +02002414#if defined(MBEDTLS_MD_C)
2415static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2416{
Gilles Peskine3f108122018-12-07 18:14:53 +01002417 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002418 return( psa_hash_abort( &hmac->hash_ctx ) );
2419}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002420
Janos Follath999f6482019-06-11 12:04:10 +01002421#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002422static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2423{
2424 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2425 memset( hmac, 0, sizeof( *hmac ) );
2426}
Janos Follath999f6482019-06-11 12:04:10 +01002427#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002428#endif /* MBEDTLS_MD_C */
2429
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2431{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002434 /* The object has (apparently) been initialized but it is not
2435 * in use. It's ok to call abort on such an object, and there's
2436 * nothing to do. */
2437 return( PSA_SUCCESS );
2438 }
2439 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002441 if( operation->alg == PSA_ALG_CMAC )
2442 {
2443 mbedtls_cipher_free( &operation->ctx.cmac );
2444 }
2445 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002447#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002448 if( PSA_ALG_IS_HMAC( operation->alg ) )
2449 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002450 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002451 }
2452 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002454 {
2455 /* Sanity check (shouldn't happen: operation->alg should
2456 * always have been initialized to a valid value). */
2457 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458 }
Moran Peker41deec42018-04-04 15:43:05 +03002459
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460 operation->alg = 0;
2461 operation->key_set = 0;
2462 operation->iv_set = 0;
2463 operation->iv_required = 0;
2464 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002466
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002468
2469bad_state:
2470 /* If abort is called on an uninitialized object, we can't trust
2471 * anything. Wipe the object in case it contains confidential data.
2472 * This may result in a memory leak if a pointer gets overwritten,
2473 * but it's too late to do anything about this. */
2474 memset( operation, 0, sizeof( *operation ) );
2475 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476}
2477
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002478#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002479static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002480 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002481 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002482 const mbedtls_cipher_info_t *cipher_info )
2483{
2484 int ret;
2485
2486 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002487
2488 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2489 if( ret != 0 )
2490 return( ret );
2491
2492 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2493 slot->data.raw.data,
2494 key_bits );
2495 return( ret );
2496}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002497#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498
Gilles Peskine248051a2018-06-20 16:09:38 +02002499#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002500static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2501 const uint8_t *key,
2502 size_t key_length,
2503 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002505 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002506 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002507 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002508 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002509 psa_status_t status;
2510
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002511 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2512 * overflow below. This should never trigger if the hash algorithm
2513 * is implemented correctly. */
2514 /* The size checks against the ipad and opad buffers cannot be written
2515 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2516 * because that triggers -Wlogical-op on GCC 7.3. */
2517 if( block_size > sizeof( ipad ) )
2518 return( PSA_ERROR_NOT_SUPPORTED );
2519 if( block_size > sizeof( hmac->opad ) )
2520 return( PSA_ERROR_NOT_SUPPORTED );
2521 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002522 return( PSA_ERROR_NOT_SUPPORTED );
2523
Gilles Peskined223b522018-06-11 18:12:58 +02002524 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002525 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002526 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002527 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002528 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002529 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002530 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002531 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002532 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002533 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002534 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002535 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002536 }
Gilles Peskine96889972018-07-12 17:07:03 +02002537 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2538 * but it is permitted. It is common when HMAC is used in HKDF, for
2539 * example. Don't call `memcpy` in the 0-length because `key` could be
2540 * an invalid pointer which would make the behavior undefined. */
2541 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002542 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002543
Gilles Peskined223b522018-06-11 18:12:58 +02002544 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2545 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002546 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002547 ipad[i] ^= 0x36;
2548 memset( ipad + key_length, 0x36, block_size - key_length );
2549
2550 /* Copy the key material from ipad to opad, flipping the requisite bits,
2551 * and filling the rest of opad with the requisite constant. */
2552 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002553 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2554 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002555
Gilles Peskine01126fa2018-07-12 17:04:55 +02002556 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002557 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002558 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002559
Gilles Peskine01126fa2018-07-12 17:04:55 +02002560 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002561
2562cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002563 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002564
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002565 return( status );
2566}
Gilles Peskine248051a2018-06-20 16:09:38 +02002567#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002568
Gilles Peskine89167cb2018-07-08 20:12:23 +02002569static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002570 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002571 psa_algorithm_t alg,
2572 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002575 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002577 psa_key_usage_t usage =
2578 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002579 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002580 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002582 /* A context must be freshly initialized before it can be set up. */
2583 if( operation->alg != 0 )
2584 {
2585 return( PSA_ERROR_BAD_STATE );
2586 }
2587
Gilles Peskined911eb72018-08-14 15:18:45 +02002588 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002589 if( status != PSA_SUCCESS )
2590 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002591 if( is_sign )
2592 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593
Gilles Peskine28f8f302019-07-24 13:30:31 +02002594 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002595 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002596 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002597 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002598
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002600 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002601 {
2602 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002603 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002604 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002605 int ret;
2606 if( cipher_info == NULL )
2607 {
2608 status = PSA_ERROR_NOT_SUPPORTED;
2609 goto exit;
2610 }
2611 operation->mac_size = cipher_info->block_size;
2612 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2613 status = mbedtls_to_psa_error( ret );
2614 }
2615 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002618 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002619 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002620 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002621 if( hash_alg == 0 )
2622 {
2623 status = PSA_ERROR_NOT_SUPPORTED;
2624 goto exit;
2625 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002626
2627 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2628 /* Sanity check. This shouldn't fail on a valid configuration. */
2629 if( operation->mac_size == 0 ||
2630 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2631 {
2632 status = PSA_ERROR_NOT_SUPPORTED;
2633 goto exit;
2634 }
2635
Gilles Peskine8e338702019-07-30 20:06:31 +02002636 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002637 {
2638 status = PSA_ERROR_INVALID_ARGUMENT;
2639 goto exit;
2640 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002641
Gilles Peskine01126fa2018-07-12 17:04:55 +02002642 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2643 slot->data.raw.data,
2644 slot->data.raw.bytes,
2645 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646 }
2647 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002649 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002650 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002651 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002653
Gilles Peskined911eb72018-08-14 15:18:45 +02002654 if( truncated == 0 )
2655 {
2656 /* The "normal" case: untruncated algorithm. Nothing to do. */
2657 }
2658 else if( truncated < 4 )
2659 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002660 /* A very short MAC is too short for security since it can be
2661 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2662 * so we make this our minimum, even though 32 bits is still
2663 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002664 status = PSA_ERROR_NOT_SUPPORTED;
2665 }
2666 else if( truncated > operation->mac_size )
2667 {
2668 /* It's impossible to "truncate" to a larger length. */
2669 status = PSA_ERROR_INVALID_ARGUMENT;
2670 }
2671 else
2672 operation->mac_size = truncated;
2673
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002675 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002677 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002679 else
2680 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681 operation->key_set = 1;
2682 }
2683 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684}
2685
Gilles Peskine89167cb2018-07-08 20:12:23 +02002686psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002687 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002688 psa_algorithm_t alg )
2689{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002690 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002691}
2692
2693psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002694 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002695 psa_algorithm_t alg )
2696{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002697 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002698}
2699
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2701 const uint8_t *input,
2702 size_t input_length )
2703{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002704 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002706 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002708 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709 operation->has_input = 1;
2710
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002712 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002713 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002714 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2715 input, input_length );
2716 status = mbedtls_to_psa_error( ret );
2717 }
2718 else
2719#endif /* MBEDTLS_CMAC_C */
2720#if defined(MBEDTLS_MD_C)
2721 if( PSA_ALG_IS_HMAC( operation->alg ) )
2722 {
2723 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2724 input_length );
2725 }
2726 else
2727#endif /* MBEDTLS_MD_C */
2728 {
2729 /* This shouldn't happen if `operation` was initialized by
2730 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002731 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002732 }
2733
Gilles Peskinefbfac682018-07-08 20:51:54 +02002734 if( status != PSA_SUCCESS )
2735 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002736 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737}
2738
Gilles Peskine01126fa2018-07-12 17:04:55 +02002739#if defined(MBEDTLS_MD_C)
2740static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2741 uint8_t *mac,
2742 size_t mac_size )
2743{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002744 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002745 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2746 size_t hash_size = 0;
2747 size_t block_size = psa_get_hash_block_size( hash_alg );
2748 psa_status_t status;
2749
Gilles Peskine01126fa2018-07-12 17:04:55 +02002750 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2751 if( status != PSA_SUCCESS )
2752 return( status );
2753 /* From here on, tmp needs to be wiped. */
2754
2755 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2756 if( status != PSA_SUCCESS )
2757 goto exit;
2758
2759 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2760 if( status != PSA_SUCCESS )
2761 goto exit;
2762
2763 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2764 if( status != PSA_SUCCESS )
2765 goto exit;
2766
Gilles Peskined911eb72018-08-14 15:18:45 +02002767 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2768 if( status != PSA_SUCCESS )
2769 goto exit;
2770
2771 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002772
2773exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002774 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002775 return( status );
2776}
2777#endif /* MBEDTLS_MD_C */
2778
mohammad16036df908f2018-04-02 08:34:15 -07002779static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002780 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002781 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002783 if( ! operation->key_set )
2784 return( PSA_ERROR_BAD_STATE );
2785 if( operation->iv_required && ! operation->iv_set )
2786 return( PSA_ERROR_BAD_STATE );
2787
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 if( mac_size < operation->mac_size )
2789 return( PSA_ERROR_BUFFER_TOO_SMALL );
2790
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002792 if( operation->alg == PSA_ALG_CMAC )
2793 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002794 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2795 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2796 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002797 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002798 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002799 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002801 else
2802#endif /* MBEDTLS_CMAC_C */
2803#if defined(MBEDTLS_MD_C)
2804 if( PSA_ALG_IS_HMAC( operation->alg ) )
2805 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002806 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002807 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002808 }
2809 else
2810#endif /* MBEDTLS_MD_C */
2811 {
2812 /* This shouldn't happen if `operation` was initialized by
2813 * a setup function. */
2814 return( PSA_ERROR_BAD_STATE );
2815 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816}
2817
Gilles Peskineacd4be32018-07-08 19:56:25 +02002818psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2819 uint8_t *mac,
2820 size_t mac_size,
2821 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002822{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002823 psa_status_t status;
2824
Jaeden Amero252ef282019-02-15 14:05:35 +00002825 if( operation->alg == 0 )
2826 {
2827 return( PSA_ERROR_BAD_STATE );
2828 }
2829
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002830 /* Fill the output buffer with something that isn't a valid mac
2831 * (barring an attack on the mac and deliberately-crafted input),
2832 * in case the caller doesn't check the return status properly. */
2833 *mac_length = mac_size;
2834 /* If mac_size is 0 then mac may be NULL and then the
2835 * call to memset would have undefined behavior. */
2836 if( mac_size != 0 )
2837 memset( mac, '!', mac_size );
2838
Gilles Peskine89167cb2018-07-08 20:12:23 +02002839 if( ! operation->is_sign )
2840 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002841 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002842 }
mohammad16036df908f2018-04-02 08:34:15 -07002843
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002844 status = psa_mac_finish_internal( operation, mac, mac_size );
2845
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002846 if( status == PSA_SUCCESS )
2847 {
2848 status = psa_mac_abort( operation );
2849 if( status == PSA_SUCCESS )
2850 *mac_length = operation->mac_size;
2851 else
2852 memset( mac, '!', mac_size );
2853 }
2854 else
2855 psa_mac_abort( operation );
2856 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002857}
2858
Gilles Peskineacd4be32018-07-08 19:56:25 +02002859psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2860 const uint8_t *mac,
2861 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862{
Gilles Peskine828ed142018-06-18 23:25:51 +02002863 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002864 psa_status_t status;
2865
Jaeden Amero252ef282019-02-15 14:05:35 +00002866 if( operation->alg == 0 )
2867 {
2868 return( PSA_ERROR_BAD_STATE );
2869 }
2870
Gilles Peskine89167cb2018-07-08 20:12:23 +02002871 if( operation->is_sign )
2872 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002873 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002874 }
2875 if( operation->mac_size != mac_length )
2876 {
2877 status = PSA_ERROR_INVALID_SIGNATURE;
2878 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002879 }
mohammad16036df908f2018-04-02 08:34:15 -07002880
2881 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002882 actual_mac, sizeof( actual_mac ) );
2883
2884 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2885 status = PSA_ERROR_INVALID_SIGNATURE;
2886
2887cleanup:
2888 if( status == PSA_SUCCESS )
2889 status = psa_mac_abort( operation );
2890 else
2891 psa_mac_abort( operation );
2892
Gilles Peskine3f108122018-12-07 18:14:53 +01002893 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002894
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002895 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896}
2897
2898
Gilles Peskine20035e32018-02-03 22:44:14 +01002899
Gilles Peskine20035e32018-02-03 22:44:14 +01002900/****************************************************************/
2901/* Asymmetric cryptography */
2902/****************************************************************/
2903
Gilles Peskine2b450e32018-06-27 15:42:46 +02002904#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002905/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002906 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002907static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2908 size_t hash_length,
2909 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002910{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002911 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002912 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002913 *md_alg = mbedtls_md_get_type( md_info );
2914
2915 /* The Mbed TLS RSA module uses an unsigned int for hash length
2916 * parameters. Validate that it fits so that we don't risk an
2917 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002918#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002919 if( hash_length > UINT_MAX )
2920 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002921#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002922
2923#if defined(MBEDTLS_PKCS1_V15)
2924 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2925 * must be correct. */
2926 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2927 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002928 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002929 if( md_info == NULL )
2930 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002931 if( mbedtls_md_get_size( md_info ) != hash_length )
2932 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002933 }
2934#endif /* MBEDTLS_PKCS1_V15 */
2935
2936#if defined(MBEDTLS_PKCS1_V21)
2937 /* PSS requires a hash internally. */
2938 if( PSA_ALG_IS_RSA_PSS( alg ) )
2939 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002940 if( md_info == NULL )
2941 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002942 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002943#endif /* MBEDTLS_PKCS1_V21 */
2944
Gilles Peskine61b91d42018-06-08 16:09:36 +02002945 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002946}
2947
Gilles Peskine2b450e32018-06-27 15:42:46 +02002948static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2949 psa_algorithm_t alg,
2950 const uint8_t *hash,
2951 size_t hash_length,
2952 uint8_t *signature,
2953 size_t signature_size,
2954 size_t *signature_length )
2955{
2956 psa_status_t status;
2957 int ret;
2958 mbedtls_md_type_t md_alg;
2959
2960 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2961 if( status != PSA_SUCCESS )
2962 return( status );
2963
Gilles Peskine630a18a2018-06-29 17:49:35 +02002964 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002965 return( PSA_ERROR_BUFFER_TOO_SMALL );
2966
2967#if defined(MBEDTLS_PKCS1_V15)
2968 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2969 {
2970 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2971 MBEDTLS_MD_NONE );
2972 ret = mbedtls_rsa_pkcs1_sign( rsa,
2973 mbedtls_ctr_drbg_random,
2974 &global_data.ctr_drbg,
2975 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002976 md_alg,
2977 (unsigned int) hash_length,
2978 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002979 signature );
2980 }
2981 else
2982#endif /* MBEDTLS_PKCS1_V15 */
2983#if defined(MBEDTLS_PKCS1_V21)
2984 if( PSA_ALG_IS_RSA_PSS( alg ) )
2985 {
2986 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2987 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2988 mbedtls_ctr_drbg_random,
2989 &global_data.ctr_drbg,
2990 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002991 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002992 (unsigned int) hash_length,
2993 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002994 signature );
2995 }
2996 else
2997#endif /* MBEDTLS_PKCS1_V21 */
2998 {
2999 return( PSA_ERROR_INVALID_ARGUMENT );
3000 }
3001
3002 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003003 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003004 return( mbedtls_to_psa_error( ret ) );
3005}
3006
3007static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3008 psa_algorithm_t alg,
3009 const uint8_t *hash,
3010 size_t hash_length,
3011 const uint8_t *signature,
3012 size_t signature_length )
3013{
3014 psa_status_t status;
3015 int ret;
3016 mbedtls_md_type_t md_alg;
3017
3018 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3019 if( status != PSA_SUCCESS )
3020 return( status );
3021
Gilles Peskine630a18a2018-06-29 17:49:35 +02003022 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003023 return( PSA_ERROR_BUFFER_TOO_SMALL );
3024
3025#if defined(MBEDTLS_PKCS1_V15)
3026 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3027 {
3028 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3029 MBEDTLS_MD_NONE );
3030 ret = mbedtls_rsa_pkcs1_verify( rsa,
3031 mbedtls_ctr_drbg_random,
3032 &global_data.ctr_drbg,
3033 MBEDTLS_RSA_PUBLIC,
3034 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003035 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003036 hash,
3037 signature );
3038 }
3039 else
3040#endif /* MBEDTLS_PKCS1_V15 */
3041#if defined(MBEDTLS_PKCS1_V21)
3042 if( PSA_ALG_IS_RSA_PSS( alg ) )
3043 {
3044 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3045 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3046 mbedtls_ctr_drbg_random,
3047 &global_data.ctr_drbg,
3048 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003049 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003050 (unsigned int) hash_length,
3051 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003052 signature );
3053 }
3054 else
3055#endif /* MBEDTLS_PKCS1_V21 */
3056 {
3057 return( PSA_ERROR_INVALID_ARGUMENT );
3058 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003059
3060 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3061 * the rest of the signature is invalid". This has little use in
3062 * practice and PSA doesn't report this distinction. */
3063 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3064 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003065 return( mbedtls_to_psa_error( ret ) );
3066}
3067#endif /* MBEDTLS_RSA_C */
3068
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003069#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003070/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3071 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3072 * (even though these functions don't modify it). */
3073static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3074 psa_algorithm_t alg,
3075 const uint8_t *hash,
3076 size_t hash_length,
3077 uint8_t *signature,
3078 size_t signature_size,
3079 size_t *signature_length )
3080{
3081 int ret;
3082 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003083 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003084 mbedtls_mpi_init( &r );
3085 mbedtls_mpi_init( &s );
3086
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003087 if( signature_size < 2 * curve_bytes )
3088 {
3089 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3090 goto cleanup;
3091 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003092
Gilles Peskinea05219c2018-11-16 16:02:56 +01003093#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003094 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3095 {
3096 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3097 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3098 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3099 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3100 hash, hash_length,
3101 md_alg ) );
3102 }
3103 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003104#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003105 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003106 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003107 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3108 hash, hash_length,
3109 mbedtls_ctr_drbg_random,
3110 &global_data.ctr_drbg ) );
3111 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003112
3113 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3114 signature,
3115 curve_bytes ) );
3116 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3117 signature + curve_bytes,
3118 curve_bytes ) );
3119
3120cleanup:
3121 mbedtls_mpi_free( &r );
3122 mbedtls_mpi_free( &s );
3123 if( ret == 0 )
3124 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003125 return( mbedtls_to_psa_error( ret ) );
3126}
3127
3128static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3129 const uint8_t *hash,
3130 size_t hash_length,
3131 const uint8_t *signature,
3132 size_t signature_length )
3133{
3134 int ret;
3135 mbedtls_mpi r, s;
3136 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3137 mbedtls_mpi_init( &r );
3138 mbedtls_mpi_init( &s );
3139
3140 if( signature_length != 2 * curve_bytes )
3141 return( PSA_ERROR_INVALID_SIGNATURE );
3142
3143 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3144 signature,
3145 curve_bytes ) );
3146 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3147 signature + curve_bytes,
3148 curve_bytes ) );
3149
3150 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3151 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003152
3153cleanup:
3154 mbedtls_mpi_free( &r );
3155 mbedtls_mpi_free( &s );
3156 return( mbedtls_to_psa_error( ret ) );
3157}
3158#endif /* MBEDTLS_ECDSA_C */
3159
Gilles Peskinec5487a82018-12-03 18:08:14 +01003160psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003161 psa_algorithm_t alg,
3162 const uint8_t *hash,
3163 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003164 uint8_t *signature,
3165 size_t signature_size,
3166 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003167{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003168 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003169 psa_status_t status;
3170
3171 *signature_length = signature_size;
3172
Gilles Peskine28f8f302019-07-24 13:30:31 +02003173 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003174 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003175 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003176 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003177 {
3178 status = PSA_ERROR_INVALID_ARGUMENT;
3179 goto exit;
3180 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003181
Gilles Peskine20035e32018-02-03 22:44:14 +01003182#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003183 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003184 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003185 status = psa_rsa_sign( slot->data.rsa,
3186 alg,
3187 hash, hash_length,
3188 signature, signature_size,
3189 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003190 }
3191 else
3192#endif /* defined(MBEDTLS_RSA_C) */
3193#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003194 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003195 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003196#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003197 if(
3198#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3199 PSA_ALG_IS_ECDSA( alg )
3200#else
3201 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3202#endif
3203 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003204 status = psa_ecdsa_sign( slot->data.ecp,
3205 alg,
3206 hash, hash_length,
3207 signature, signature_size,
3208 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003209 else
3210#endif /* defined(MBEDTLS_ECDSA_C) */
3211 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003212 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003213 }
itayzafrir5c753392018-05-08 11:18:38 +03003214 }
3215 else
3216#endif /* defined(MBEDTLS_ECP_C) */
3217 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003218 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003219 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003220
3221exit:
3222 /* Fill the unused part of the output buffer (the whole buffer on error,
3223 * the trailing part on success) with something that isn't a valid mac
3224 * (barring an attack on the mac and deliberately-crafted input),
3225 * in case the caller doesn't check the return status properly. */
3226 if( status == PSA_SUCCESS )
3227 memset( signature + *signature_length, '!',
3228 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003229 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003230 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003231 /* If signature_size is 0 then we have nothing to do. We must not call
3232 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003233 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003234}
3235
Gilles Peskinec5487a82018-12-03 18:08:14 +01003236psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003237 psa_algorithm_t alg,
3238 const uint8_t *hash,
3239 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003240 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003241 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003242{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003243 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003244 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003245
Gilles Peskine28f8f302019-07-24 13:30:31 +02003246 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003247 if( status != PSA_SUCCESS )
3248 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003249
Gilles Peskine61b91d42018-06-08 16:09:36 +02003250#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003251 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003253 return( psa_rsa_verify( slot->data.rsa,
3254 alg,
3255 hash, hash_length,
3256 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 }
3258 else
3259#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003260#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003261 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003262 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003263#if defined(MBEDTLS_ECDSA_C)
3264 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003265 return( psa_ecdsa_verify( slot->data.ecp,
3266 hash, hash_length,
3267 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003268 else
3269#endif /* defined(MBEDTLS_ECDSA_C) */
3270 {
3271 return( PSA_ERROR_INVALID_ARGUMENT );
3272 }
itayzafrir5c753392018-05-08 11:18:38 +03003273 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003274 else
3275#endif /* defined(MBEDTLS_ECP_C) */
3276 {
3277 return( PSA_ERROR_NOT_SUPPORTED );
3278 }
3279}
3280
Gilles Peskine072ac562018-06-30 00:21:29 +02003281#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3282static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3283 mbedtls_rsa_context *rsa )
3284{
3285 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3286 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3287 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3288 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3289}
3290#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3291
Gilles Peskinec5487a82018-12-03 18:08:14 +01003292psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003293 psa_algorithm_t alg,
3294 const uint8_t *input,
3295 size_t input_length,
3296 const uint8_t *salt,
3297 size_t salt_length,
3298 uint8_t *output,
3299 size_t output_size,
3300 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003301{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003302 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003303 psa_status_t status;
3304
Darryl Green5cc689a2018-07-24 15:34:10 +01003305 (void) input;
3306 (void) input_length;
3307 (void) salt;
3308 (void) output;
3309 (void) output_size;
3310
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003311 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003313 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3314 return( PSA_ERROR_INVALID_ARGUMENT );
3315
Gilles Peskine28f8f302019-07-24 13:30:31 +02003316 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003317 if( status != PSA_SUCCESS )
3318 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003319 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3320 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322
3323#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003324 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325 {
3326 mbedtls_rsa_context *rsa = slot->data.rsa;
3327 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003328 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003329 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003330#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003331 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003333 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3334 mbedtls_ctr_drbg_random,
3335 &global_data.ctr_drbg,
3336 MBEDTLS_RSA_PUBLIC,
3337 input_length,
3338 input,
3339 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003340 }
3341 else
3342#endif /* MBEDTLS_PKCS1_V15 */
3343#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003344 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003346 psa_rsa_oaep_set_padding_mode( alg, rsa );
3347 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3348 mbedtls_ctr_drbg_random,
3349 &global_data.ctr_drbg,
3350 MBEDTLS_RSA_PUBLIC,
3351 salt, salt_length,
3352 input_length,
3353 input,
3354 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003355 }
3356 else
3357#endif /* MBEDTLS_PKCS1_V21 */
3358 {
3359 return( PSA_ERROR_INVALID_ARGUMENT );
3360 }
3361 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003362 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003363 return( mbedtls_to_psa_error( ret ) );
3364 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003366#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003367 {
3368 return( PSA_ERROR_NOT_SUPPORTED );
3369 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003370}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371
Gilles Peskinec5487a82018-12-03 18:08:14 +01003372psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003373 psa_algorithm_t alg,
3374 const uint8_t *input,
3375 size_t input_length,
3376 const uint8_t *salt,
3377 size_t salt_length,
3378 uint8_t *output,
3379 size_t output_size,
3380 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003381{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003382 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003383 psa_status_t status;
3384
Darryl Green5cc689a2018-07-24 15:34:10 +01003385 (void) input;
3386 (void) input_length;
3387 (void) salt;
3388 (void) output;
3389 (void) output_size;
3390
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003391 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003392
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003393 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3394 return( PSA_ERROR_INVALID_ARGUMENT );
3395
Gilles Peskine28f8f302019-07-24 13:30:31 +02003396 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003397 if( status != PSA_SUCCESS )
3398 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003399 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003400 return( PSA_ERROR_INVALID_ARGUMENT );
3401
3402#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003403 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404 {
3405 mbedtls_rsa_context *rsa = slot->data.rsa;
3406 int ret;
3407
Gilles Peskine630a18a2018-06-29 17:49:35 +02003408 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003409 return( PSA_ERROR_INVALID_ARGUMENT );
3410
3411#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003412 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003413 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003414 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3415 mbedtls_ctr_drbg_random,
3416 &global_data.ctr_drbg,
3417 MBEDTLS_RSA_PRIVATE,
3418 output_length,
3419 input,
3420 output,
3421 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003422 }
3423 else
3424#endif /* MBEDTLS_PKCS1_V15 */
3425#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003426 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003427 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003428 psa_rsa_oaep_set_padding_mode( alg, rsa );
3429 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3430 mbedtls_ctr_drbg_random,
3431 &global_data.ctr_drbg,
3432 MBEDTLS_RSA_PRIVATE,
3433 salt, salt_length,
3434 output_length,
3435 input,
3436 output,
3437 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003438 }
3439 else
3440#endif /* MBEDTLS_PKCS1_V21 */
3441 {
3442 return( PSA_ERROR_INVALID_ARGUMENT );
3443 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003444
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003445 return( mbedtls_to_psa_error( ret ) );
3446 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003447 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003448#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003449 {
3450 return( PSA_ERROR_NOT_SUPPORTED );
3451 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003452}
Gilles Peskine20035e32018-02-03 22:44:14 +01003453
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003454
3455
mohammad1603503973b2018-03-12 15:59:30 +02003456/****************************************************************/
3457/* Symmetric cryptography */
3458/****************************************************************/
3459
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003460/* Initialize the cipher operation structure. Once this function has been
3461 * called, psa_cipher_abort can run and will do the right thing. */
3462static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3463 psa_algorithm_t alg )
3464{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003465 if( ! PSA_ALG_IS_CIPHER( alg ) )
3466 {
3467 memset( operation, 0, sizeof( *operation ) );
3468 return( PSA_ERROR_INVALID_ARGUMENT );
3469 }
3470
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003471 operation->alg = alg;
3472 operation->key_set = 0;
3473 operation->iv_set = 0;
3474 operation->iv_required = 1;
3475 operation->iv_size = 0;
3476 operation->block_size = 0;
3477 mbedtls_cipher_init( &operation->ctx.cipher );
3478 return( PSA_SUCCESS );
3479}
3480
Gilles Peskinee553c652018-06-04 16:22:46 +02003481static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003482 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003483 psa_algorithm_t alg,
3484 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003485{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003486 int ret = 0;
3487 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003488 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003489 size_t key_bits;
3490 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003491 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3492 PSA_KEY_USAGE_ENCRYPT :
3493 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003494
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003495 /* A context must be freshly initialized before it can be set up. */
3496 if( operation->alg != 0 )
3497 {
3498 return( PSA_ERROR_BAD_STATE );
3499 }
3500
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003501 status = psa_cipher_init( operation, alg );
3502 if( status != PSA_SUCCESS )
3503 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003504
Gilles Peskine28f8f302019-07-24 13:30:31 +02003505 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003506 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003507 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003508 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003509
Gilles Peskine8e338702019-07-30 20:06:31 +02003510 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003511 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003512 {
3513 status = PSA_ERROR_NOT_SUPPORTED;
3514 goto exit;
3515 }
mohammad1603503973b2018-03-12 15:59:30 +02003516
mohammad1603503973b2018-03-12 15:59:30 +02003517 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003518 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003519 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003520
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003521#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003522 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003523 {
3524 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003525 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003526 memcpy( keys, slot->data.raw.data, 16 );
3527 memcpy( keys + 16, slot->data.raw.data, 8 );
3528 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3529 keys,
3530 192, cipher_operation );
3531 }
3532 else
3533#endif
3534 {
3535 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3536 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003537 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003538 }
Moran Peker41deec42018-04-04 15:43:05 +03003539 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003540 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003541
mohammad16038481e742018-03-18 13:57:31 +02003542#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003543 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003544 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003545 case PSA_ALG_CBC_NO_PADDING:
3546 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3547 MBEDTLS_PADDING_NONE );
3548 break;
3549 case PSA_ALG_CBC_PKCS7:
3550 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3551 MBEDTLS_PADDING_PKCS7 );
3552 break;
3553 default:
3554 /* The algorithm doesn't involve padding. */
3555 ret = 0;
3556 break;
3557 }
3558 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003559 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003560#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3561
mohammad1603503973b2018-03-12 15:59:30 +02003562 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003563 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003564 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003565 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003566 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003567 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003568 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003569#if defined(MBEDTLS_CHACHA20_C)
3570 else
3571 if( alg == PSA_ALG_CHACHA20 )
3572 operation->iv_size = 12;
3573#endif
mohammad1603503973b2018-03-12 15:59:30 +02003574
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003575exit:
3576 if( status == 0 )
3577 status = mbedtls_to_psa_error( ret );
3578 if( status != 0 )
3579 psa_cipher_abort( operation );
3580 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003581}
3582
Gilles Peskinefe119512018-07-08 21:39:34 +02003583psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003584 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003585 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003586{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003587 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003588}
3589
Gilles Peskinefe119512018-07-08 21:39:34 +02003590psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003591 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003592 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003593{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003594 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003595}
3596
Gilles Peskinefe119512018-07-08 21:39:34 +02003597psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003598 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003599 size_t iv_size,
3600 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003601{
itayzafrir534bd7c2018-08-02 13:56:32 +03003602 psa_status_t status;
3603 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003604 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003605 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003606 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003607 }
Moran Peker41deec42018-04-04 15:43:05 +03003608 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003609 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003610 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003611 goto exit;
3612 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003613 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3614 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003615 if( ret != 0 )
3616 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003617 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003618 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003619 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003620
mohammad16038481e742018-03-18 13:57:31 +02003621 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003622 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003623
Moran Peker395db872018-05-31 14:07:14 +03003624exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003625 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003626 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003627 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003628}
3629
Gilles Peskinefe119512018-07-08 21:39:34 +02003630psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003631 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003632 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003633{
itayzafrir534bd7c2018-08-02 13:56:32 +03003634 psa_status_t status;
3635 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003636 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003637 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003638 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003639 }
Moran Pekera28258c2018-05-29 16:25:04 +03003640 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003641 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003642 status = PSA_ERROR_INVALID_ARGUMENT;
3643 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003644 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003645 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3646 status = mbedtls_to_psa_error( ret );
3647exit:
3648 if( status == PSA_SUCCESS )
3649 operation->iv_set = 1;
3650 else
mohammad1603503973b2018-03-12 15:59:30 +02003651 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003652 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003653}
3654
Gilles Peskinee553c652018-06-04 16:22:46 +02003655psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3656 const uint8_t *input,
3657 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003658 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003659 size_t output_size,
3660 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003661{
itayzafrir534bd7c2018-08-02 13:56:32 +03003662 psa_status_t status;
3663 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003664 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003665
3666 if( operation->alg == 0 )
3667 {
3668 return( PSA_ERROR_BAD_STATE );
3669 }
3670
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003671 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003672 {
3673 /* Take the unprocessed partial block left over from previous
3674 * update calls, if any, plus the input to this call. Remove
3675 * the last partial block, if any. You get the data that will be
3676 * output in this call. */
3677 expected_output_size =
3678 ( operation->ctx.cipher.unprocessed_len + input_length )
3679 / operation->block_size * operation->block_size;
3680 }
3681 else
3682 {
3683 expected_output_size = input_length;
3684 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003685
Gilles Peskine89d789c2018-06-04 17:17:16 +02003686 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003687 {
3688 status = PSA_ERROR_BUFFER_TOO_SMALL;
3689 goto exit;
3690 }
mohammad160382759612018-03-12 18:16:40 +02003691
mohammad1603503973b2018-03-12 15:59:30 +02003692 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003693 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003694 status = mbedtls_to_psa_error( ret );
3695exit:
3696 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003697 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003698 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003699}
3700
Gilles Peskinee553c652018-06-04 16:22:46 +02003701psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3702 uint8_t *output,
3703 size_t output_size,
3704 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003705{
David Saadab4ecc272019-02-14 13:48:10 +02003706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003707 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003708 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003709
mohammad1603503973b2018-03-12 15:59:30 +02003710 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003711 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003712 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003713 }
3714 if( operation->iv_required && ! operation->iv_set )
3715 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003716 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003717 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003718
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003719 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003720 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3721 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003722 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003723 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003724 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003725 }
3726
Janos Follath315b51c2018-07-09 16:04:51 +01003727 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3728 temp_output_buffer,
3729 output_length );
3730 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003731 {
Janos Follath315b51c2018-07-09 16:04:51 +01003732 status = mbedtls_to_psa_error( cipher_ret );
3733 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003734 }
Janos Follath315b51c2018-07-09 16:04:51 +01003735
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003736 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003737 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003738 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003739 memcpy( output, temp_output_buffer, *output_length );
3740 else
3741 {
Janos Follath315b51c2018-07-09 16:04:51 +01003742 status = PSA_ERROR_BUFFER_TOO_SMALL;
3743 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003744 }
mohammad1603503973b2018-03-12 15:59:30 +02003745
Gilles Peskine3f108122018-12-07 18:14:53 +01003746 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003747 status = psa_cipher_abort( operation );
3748
3749 return( status );
3750
3751error:
3752
3753 *output_length = 0;
3754
Gilles Peskine3f108122018-12-07 18:14:53 +01003755 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003756 (void) psa_cipher_abort( operation );
3757
3758 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003759}
3760
Gilles Peskinee553c652018-06-04 16:22:46 +02003761psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3762{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003763 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003764 {
3765 /* The object has (apparently) been initialized but it is not
3766 * in use. It's ok to call abort on such an object, and there's
3767 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003768 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003769 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003770
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003771 /* Sanity check (shouldn't happen: operation->alg should
3772 * always have been initialized to a valid value). */
3773 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3774 return( PSA_ERROR_BAD_STATE );
3775
mohammad1603503973b2018-03-12 15:59:30 +02003776 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003777
Moran Peker41deec42018-04-04 15:43:05 +03003778 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003779 operation->key_set = 0;
3780 operation->iv_set = 0;
3781 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003782 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003783 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003784
Moran Peker395db872018-05-31 14:07:14 +03003785 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003786}
3787
Gilles Peskinea0655c32018-04-30 17:06:50 +02003788
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003789
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003790
mohammad16035955c982018-04-26 00:53:03 +03003791/****************************************************************/
3792/* AEAD */
3793/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003794
Gilles Peskineedf9a652018-08-17 18:11:56 +02003795typedef struct
3796{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003797 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003798 const mbedtls_cipher_info_t *cipher_info;
3799 union
3800 {
3801#if defined(MBEDTLS_CCM_C)
3802 mbedtls_ccm_context ccm;
3803#endif /* MBEDTLS_CCM_C */
3804#if defined(MBEDTLS_GCM_C)
3805 mbedtls_gcm_context gcm;
3806#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003807#if defined(MBEDTLS_CHACHAPOLY_C)
3808 mbedtls_chachapoly_context chachapoly;
3809#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003810 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003811 psa_algorithm_t core_alg;
3812 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003813 uint8_t tag_length;
3814} aead_operation_t;
3815
Gilles Peskine30a9e412019-01-14 18:36:12 +01003816static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003817{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003818 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819 {
3820#if defined(MBEDTLS_CCM_C)
3821 case PSA_ALG_CCM:
3822 mbedtls_ccm_free( &operation->ctx.ccm );
3823 break;
3824#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003825#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003826 case PSA_ALG_GCM:
3827 mbedtls_gcm_free( &operation->ctx.gcm );
3828 break;
3829#endif /* MBEDTLS_GCM_C */
3830 }
3831}
3832
3833static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003834 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003835 psa_key_usage_t usage,
3836 psa_algorithm_t alg )
3837{
3838 psa_status_t status;
3839 size_t key_bits;
3840 mbedtls_cipher_id_t cipher_id;
3841
Gilles Peskine28f8f302019-07-24 13:30:31 +02003842 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003843 if( status != PSA_SUCCESS )
3844 return( status );
3845
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003846 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003847
3848 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003849 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 &cipher_id );
3851 if( operation->cipher_info == NULL )
3852 return( PSA_ERROR_NOT_SUPPORTED );
3853
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003854 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003855 {
3856#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003857 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3858 operation->core_alg = PSA_ALG_CCM;
3859 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003860 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3861 * The call to mbedtls_ccm_encrypt_and_tag or
3862 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003863 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003864 return( PSA_ERROR_INVALID_ARGUMENT );
3865 mbedtls_ccm_init( &operation->ctx.ccm );
3866 status = mbedtls_to_psa_error(
3867 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3868 operation->slot->data.raw.data,
3869 (unsigned int) key_bits ) );
3870 if( status != 0 )
3871 goto cleanup;
3872 break;
3873#endif /* MBEDTLS_CCM_C */
3874
3875#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003876 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3877 operation->core_alg = PSA_ALG_GCM;
3878 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003879 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3880 * The call to mbedtls_gcm_crypt_and_tag or
3881 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003882 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003883 return( PSA_ERROR_INVALID_ARGUMENT );
3884 mbedtls_gcm_init( &operation->ctx.gcm );
3885 status = mbedtls_to_psa_error(
3886 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3887 operation->slot->data.raw.data,
3888 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003889 if( status != 0 )
3890 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003891 break;
3892#endif /* MBEDTLS_GCM_C */
3893
Gilles Peskine26869f22019-05-06 15:25:00 +02003894#if defined(MBEDTLS_CHACHAPOLY_C)
3895 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3896 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3897 operation->full_tag_length = 16;
3898 /* We only support the default tag length. */
3899 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3900 return( PSA_ERROR_NOT_SUPPORTED );
3901 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3902 status = mbedtls_to_psa_error(
3903 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3904 operation->slot->data.raw.data ) );
3905 if( status != 0 )
3906 goto cleanup;
3907 break;
3908#endif /* MBEDTLS_CHACHAPOLY_C */
3909
Gilles Peskineedf9a652018-08-17 18:11:56 +02003910 default:
3911 return( PSA_ERROR_NOT_SUPPORTED );
3912 }
3913
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003914 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3915 {
3916 status = PSA_ERROR_INVALID_ARGUMENT;
3917 goto cleanup;
3918 }
3919 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003920
Gilles Peskineedf9a652018-08-17 18:11:56 +02003921 return( PSA_SUCCESS );
3922
3923cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003924 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003925 return( status );
3926}
3927
Gilles Peskinec5487a82018-12-03 18:08:14 +01003928psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003929 psa_algorithm_t alg,
3930 const uint8_t *nonce,
3931 size_t nonce_length,
3932 const uint8_t *additional_data,
3933 size_t additional_data_length,
3934 const uint8_t *plaintext,
3935 size_t plaintext_length,
3936 uint8_t *ciphertext,
3937 size_t ciphertext_size,
3938 size_t *ciphertext_length )
3939{
mohammad16035955c982018-04-26 00:53:03 +03003940 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003941 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003942 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003943
mohammad1603f08a5502018-06-03 15:05:47 +03003944 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003945
Gilles Peskinec5487a82018-12-03 18:08:14 +01003946 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003947 if( status != PSA_SUCCESS )
3948 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003949
Gilles Peskineedf9a652018-08-17 18:11:56 +02003950 /* For all currently supported modes, the tag is at the end of the
3951 * ciphertext. */
3952 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3953 {
3954 status = PSA_ERROR_BUFFER_TOO_SMALL;
3955 goto exit;
3956 }
3957 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003958
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003959#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003960 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003961 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003962 status = mbedtls_to_psa_error(
3963 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3964 MBEDTLS_GCM_ENCRYPT,
3965 plaintext_length,
3966 nonce, nonce_length,
3967 additional_data, additional_data_length,
3968 plaintext, ciphertext,
3969 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003970 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003971 else
3972#endif /* MBEDTLS_GCM_C */
3973#if defined(MBEDTLS_CCM_C)
3974 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003975 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003976 status = mbedtls_to_psa_error(
3977 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3978 plaintext_length,
3979 nonce, nonce_length,
3980 additional_data,
3981 additional_data_length,
3982 plaintext, ciphertext,
3983 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003984 }
mohammad16035c8845f2018-05-09 05:40:09 -07003985 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003986#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003987#if defined(MBEDTLS_CHACHAPOLY_C)
3988 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3989 {
3990 if( nonce_length != 12 || operation.tag_length != 16 )
3991 {
3992 status = PSA_ERROR_NOT_SUPPORTED;
3993 goto exit;
3994 }
3995 status = mbedtls_to_psa_error(
3996 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3997 plaintext_length,
3998 nonce,
3999 additional_data,
4000 additional_data_length,
4001 plaintext,
4002 ciphertext,
4003 tag ) );
4004 }
4005 else
4006#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004007 {
mohammad1603554faad2018-06-03 15:07:38 +03004008 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004009 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004010
Gilles Peskineedf9a652018-08-17 18:11:56 +02004011 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4012 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004013
Gilles Peskineedf9a652018-08-17 18:11:56 +02004014exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004015 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004016 if( status == PSA_SUCCESS )
4017 *ciphertext_length = plaintext_length + operation.tag_length;
4018 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004019}
4020
Gilles Peskineee652a32018-06-01 19:23:52 +02004021/* Locate the tag in a ciphertext buffer containing the encrypted data
4022 * followed by the tag. Return the length of the part preceding the tag in
4023 * *plaintext_length. This is the size of the plaintext in modes where
4024 * the encrypted data has the same size as the plaintext, such as
4025 * CCM and GCM. */
4026static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4027 const uint8_t *ciphertext,
4028 size_t ciphertext_length,
4029 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004030 const uint8_t **p_tag )
4031{
4032 size_t payload_length;
4033 if( tag_length > ciphertext_length )
4034 return( PSA_ERROR_INVALID_ARGUMENT );
4035 payload_length = ciphertext_length - tag_length;
4036 if( payload_length > plaintext_size )
4037 return( PSA_ERROR_BUFFER_TOO_SMALL );
4038 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004039 return( PSA_SUCCESS );
4040}
4041
Gilles Peskinec5487a82018-12-03 18:08:14 +01004042psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004043 psa_algorithm_t alg,
4044 const uint8_t *nonce,
4045 size_t nonce_length,
4046 const uint8_t *additional_data,
4047 size_t additional_data_length,
4048 const uint8_t *ciphertext,
4049 size_t ciphertext_length,
4050 uint8_t *plaintext,
4051 size_t plaintext_size,
4052 size_t *plaintext_length )
4053{
mohammad16035955c982018-04-26 00:53:03 +03004054 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004055 aead_operation_t operation;
4056 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004057
Gilles Peskineee652a32018-06-01 19:23:52 +02004058 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004059
Gilles Peskinec5487a82018-12-03 18:08:14 +01004060 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004061 if( status != PSA_SUCCESS )
4062 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004063
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004064 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4065 ciphertext, ciphertext_length,
4066 plaintext_size, &tag );
4067 if( status != PSA_SUCCESS )
4068 goto exit;
4069
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004070#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004071 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004072 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073 status = mbedtls_to_psa_error(
4074 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4075 ciphertext_length - operation.tag_length,
4076 nonce, nonce_length,
4077 additional_data,
4078 additional_data_length,
4079 tag, operation.tag_length,
4080 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004081 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004082 else
4083#endif /* MBEDTLS_GCM_C */
4084#if defined(MBEDTLS_CCM_C)
4085 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004086 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 status = mbedtls_to_psa_error(
4088 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4089 ciphertext_length - operation.tag_length,
4090 nonce, nonce_length,
4091 additional_data,
4092 additional_data_length,
4093 ciphertext, plaintext,
4094 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004095 }
mohammad160339574652018-06-01 04:39:53 -07004096 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004097#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004098#if defined(MBEDTLS_CHACHAPOLY_C)
4099 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4100 {
4101 if( nonce_length != 12 || operation.tag_length != 16 )
4102 {
4103 status = PSA_ERROR_NOT_SUPPORTED;
4104 goto exit;
4105 }
4106 status = mbedtls_to_psa_error(
4107 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4108 ciphertext_length - operation.tag_length,
4109 nonce,
4110 additional_data,
4111 additional_data_length,
4112 tag,
4113 ciphertext,
4114 plaintext ) );
4115 }
4116 else
4117#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004118 {
mohammad1603554faad2018-06-03 15:07:38 +03004119 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004120 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004121
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 if( status != PSA_SUCCESS && plaintext_size != 0 )
4123 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004124
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004126 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 if( status == PSA_SUCCESS )
4128 *plaintext_length = ciphertext_length - operation.tag_length;
4129 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004130}
4131
Gilles Peskinea0655c32018-04-30 17:06:50 +02004132
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004133
Gilles Peskine20035e32018-02-03 22:44:14 +01004134/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004135/* Generators */
4136/****************************************************************/
4137
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004138#define HKDF_STATE_INIT 0 /* no input yet */
4139#define HKDF_STATE_STARTED 1 /* got salt */
4140#define HKDF_STATE_KEYED 2 /* got key */
4141#define HKDF_STATE_OUTPUT 3 /* output started */
4142
Gilles Peskinecbe66502019-05-16 16:59:18 +02004143static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004145{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004146 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4147 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004148 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004149 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004150}
4151
4152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004153psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004154{
4155 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004157 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004158 {
4159 /* The object has (apparently) been initialized but it is not
4160 * in use. It's ok to call abort on such an object, and there's
4161 * nothing to do. */
4162 }
4163 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004164#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004165 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004166 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004167 mbedtls_free( operation->ctx.hkdf.info );
4168 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004169 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004170 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004172 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004173 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004174#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004175 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004176 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4178 operation->ctx.tls12_prf.key_len );
4179 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004180 }
Hanno Becker580fba12018-11-13 20:50:45 +00004181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004183 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004184 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4185 operation->ctx.tls12_prf.Ai_with_seed_len );
4186 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004187 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004188#else
4189 if( operation->ctx.tls12_prf.seed != NULL )
4190 {
4191 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4192 operation->ctx.tls12_prf.seed_length );
4193 mbedtls_free( operation->ctx.tls12_prf.seed );
4194 }
4195
4196 if( operation->ctx.tls12_prf.label != NULL )
4197 {
4198 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4199 operation->ctx.tls12_prf.label_length );
4200 mbedtls_free( operation->ctx.tls12_prf.label );
4201 }
4202
4203 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4204
4205 /* We leave the fields Ai and output_block to be erased safely by the
4206 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004207#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004208 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004209 else
4210#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004211 {
4212 status = PSA_ERROR_BAD_STATE;
4213 }
Janos Follath083036a2019-06-11 10:22:26 +01004214 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004215 return( status );
4216}
4217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004219 size_t *capacity)
4220{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004221 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004222 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004223 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004224 return PSA_ERROR_BAD_STATE;
4225 }
4226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004227 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004228 return( PSA_SUCCESS );
4229}
4230
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004231psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004232 size_t capacity )
4233{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004234 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004235 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004236 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004238 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004239 return( PSA_SUCCESS );
4240}
4241
Gilles Peskinebef7f142018-07-12 17:22:21 +02004242#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004243/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004244 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004245static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004246 psa_algorithm_t hash_alg,
4247 uint8_t *output,
4248 size_t output_length )
4249{
4250 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4251 psa_status_t status;
4252
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004253 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4254 return( PSA_ERROR_BAD_STATE );
4255 hkdf->state = HKDF_STATE_OUTPUT;
4256
Gilles Peskinebef7f142018-07-12 17:22:21 +02004257 while( output_length != 0 )
4258 {
4259 /* Copy what remains of the current block */
4260 uint8_t n = hash_length - hkdf->offset_in_block;
4261 if( n > output_length )
4262 n = (uint8_t) output_length;
4263 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4264 output += n;
4265 output_length -= n;
4266 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004267 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004268 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004269 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004270 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004272 * object was corrupted or if this function is called directly
4273 * inside the library. */
4274 if( hkdf->block_number == 0xff )
4275 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004276
4277 /* We need a new block */
4278 ++hkdf->block_number;
4279 hkdf->offset_in_block = 0;
4280 status = psa_hmac_setup_internal( &hkdf->hmac,
4281 hkdf->prk, hash_length,
4282 hash_alg );
4283 if( status != PSA_SUCCESS )
4284 return( status );
4285 if( hkdf->block_number != 1 )
4286 {
4287 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4288 hkdf->output_block,
4289 hash_length );
4290 if( status != PSA_SUCCESS )
4291 return( status );
4292 }
4293 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4294 hkdf->info,
4295 hkdf->info_length );
4296 if( status != PSA_SUCCESS )
4297 return( status );
4298 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4299 &hkdf->block_number, 1 );
4300 if( status != PSA_SUCCESS )
4301 return( status );
4302 status = psa_hmac_finish_internal( &hkdf->hmac,
4303 hkdf->output_block,
4304 sizeof( hkdf->output_block ) );
4305 if( status != PSA_SUCCESS )
4306 return( status );
4307 }
4308
4309 return( PSA_SUCCESS );
4310}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004311
Janos Follath999f6482019-06-11 12:04:10 +01004312#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004313static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4314 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004315 psa_algorithm_t alg )
4316{
4317 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4318 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4319 psa_hmac_internal_data hmac;
4320 psa_status_t status, cleanup_status;
4321
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004322 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004323 size_t Ai_len;
4324
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004325 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004326 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004328 * object was corrupted or if this function is called directly
4329 * inside the library. */
4330 if( tls12_prf->block_number == 0xff )
4331 return( PSA_ERROR_BAD_STATE );
4332
4333 /* We need a new block */
4334 ++tls12_prf->block_number;
4335 tls12_prf->offset_in_block = 0;
4336
4337 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4338 *
4339 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4340 *
4341 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4342 * HMAC_hash(secret, A(2) + seed) +
4343 * HMAC_hash(secret, A(3) + seed) + ...
4344 *
4345 * A(0) = seed
4346 * A(i) = HMAC_hash( secret, A(i-1) )
4347 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004348 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004349 * `HMAC_hash(secret, A(i) + seed)` from which the output
4350 * is currently extracted as `output_block`, while
4351 * `A(i) + seed` is stored in `Ai_with_seed`.
4352 *
4353 * Generating a new block means recalculating `Ai_with_seed`
4354 * from the A(i)-part of it, and afterwards recalculating
4355 * `output_block`.
4356 *
4357 * A(0) is computed at setup time.
4358 *
4359 */
4360
4361 psa_hmac_init_internal( &hmac );
4362
4363 /* We must distinguish the calculation of A(1) from those
4364 * of A(2) and higher, because A(0)=seed has a different
4365 * length than the other A(i). */
4366 if( tls12_prf->block_number == 1 )
4367 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004368 Ai = tls12_prf->Ai_with_seed + hash_length;
4369 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370 }
4371 else
4372 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004373 Ai = tls12_prf->Ai_with_seed;
4374 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004375 }
4376
Hanno Becker3b339e22018-11-13 20:56:14 +00004377 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4378 status = psa_hmac_setup_internal( &hmac,
4379 tls12_prf->key,
4380 tls12_prf->key_len,
4381 hash_alg );
4382 if( status != PSA_SUCCESS )
4383 goto cleanup;
4384
4385 status = psa_hash_update( &hmac.hash_ctx,
4386 Ai, Ai_len );
4387 if( status != PSA_SUCCESS )
4388 goto cleanup;
4389
4390 status = psa_hmac_finish_internal( &hmac,
4391 tls12_prf->Ai_with_seed,
4392 hash_length );
4393 if( status != PSA_SUCCESS )
4394 goto cleanup;
4395
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004396 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4397 status = psa_hmac_setup_internal( &hmac,
4398 tls12_prf->key,
4399 tls12_prf->key_len,
4400 hash_alg );
4401 if( status != PSA_SUCCESS )
4402 goto cleanup;
4403
4404 status = psa_hash_update( &hmac.hash_ctx,
4405 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004406 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004407 if( status != PSA_SUCCESS )
4408 goto cleanup;
4409
4410 status = psa_hmac_finish_internal( &hmac,
4411 tls12_prf->output_block,
4412 hash_length );
4413 if( status != PSA_SUCCESS )
4414 goto cleanup;
4415
4416cleanup:
4417
4418 cleanup_status = psa_hmac_abort_internal( &hmac );
4419 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4420 status = cleanup_status;
4421
4422 return( status );
4423}
Janos Follath7742fee2019-06-17 12:58:10 +01004424#else
4425static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4426 psa_tls12_prf_key_derivation_t *tls12_prf,
4427 psa_algorithm_t alg )
4428{
4429 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4430 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004431 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4432 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004433
Janos Follath7742fee2019-06-17 12:58:10 +01004434 /* We can't be wanting more output after block 0xff, otherwise
4435 * the capacity check in psa_key_derivation_output_bytes() would have
4436 * prevented this call. It could happen only if the operation
4437 * object was corrupted or if this function is called directly
4438 * inside the library. */
4439 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004440 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004441
4442 /* We need a new block */
4443 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004444 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004445
4446 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4447 *
4448 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4449 *
4450 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4451 * HMAC_hash(secret, A(2) + seed) +
4452 * HMAC_hash(secret, A(3) + seed) + ...
4453 *
4454 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004455 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004456 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004457 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004458 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004459 * is currently extracted as `output_block` and where i is
4460 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004461 */
4462
Janos Follathea29bfb2019-06-19 12:21:20 +01004463 /* Save the hash context before using it, to preserve the hash state with
4464 * only the inner padding in it. We need this, because inner padding depends
4465 * on the key (secret in the RFC's terminology). */
4466 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4467 if( status != PSA_SUCCESS )
4468 goto cleanup;
4469
4470 /* Calculate A(i) where i = tls12_prf->block_number. */
4471 if( tls12_prf->block_number == 1 )
4472 {
4473 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4474 * the variable seed and in this instance means it in the context of the
4475 * P_hash function, where seed = label + seed.) */
4476 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4477 tls12_prf->label, tls12_prf->label_length );
4478 if( status != PSA_SUCCESS )
4479 goto cleanup;
4480 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4481 tls12_prf->seed, tls12_prf->seed_length );
4482 if( status != PSA_SUCCESS )
4483 goto cleanup;
4484 }
4485 else
4486 {
4487 /* A(i) = HMAC_hash(secret, A(i-1)) */
4488 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4489 tls12_prf->Ai, hash_length );
4490 if( status != PSA_SUCCESS )
4491 goto cleanup;
4492 }
4493
4494 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4495 tls12_prf->Ai, hash_length );
4496 if( status != PSA_SUCCESS )
4497 goto cleanup;
4498 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4499 if( status != PSA_SUCCESS )
4500 goto cleanup;
4501
4502 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4503 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4504 tls12_prf->Ai, hash_length );
4505 if( status != PSA_SUCCESS )
4506 goto cleanup;
4507 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4508 tls12_prf->label, tls12_prf->label_length );
4509 if( status != PSA_SUCCESS )
4510 goto cleanup;
4511 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4512 tls12_prf->seed, tls12_prf->seed_length );
4513 if( status != PSA_SUCCESS )
4514 goto cleanup;
4515 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4516 tls12_prf->output_block, hash_length );
4517 if( status != PSA_SUCCESS )
4518 goto cleanup;
4519 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4520 if( status != PSA_SUCCESS )
4521 goto cleanup;
4522
Janos Follath7742fee2019-06-17 12:58:10 +01004523
4524cleanup:
4525
Janos Follathea29bfb2019-06-19 12:21:20 +01004526 cleanup_status = psa_hash_abort( &backup );
4527 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4528 status = cleanup_status;
4529
4530 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004531}
Janos Follath999f6482019-06-11 12:04:10 +01004532#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004533
Janos Follath999f6482019-06-11 12:04:10 +01004534#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004536 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004537static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004538 psa_tls12_prf_key_derivation_t *tls12_prf,
4539 psa_algorithm_t alg,
4540 uint8_t *output,
4541 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004542{
4543 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4544 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4545 psa_status_t status;
4546
4547 while( output_length != 0 )
4548 {
4549 /* Copy what remains of the current block */
4550 uint8_t n = hash_length - tls12_prf->offset_in_block;
4551
4552 /* Check if we have fully processed the current block. */
4553 if( n == 0 )
4554 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004555 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004556 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004557 if( status != PSA_SUCCESS )
4558 return( status );
4559
4560 continue;
4561 }
4562
4563 if( n > output_length )
4564 n = (uint8_t) output_length;
4565 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4566 n );
4567 output += n;
4568 output_length -= n;
4569 tls12_prf->offset_in_block += n;
4570 }
4571
4572 return( PSA_SUCCESS );
4573}
Janos Follath844eb0e2019-06-19 12:10:49 +01004574#else
4575static psa_status_t psa_key_derivation_tls12_prf_read(
4576 psa_tls12_prf_key_derivation_t *tls12_prf,
4577 psa_algorithm_t alg,
4578 uint8_t *output,
4579 size_t output_length )
4580{
4581 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4582 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4583 psa_status_t status;
4584 uint8_t offset, length;
4585
4586 while( output_length != 0 )
4587 {
4588 /* Check if we have fully processed the current block. */
4589 if( tls12_prf->left_in_block == 0 )
4590 {
4591 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4592 alg );
4593 if( status != PSA_SUCCESS )
4594 return( status );
4595
4596 continue;
4597 }
4598
4599 if( tls12_prf->left_in_block > output_length )
4600 length = (uint8_t) output_length;
4601 else
4602 length = tls12_prf->left_in_block;
4603
4604 offset = hash_length - tls12_prf->left_in_block;
4605 memcpy( output, tls12_prf->output_block + offset, length );
4606 output += length;
4607 output_length -= length;
4608 tls12_prf->left_in_block -= length;
4609 }
4610
4611 return( PSA_SUCCESS );
4612}
Janos Follath999f6482019-06-11 12:04:10 +01004613#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004614#endif /* MBEDTLS_MD_C */
4615
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004616psa_status_t psa_key_derivation_output_bytes(
4617 psa_key_derivation_operation_t *operation,
4618 uint8_t *output,
4619 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004620{
4621 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004623
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004625 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004627 return PSA_ERROR_BAD_STATE;
4628 }
4629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004631 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004633 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004635 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004636 goto exit;
4637 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004639 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004641 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004642 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4643 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004645 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004646 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004647 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004649
Gilles Peskinebef7f142018-07-12 17:22:21 +02004650#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004651 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004652 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004653 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004655 output, output_length );
4656 }
Janos Follathadbec812019-06-14 11:05:39 +01004657 else
Janos Follathadbec812019-06-14 11:05:39 +01004658 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004659 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004660 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004661 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004662 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004663 output_length );
4664 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004665 else
4666#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667 {
4668 return( PSA_ERROR_BAD_STATE );
4669 }
4670
4671exit:
4672 if( status != PSA_SUCCESS )
4673 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004674 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 * This allows us to differentiate between exhausted operations and
4676 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4677 * operations. */
4678 psa_algorithm_t alg = operation->alg;
4679 psa_key_derivation_abort( operation );
4680 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004681 memset( output, '!', output_length );
4682 }
4683 return( status );
4684}
4685
Gilles Peskine08542d82018-07-19 17:05:42 +02004686#if defined(MBEDTLS_DES_C)
4687static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4688{
4689 if( data_size >= 8 )
4690 mbedtls_des_key_set_parity( data );
4691 if( data_size >= 16 )
4692 mbedtls_des_key_set_parity( data + 8 );
4693 if( data_size >= 24 )
4694 mbedtls_des_key_set_parity( data + 16 );
4695}
4696#endif /* MBEDTLS_DES_C */
4697
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004698static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004699 psa_key_slot_t *slot,
4700 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004702{
4703 uint8_t *data = NULL;
4704 size_t bytes = PSA_BITS_TO_BYTES( bits );
4705 psa_status_t status;
4706
Gilles Peskine8e338702019-07-30 20:06:31 +02004707 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004708 return( PSA_ERROR_INVALID_ARGUMENT );
4709 if( bits % 8 != 0 )
4710 return( PSA_ERROR_INVALID_ARGUMENT );
4711 data = mbedtls_calloc( 1, bytes );
4712 if( data == NULL )
4713 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004716 if( status != PSA_SUCCESS )
4717 goto exit;
4718#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004719 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004720 psa_des_set_key_parity( data, bytes );
4721#endif /* MBEDTLS_DES_C */
4722 status = psa_import_key_into_slot( slot, data, bytes );
4723
4724exit:
4725 mbedtls_free( data );
4726 return( status );
4727}
4728
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004729psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004731 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004732{
4733 psa_status_t status;
4734 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004735 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004736 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004737#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4738 if( driver != NULL )
4739 {
4740 /* Deriving a key in a secure element is not implemented yet. */
4741 status = PSA_ERROR_NOT_SUPPORTED;
4742 }
4743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004744 if( status == PSA_SUCCESS )
4745 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004746 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004747 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004749 }
4750 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004751 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004752 if( status != PSA_SUCCESS )
4753 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004754 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004755 *handle = 0;
4756 }
4757 return( status );
4758}
4759
Gilles Peskineeab56e42018-07-12 17:12:33 +02004760
4761
4762/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004763/* Key derivation */
4764/****************************************************************/
4765
Gilles Peskinea05219c2018-11-16 16:02:56 +01004766#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004767#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004769 * of the HKDF algorithm.
4770 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004771 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004772 * to potentially free embedded data structures and wipe confidential data.
4773 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004774static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004775 const uint8_t *secret,
4776 size_t secret_length,
4777 psa_algorithm_t hash_alg,
4778 const uint8_t *salt,
4779 size_t salt_length,
4780 const uint8_t *label,
4781 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004782{
4783 psa_status_t status;
4784 status = psa_hmac_setup_internal( &hkdf->hmac,
4785 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004786 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004787 if( status != PSA_SUCCESS )
4788 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004789 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004790 if( status != PSA_SUCCESS )
4791 return( status );
4792 status = psa_hmac_finish_internal( &hkdf->hmac,
4793 hkdf->prk,
4794 sizeof( hkdf->prk ) );
4795 if( status != PSA_SUCCESS )
4796 return( status );
4797 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4798 hkdf->block_number = 0;
4799 hkdf->info_length = label_length;
4800 if( label_length != 0 )
4801 {
4802 hkdf->info = mbedtls_calloc( 1, label_length );
4803 if( hkdf->info == NULL )
4804 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4805 memcpy( hkdf->info, label, label_length );
4806 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004807 hkdf->state = HKDF_STATE_KEYED;
4808 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004809 return( PSA_SUCCESS );
4810}
Janos Follath71a4c912019-06-11 09:14:47 +01004811#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004812#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004813
Gilles Peskinea05219c2018-11-16 16:02:56 +01004814#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004815#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004817 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004818 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004819 * to potentially free embedded data structures and wipe confidential data.
4820 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004821static psa_status_t psa_key_derivation_tls12_prf_setup(
4822 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004823 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004824 size_t key_len,
4825 psa_algorithm_t hash_alg,
4826 const uint8_t *salt,
4827 size_t salt_length,
4828 const uint8_t *label,
4829 size_t label_length )
4830{
4831 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004832 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4833 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004834
4835 tls12_prf->key = mbedtls_calloc( 1, key_len );
4836 if( tls12_prf->key == NULL )
4837 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4838 tls12_prf->key_len = key_len;
4839 memcpy( tls12_prf->key, key, key_len );
4840
Hanno Becker580fba12018-11-13 20:50:45 +00004841 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004842 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004843 if( overflow )
4844 return( PSA_ERROR_INVALID_ARGUMENT );
4845
4846 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4847 if( tls12_prf->Ai_with_seed == NULL )
4848 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4849 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4850
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004851 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4852 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004853 if( label_length != 0 )
4854 {
4855 memcpy( tls12_prf->Ai_with_seed + hash_length,
4856 label, label_length );
4857 }
4858
4859 if( salt_length != 0 )
4860 {
4861 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4862 salt, salt_length );
4863 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004864
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004865 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004866 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004867 tls12_prf->block_number = 0;
4868 tls12_prf->offset_in_block = hash_length;
4869
4870 return( PSA_SUCCESS );
4871}
Janos Follath71a4c912019-06-11 09:14:47 +01004872#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004873
Janos Follath71a4c912019-06-11 09:14:47 +01004874#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004876static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4877 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004878 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004879 size_t psk_len,
4880 psa_algorithm_t hash_alg,
4881 const uint8_t *salt,
4882 size_t salt_length,
4883 const uint8_t *label,
4884 size_t label_length )
4885{
4886 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004887 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004888
4889 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4890 return( PSA_ERROR_INVALID_ARGUMENT );
4891
4892 /* Quoting RFC 4279, Section 2:
4893 *
4894 * The premaster secret is formed as follows: if the PSK is N octets
4895 * long, concatenate a uint16 with the value N, N zero octets, a second
4896 * uint16 with the value N, and the PSK itself.
4897 */
4898
4899 pms[0] = ( psk_len >> 8 ) & 0xff;
4900 pms[1] = ( psk_len >> 0 ) & 0xff;
4901 memset( pms + 2, 0, psk_len );
4902 pms[2 + psk_len + 0] = pms[0];
4903 pms[2 + psk_len + 1] = pms[1];
4904 memcpy( pms + 4 + psk_len, psk, psk_len );
4905
Gilles Peskinecbe66502019-05-16 16:59:18 +02004906 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004907 pms, 4 + 2 * psk_len,
4908 hash_alg,
4909 salt, salt_length,
4910 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004911
Gilles Peskine3f108122018-12-07 18:14:53 +01004912 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004913 return( status );
4914}
Janos Follath71a4c912019-06-11 09:14:47 +01004915#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004916#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004917
Janos Follath71a4c912019-06-11 09:14:47 +01004918#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004919/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004920 * to potentially free embedded data structures and wipe confidential data.
4921 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004922static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004924 const uint8_t *secret, size_t secret_length,
4925 psa_algorithm_t alg,
4926 const uint8_t *salt, size_t salt_length,
4927 const uint8_t *label, size_t label_length,
4928 size_t capacity )
4929{
4930 psa_status_t status;
4931 size_t max_capacity;
4932
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933 /* Set operation->alg even on failure so that abort knows what to do. */
4934 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004935
4936#if defined(MBEDTLS_MD_C)
4937 if( PSA_ALG_IS_HKDF( alg ) )
4938 {
4939 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4940 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4941 if( hash_size == 0 )
4942 return( PSA_ERROR_NOT_SUPPORTED );
4943 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004944 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004945 secret, secret_length,
4946 hash_alg,
4947 salt, salt_length,
4948 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004949 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004950 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4951 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4952 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004953 {
4954 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4955 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4956
4957 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4958 if( hash_alg != PSA_ALG_SHA_256 &&
4959 hash_alg != PSA_ALG_SHA_384 )
4960 {
4961 return( PSA_ERROR_NOT_SUPPORTED );
4962 }
4963
4964 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004965
4966 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4967 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004969 secret, secret_length,
4970 hash_alg, salt, salt_length,
4971 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004972 }
4973 else
4974 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004975 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004977 secret, secret_length,
4978 hash_alg, salt, salt_length,
4979 label, label_length );
4980 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004981 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004982 else
4983#endif
4984 {
4985 return( PSA_ERROR_NOT_SUPPORTED );
4986 }
4987
4988 if( status != PSA_SUCCESS )
4989 return( status );
4990
4991 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004992 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004993 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004995 else
4996 return( PSA_ERROR_INVALID_ARGUMENT );
4997
4998 return( PSA_SUCCESS );
4999}
Janos Follath71a4c912019-06-11 09:14:47 +01005000#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005001
Janos Follath71a4c912019-06-11 09:14:47 +01005002#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005004 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005005 psa_algorithm_t alg,
5006 const uint8_t *salt,
5007 size_t salt_length,
5008 const uint8_t *label,
5009 size_t label_length,
5010 size_t capacity )
5011{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005012 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005013 psa_status_t status;
5014
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005016 return( PSA_ERROR_BAD_STATE );
5017
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005018 /* Make sure that alg is a key derivation algorithm. This prevents
5019 * key selection algorithms, which psa_key_derivation_internal
5020 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005021 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5022 return( PSA_ERROR_INVALID_ARGUMENT );
5023
Gilles Peskine28f8f302019-07-24 13:30:31 +02005024 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005025 if( status != PSA_SUCCESS )
5026 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005027
Gilles Peskine8e338702019-07-30 20:06:31 +02005028 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005029 return( PSA_ERROR_INVALID_ARGUMENT );
5030
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005032 slot->data.raw.data,
5033 slot->data.raw.bytes,
5034 alg,
5035 salt, salt_length,
5036 label, label_length,
5037 capacity );
5038 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005040 return( status );
5041}
Janos Follath71a4c912019-06-11 09:14:47 +01005042#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005043
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005047{
Janos Follath5fe19732019-06-20 15:09:30 +01005048 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5049 * initialisers for this union leave some bytes unspecified.) */
5050 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5051
Gilles Peskine969c5d62019-01-16 15:53:06 +01005052 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005053#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005054 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5055 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5056 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005057 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005058 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005059 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5060 if( hash_size == 0 )
5061 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005062 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5063 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005064 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065 {
5066 return( PSA_ERROR_NOT_SUPPORTED );
5067 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005069 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005070 }
5071#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005073 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005074}
5075
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005076psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005077 psa_algorithm_t alg )
5078{
5079 psa_status_t status;
5080
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005082 return( PSA_ERROR_BAD_STATE );
5083
Gilles Peskine6843c292019-01-18 16:44:49 +01005084 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5085 return( PSA_ERROR_INVALID_ARGUMENT );
5086 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005087 {
5088 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005089 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005090 }
5091 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5092 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005094 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005095 else
5096 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005097
5098 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005100 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005101}
5102
5103#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005104static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105 psa_algorithm_t hash_alg,
5106 psa_key_derivation_step_t step,
5107 const uint8_t *data,
5108 size_t data_length )
5109{
5110 psa_status_t status;
5111 switch( step )
5112 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005113 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005114 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005116 status = psa_hmac_setup_internal( &hkdf->hmac,
5117 data, data_length,
5118 hash_alg );
5119 if( status != PSA_SUCCESS )
5120 return( status );
5121 hkdf->state = HKDF_STATE_STARTED;
5122 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005123 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005124 /* If no salt was provided, use an empty salt. */
5125 if( hkdf->state == HKDF_STATE_INIT )
5126 {
5127 status = psa_hmac_setup_internal( &hkdf->hmac,
5128 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005129 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130 if( status != PSA_SUCCESS )
5131 return( status );
5132 hkdf->state = HKDF_STATE_STARTED;
5133 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005134 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005136 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5137 data, data_length );
5138 if( status != PSA_SUCCESS )
5139 return( status );
5140 status = psa_hmac_finish_internal( &hkdf->hmac,
5141 hkdf->prk,
5142 sizeof( hkdf->prk ) );
5143 if( status != PSA_SUCCESS )
5144 return( status );
5145 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5146 hkdf->block_number = 0;
5147 hkdf->state = HKDF_STATE_KEYED;
5148 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005149 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005150 if( hkdf->state == HKDF_STATE_OUTPUT )
5151 return( PSA_ERROR_BAD_STATE );
5152 if( hkdf->info_set )
5153 return( PSA_ERROR_BAD_STATE );
5154 hkdf->info_length = data_length;
5155 if( data_length != 0 )
5156 {
5157 hkdf->info = mbedtls_calloc( 1, data_length );
5158 if( hkdf->info == NULL )
5159 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5160 memcpy( hkdf->info, data, data_length );
5161 }
5162 hkdf->info_set = 1;
5163 return( PSA_SUCCESS );
5164 default:
5165 return( PSA_ERROR_INVALID_ARGUMENT );
5166 }
5167}
Janos Follathb03233e2019-06-11 15:30:30 +01005168
5169#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5170static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5171 psa_algorithm_t hash_alg,
5172 psa_key_derivation_step_t step,
5173 const uint8_t *data,
5174 size_t data_length )
5175{
5176 (void) prf;
5177 (void) hash_alg;
5178 (void) step;
5179 (void) data;
5180 (void) data_length;
5181
5182 return( PSA_ERROR_INVALID_ARGUMENT );
5183}
Janos Follath6660f0e2019-06-17 08:44:03 +01005184
5185static psa_status_t psa_tls12_prf_psk_to_ms_input(
5186 psa_tls12_prf_key_derivation_t *prf,
5187 psa_algorithm_t hash_alg,
5188 psa_key_derivation_step_t step,
5189 const uint8_t *data,
5190 size_t data_length )
5191{
5192 (void) prf;
5193 (void) hash_alg;
5194 (void) step;
5195 (void) data;
5196 (void) data_length;
5197
5198 return( PSA_ERROR_INVALID_ARGUMENT );
5199}
Janos Follathb03233e2019-06-11 15:30:30 +01005200#else
Janos Follathf08e2652019-06-13 09:05:41 +01005201static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5202 const uint8_t *data,
5203 size_t data_length )
5204{
5205 if( prf->state != TLS12_PRF_STATE_INIT )
5206 return( PSA_ERROR_BAD_STATE );
5207
Janos Follathd6dce9f2019-07-04 09:11:38 +01005208 if( data_length != 0 )
5209 {
5210 prf->seed = mbedtls_calloc( 1, data_length );
5211 if( prf->seed == NULL )
5212 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005213
Janos Follathd6dce9f2019-07-04 09:11:38 +01005214 memcpy( prf->seed, data, data_length );
5215 prf->seed_length = data_length;
5216 }
Janos Follathf08e2652019-06-13 09:05:41 +01005217
5218 prf->state = TLS12_PRF_STATE_SEED_SET;
5219
5220 return( PSA_SUCCESS );
5221}
5222
Janos Follath81550542019-06-13 14:26:34 +01005223static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5224 psa_algorithm_t hash_alg,
5225 const uint8_t *data,
5226 size_t data_length )
5227{
5228 psa_status_t status;
5229 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5230 return( PSA_ERROR_BAD_STATE );
5231
5232 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5233 if( status != PSA_SUCCESS )
5234 return( status );
5235
5236 prf->state = TLS12_PRF_STATE_KEY_SET;
5237
5238 return( PSA_SUCCESS );
5239}
5240
Janos Follath6660f0e2019-06-17 08:44:03 +01005241static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5242 psa_tls12_prf_key_derivation_t *prf,
5243 psa_algorithm_t hash_alg,
5244 const uint8_t *data,
5245 size_t data_length )
5246{
5247 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005248 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5249 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005250
5251 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5252 return( PSA_ERROR_INVALID_ARGUMENT );
5253
5254 /* Quoting RFC 4279, Section 2:
5255 *
5256 * The premaster secret is formed as follows: if the PSK is N octets
5257 * long, concatenate a uint16 with the value N, N zero octets, a second
5258 * uint16 with the value N, and the PSK itself.
5259 */
5260
Janos Follath40e13932019-06-26 13:22:29 +01005261 *cur++ = ( data_length >> 8 ) & 0xff;
5262 *cur++ = ( data_length >> 0 ) & 0xff;
5263 memset( cur, 0, data_length );
5264 cur += data_length;
5265 *cur++ = pms[0];
5266 *cur++ = pms[1];
5267 memcpy( cur, data, data_length );
5268 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005269
Janos Follath40e13932019-06-26 13:22:29 +01005270 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005271
5272 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5273 return( status );
5274}
5275
Janos Follath63028dd2019-06-13 09:15:47 +01005276static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5277 const uint8_t *data,
5278 size_t data_length )
5279{
5280 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5281 return( PSA_ERROR_BAD_STATE );
5282
Janos Follathd6dce9f2019-07-04 09:11:38 +01005283 if( data_length != 0 )
5284 {
5285 prf->label = mbedtls_calloc( 1, data_length );
5286 if( prf->label == NULL )
5287 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005288
Janos Follathd6dce9f2019-07-04 09:11:38 +01005289 memcpy( prf->label, data, data_length );
5290 prf->label_length = data_length;
5291 }
Janos Follath63028dd2019-06-13 09:15:47 +01005292
5293 prf->state = TLS12_PRF_STATE_LABEL_SET;
5294
5295 return( PSA_SUCCESS );
5296}
5297
Janos Follathb03233e2019-06-11 15:30:30 +01005298static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5299 psa_algorithm_t hash_alg,
5300 psa_key_derivation_step_t step,
5301 const uint8_t *data,
5302 size_t data_length )
5303{
Janos Follathb03233e2019-06-11 15:30:30 +01005304 switch( step )
5305 {
Janos Follathf08e2652019-06-13 09:05:41 +01005306 case PSA_KEY_DERIVATION_INPUT_SEED:
5307 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005308 case PSA_KEY_DERIVATION_INPUT_SECRET:
5309 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005310 case PSA_KEY_DERIVATION_INPUT_LABEL:
5311 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005312 default:
5313 return( PSA_ERROR_INVALID_ARGUMENT );
5314 }
5315}
Janos Follath6660f0e2019-06-17 08:44:03 +01005316
5317static psa_status_t psa_tls12_prf_psk_to_ms_input(
5318 psa_tls12_prf_key_derivation_t *prf,
5319 psa_algorithm_t hash_alg,
5320 psa_key_derivation_step_t step,
5321 const uint8_t *data,
5322 size_t data_length )
5323{
5324 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005325 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005326 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5327 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005328 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005329
5330 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5331}
Janos Follathb03233e2019-06-11 15:30:30 +01005332#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005333#endif /* MBEDTLS_MD_C */
5334
Janos Follathb80a94e2019-06-12 15:54:46 +01005335static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005337 psa_key_derivation_step_t step,
5338 const uint8_t *data,
5339 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005340{
5341 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005343
5344#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005345 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005346 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005348 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349 step, data, data_length );
5350 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005351 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005352#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005353#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005354 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005355 {
Janos Follathb03233e2019-06-11 15:30:30 +01005356 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5357 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5358 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005359 }
5360 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5361 {
5362 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5363 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5364 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365 }
5366 else
5367#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005368 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005370 return( PSA_ERROR_BAD_STATE );
5371 }
5372
5373 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005375 return( status );
5376}
5377
Janos Follath51f4a0f2019-06-14 11:35:55 +01005378psa_status_t psa_key_derivation_input_bytes(
5379 psa_key_derivation_operation_t *operation,
5380 psa_key_derivation_step_t step,
5381 const uint8_t *data,
5382 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005383{
Janos Follathc5621512019-06-14 11:27:57 +01005384 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5385 return( PSA_ERROR_INVALID_ARGUMENT );
5386
5387 return( psa_key_derivation_input_internal( operation, step,
5388 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005389}
5390
Janos Follath51f4a0f2019-06-14 11:35:55 +01005391psa_status_t psa_key_derivation_input_key(
5392 psa_key_derivation_operation_t *operation,
5393 psa_key_derivation_step_t step,
5394 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005395{
5396 psa_key_slot_t *slot;
5397 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005398 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005399 PSA_KEY_USAGE_DERIVE,
5400 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005401 if( status != PSA_SUCCESS )
5402 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005403 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005404 return( PSA_ERROR_INVALID_ARGUMENT );
5405 /* Don't allow a key to be used as an input that is usually public.
5406 * This is debatable. It's ok from a cryptographic perspective to
5407 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005408 * the material should be dedicated to a particular input step,
5409 * otherwise this may allow the key to be used in an unintended way
5410 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005411 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005412 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005413 return( psa_key_derivation_input_internal( operation,
5414 step,
5415 slot->data.raw.data,
5416 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005417}
5418
Gilles Peskineea0fb492018-07-12 17:17:20 +02005419
5420
5421/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005422/* Key agreement */
5423/****************************************************************/
5424
Gilles Peskinea05219c2018-11-16 16:02:56 +01005425#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005426static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5427 size_t peer_key_length,
5428 const mbedtls_ecp_keypair *our_key,
5429 uint8_t *shared_secret,
5430 size_t shared_secret_size,
5431 size_t *shared_secret_length )
5432{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005433 mbedtls_ecp_keypair *their_key = NULL;
5434 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005435 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005436 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005437
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005438 status = psa_import_ec_public_key(
5439 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5440 peer_key, peer_key_length,
5441 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005442 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005443 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005444
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005445 status = mbedtls_to_psa_error(
5446 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5447 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005448 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005449 status = mbedtls_to_psa_error(
5450 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5451 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005452 goto exit;
5453
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005454 status = mbedtls_to_psa_error(
5455 mbedtls_ecdh_calc_secret( &ecdh,
5456 shared_secret_length,
5457 shared_secret, shared_secret_size,
5458 mbedtls_ctr_drbg_random,
5459 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005460
5461exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005462 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005463 mbedtls_ecp_keypair_free( their_key );
5464 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005465 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005466}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005467#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005468
Gilles Peskine01d718c2018-09-18 12:01:02 +02005469#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5470
Gilles Peskine0216fe12019-04-11 21:23:21 +02005471static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5472 psa_key_slot_t *private_key,
5473 const uint8_t *peer_key,
5474 size_t peer_key_length,
5475 uint8_t *shared_secret,
5476 size_t shared_secret_size,
5477 size_t *shared_secret_length )
5478{
5479 switch( alg )
5480 {
5481#if defined(MBEDTLS_ECDH_C)
5482 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005483 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005484 return( PSA_ERROR_INVALID_ARGUMENT );
5485 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5486 private_key->data.ecp,
5487 shared_secret, shared_secret_size,
5488 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005489#endif /* MBEDTLS_ECDH_C */
5490 default:
5491 (void) private_key;
5492 (void) peer_key;
5493 (void) peer_key_length;
5494 (void) shared_secret;
5495 (void) shared_secret_size;
5496 (void) shared_secret_length;
5497 return( PSA_ERROR_NOT_SUPPORTED );
5498 }
5499}
5500
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005501/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005502 * to potentially free embedded data structures and wipe confidential data.
5503 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005505 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005506 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005507 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005508 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005509{
5510 psa_status_t status;
5511 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5512 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005513 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005514
5515 /* Step 1: run the secret agreement algorithm to generate the shared
5516 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005517 status = psa_key_agreement_raw_internal( ka_alg,
5518 private_key,
5519 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005520 shared_secret,
5521 sizeof( shared_secret ),
5522 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005523 if( status != PSA_SUCCESS )
5524 goto exit;
5525
5526 /* Step 2: set up the key derivation to generate key material from
5527 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005528 status = psa_key_derivation_input_internal( operation, step,
5529 shared_secret,
5530 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005531
Gilles Peskine01d718c2018-09-18 12:01:02 +02005532exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005533 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005534 return( status );
5535}
5536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005538 psa_key_derivation_step_t step,
5539 psa_key_handle_t private_key,
5540 const uint8_t *peer_key,
5541 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005542{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005543 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005544 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005546 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005547 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005548 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005549 if( status != PSA_SUCCESS )
5550 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005552 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005553 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005554 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005556 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005557}
5558
Gilles Peskinebe697d82019-05-16 18:00:41 +02005559psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5560 psa_key_handle_t private_key,
5561 const uint8_t *peer_key,
5562 size_t peer_key_length,
5563 uint8_t *output,
5564 size_t output_size,
5565 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005566{
5567 psa_key_slot_t *slot;
5568 psa_status_t status;
5569
5570 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5571 {
5572 status = PSA_ERROR_INVALID_ARGUMENT;
5573 goto exit;
5574 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005575 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005576 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005577 if( status != PSA_SUCCESS )
5578 goto exit;
5579
5580 status = psa_key_agreement_raw_internal( alg, slot,
5581 peer_key, peer_key_length,
5582 output, output_size,
5583 output_length );
5584
5585exit:
5586 if( status != PSA_SUCCESS )
5587 {
5588 /* If an error happens and is not handled properly, the output
5589 * may be used as a key to protect sensitive data. Arrange for such
5590 * a key to be random, which is likely to result in decryption or
5591 * verification errors. This is better than filling the buffer with
5592 * some constant data such as zeros, which would result in the data
5593 * being protected with a reproducible, easily knowable key.
5594 */
5595 psa_generate_random( output, output_size );
5596 *output_length = output_size;
5597 }
5598 return( status );
5599}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005600
5601
5602/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005603/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005604/****************************************************************/
5605
5606psa_status_t psa_generate_random( uint8_t *output,
5607 size_t output_size )
5608{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005609 int ret;
5610 GUARD_MODULE_INITIALIZED;
5611
5612 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005613 return( mbedtls_to_psa_error( ret ) );
5614}
5615
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005616#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5617#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005618
Gilles Peskine7228da22019-07-15 11:06:15 +02005619psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005620 size_t seed_size )
5621{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005622 if( global_data.initialized )
5623 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005624
5625 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5626 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5627 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5628 return( PSA_ERROR_INVALID_ARGUMENT );
5629
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005630 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005631}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005632#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005633
Gilles Peskinee56e8782019-04-26 17:34:02 +02005634#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5635static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5636 size_t domain_parameters_size,
5637 int *exponent )
5638{
5639 size_t i;
5640 uint32_t acc = 0;
5641
5642 if( domain_parameters_size == 0 )
5643 {
5644 *exponent = 65537;
5645 return( PSA_SUCCESS );
5646 }
5647
5648 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5649 * support values that fit in a 32-bit integer, which is larger than
5650 * int on just about every platform anyway. */
5651 if( domain_parameters_size > sizeof( acc ) )
5652 return( PSA_ERROR_NOT_SUPPORTED );
5653 for( i = 0; i < domain_parameters_size; i++ )
5654 acc = ( acc << 8 ) | domain_parameters[i];
5655 if( acc > INT_MAX )
5656 return( PSA_ERROR_NOT_SUPPORTED );
5657 *exponent = acc;
5658 return( PSA_SUCCESS );
5659}
5660#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5661
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005662static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005663 psa_key_slot_t *slot, size_t bits,
5664 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005665{
Gilles Peskine8e338702019-07-30 20:06:31 +02005666 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005667
Gilles Peskinee56e8782019-04-26 17:34:02 +02005668 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005669 return( PSA_ERROR_INVALID_ARGUMENT );
5670
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005671 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005672 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005673 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005674 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005675 if( status != PSA_SUCCESS )
5676 return( status );
5677 status = psa_generate_random( slot->data.raw.data,
5678 slot->data.raw.bytes );
5679 if( status != PSA_SUCCESS )
5680 {
5681 mbedtls_free( slot->data.raw.data );
5682 return( status );
5683 }
5684#if defined(MBEDTLS_DES_C)
5685 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005686 psa_des_set_key_parity( slot->data.raw.data,
5687 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005688#endif /* MBEDTLS_DES_C */
5689 }
5690 else
5691
5692#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005693 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005694 {
5695 mbedtls_rsa_context *rsa;
5696 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005697 int exponent;
5698 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005699 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5700 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005701 /* Accept only byte-aligned keys, for the same reasons as
5702 * in psa_import_rsa_key(). */
5703 if( bits % 8 != 0 )
5704 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005705 status = psa_read_rsa_exponent( domain_parameters,
5706 domain_parameters_size,
5707 &exponent );
5708 if( status != PSA_SUCCESS )
5709 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005710 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5711 if( rsa == NULL )
5712 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5713 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5714 ret = mbedtls_rsa_gen_key( rsa,
5715 mbedtls_ctr_drbg_random,
5716 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005717 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005718 exponent );
5719 if( ret != 0 )
5720 {
5721 mbedtls_rsa_free( rsa );
5722 mbedtls_free( rsa );
5723 return( mbedtls_to_psa_error( ret ) );
5724 }
5725 slot->data.rsa = rsa;
5726 }
5727 else
5728#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5729
5730#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005731 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005732 {
5733 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5734 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5735 const mbedtls_ecp_curve_info *curve_info =
5736 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5737 mbedtls_ecp_keypair *ecp;
5738 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005739 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005740 return( PSA_ERROR_NOT_SUPPORTED );
5741 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5742 return( PSA_ERROR_NOT_SUPPORTED );
5743 if( curve_info->bit_size != bits )
5744 return( PSA_ERROR_INVALID_ARGUMENT );
5745 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5746 if( ecp == NULL )
5747 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5748 mbedtls_ecp_keypair_init( ecp );
5749 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5750 mbedtls_ctr_drbg_random,
5751 &global_data.ctr_drbg );
5752 if( ret != 0 )
5753 {
5754 mbedtls_ecp_keypair_free( ecp );
5755 mbedtls_free( ecp );
5756 return( mbedtls_to_psa_error( ret ) );
5757 }
5758 slot->data.ecp = ecp;
5759 }
5760 else
5761#endif /* MBEDTLS_ECP_C */
5762
5763 return( PSA_ERROR_NOT_SUPPORTED );
5764
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005765 return( PSA_SUCCESS );
5766}
5767
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005768psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005769 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005770{
5771 psa_status_t status;
5772 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005773 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005774 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005775#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5776 if( driver != NULL )
5777 {
5778 /* Generating a key in a secure element is not implemented yet. */
5779 status = PSA_ERROR_NOT_SUPPORTED;
5780 }
5781#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005782 if( status == PSA_SUCCESS )
5783 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005784 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005785 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005786 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005787 }
5788 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005789 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005790 if( status != PSA_SUCCESS )
5791 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005792 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005793 *handle = 0;
5794 }
5795 return( status );
5796}
5797
5798
Gilles Peskine05d69892018-06-19 22:00:52 +02005799
5800/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005801/* Module setup */
5802/****************************************************************/
5803
Gilles Peskine5e769522018-11-20 21:59:56 +01005804psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5805 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5806 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5807{
5808 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5809 return( PSA_ERROR_BAD_STATE );
5810 global_data.entropy_init = entropy_init;
5811 global_data.entropy_free = entropy_free;
5812 return( PSA_SUCCESS );
5813}
5814
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815void mbedtls_psa_crypto_free( void )
5816{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005817 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005818 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5819 {
5820 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005821 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005822 }
5823 /* Wipe all remaining data, including configuration.
5824 * In particular, this sets all state indicator to the value
5825 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005826 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005827#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005828 /* Unregister all secure element drivers, so that we restart from
5829 * a pristine state. */
5830 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005831#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005832}
5833
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005834#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5835/** Recover a transaction that was interrupted by a power failure.
5836 *
5837 * This function is called during initialization, before psa_crypto_init()
5838 * returns. If this function returns a failure status, the initialization
5839 * fails.
5840 */
5841static psa_status_t psa_crypto_recover_transaction(
5842 const psa_crypto_transaction_t *transaction )
5843{
5844 switch( transaction->unknown.type )
5845 {
5846 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5847 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5848 /* TOnogrepDO - fall through to the failure case until this
5849 * is implemented */
5850 default:
5851 /* We found an unsupported transaction in the storage.
5852 * We don't know what state the storage is in. Give up. */
5853 return( PSA_ERROR_STORAGE_FAILURE );
5854 }
5855}
5856#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5857
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858psa_status_t psa_crypto_init( void )
5859{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005860 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005861 const unsigned char drbg_seed[] = "PSA";
5862
Gilles Peskinec6b69072018-11-20 21:42:52 +01005863 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005864 if( global_data.initialized != 0 )
5865 return( PSA_SUCCESS );
5866
Gilles Peskine5e769522018-11-20 21:59:56 +01005867 /* Set default configuration if
5868 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5869 if( global_data.entropy_init == NULL )
5870 global_data.entropy_init = mbedtls_entropy_init;
5871 if( global_data.entropy_free == NULL )
5872 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873
Gilles Peskinec6b69072018-11-20 21:42:52 +01005874 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005875 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005877 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005878 status = mbedtls_to_psa_error(
5879 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5880 mbedtls_entropy_func,
5881 &global_data.entropy,
5882 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5883 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005884 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005885 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005886
Gilles Peskine66fb1262018-12-10 16:29:04 +01005887 status = psa_initialize_key_slots( );
5888 if( status != PSA_SUCCESS )
5889 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005890
Gilles Peskine4b734222019-07-24 15:56:31 +02005891#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005892 status = psa_crypto_load_transaction( );
5893 if( status == PSA_SUCCESS )
5894 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005895 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5896 if( status != PSA_SUCCESS )
5897 goto exit;
5898 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005899 }
5900 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5901 {
5902 /* There's no transaction to complete. It's all good. */
5903 status = PSA_SUCCESS;
5904 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005905#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005906
Gilles Peskinec6b69072018-11-20 21:42:52 +01005907 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005908 global_data.initialized = 1;
5909
Gilles Peskinee59236f2018-01-27 23:32:46 +01005910exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005911 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005912 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005913 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005914}
5915
5916#endif /* MBEDTLS_PSA_CRYPTO_C */