blob: 258caad598b85851ca49273024fa418d09079241 [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 );
Darryl Green06fd18d2018-07-16 11:21:11 +0100900
901 /* Enforce that usage policy for the key slot contains all the flags
902 * required by the usage parameter. There is one exception: public
903 * keys can always be exported, so we treat public key objects as
904 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200907 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100908 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100909
910 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200911 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100912 return( PSA_ERROR_NOT_PERMITTED );
913
914 *p_slot = slot;
915 return( PSA_SUCCESS );
916}
Darryl Green940d72c2018-07-13 13:18:51 +0100917
Gilles Peskine28f8f302019-07-24 13:30:31 +0200918/** Retrieve a slot which must contain a transparent key.
919 *
920 * A transparent key is a key for which the key material is directly
921 * available, as opposed to a key in a secure element.
922 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200923 * This is a temporary function to use instead of psa_get_key_from_slot()
924 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200925 */
926#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
927static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
928 psa_key_slot_t **p_slot,
929 psa_key_usage_t usage,
930 psa_algorithm_t alg )
931{
932 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
933 if( status != PSA_SUCCESS )
934 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200935 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200936 {
937 *p_slot = NULL;
938 return( PSA_ERROR_NOT_SUPPORTED );
939 }
940 return( PSA_SUCCESS );
941}
942#else /* MBEDTLS_PSA_CRYPTO_SE_C */
943/* With no secure element support, all keys are transparent. */
944#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
945 psa_get_key_from_slot( handle, p_slot, usage, alg )
946#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
947
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100948/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100949static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000950{
Gilles Peskine73167e12019-07-12 23:44:37 +0200951#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
952 if( psa_key_slot_is_external( slot ) )
953 {
954 /* No key material to clean. */
955 }
956 else
957#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200958 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000959 {
960 /* No key material to clean. */
961 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200962 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000963 {
964 mbedtls_free( slot->data.raw.data );
965 }
966 else
967#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200968 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000969 {
970 mbedtls_rsa_free( slot->data.rsa );
971 mbedtls_free( slot->data.rsa );
972 }
973 else
974#endif /* defined(MBEDTLS_RSA_C) */
975#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200976 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000977 {
978 mbedtls_ecp_keypair_free( slot->data.ecp );
979 mbedtls_free( slot->data.ecp );
980 }
981 else
982#endif /* defined(MBEDTLS_ECP_C) */
983 {
984 /* Shouldn't happen: the key type is not any type that we
985 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200986 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000987 }
988
989 return( PSA_SUCCESS );
990}
991
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100992static void psa_abort_operations_using_key( psa_key_slot_t *slot )
993{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200994 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100995 (void) slot;
996}
997
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100998/** Completely wipe a slot in memory, including its policy.
999 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001000psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001{
1002 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001003 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001004 /* At this point, key material and other type-specific content has
1005 * been wiped. Clear remaining metadata. We can call memset and not
1006 * zeroize because the metadata is not particularly sensitive. */
1007 memset( slot, 0, sizeof( *slot ) );
1008 return( status );
1009}
1010
Gilles Peskinec5487a82018-12-03 18:08:14 +01001011psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001013 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001014 psa_status_t status = PSA_SUCCESS;
1015 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1017 psa_se_drv_table_entry_t *driver;
1018#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019
Gilles Peskinec5487a82018-12-03 18:08:14 +01001020 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001021 if( status != PSA_SUCCESS )
1022 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001023
1024#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001025 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001026 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001027 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001028 /* For a key in a secure element, we need to do three things:
1029 * remove the key file in internal storage, destroy the
1030 * key inside the secure element, and update the driver's
1031 * persistent data. Start a transaction that will encompass these
1032 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001034 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001035 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001036 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 status = psa_crypto_save_transaction( );
1038 if( status != PSA_SUCCESS )
1039 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001040 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001041 /* TOnogrepDO: destroy what can be destroyed anyway */
1042 return( status );
1043 }
1044
Gilles Peskine354f7672019-07-12 23:46:38 +02001045 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001047#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1048
Darryl Greend49a4992018-06-18 17:27:26 +01001049#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001050 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001051 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001052 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001053 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001054 }
1055#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001056
Gilles Peskinefc762652019-07-22 19:30:34 +02001057#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1058 if( driver != NULL )
1059 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001060 psa_status_t status2;
1061 status = psa_save_se_persistent_data( driver );
1062 status2 = psa_crypto_stop_transaction( );
1063 if( status == PSA_SUCCESS )
1064 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 if( status != PSA_SUCCESS )
1066 {
1067 /* TOnogrepDO: destroy what can be destroyed anyway */
1068 return( status );
1069 }
1070 }
1071#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1072
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001073 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001074 if( status != PSA_SUCCESS )
1075 return( status );
1076 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077}
1078
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001079void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1080{
Gilles Peskineb699f072019-04-26 16:06:02 +02001081 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001082 memset( attributes, 0, sizeof( *attributes ) );
1083}
1084
Gilles Peskineb699f072019-04-26 16:06:02 +02001085psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1086 psa_key_type_t type,
1087 const uint8_t *data,
1088 size_t data_length )
1089{
1090 uint8_t *copy = NULL;
1091
1092 if( data_length != 0 )
1093 {
1094 copy = mbedtls_calloc( 1, data_length );
1095 if( copy == NULL )
1096 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1097 memcpy( copy, data, data_length );
1098 }
1099 /* After this point, this function is guaranteed to succeed, so it
1100 * can start modifying `*attributes`. */
1101
1102 if( attributes->domain_parameters != NULL )
1103 {
1104 mbedtls_free( attributes->domain_parameters );
1105 attributes->domain_parameters = NULL;
1106 attributes->domain_parameters_size = 0;
1107 }
1108
1109 attributes->domain_parameters = copy;
1110 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001111 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001112 return( PSA_SUCCESS );
1113}
1114
1115psa_status_t psa_get_key_domain_parameters(
1116 const psa_key_attributes_t *attributes,
1117 uint8_t *data, size_t data_size, size_t *data_length )
1118{
1119 if( attributes->domain_parameters_size > data_size )
1120 return( PSA_ERROR_BUFFER_TOO_SMALL );
1121 *data_length = attributes->domain_parameters_size;
1122 if( attributes->domain_parameters_size != 0 )
1123 memcpy( data, attributes->domain_parameters,
1124 attributes->domain_parameters_size );
1125 return( PSA_SUCCESS );
1126}
1127
1128#if defined(MBEDTLS_RSA_C)
1129static psa_status_t psa_get_rsa_public_exponent(
1130 const mbedtls_rsa_context *rsa,
1131 psa_key_attributes_t *attributes )
1132{
1133 mbedtls_mpi mpi;
1134 int ret;
1135 uint8_t *buffer = NULL;
1136 size_t buflen;
1137 mbedtls_mpi_init( &mpi );
1138
1139 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1140 if( ret != 0 )
1141 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001142 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1143 {
1144 /* It's the default value, which is reported as an empty string,
1145 * so there's nothing to do. */
1146 goto exit;
1147 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001148
1149 buflen = mbedtls_mpi_size( &mpi );
1150 buffer = mbedtls_calloc( 1, buflen );
1151 if( buffer == NULL )
1152 {
1153 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1154 goto exit;
1155 }
1156 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1157 if( ret != 0 )
1158 goto exit;
1159 attributes->domain_parameters = buffer;
1160 attributes->domain_parameters_size = buflen;
1161
1162exit:
1163 mbedtls_mpi_free( &mpi );
1164 if( ret != 0 )
1165 mbedtls_free( buffer );
1166 return( mbedtls_to_psa_error( ret ) );
1167}
1168#endif /* MBEDTLS_RSA_C */
1169
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001170/** Retrieve all the publicly-accessible attributes of a key.
1171 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001172psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1173 psa_key_attributes_t *attributes )
1174{
1175 psa_key_slot_t *slot;
1176 psa_status_t status;
1177
1178 psa_reset_key_attributes( attributes );
1179
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001180 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181 if( status != PSA_SUCCESS )
1182 return( status );
1183
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001184 attributes->core = slot->attr;
Gilles Peskineb699f072019-04-26 16:06:02 +02001185
Gilles Peskine8e338702019-07-30 20:06:31 +02001186 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001187 {
1188#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001189 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001190 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1192 /* TOnogrepDO: reporting the public exponent for opaque keys
1193 * is not yet implemented. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001194 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001195 break;
1196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001197 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1198 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001199#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 default:
1201 /* Nothing else to do. */
1202 break;
1203 }
1204
1205 if( status != PSA_SUCCESS )
1206 psa_reset_key_attributes( attributes );
1207 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001208}
1209
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001210#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001211static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1212 unsigned char *buf, size_t size )
1213{
1214 int ret;
1215 unsigned char *c;
1216 size_t len = 0;
1217
1218 c = buf + size;
1219
1220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1221
1222 return( (int) len );
1223}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001224#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001225
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001226static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1227 uint8_t *data,
1228 size_t data_size,
1229 size_t *data_length,
1230 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231{
Gilles Peskine5d309672019-07-12 23:47:28 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1233 const psa_drv_se_t *drv;
1234 psa_drv_se_context_t *drv_context;
1235#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1236
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001237 *data_length = 0;
1238
Gilles Peskine8e338702019-07-30 20:06:31 +02001239 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001240 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001241
Gilles Peskine5d309672019-07-12 23:47:28 +02001242#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001243 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001244 {
1245 psa_drv_se_export_key_t method;
1246 if( drv->key_management == NULL )
1247 return( PSA_ERROR_NOT_SUPPORTED );
1248 method = ( export_public_key ?
1249 drv->key_management->p_export_public :
1250 drv->key_management->p_export );
1251 if( method == NULL )
1252 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001253 return( method( drv_context,
1254 slot->data.se.slot_number,
1255 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001256 }
1257#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1258
Gilles Peskine8e338702019-07-30 20:06:31 +02001259 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 {
1261 if( slot->data.raw.bytes > data_size )
1262 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001263 if( data_size != 0 )
1264 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001265 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001266 memset( data + slot->data.raw.bytes, 0,
1267 data_size - slot->data.raw.bytes );
1268 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269 *data_length = slot->data.raw.bytes;
1270 return( PSA_SUCCESS );
1271 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001272#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001273 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001274 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001275 psa_status_t status;
1276
Gilles Peskineb46bef22019-07-30 21:32:04 +02001277 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001278 if( bytes > data_size )
1279 return( PSA_ERROR_BUFFER_TOO_SMALL );
1280 status = mbedtls_to_psa_error(
1281 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1282 if( status != PSA_SUCCESS )
1283 return( status );
1284 memset( data + bytes, 0, data_size - bytes );
1285 *data_length = bytes;
1286 return( PSA_SUCCESS );
1287 }
1288#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289 else
Moran Peker17e36e12018-05-02 12:55:20 +03001290 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001291#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001292 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1293 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001294 {
Moran Pekera998bc62018-04-16 18:16:20 +03001295 mbedtls_pk_context pk;
1296 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001297 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001298 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001299#if defined(MBEDTLS_RSA_C)
1300 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001301 pk.pk_info = &mbedtls_rsa_info;
1302 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001303#else
1304 return( PSA_ERROR_NOT_SUPPORTED );
1305#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001306 }
1307 else
1308 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001309#if defined(MBEDTLS_ECP_C)
1310 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001311 pk.pk_info = &mbedtls_eckey_info;
1312 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001313#else
1314 return( PSA_ERROR_NOT_SUPPORTED );
1315#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001316 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001317 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001318 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001319 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001320 }
Moran Peker17e36e12018-05-02 12:55:20 +03001321 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001322 {
Moran Peker17e36e12018-05-02 12:55:20 +03001323 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001324 }
Moran Peker60364322018-04-29 11:34:58 +03001325 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001326 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001327 /* If data_size is 0 then data may be NULL and then the
1328 * call to memset would have undefined behavior. */
1329 if( data_size != 0 )
1330 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001331 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001332 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001333 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1334 * Move the data to the beginning and erase remaining data
1335 * at the original location. */
1336 if( 2 * (size_t) ret <= data_size )
1337 {
1338 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001339 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001340 }
1341 else if( (size_t) ret < data_size )
1342 {
1343 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001344 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001345 }
Moran Pekera998bc62018-04-16 18:16:20 +03001346 *data_length = ret;
1347 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001348 }
1349 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001350#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001351 {
1352 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001353 it is valid for a special-purpose implementation to omit
1354 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001355 return( PSA_ERROR_NOT_SUPPORTED );
1356 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357 }
1358}
1359
Gilles Peskinec5487a82018-12-03 18:08:14 +01001360psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001361 uint8_t *data,
1362 size_t data_size,
1363 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001364{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001365 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001366 psa_status_t status;
1367
1368 /* Set the key to empty now, so that even when there are errors, we always
1369 * set data_length to a value between 0 and data_size. On error, setting
1370 * the key to empty is a good choice because an empty key representation is
1371 * unlikely to be accepted anywhere. */
1372 *data_length = 0;
1373
1374 /* Export requires the EXPORT flag. There is an exception for public keys,
1375 * which don't require any flag, but psa_get_key_from_slot takes
1376 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001377 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001378 if( status != PSA_SUCCESS )
1379 return( status );
1380 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001381 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001383
Gilles Peskinec5487a82018-12-03 18:08:14 +01001384psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001385 uint8_t *data,
1386 size_t data_size,
1387 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001388{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001389 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001390 psa_status_t status;
1391
1392 /* Set the key to empty now, so that even when there are errors, we always
1393 * set data_length to a value between 0 and data_size. On error, setting
1394 * the key to empty is a good choice because an empty key representation is
1395 * unlikely to be accepted anywhere. */
1396 *data_length = 0;
1397
1398 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001399 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001400 if( status != PSA_SUCCESS )
1401 return( status );
1402 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001403 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001404}
1405
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001406static psa_status_t psa_check_key_slot_policy(
1407 const psa_key_slot_t *slot )
Gilles Peskine4747d192019-04-17 15:05:45 +02001408{
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001409 const psa_key_policy_t *policy = &slot->attr.policy;
Gilles Peskine4747d192019-04-17 15:05:45 +02001410 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001411 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001412 PSA_KEY_USAGE_ENCRYPT |
1413 PSA_KEY_USAGE_DECRYPT |
1414 PSA_KEY_USAGE_SIGN |
1415 PSA_KEY_USAGE_VERIFY |
1416 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1417 return( PSA_ERROR_INVALID_ARGUMENT );
1418
Gilles Peskine4747d192019-04-17 15:05:45 +02001419 return( PSA_SUCCESS );
1420}
1421
1422/** Prepare a key slot to receive key material.
1423 *
1424 * This function allocates a key slot and sets its metadata.
1425 *
1426 * If this function fails, call psa_fail_key_creation().
1427 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001428 * This function is intended to be used as follows:
1429 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1430 * it with the specified attributes, and assign it a handle.
1431 * -# Populate the slot with the key material.
1432 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1433 * In case of failure at any step, stop the sequence and call
1434 * psa_fail_key_creation().
1435 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001436 * \param[in] attributes Key attributes for the new key.
1437 * \param[out] handle On success, a handle for the allocated slot.
1438 * \param[out] p_slot On success, a pointer to the prepared slot.
1439 * \param[out] p_drv On any return, the driver for the key, if any.
1440 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001441 *
1442 * \retval #PSA_SUCCESS
1443 * The key slot is ready to receive key material.
1444 * \return If this function fails, the key slot is an invalid state.
1445 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001446 */
1447static psa_status_t psa_start_key_creation(
1448 const psa_key_attributes_t *attributes,
1449 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001450 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001451 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001452{
1453 psa_status_t status;
1454 psa_key_slot_t *slot;
1455
Gilles Peskine011e4282019-06-26 18:34:38 +02001456 *p_drv = NULL;
1457
Gilles Peskine267c6562019-05-27 19:01:54 +02001458 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001459 if( status != PSA_SUCCESS )
1460 return( status );
1461 slot = *p_slot;
1462
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001463 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001464 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001465 status = psa_validate_persistent_key_parameters( attributes->core.lifetime,
1466 attributes->core.id,
Gilles Peskine011e4282019-06-26 18:34:38 +02001467 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001468 if( status != PSA_SUCCESS )
1469 return( status );
Gilles Peskined167b942019-04-19 18:19:40 +02001470 }
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001471
Gilles Peskinec744d992019-07-30 17:26:54 +02001472 /* Refuse to create overly large keys.
1473 * Note that this doesn't trigger on import if the attributes don't
1474 * explicitly specify a size (so psa_get_key_bits returns 0), so
1475 * psa_import_key() needs its own checks. */
1476 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1477 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001478 /* We're storing the declared bit-size of the key. It's up to each
1479 * creation mechanism to verify that this information is correct.
1480 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001481 * an input (generate, device) but not for those where the bit-size
1482 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001483
1484 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001485
Gilles Peskine3825e142019-07-31 15:54:33 +02001486 status = psa_check_key_slot_policy( slot );
1487 if( status != PSA_SUCCESS )
1488 return( status );
1489
Gilles Peskinecbaff462019-07-12 23:46:04 +02001490#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001491 /* For a key in a secure element, we need to do three things:
1492 * create the key file in internal storage, create the
1493 * key inside the secure element, and update the driver's
1494 * persistent data. Start a transaction that will encompass these
1495 * three actions. */
1496 /* The first thing to do is to find a slot number for the new key.
1497 * We save the slot number in persistent storage as part of the
1498 * transaction data. It will be needed to recover if the power
1499 * fails during the key creation process, to clean up on the secure
1500 * element side after restarting. Obtaining a slot number from the
1501 * secure element driver updates its persistent state, but we do not yet
1502 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001503 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001504 if( *p_drv != NULL )
1505 {
1506 status = psa_find_se_slot_for_key( attributes, *p_drv,
1507 &slot->data.se.slot_number );
1508 if( status != PSA_SUCCESS )
1509 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001510 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001511 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001512 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001513 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001514 status = psa_crypto_save_transaction( );
1515 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001516 {
1517 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001518 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001519 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001520 }
1521#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1522
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 return( status );
1524}
1525
1526/** Finalize the creation of a key once its key material has been set.
1527 *
1528 * This entails writing the key to persistent storage.
1529 *
1530 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001531 * See the documentation of psa_start_key_creation() for the intended use
1532 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001533 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001534 * \param[in,out] slot Pointer to the slot with key material.
1535 * \param[in] driver The secure element driver for the key,
1536 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The key was successfully created. The handle is now valid.
1540 * \return If this function fails, the key slot is an invalid state.
1541 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001543static psa_status_t psa_finish_key_creation(
1544 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001545 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001546{
1547 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001548 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001549 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001550
1551#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001552 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001553 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001554#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1555 if( driver != NULL )
1556 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001557 psa_se_key_data_storage_t data;
1558#if defined(static_assert)
1559 static_assert( sizeof( slot->data.se.slot_number ) ==
1560 sizeof( data.slot_number ),
1561 "Slot number size does not match psa_se_key_data_storage_t" );
1562 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1563 "Bit-size size does not match psa_se_key_data_storage_t" );
1564#endif
1565 memcpy( &data.slot_number, &slot->data.se.slot_number,
1566 sizeof( slot->data.se.slot_number ) );
1567 memcpy( &data.bits, &slot->attr.bits,
1568 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001569 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001570 (uint8_t*) &data,
1571 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001572 }
1573 else
1574#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1575 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001576 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001577 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001578 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001579 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1580 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001581 if( buffer == NULL && buffer_size != 0 )
1582 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1583 status = psa_internal_export_key( slot,
1584 buffer, buffer_size, &length,
1585 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001586 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001587 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001588 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001589
Gilles Peskine1df83d42019-07-23 16:13:14 +02001590 if( buffer_size != 0 )
1591 mbedtls_platform_zeroize( buffer, buffer_size );
1592 mbedtls_free( buffer );
1593 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001594 }
1595#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1596
Gilles Peskinecbaff462019-07-12 23:46:04 +02001597#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1598 if( driver != NULL )
1599 {
1600 status = psa_save_se_persistent_data( driver );
1601 if( status != PSA_SUCCESS )
1602 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001603 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001604 return( status );
1605 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001606 status = psa_crypto_stop_transaction( );
1607 if( status != PSA_SUCCESS )
1608 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001609 }
1610#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1611
Gilles Peskine4747d192019-04-17 15:05:45 +02001612 return( status );
1613}
1614
1615/** Abort the creation of a key.
1616 *
1617 * You may call this function after calling psa_start_key_creation(),
1618 * or after psa_finish_key_creation() fails. In other circumstances, this
1619 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 * See the documentation of psa_start_key_creation() for the intended use
1621 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001622 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001623 * \param[in,out] slot Pointer to the slot with key material.
1624 * \param[in] driver The secure element driver for the key,
1625 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001626 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001627static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001628 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001629{
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 (void) driver;
1631
Gilles Peskine4747d192019-04-17 15:05:45 +02001632 if( slot == NULL )
1633 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001634
1635#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1636 /* TOnogrepDO: If the key has already been created in the secure
1637 * element, and the failure happened later (when saving metadata
1638 * to internal storage), we need to destroy the key in the secure
1639 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001640
1641 /* Abort the ongoing transaction if any. We already did what it
1642 * takes to undo any partial creation. All that's left is to update
1643 * the transaction data itself. */
1644 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001645#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1646
Gilles Peskine4747d192019-04-17 15:05:45 +02001647 psa_wipe_key_slot( slot );
1648}
1649
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001650static psa_status_t psa_check_key_slot_attributes(
1651 const psa_key_slot_t *slot,
1652 const psa_key_attributes_t *attributes )
1653{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001654 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001655 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001656 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001657 return( PSA_ERROR_INVALID_ARGUMENT );
1658 }
1659
1660 if( attributes->domain_parameters_size != 0 )
1661 {
1662#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001663 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001664 {
1665 mbedtls_mpi actual, required;
1666 int ret;
1667 mbedtls_mpi_init( &actual );
1668 mbedtls_mpi_init( &required );
1669 ret = mbedtls_rsa_export( slot->data.rsa,
1670 NULL, NULL, NULL, NULL, &actual );
1671 if( ret != 0 )
1672 goto rsa_exit;
1673 ret = mbedtls_mpi_read_binary( &required,
1674 attributes->domain_parameters,
1675 attributes->domain_parameters_size );
1676 if( ret != 0 )
1677 goto rsa_exit;
1678 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1679 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1680 rsa_exit:
1681 mbedtls_mpi_free( &actual );
1682 mbedtls_mpi_free( &required );
1683 if( ret != 0)
1684 return( mbedtls_to_psa_error( ret ) );
1685 }
1686 else
1687#endif
1688 {
1689 return( PSA_ERROR_INVALID_ARGUMENT );
1690 }
1691 }
1692
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001693 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001694 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001695 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001696 return( PSA_ERROR_INVALID_ARGUMENT );
1697 }
1698
1699 return( PSA_SUCCESS );
1700}
1701
Gilles Peskine4747d192019-04-17 15:05:45 +02001702psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001704 size_t data_length,
1705 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001706{
1707 psa_status_t status;
1708 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001709 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001710
Gilles Peskine011e4282019-06-26 18:34:38 +02001711 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001712 if( status != PSA_SUCCESS )
1713 goto exit;
1714
Gilles Peskine5d309672019-07-12 23:47:28 +02001715#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1716 if( driver != NULL )
1717 {
1718 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001719 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001720 if( drv->key_management == NULL ||
1721 drv->key_management->p_import == NULL )
1722 {
1723 status = PSA_ERROR_NOT_SUPPORTED;
1724 goto exit;
1725 }
1726 status = drv->key_management->p_import(
1727 psa_get_se_driver_context( driver ),
1728 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001729 slot->attr.lifetime, slot->attr.type,
1730 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001731 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001732 &bits );
1733 if( status != PSA_SUCCESS )
1734 goto exit;
1735 if( bits > PSA_MAX_KEY_BITS )
1736 {
1737 status = PSA_ERROR_NOT_SUPPORTED;
1738 goto exit;
1739 }
1740 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001741 }
1742 else
1743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1744 {
1745 status = psa_import_key_into_slot( slot, data, data_length );
1746 if( status != PSA_SUCCESS )
1747 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001748 }
Gilles Peskine18017402019-07-24 20:25:59 +02001749 status = psa_check_key_slot_attributes( slot, attributes );
1750 if( status != PSA_SUCCESS )
1751 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001752
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001754exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 if( status != PSA_SUCCESS )
1756 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001757 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 *handle = 0;
1759 }
1760 return( status );
1761}
1762
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001763static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001764 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001765{
1766 psa_status_t status;
1767 uint8_t *buffer = NULL;
1768 size_t buffer_size = 0;
1769 size_t length;
1770
Gilles Peskine8e338702019-07-30 20:06:31 +02001771 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001772 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001773 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001774 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001775 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001776 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1777 if( status != PSA_SUCCESS )
1778 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001779 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001780 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001781
1782exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001783 if( buffer_size != 0 )
1784 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001785 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001786 return( status );
1787}
1788
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001789psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1790 const psa_key_attributes_t *specified_attributes,
1791 psa_key_handle_t *target_handle )
1792{
1793 psa_status_t status;
1794 psa_key_slot_t *source_slot = NULL;
1795 psa_key_slot_t *target_slot = NULL;
1796 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001797 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001798
Gilles Peskine28f8f302019-07-24 13:30:31 +02001799 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001800 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001801 if( status != PSA_SUCCESS )
1802 goto exit;
1803
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1805 if( status != PSA_SUCCESS )
1806 goto exit;
1807
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001808 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001809 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001810 if( status != PSA_SUCCESS )
1811 goto exit;
1812
1813 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001814 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001815 if( status != PSA_SUCCESS )
1816 goto exit;
1817
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1819 if( driver != NULL )
1820 {
1821 /* Copying to a secure element is not implemented yet. */
1822 status = PSA_ERROR_NOT_SUPPORTED;
1823 goto exit;
1824 }
1825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1826
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001827 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 if( status != PSA_SUCCESS )
1829 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001830
Gilles Peskine011e4282019-06-26 18:34:38 +02001831 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001832exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001833 if( status != PSA_SUCCESS )
1834 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001835 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001836 *target_handle = 0;
1837 }
1838 return( status );
1839}
1840
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001841
1842
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001843/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001844/* Message digests */
1845/****************************************************************/
1846
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001847static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001848{
1849 switch( alg )
1850 {
1851#if defined(MBEDTLS_MD2_C)
1852 case PSA_ALG_MD2:
1853 return( &mbedtls_md2_info );
1854#endif
1855#if defined(MBEDTLS_MD4_C)
1856 case PSA_ALG_MD4:
1857 return( &mbedtls_md4_info );
1858#endif
1859#if defined(MBEDTLS_MD5_C)
1860 case PSA_ALG_MD5:
1861 return( &mbedtls_md5_info );
1862#endif
1863#if defined(MBEDTLS_RIPEMD160_C)
1864 case PSA_ALG_RIPEMD160:
1865 return( &mbedtls_ripemd160_info );
1866#endif
1867#if defined(MBEDTLS_SHA1_C)
1868 case PSA_ALG_SHA_1:
1869 return( &mbedtls_sha1_info );
1870#endif
1871#if defined(MBEDTLS_SHA256_C)
1872 case PSA_ALG_SHA_224:
1873 return( &mbedtls_sha224_info );
1874 case PSA_ALG_SHA_256:
1875 return( &mbedtls_sha256_info );
1876#endif
1877#if defined(MBEDTLS_SHA512_C)
1878 case PSA_ALG_SHA_384:
1879 return( &mbedtls_sha384_info );
1880 case PSA_ALG_SHA_512:
1881 return( &mbedtls_sha512_info );
1882#endif
1883 default:
1884 return( NULL );
1885 }
1886}
1887
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001888psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1889{
1890 switch( operation->alg )
1891 {
Gilles Peskine81736312018-06-26 15:04:31 +02001892 case 0:
1893 /* The object has (apparently) been initialized but it is not
1894 * in use. It's ok to call abort on such an object, and there's
1895 * nothing to do. */
1896 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001897#if defined(MBEDTLS_MD2_C)
1898 case PSA_ALG_MD2:
1899 mbedtls_md2_free( &operation->ctx.md2 );
1900 break;
1901#endif
1902#if defined(MBEDTLS_MD4_C)
1903 case PSA_ALG_MD4:
1904 mbedtls_md4_free( &operation->ctx.md4 );
1905 break;
1906#endif
1907#if defined(MBEDTLS_MD5_C)
1908 case PSA_ALG_MD5:
1909 mbedtls_md5_free( &operation->ctx.md5 );
1910 break;
1911#endif
1912#if defined(MBEDTLS_RIPEMD160_C)
1913 case PSA_ALG_RIPEMD160:
1914 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1915 break;
1916#endif
1917#if defined(MBEDTLS_SHA1_C)
1918 case PSA_ALG_SHA_1:
1919 mbedtls_sha1_free( &operation->ctx.sha1 );
1920 break;
1921#endif
1922#if defined(MBEDTLS_SHA256_C)
1923 case PSA_ALG_SHA_224:
1924 case PSA_ALG_SHA_256:
1925 mbedtls_sha256_free( &operation->ctx.sha256 );
1926 break;
1927#endif
1928#if defined(MBEDTLS_SHA512_C)
1929 case PSA_ALG_SHA_384:
1930 case PSA_ALG_SHA_512:
1931 mbedtls_sha512_free( &operation->ctx.sha512 );
1932 break;
1933#endif
1934 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001935 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001936 }
1937 operation->alg = 0;
1938 return( PSA_SUCCESS );
1939}
1940
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001941psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001942 psa_algorithm_t alg )
1943{
1944 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001945
1946 /* A context must be freshly initialized before it can be set up. */
1947 if( operation->alg != 0 )
1948 {
1949 return( PSA_ERROR_BAD_STATE );
1950 }
1951
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001952 switch( alg )
1953 {
1954#if defined(MBEDTLS_MD2_C)
1955 case PSA_ALG_MD2:
1956 mbedtls_md2_init( &operation->ctx.md2 );
1957 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1958 break;
1959#endif
1960#if defined(MBEDTLS_MD4_C)
1961 case PSA_ALG_MD4:
1962 mbedtls_md4_init( &operation->ctx.md4 );
1963 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1964 break;
1965#endif
1966#if defined(MBEDTLS_MD5_C)
1967 case PSA_ALG_MD5:
1968 mbedtls_md5_init( &operation->ctx.md5 );
1969 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1970 break;
1971#endif
1972#if defined(MBEDTLS_RIPEMD160_C)
1973 case PSA_ALG_RIPEMD160:
1974 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1975 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1976 break;
1977#endif
1978#if defined(MBEDTLS_SHA1_C)
1979 case PSA_ALG_SHA_1:
1980 mbedtls_sha1_init( &operation->ctx.sha1 );
1981 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1982 break;
1983#endif
1984#if defined(MBEDTLS_SHA256_C)
1985 case PSA_ALG_SHA_224:
1986 mbedtls_sha256_init( &operation->ctx.sha256 );
1987 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1988 break;
1989 case PSA_ALG_SHA_256:
1990 mbedtls_sha256_init( &operation->ctx.sha256 );
1991 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1992 break;
1993#endif
1994#if defined(MBEDTLS_SHA512_C)
1995 case PSA_ALG_SHA_384:
1996 mbedtls_sha512_init( &operation->ctx.sha512 );
1997 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1998 break;
1999 case PSA_ALG_SHA_512:
2000 mbedtls_sha512_init( &operation->ctx.sha512 );
2001 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2002 break;
2003#endif
2004 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002005 return( PSA_ALG_IS_HASH( alg ) ?
2006 PSA_ERROR_NOT_SUPPORTED :
2007 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002008 }
2009 if( ret == 0 )
2010 operation->alg = alg;
2011 else
2012 psa_hash_abort( operation );
2013 return( mbedtls_to_psa_error( ret ) );
2014}
2015
2016psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2017 const uint8_t *input,
2018 size_t input_length )
2019{
2020 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002021
2022 /* Don't require hash implementations to behave correctly on a
2023 * zero-length input, which may have an invalid pointer. */
2024 if( input_length == 0 )
2025 return( PSA_SUCCESS );
2026
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002027 switch( operation->alg )
2028 {
2029#if defined(MBEDTLS_MD2_C)
2030 case PSA_ALG_MD2:
2031 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2032 input, input_length );
2033 break;
2034#endif
2035#if defined(MBEDTLS_MD4_C)
2036 case PSA_ALG_MD4:
2037 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2038 input, input_length );
2039 break;
2040#endif
2041#if defined(MBEDTLS_MD5_C)
2042 case PSA_ALG_MD5:
2043 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2044 input, input_length );
2045 break;
2046#endif
2047#if defined(MBEDTLS_RIPEMD160_C)
2048 case PSA_ALG_RIPEMD160:
2049 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2050 input, input_length );
2051 break;
2052#endif
2053#if defined(MBEDTLS_SHA1_C)
2054 case PSA_ALG_SHA_1:
2055 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2056 input, input_length );
2057 break;
2058#endif
2059#if defined(MBEDTLS_SHA256_C)
2060 case PSA_ALG_SHA_224:
2061 case PSA_ALG_SHA_256:
2062 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2063 input, input_length );
2064 break;
2065#endif
2066#if defined(MBEDTLS_SHA512_C)
2067 case PSA_ALG_SHA_384:
2068 case PSA_ALG_SHA_512:
2069 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2070 input, input_length );
2071 break;
2072#endif
2073 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002074 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002075 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002076
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002077 if( ret != 0 )
2078 psa_hash_abort( operation );
2079 return( mbedtls_to_psa_error( ret ) );
2080}
2081
2082psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2083 uint8_t *hash,
2084 size_t hash_size,
2085 size_t *hash_length )
2086{
itayzafrir40835d42018-08-02 13:14:17 +03002087 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002088 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002089 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002090
2091 /* Fill the output buffer with something that isn't a valid hash
2092 * (barring an attack on the hash and deliberately-crafted input),
2093 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002094 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002095 /* If hash_size is 0 then hash may be NULL and then the
2096 * call to memset would have undefined behavior. */
2097 if( hash_size != 0 )
2098 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002099
2100 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002101 {
2102 status = PSA_ERROR_BUFFER_TOO_SMALL;
2103 goto exit;
2104 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002105
2106 switch( operation->alg )
2107 {
2108#if defined(MBEDTLS_MD2_C)
2109 case PSA_ALG_MD2:
2110 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2111 break;
2112#endif
2113#if defined(MBEDTLS_MD4_C)
2114 case PSA_ALG_MD4:
2115 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2116 break;
2117#endif
2118#if defined(MBEDTLS_MD5_C)
2119 case PSA_ALG_MD5:
2120 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2121 break;
2122#endif
2123#if defined(MBEDTLS_RIPEMD160_C)
2124 case PSA_ALG_RIPEMD160:
2125 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2126 break;
2127#endif
2128#if defined(MBEDTLS_SHA1_C)
2129 case PSA_ALG_SHA_1:
2130 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2131 break;
2132#endif
2133#if defined(MBEDTLS_SHA256_C)
2134 case PSA_ALG_SHA_224:
2135 case PSA_ALG_SHA_256:
2136 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2137 break;
2138#endif
2139#if defined(MBEDTLS_SHA512_C)
2140 case PSA_ALG_SHA_384:
2141 case PSA_ALG_SHA_512:
2142 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2143 break;
2144#endif
2145 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002146 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147 }
itayzafrir40835d42018-08-02 13:14:17 +03002148 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002149
itayzafrir40835d42018-08-02 13:14:17 +03002150exit:
2151 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002153 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154 return( psa_hash_abort( operation ) );
2155 }
2156 else
2157 {
2158 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002159 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002160 }
2161}
2162
Gilles Peskine2d277862018-06-18 15:41:12 +02002163psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2164 const uint8_t *hash,
2165 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002166{
2167 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2168 size_t actual_hash_length;
2169 psa_status_t status = psa_hash_finish( operation,
2170 actual_hash, sizeof( actual_hash ),
2171 &actual_hash_length );
2172 if( status != PSA_SUCCESS )
2173 return( status );
2174 if( actual_hash_length != hash_length )
2175 return( PSA_ERROR_INVALID_SIGNATURE );
2176 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2177 return( PSA_ERROR_INVALID_SIGNATURE );
2178 return( PSA_SUCCESS );
2179}
2180
Gilles Peskineeb35d782019-01-22 17:56:16 +01002181psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2182 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002183{
2184 if( target_operation->alg != 0 )
2185 return( PSA_ERROR_BAD_STATE );
2186
2187 switch( source_operation->alg )
2188 {
2189 case 0:
2190 return( PSA_ERROR_BAD_STATE );
2191#if defined(MBEDTLS_MD2_C)
2192 case PSA_ALG_MD2:
2193 mbedtls_md2_clone( &target_operation->ctx.md2,
2194 &source_operation->ctx.md2 );
2195 break;
2196#endif
2197#if defined(MBEDTLS_MD4_C)
2198 case PSA_ALG_MD4:
2199 mbedtls_md4_clone( &target_operation->ctx.md4,
2200 &source_operation->ctx.md4 );
2201 break;
2202#endif
2203#if defined(MBEDTLS_MD5_C)
2204 case PSA_ALG_MD5:
2205 mbedtls_md5_clone( &target_operation->ctx.md5,
2206 &source_operation->ctx.md5 );
2207 break;
2208#endif
2209#if defined(MBEDTLS_RIPEMD160_C)
2210 case PSA_ALG_RIPEMD160:
2211 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2212 &source_operation->ctx.ripemd160 );
2213 break;
2214#endif
2215#if defined(MBEDTLS_SHA1_C)
2216 case PSA_ALG_SHA_1:
2217 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2218 &source_operation->ctx.sha1 );
2219 break;
2220#endif
2221#if defined(MBEDTLS_SHA256_C)
2222 case PSA_ALG_SHA_224:
2223 case PSA_ALG_SHA_256:
2224 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2225 &source_operation->ctx.sha256 );
2226 break;
2227#endif
2228#if defined(MBEDTLS_SHA512_C)
2229 case PSA_ALG_SHA_384:
2230 case PSA_ALG_SHA_512:
2231 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2232 &source_operation->ctx.sha512 );
2233 break;
2234#endif
2235 default:
2236 return( PSA_ERROR_NOT_SUPPORTED );
2237 }
2238
2239 target_operation->alg = source_operation->alg;
2240 return( PSA_SUCCESS );
2241}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242
2243
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245/* MAC */
2246/****************************************************************/
2247
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002248static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002249 psa_algorithm_t alg,
2250 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002251 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002252 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002253{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002254 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002255 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002256
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002257 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002258 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002259
Gilles Peskine8c9def32018-02-08 10:02:12 +01002260 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2261 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002262 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002263 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002264 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002265 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002266 mode = MBEDTLS_MODE_STREAM;
2267 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002268 case PSA_ALG_CTR:
2269 mode = MBEDTLS_MODE_CTR;
2270 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002271 case PSA_ALG_CFB:
2272 mode = MBEDTLS_MODE_CFB;
2273 break;
2274 case PSA_ALG_OFB:
2275 mode = MBEDTLS_MODE_OFB;
2276 break;
2277 case PSA_ALG_CBC_NO_PADDING:
2278 mode = MBEDTLS_MODE_CBC;
2279 break;
2280 case PSA_ALG_CBC_PKCS7:
2281 mode = MBEDTLS_MODE_CBC;
2282 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002283 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002284 mode = MBEDTLS_MODE_CCM;
2285 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002286 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002287 mode = MBEDTLS_MODE_GCM;
2288 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002289 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2290 mode = MBEDTLS_MODE_CHACHAPOLY;
2291 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292 default:
2293 return( NULL );
2294 }
2295 }
2296 else if( alg == PSA_ALG_CMAC )
2297 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002298 else
2299 return( NULL );
2300
2301 switch( key_type )
2302 {
2303 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002304 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002305 break;
2306 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002307 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2308 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002309 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002310 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002312 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002313 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2314 * but two-key Triple-DES is functionally three-key Triple-DES
2315 * with K1=K3, so that's how we present it to mbedtls. */
2316 if( key_bits == 128 )
2317 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002318 break;
2319 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002320 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002321 break;
2322 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002323 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002325 case PSA_KEY_TYPE_CHACHA20:
2326 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2327 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002328 default:
2329 return( NULL );
2330 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002331 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002332 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333
Jaeden Amero23bbb752018-06-26 14:16:54 +01002334 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2335 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002336}
2337
Gilles Peskinea05219c2018-11-16 16:02:56 +01002338#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002339static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002340{
Gilles Peskine2d277862018-06-18 15:41:12 +02002341 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002342 {
2343 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002344 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002345 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002346 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002347 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002348 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002349 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002350 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002351 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002352 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002353 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002354 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002355 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002356 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002357 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002358 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002359 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002360 return( 128 );
2361 default:
2362 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002363 }
2364}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002365#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002366
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002367/* Initialize the MAC operation structure. Once this function has been
2368 * called, psa_mac_abort can run and will do the right thing. */
2369static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2370 psa_algorithm_t alg )
2371{
2372 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2373
2374 operation->alg = alg;
2375 operation->key_set = 0;
2376 operation->iv_set = 0;
2377 operation->iv_required = 0;
2378 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002379 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380
2381#if defined(MBEDTLS_CMAC_C)
2382 if( alg == PSA_ALG_CMAC )
2383 {
2384 operation->iv_required = 0;
2385 mbedtls_cipher_init( &operation->ctx.cmac );
2386 status = PSA_SUCCESS;
2387 }
2388 else
2389#endif /* MBEDTLS_CMAC_C */
2390#if defined(MBEDTLS_MD_C)
2391 if( PSA_ALG_IS_HMAC( operation->alg ) )
2392 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002393 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2394 operation->ctx.hmac.hash_ctx.alg = 0;
2395 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396 }
2397 else
2398#endif /* MBEDTLS_MD_C */
2399 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002400 if( ! PSA_ALG_IS_MAC( alg ) )
2401 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402 }
2403
2404 if( status != PSA_SUCCESS )
2405 memset( operation, 0, sizeof( *operation ) );
2406 return( status );
2407}
2408
Gilles Peskine01126fa2018-07-12 17:04:55 +02002409#if defined(MBEDTLS_MD_C)
2410static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2411{
Gilles Peskine3f108122018-12-07 18:14:53 +01002412 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002413 return( psa_hash_abort( &hmac->hash_ctx ) );
2414}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002415
Janos Follath999f6482019-06-11 12:04:10 +01002416#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002417static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2418{
2419 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2420 memset( hmac, 0, sizeof( *hmac ) );
2421}
Janos Follath999f6482019-06-11 12:04:10 +01002422#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002423#endif /* MBEDTLS_MD_C */
2424
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2426{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002427 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002429 /* The object has (apparently) been initialized but it is not
2430 * in use. It's ok to call abort on such an object, and there's
2431 * nothing to do. */
2432 return( PSA_SUCCESS );
2433 }
2434 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002436 if( operation->alg == PSA_ALG_CMAC )
2437 {
2438 mbedtls_cipher_free( &operation->ctx.cmac );
2439 }
2440 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002442#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002443 if( PSA_ALG_IS_HMAC( operation->alg ) )
2444 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002445 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002446 }
2447 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002449 {
2450 /* Sanity check (shouldn't happen: operation->alg should
2451 * always have been initialized to a valid value). */
2452 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 }
Moran Peker41deec42018-04-04 15:43:05 +03002454
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455 operation->alg = 0;
2456 operation->key_set = 0;
2457 operation->iv_set = 0;
2458 operation->iv_required = 0;
2459 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002460 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002461
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002463
2464bad_state:
2465 /* If abort is called on an uninitialized object, we can't trust
2466 * anything. Wipe the object in case it contains confidential data.
2467 * This may result in a memory leak if a pointer gets overwritten,
2468 * but it's too late to do anything about this. */
2469 memset( operation, 0, sizeof( *operation ) );
2470 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002471}
2472
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002473#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002474static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002475 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002476 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002477 const mbedtls_cipher_info_t *cipher_info )
2478{
2479 int ret;
2480
2481 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002482
2483 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2484 if( ret != 0 )
2485 return( ret );
2486
2487 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2488 slot->data.raw.data,
2489 key_bits );
2490 return( ret );
2491}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002492#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002493
Gilles Peskine248051a2018-06-20 16:09:38 +02002494#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002495static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2496 const uint8_t *key,
2497 size_t key_length,
2498 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002499{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002500 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002501 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002502 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002503 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504 psa_status_t status;
2505
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002506 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2507 * overflow below. This should never trigger if the hash algorithm
2508 * is implemented correctly. */
2509 /* The size checks against the ipad and opad buffers cannot be written
2510 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2511 * because that triggers -Wlogical-op on GCC 7.3. */
2512 if( block_size > sizeof( ipad ) )
2513 return( PSA_ERROR_NOT_SUPPORTED );
2514 if( block_size > sizeof( hmac->opad ) )
2515 return( PSA_ERROR_NOT_SUPPORTED );
2516 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002517 return( PSA_ERROR_NOT_SUPPORTED );
2518
Gilles Peskined223b522018-06-11 18:12:58 +02002519 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002520 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002521 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002522 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002523 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002524 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002525 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002526 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002527 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002528 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002529 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002530 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002531 }
Gilles Peskine96889972018-07-12 17:07:03 +02002532 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2533 * but it is permitted. It is common when HMAC is used in HKDF, for
2534 * example. Don't call `memcpy` in the 0-length because `key` could be
2535 * an invalid pointer which would make the behavior undefined. */
2536 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002537 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002538
Gilles Peskined223b522018-06-11 18:12:58 +02002539 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2540 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002541 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002542 ipad[i] ^= 0x36;
2543 memset( ipad + key_length, 0x36, block_size - key_length );
2544
2545 /* Copy the key material from ipad to opad, flipping the requisite bits,
2546 * and filling the rest of opad with the requisite constant. */
2547 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002548 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2549 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002550
Gilles Peskine01126fa2018-07-12 17:04:55 +02002551 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002552 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002553 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002554
Gilles Peskine01126fa2018-07-12 17:04:55 +02002555 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002556
2557cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002558 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002559
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002560 return( status );
2561}
Gilles Peskine248051a2018-06-20 16:09:38 +02002562#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002563
Gilles Peskine89167cb2018-07-08 20:12:23 +02002564static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002565 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002566 psa_algorithm_t alg,
2567 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002570 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002572 psa_key_usage_t usage =
2573 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002574 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002575 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002577 /* A context must be freshly initialized before it can be set up. */
2578 if( operation->alg != 0 )
2579 {
2580 return( PSA_ERROR_BAD_STATE );
2581 }
2582
Gilles Peskined911eb72018-08-14 15:18:45 +02002583 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584 if( status != PSA_SUCCESS )
2585 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002586 if( is_sign )
2587 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588
Gilles Peskine28f8f302019-07-24 13:30:31 +02002589 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002590 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002591 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002592 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002593
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002595 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002596 {
2597 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002598 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002599 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002600 int ret;
2601 if( cipher_info == NULL )
2602 {
2603 status = PSA_ERROR_NOT_SUPPORTED;
2604 goto exit;
2605 }
2606 operation->mac_size = cipher_info->block_size;
2607 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2608 status = mbedtls_to_psa_error( ret );
2609 }
2610 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002613 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002614 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002615 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002616 if( hash_alg == 0 )
2617 {
2618 status = PSA_ERROR_NOT_SUPPORTED;
2619 goto exit;
2620 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002621
2622 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2623 /* Sanity check. This shouldn't fail on a valid configuration. */
2624 if( operation->mac_size == 0 ||
2625 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2626 {
2627 status = PSA_ERROR_NOT_SUPPORTED;
2628 goto exit;
2629 }
2630
Gilles Peskine8e338702019-07-30 20:06:31 +02002631 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002632 {
2633 status = PSA_ERROR_INVALID_ARGUMENT;
2634 goto exit;
2635 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002636
Gilles Peskine01126fa2018-07-12 17:04:55 +02002637 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2638 slot->data.raw.data,
2639 slot->data.raw.bytes,
2640 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002641 }
2642 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002644 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002645 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002648
Gilles Peskined911eb72018-08-14 15:18:45 +02002649 if( truncated == 0 )
2650 {
2651 /* The "normal" case: untruncated algorithm. Nothing to do. */
2652 }
2653 else if( truncated < 4 )
2654 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002655 /* A very short MAC is too short for security since it can be
2656 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2657 * so we make this our minimum, even though 32 bits is still
2658 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002659 status = PSA_ERROR_NOT_SUPPORTED;
2660 }
2661 else if( truncated > operation->mac_size )
2662 {
2663 /* It's impossible to "truncate" to a larger length. */
2664 status = PSA_ERROR_INVALID_ARGUMENT;
2665 }
2666 else
2667 operation->mac_size = truncated;
2668
Gilles Peskinefbfac682018-07-08 20:51:54 +02002669exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002670 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002672 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002674 else
2675 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676 operation->key_set = 1;
2677 }
2678 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679}
2680
Gilles Peskine89167cb2018-07-08 20:12:23 +02002681psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002682 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002683 psa_algorithm_t alg )
2684{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002685 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002686}
2687
2688psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002689 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002690 psa_algorithm_t alg )
2691{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002692 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002693}
2694
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2696 const uint8_t *input,
2697 size_t input_length )
2698{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002699 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002701 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002703 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704 operation->has_input = 1;
2705
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002708 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002709 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2710 input, input_length );
2711 status = mbedtls_to_psa_error( ret );
2712 }
2713 else
2714#endif /* MBEDTLS_CMAC_C */
2715#if defined(MBEDTLS_MD_C)
2716 if( PSA_ALG_IS_HMAC( operation->alg ) )
2717 {
2718 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2719 input_length );
2720 }
2721 else
2722#endif /* MBEDTLS_MD_C */
2723 {
2724 /* This shouldn't happen if `operation` was initialized by
2725 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002726 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002727 }
2728
Gilles Peskinefbfac682018-07-08 20:51:54 +02002729 if( status != PSA_SUCCESS )
2730 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002731 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732}
2733
Gilles Peskine01126fa2018-07-12 17:04:55 +02002734#if defined(MBEDTLS_MD_C)
2735static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2736 uint8_t *mac,
2737 size_t mac_size )
2738{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002739 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002740 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2741 size_t hash_size = 0;
2742 size_t block_size = psa_get_hash_block_size( hash_alg );
2743 psa_status_t status;
2744
Gilles Peskine01126fa2018-07-12 17:04:55 +02002745 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2746 if( status != PSA_SUCCESS )
2747 return( status );
2748 /* From here on, tmp needs to be wiped. */
2749
2750 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2751 if( status != PSA_SUCCESS )
2752 goto exit;
2753
2754 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2755 if( status != PSA_SUCCESS )
2756 goto exit;
2757
2758 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2759 if( status != PSA_SUCCESS )
2760 goto exit;
2761
Gilles Peskined911eb72018-08-14 15:18:45 +02002762 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2763 if( status != PSA_SUCCESS )
2764 goto exit;
2765
2766 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002767
2768exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002769 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 return( status );
2771}
2772#endif /* MBEDTLS_MD_C */
2773
mohammad16036df908f2018-04-02 08:34:15 -07002774static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002775 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002776 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002778 if( ! operation->key_set )
2779 return( PSA_ERROR_BAD_STATE );
2780 if( operation->iv_required && ! operation->iv_set )
2781 return( PSA_ERROR_BAD_STATE );
2782
Gilles Peskine8c9def32018-02-08 10:02:12 +01002783 if( mac_size < operation->mac_size )
2784 return( PSA_ERROR_BUFFER_TOO_SMALL );
2785
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002787 if( operation->alg == PSA_ALG_CMAC )
2788 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002789 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2790 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2791 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002792 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002793 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002794 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002796 else
2797#endif /* MBEDTLS_CMAC_C */
2798#if defined(MBEDTLS_MD_C)
2799 if( PSA_ALG_IS_HMAC( operation->alg ) )
2800 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002801 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002802 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002803 }
2804 else
2805#endif /* MBEDTLS_MD_C */
2806 {
2807 /* This shouldn't happen if `operation` was initialized by
2808 * a setup function. */
2809 return( PSA_ERROR_BAD_STATE );
2810 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002811}
2812
Gilles Peskineacd4be32018-07-08 19:56:25 +02002813psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2814 uint8_t *mac,
2815 size_t mac_size,
2816 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002817{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002818 psa_status_t status;
2819
Jaeden Amero252ef282019-02-15 14:05:35 +00002820 if( operation->alg == 0 )
2821 {
2822 return( PSA_ERROR_BAD_STATE );
2823 }
2824
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002825 /* Fill the output buffer with something that isn't a valid mac
2826 * (barring an attack on the mac and deliberately-crafted input),
2827 * in case the caller doesn't check the return status properly. */
2828 *mac_length = mac_size;
2829 /* If mac_size is 0 then mac may be NULL and then the
2830 * call to memset would have undefined behavior. */
2831 if( mac_size != 0 )
2832 memset( mac, '!', mac_size );
2833
Gilles Peskine89167cb2018-07-08 20:12:23 +02002834 if( ! operation->is_sign )
2835 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002836 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002837 }
mohammad16036df908f2018-04-02 08:34:15 -07002838
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002839 status = psa_mac_finish_internal( operation, mac, mac_size );
2840
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002841 if( status == PSA_SUCCESS )
2842 {
2843 status = psa_mac_abort( operation );
2844 if( status == PSA_SUCCESS )
2845 *mac_length = operation->mac_size;
2846 else
2847 memset( mac, '!', mac_size );
2848 }
2849 else
2850 psa_mac_abort( operation );
2851 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002852}
2853
Gilles Peskineacd4be32018-07-08 19:56:25 +02002854psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2855 const uint8_t *mac,
2856 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857{
Gilles Peskine828ed142018-06-18 23:25:51 +02002858 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002859 psa_status_t status;
2860
Jaeden Amero252ef282019-02-15 14:05:35 +00002861 if( operation->alg == 0 )
2862 {
2863 return( PSA_ERROR_BAD_STATE );
2864 }
2865
Gilles Peskine89167cb2018-07-08 20:12:23 +02002866 if( operation->is_sign )
2867 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002868 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002869 }
2870 if( operation->mac_size != mac_length )
2871 {
2872 status = PSA_ERROR_INVALID_SIGNATURE;
2873 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002874 }
mohammad16036df908f2018-04-02 08:34:15 -07002875
2876 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002877 actual_mac, sizeof( actual_mac ) );
2878
2879 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2880 status = PSA_ERROR_INVALID_SIGNATURE;
2881
2882cleanup:
2883 if( status == PSA_SUCCESS )
2884 status = psa_mac_abort( operation );
2885 else
2886 psa_mac_abort( operation );
2887
Gilles Peskine3f108122018-12-07 18:14:53 +01002888 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002889
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002890 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891}
2892
2893
Gilles Peskine20035e32018-02-03 22:44:14 +01002894
Gilles Peskine20035e32018-02-03 22:44:14 +01002895/****************************************************************/
2896/* Asymmetric cryptography */
2897/****************************************************************/
2898
Gilles Peskine2b450e32018-06-27 15:42:46 +02002899#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002900/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002901 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002902static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2903 size_t hash_length,
2904 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002905{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002906 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002907 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002908 *md_alg = mbedtls_md_get_type( md_info );
2909
2910 /* The Mbed TLS RSA module uses an unsigned int for hash length
2911 * parameters. Validate that it fits so that we don't risk an
2912 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002913#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002914 if( hash_length > UINT_MAX )
2915 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002916#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002917
2918#if defined(MBEDTLS_PKCS1_V15)
2919 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2920 * must be correct. */
2921 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2922 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002923 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002924 if( md_info == NULL )
2925 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002926 if( mbedtls_md_get_size( md_info ) != hash_length )
2927 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002928 }
2929#endif /* MBEDTLS_PKCS1_V15 */
2930
2931#if defined(MBEDTLS_PKCS1_V21)
2932 /* PSS requires a hash internally. */
2933 if( PSA_ALG_IS_RSA_PSS( alg ) )
2934 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002935 if( md_info == NULL )
2936 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002937 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002938#endif /* MBEDTLS_PKCS1_V21 */
2939
Gilles Peskine61b91d42018-06-08 16:09:36 +02002940 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002941}
2942
Gilles Peskine2b450e32018-06-27 15:42:46 +02002943static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2944 psa_algorithm_t alg,
2945 const uint8_t *hash,
2946 size_t hash_length,
2947 uint8_t *signature,
2948 size_t signature_size,
2949 size_t *signature_length )
2950{
2951 psa_status_t status;
2952 int ret;
2953 mbedtls_md_type_t md_alg;
2954
2955 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2956 if( status != PSA_SUCCESS )
2957 return( status );
2958
Gilles Peskine630a18a2018-06-29 17:49:35 +02002959 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002960 return( PSA_ERROR_BUFFER_TOO_SMALL );
2961
2962#if defined(MBEDTLS_PKCS1_V15)
2963 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2964 {
2965 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2966 MBEDTLS_MD_NONE );
2967 ret = mbedtls_rsa_pkcs1_sign( rsa,
2968 mbedtls_ctr_drbg_random,
2969 &global_data.ctr_drbg,
2970 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002971 md_alg,
2972 (unsigned int) hash_length,
2973 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002974 signature );
2975 }
2976 else
2977#endif /* MBEDTLS_PKCS1_V15 */
2978#if defined(MBEDTLS_PKCS1_V21)
2979 if( PSA_ALG_IS_RSA_PSS( alg ) )
2980 {
2981 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2982 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2983 mbedtls_ctr_drbg_random,
2984 &global_data.ctr_drbg,
2985 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002986 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002987 (unsigned int) hash_length,
2988 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002989 signature );
2990 }
2991 else
2992#endif /* MBEDTLS_PKCS1_V21 */
2993 {
2994 return( PSA_ERROR_INVALID_ARGUMENT );
2995 }
2996
2997 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002998 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002999 return( mbedtls_to_psa_error( ret ) );
3000}
3001
3002static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3003 psa_algorithm_t alg,
3004 const uint8_t *hash,
3005 size_t hash_length,
3006 const uint8_t *signature,
3007 size_t signature_length )
3008{
3009 psa_status_t status;
3010 int ret;
3011 mbedtls_md_type_t md_alg;
3012
3013 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3014 if( status != PSA_SUCCESS )
3015 return( status );
3016
Gilles Peskine630a18a2018-06-29 17:49:35 +02003017 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003018 return( PSA_ERROR_BUFFER_TOO_SMALL );
3019
3020#if defined(MBEDTLS_PKCS1_V15)
3021 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3022 {
3023 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3024 MBEDTLS_MD_NONE );
3025 ret = mbedtls_rsa_pkcs1_verify( rsa,
3026 mbedtls_ctr_drbg_random,
3027 &global_data.ctr_drbg,
3028 MBEDTLS_RSA_PUBLIC,
3029 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003030 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003031 hash,
3032 signature );
3033 }
3034 else
3035#endif /* MBEDTLS_PKCS1_V15 */
3036#if defined(MBEDTLS_PKCS1_V21)
3037 if( PSA_ALG_IS_RSA_PSS( alg ) )
3038 {
3039 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3040 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3041 mbedtls_ctr_drbg_random,
3042 &global_data.ctr_drbg,
3043 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003044 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003045 (unsigned int) hash_length,
3046 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003047 signature );
3048 }
3049 else
3050#endif /* MBEDTLS_PKCS1_V21 */
3051 {
3052 return( PSA_ERROR_INVALID_ARGUMENT );
3053 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003054
3055 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3056 * the rest of the signature is invalid". This has little use in
3057 * practice and PSA doesn't report this distinction. */
3058 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3059 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003060 return( mbedtls_to_psa_error( ret ) );
3061}
3062#endif /* MBEDTLS_RSA_C */
3063
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003064#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003065/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3066 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3067 * (even though these functions don't modify it). */
3068static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3069 psa_algorithm_t alg,
3070 const uint8_t *hash,
3071 size_t hash_length,
3072 uint8_t *signature,
3073 size_t signature_size,
3074 size_t *signature_length )
3075{
3076 int ret;
3077 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003078 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003079 mbedtls_mpi_init( &r );
3080 mbedtls_mpi_init( &s );
3081
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003082 if( signature_size < 2 * curve_bytes )
3083 {
3084 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3085 goto cleanup;
3086 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003087
Gilles Peskinea05219c2018-11-16 16:02:56 +01003088#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003089 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3090 {
3091 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3092 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3093 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3094 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3095 hash, hash_length,
3096 md_alg ) );
3097 }
3098 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003099#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003100 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003101 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003102 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3103 hash, hash_length,
3104 mbedtls_ctr_drbg_random,
3105 &global_data.ctr_drbg ) );
3106 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003107
3108 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3109 signature,
3110 curve_bytes ) );
3111 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3112 signature + curve_bytes,
3113 curve_bytes ) );
3114
3115cleanup:
3116 mbedtls_mpi_free( &r );
3117 mbedtls_mpi_free( &s );
3118 if( ret == 0 )
3119 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003120 return( mbedtls_to_psa_error( ret ) );
3121}
3122
3123static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3124 const uint8_t *hash,
3125 size_t hash_length,
3126 const uint8_t *signature,
3127 size_t signature_length )
3128{
3129 int ret;
3130 mbedtls_mpi r, s;
3131 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3132 mbedtls_mpi_init( &r );
3133 mbedtls_mpi_init( &s );
3134
3135 if( signature_length != 2 * curve_bytes )
3136 return( PSA_ERROR_INVALID_SIGNATURE );
3137
3138 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3139 signature,
3140 curve_bytes ) );
3141 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3142 signature + curve_bytes,
3143 curve_bytes ) );
3144
3145 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3146 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003147
3148cleanup:
3149 mbedtls_mpi_free( &r );
3150 mbedtls_mpi_free( &s );
3151 return( mbedtls_to_psa_error( ret ) );
3152}
3153#endif /* MBEDTLS_ECDSA_C */
3154
Gilles Peskinec5487a82018-12-03 18:08:14 +01003155psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003156 psa_algorithm_t alg,
3157 const uint8_t *hash,
3158 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003159 uint8_t *signature,
3160 size_t signature_size,
3161 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003162{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003163 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003164 psa_status_t status;
3165
3166 *signature_length = signature_size;
3167
Gilles Peskine28f8f302019-07-24 13:30:31 +02003168 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003169 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003170 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003171 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003172 {
3173 status = PSA_ERROR_INVALID_ARGUMENT;
3174 goto exit;
3175 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003176
Gilles Peskine20035e32018-02-03 22:44:14 +01003177#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003178 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003179 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003180 status = psa_rsa_sign( slot->data.rsa,
3181 alg,
3182 hash, hash_length,
3183 signature, signature_size,
3184 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003185 }
3186 else
3187#endif /* defined(MBEDTLS_RSA_C) */
3188#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003189 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003190 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003191#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003192 if(
3193#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3194 PSA_ALG_IS_ECDSA( alg )
3195#else
3196 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3197#endif
3198 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003199 status = psa_ecdsa_sign( slot->data.ecp,
3200 alg,
3201 hash, hash_length,
3202 signature, signature_size,
3203 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003204 else
3205#endif /* defined(MBEDTLS_ECDSA_C) */
3206 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003207 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003208 }
itayzafrir5c753392018-05-08 11:18:38 +03003209 }
3210 else
3211#endif /* defined(MBEDTLS_ECP_C) */
3212 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003213 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003214 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003215
3216exit:
3217 /* Fill the unused part of the output buffer (the whole buffer on error,
3218 * the trailing part on success) with something that isn't a valid mac
3219 * (barring an attack on the mac and deliberately-crafted input),
3220 * in case the caller doesn't check the return status properly. */
3221 if( status == PSA_SUCCESS )
3222 memset( signature + *signature_length, '!',
3223 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003224 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003225 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003226 /* If signature_size is 0 then we have nothing to do. We must not call
3227 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003228 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003229}
3230
Gilles Peskinec5487a82018-12-03 18:08:14 +01003231psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003232 psa_algorithm_t alg,
3233 const uint8_t *hash,
3234 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003235 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003236 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003237{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003238 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003239 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003240
Gilles Peskine28f8f302019-07-24 13:30:31 +02003241 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003242 if( status != PSA_SUCCESS )
3243 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003244
Gilles Peskine61b91d42018-06-08 16:09:36 +02003245#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003246 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003248 return( psa_rsa_verify( slot->data.rsa,
3249 alg,
3250 hash, hash_length,
3251 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 }
3253 else
3254#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003255#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003256 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003257 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003258#if defined(MBEDTLS_ECDSA_C)
3259 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003260 return( psa_ecdsa_verify( slot->data.ecp,
3261 hash, hash_length,
3262 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003263 else
3264#endif /* defined(MBEDTLS_ECDSA_C) */
3265 {
3266 return( PSA_ERROR_INVALID_ARGUMENT );
3267 }
itayzafrir5c753392018-05-08 11:18:38 +03003268 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003269 else
3270#endif /* defined(MBEDTLS_ECP_C) */
3271 {
3272 return( PSA_ERROR_NOT_SUPPORTED );
3273 }
3274}
3275
Gilles Peskine072ac562018-06-30 00:21:29 +02003276#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3277static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3278 mbedtls_rsa_context *rsa )
3279{
3280 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3281 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3282 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3283 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3284}
3285#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3286
Gilles Peskinec5487a82018-12-03 18:08:14 +01003287psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003288 psa_algorithm_t alg,
3289 const uint8_t *input,
3290 size_t input_length,
3291 const uint8_t *salt,
3292 size_t salt_length,
3293 uint8_t *output,
3294 size_t output_size,
3295 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003297 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003298 psa_status_t status;
3299
Darryl Green5cc689a2018-07-24 15:34:10 +01003300 (void) input;
3301 (void) input_length;
3302 (void) salt;
3303 (void) output;
3304 (void) output_size;
3305
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003306 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003308 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3309 return( PSA_ERROR_INVALID_ARGUMENT );
3310
Gilles Peskine28f8f302019-07-24 13:30:31 +02003311 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003312 if( status != PSA_SUCCESS )
3313 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003314 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3315 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317
3318#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003319 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003320 {
3321 mbedtls_rsa_context *rsa = slot->data.rsa;
3322 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003323 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003324 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003326 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003328 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3329 mbedtls_ctr_drbg_random,
3330 &global_data.ctr_drbg,
3331 MBEDTLS_RSA_PUBLIC,
3332 input_length,
3333 input,
3334 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335 }
3336 else
3337#endif /* MBEDTLS_PKCS1_V15 */
3338#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003339 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003340 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003341 psa_rsa_oaep_set_padding_mode( alg, rsa );
3342 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3343 mbedtls_ctr_drbg_random,
3344 &global_data.ctr_drbg,
3345 MBEDTLS_RSA_PUBLIC,
3346 salt, salt_length,
3347 input_length,
3348 input,
3349 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003350 }
3351 else
3352#endif /* MBEDTLS_PKCS1_V21 */
3353 {
3354 return( PSA_ERROR_INVALID_ARGUMENT );
3355 }
3356 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003357 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003358 return( mbedtls_to_psa_error( ret ) );
3359 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003361#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362 {
3363 return( PSA_ERROR_NOT_SUPPORTED );
3364 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003365}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003366
Gilles Peskinec5487a82018-12-03 18:08:14 +01003367psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003368 psa_algorithm_t alg,
3369 const uint8_t *input,
3370 size_t input_length,
3371 const uint8_t *salt,
3372 size_t salt_length,
3373 uint8_t *output,
3374 size_t output_size,
3375 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003376{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003377 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003378 psa_status_t status;
3379
Darryl Green5cc689a2018-07-24 15:34:10 +01003380 (void) input;
3381 (void) input_length;
3382 (void) salt;
3383 (void) output;
3384 (void) output_size;
3385
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003386 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003387
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003388 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3389 return( PSA_ERROR_INVALID_ARGUMENT );
3390
Gilles Peskine28f8f302019-07-24 13:30:31 +02003391 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003392 if( status != PSA_SUCCESS )
3393 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003394 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003395 return( PSA_ERROR_INVALID_ARGUMENT );
3396
3397#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003398 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003399 {
3400 mbedtls_rsa_context *rsa = slot->data.rsa;
3401 int ret;
3402
Gilles Peskine630a18a2018-06-29 17:49:35 +02003403 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404 return( PSA_ERROR_INVALID_ARGUMENT );
3405
3406#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003407 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003408 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003409 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3410 mbedtls_ctr_drbg_random,
3411 &global_data.ctr_drbg,
3412 MBEDTLS_RSA_PRIVATE,
3413 output_length,
3414 input,
3415 output,
3416 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003417 }
3418 else
3419#endif /* MBEDTLS_PKCS1_V15 */
3420#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003421 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003422 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003423 psa_rsa_oaep_set_padding_mode( alg, rsa );
3424 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3425 mbedtls_ctr_drbg_random,
3426 &global_data.ctr_drbg,
3427 MBEDTLS_RSA_PRIVATE,
3428 salt, salt_length,
3429 output_length,
3430 input,
3431 output,
3432 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003433 }
3434 else
3435#endif /* MBEDTLS_PKCS1_V21 */
3436 {
3437 return( PSA_ERROR_INVALID_ARGUMENT );
3438 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003439
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440 return( mbedtls_to_psa_error( ret ) );
3441 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003442 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003443#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003444 {
3445 return( PSA_ERROR_NOT_SUPPORTED );
3446 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003447}
Gilles Peskine20035e32018-02-03 22:44:14 +01003448
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003449
3450
mohammad1603503973b2018-03-12 15:59:30 +02003451/****************************************************************/
3452/* Symmetric cryptography */
3453/****************************************************************/
3454
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003455/* Initialize the cipher operation structure. Once this function has been
3456 * called, psa_cipher_abort can run and will do the right thing. */
3457static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3458 psa_algorithm_t alg )
3459{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003460 if( ! PSA_ALG_IS_CIPHER( alg ) )
3461 {
3462 memset( operation, 0, sizeof( *operation ) );
3463 return( PSA_ERROR_INVALID_ARGUMENT );
3464 }
3465
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003466 operation->alg = alg;
3467 operation->key_set = 0;
3468 operation->iv_set = 0;
3469 operation->iv_required = 1;
3470 operation->iv_size = 0;
3471 operation->block_size = 0;
3472 mbedtls_cipher_init( &operation->ctx.cipher );
3473 return( PSA_SUCCESS );
3474}
3475
Gilles Peskinee553c652018-06-04 16:22:46 +02003476static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003477 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003478 psa_algorithm_t alg,
3479 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003480{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003481 int ret = 0;
3482 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003483 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003484 size_t key_bits;
3485 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003486 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3487 PSA_KEY_USAGE_ENCRYPT :
3488 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003489
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003490 /* A context must be freshly initialized before it can be set up. */
3491 if( operation->alg != 0 )
3492 {
3493 return( PSA_ERROR_BAD_STATE );
3494 }
3495
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003496 status = psa_cipher_init( operation, alg );
3497 if( status != PSA_SUCCESS )
3498 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003499
Gilles Peskine28f8f302019-07-24 13:30:31 +02003500 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003501 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003502 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003503 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003504
Gilles Peskine8e338702019-07-30 20:06:31 +02003505 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003506 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003507 {
3508 status = PSA_ERROR_NOT_SUPPORTED;
3509 goto exit;
3510 }
mohammad1603503973b2018-03-12 15:59:30 +02003511
mohammad1603503973b2018-03-12 15:59:30 +02003512 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003513 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003514 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003515
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003516#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003517 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003518 {
3519 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003520 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003521 memcpy( keys, slot->data.raw.data, 16 );
3522 memcpy( keys + 16, slot->data.raw.data, 8 );
3523 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3524 keys,
3525 192, cipher_operation );
3526 }
3527 else
3528#endif
3529 {
3530 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3531 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003532 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003533 }
Moran Peker41deec42018-04-04 15:43:05 +03003534 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003535 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003536
mohammad16038481e742018-03-18 13:57:31 +02003537#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003538 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003539 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003540 case PSA_ALG_CBC_NO_PADDING:
3541 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3542 MBEDTLS_PADDING_NONE );
3543 break;
3544 case PSA_ALG_CBC_PKCS7:
3545 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3546 MBEDTLS_PADDING_PKCS7 );
3547 break;
3548 default:
3549 /* The algorithm doesn't involve padding. */
3550 ret = 0;
3551 break;
3552 }
3553 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003554 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003555#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3556
mohammad1603503973b2018-03-12 15:59:30 +02003557 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003558 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003559 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003560 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003561 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003562 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003563 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003564#if defined(MBEDTLS_CHACHA20_C)
3565 else
3566 if( alg == PSA_ALG_CHACHA20 )
3567 operation->iv_size = 12;
3568#endif
mohammad1603503973b2018-03-12 15:59:30 +02003569
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003570exit:
3571 if( status == 0 )
3572 status = mbedtls_to_psa_error( ret );
3573 if( status != 0 )
3574 psa_cipher_abort( operation );
3575 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003576}
3577
Gilles Peskinefe119512018-07-08 21:39:34 +02003578psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003579 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003580 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003581{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003582 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003583}
3584
Gilles Peskinefe119512018-07-08 21:39:34 +02003585psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003586 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003587 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003588{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003589 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003590}
3591
Gilles Peskinefe119512018-07-08 21:39:34 +02003592psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003593 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003594 size_t iv_size,
3595 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003596{
itayzafrir534bd7c2018-08-02 13:56:32 +03003597 psa_status_t status;
3598 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003599 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003600 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003601 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003602 }
Moran Peker41deec42018-04-04 15:43:05 +03003603 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003604 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003605 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003606 goto exit;
3607 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003608 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3609 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003610 if( ret != 0 )
3611 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003612 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003613 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003614 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003615
mohammad16038481e742018-03-18 13:57:31 +02003616 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003617 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003618
Moran Peker395db872018-05-31 14:07:14 +03003619exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003620 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003621 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003622 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003623}
3624
Gilles Peskinefe119512018-07-08 21:39:34 +02003625psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003626 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003627 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003628{
itayzafrir534bd7c2018-08-02 13:56:32 +03003629 psa_status_t status;
3630 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003631 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003632 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003633 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003634 }
Moran Pekera28258c2018-05-29 16:25:04 +03003635 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003636 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003637 status = PSA_ERROR_INVALID_ARGUMENT;
3638 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003639 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003640 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3641 status = mbedtls_to_psa_error( ret );
3642exit:
3643 if( status == PSA_SUCCESS )
3644 operation->iv_set = 1;
3645 else
mohammad1603503973b2018-03-12 15:59:30 +02003646 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003647 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003648}
3649
Gilles Peskinee553c652018-06-04 16:22:46 +02003650psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3651 const uint8_t *input,
3652 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003653 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003654 size_t output_size,
3655 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003656{
itayzafrir534bd7c2018-08-02 13:56:32 +03003657 psa_status_t status;
3658 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003659 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003660
3661 if( operation->alg == 0 )
3662 {
3663 return( PSA_ERROR_BAD_STATE );
3664 }
3665
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003666 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003667 {
3668 /* Take the unprocessed partial block left over from previous
3669 * update calls, if any, plus the input to this call. Remove
3670 * the last partial block, if any. You get the data that will be
3671 * output in this call. */
3672 expected_output_size =
3673 ( operation->ctx.cipher.unprocessed_len + input_length )
3674 / operation->block_size * operation->block_size;
3675 }
3676 else
3677 {
3678 expected_output_size = input_length;
3679 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003680
Gilles Peskine89d789c2018-06-04 17:17:16 +02003681 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003682 {
3683 status = PSA_ERROR_BUFFER_TOO_SMALL;
3684 goto exit;
3685 }
mohammad160382759612018-03-12 18:16:40 +02003686
mohammad1603503973b2018-03-12 15:59:30 +02003687 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003688 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003689 status = mbedtls_to_psa_error( ret );
3690exit:
3691 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003692 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003693 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003694}
3695
Gilles Peskinee553c652018-06-04 16:22:46 +02003696psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3697 uint8_t *output,
3698 size_t output_size,
3699 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003700{
David Saadab4ecc272019-02-14 13:48:10 +02003701 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003702 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003703 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003704
mohammad1603503973b2018-03-12 15:59:30 +02003705 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003706 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003707 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003708 }
3709 if( operation->iv_required && ! operation->iv_set )
3710 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003711 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003712 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003713
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003714 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003715 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3716 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003717 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003718 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003719 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003720 }
3721
Janos Follath315b51c2018-07-09 16:04:51 +01003722 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3723 temp_output_buffer,
3724 output_length );
3725 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003726 {
Janos Follath315b51c2018-07-09 16:04:51 +01003727 status = mbedtls_to_psa_error( cipher_ret );
3728 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003729 }
Janos Follath315b51c2018-07-09 16:04:51 +01003730
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003731 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003732 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003733 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003734 memcpy( output, temp_output_buffer, *output_length );
3735 else
3736 {
Janos Follath315b51c2018-07-09 16:04:51 +01003737 status = PSA_ERROR_BUFFER_TOO_SMALL;
3738 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003739 }
mohammad1603503973b2018-03-12 15:59:30 +02003740
Gilles Peskine3f108122018-12-07 18:14:53 +01003741 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003742 status = psa_cipher_abort( operation );
3743
3744 return( status );
3745
3746error:
3747
3748 *output_length = 0;
3749
Gilles Peskine3f108122018-12-07 18:14:53 +01003750 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003751 (void) psa_cipher_abort( operation );
3752
3753 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003754}
3755
Gilles Peskinee553c652018-06-04 16:22:46 +02003756psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3757{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003758 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003759 {
3760 /* The object has (apparently) been initialized but it is not
3761 * in use. It's ok to call abort on such an object, and there's
3762 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003763 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003764 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003765
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003766 /* Sanity check (shouldn't happen: operation->alg should
3767 * always have been initialized to a valid value). */
3768 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3769 return( PSA_ERROR_BAD_STATE );
3770
mohammad1603503973b2018-03-12 15:59:30 +02003771 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003772
Moran Peker41deec42018-04-04 15:43:05 +03003773 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003774 operation->key_set = 0;
3775 operation->iv_set = 0;
3776 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003777 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003778 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003779
Moran Peker395db872018-05-31 14:07:14 +03003780 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003781}
3782
Gilles Peskinea0655c32018-04-30 17:06:50 +02003783
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003784
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003785
mohammad16035955c982018-04-26 00:53:03 +03003786/****************************************************************/
3787/* AEAD */
3788/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003789
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790typedef struct
3791{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003792 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003793 const mbedtls_cipher_info_t *cipher_info;
3794 union
3795 {
3796#if defined(MBEDTLS_CCM_C)
3797 mbedtls_ccm_context ccm;
3798#endif /* MBEDTLS_CCM_C */
3799#if defined(MBEDTLS_GCM_C)
3800 mbedtls_gcm_context gcm;
3801#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003802#if defined(MBEDTLS_CHACHAPOLY_C)
3803 mbedtls_chachapoly_context chachapoly;
3804#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003805 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003806 psa_algorithm_t core_alg;
3807 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003808 uint8_t tag_length;
3809} aead_operation_t;
3810
Gilles Peskine30a9e412019-01-14 18:36:12 +01003811static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003812{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003813 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003814 {
3815#if defined(MBEDTLS_CCM_C)
3816 case PSA_ALG_CCM:
3817 mbedtls_ccm_free( &operation->ctx.ccm );
3818 break;
3819#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003820#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003821 case PSA_ALG_GCM:
3822 mbedtls_gcm_free( &operation->ctx.gcm );
3823 break;
3824#endif /* MBEDTLS_GCM_C */
3825 }
3826}
3827
3828static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003829 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003830 psa_key_usage_t usage,
3831 psa_algorithm_t alg )
3832{
3833 psa_status_t status;
3834 size_t key_bits;
3835 mbedtls_cipher_id_t cipher_id;
3836
Gilles Peskine28f8f302019-07-24 13:30:31 +02003837 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003838 if( status != PSA_SUCCESS )
3839 return( status );
3840
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003841 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003842
3843 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003844 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003845 &cipher_id );
3846 if( operation->cipher_info == NULL )
3847 return( PSA_ERROR_NOT_SUPPORTED );
3848
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003849 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 {
3851#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003852 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3853 operation->core_alg = PSA_ALG_CCM;
3854 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003855 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3856 * The call to mbedtls_ccm_encrypt_and_tag or
3857 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003858 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003859 return( PSA_ERROR_INVALID_ARGUMENT );
3860 mbedtls_ccm_init( &operation->ctx.ccm );
3861 status = mbedtls_to_psa_error(
3862 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3863 operation->slot->data.raw.data,
3864 (unsigned int) key_bits ) );
3865 if( status != 0 )
3866 goto cleanup;
3867 break;
3868#endif /* MBEDTLS_CCM_C */
3869
3870#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003871 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3872 operation->core_alg = PSA_ALG_GCM;
3873 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003874 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3875 * The call to mbedtls_gcm_crypt_and_tag or
3876 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003877 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003878 return( PSA_ERROR_INVALID_ARGUMENT );
3879 mbedtls_gcm_init( &operation->ctx.gcm );
3880 status = mbedtls_to_psa_error(
3881 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3882 operation->slot->data.raw.data,
3883 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003884 if( status != 0 )
3885 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003886 break;
3887#endif /* MBEDTLS_GCM_C */
3888
Gilles Peskine26869f22019-05-06 15:25:00 +02003889#if defined(MBEDTLS_CHACHAPOLY_C)
3890 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3891 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3892 operation->full_tag_length = 16;
3893 /* We only support the default tag length. */
3894 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3895 return( PSA_ERROR_NOT_SUPPORTED );
3896 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3897 status = mbedtls_to_psa_error(
3898 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3899 operation->slot->data.raw.data ) );
3900 if( status != 0 )
3901 goto cleanup;
3902 break;
3903#endif /* MBEDTLS_CHACHAPOLY_C */
3904
Gilles Peskineedf9a652018-08-17 18:11:56 +02003905 default:
3906 return( PSA_ERROR_NOT_SUPPORTED );
3907 }
3908
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003909 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3910 {
3911 status = PSA_ERROR_INVALID_ARGUMENT;
3912 goto cleanup;
3913 }
3914 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003915
Gilles Peskineedf9a652018-08-17 18:11:56 +02003916 return( PSA_SUCCESS );
3917
3918cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003919 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003920 return( status );
3921}
3922
Gilles Peskinec5487a82018-12-03 18:08:14 +01003923psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003924 psa_algorithm_t alg,
3925 const uint8_t *nonce,
3926 size_t nonce_length,
3927 const uint8_t *additional_data,
3928 size_t additional_data_length,
3929 const uint8_t *plaintext,
3930 size_t plaintext_length,
3931 uint8_t *ciphertext,
3932 size_t ciphertext_size,
3933 size_t *ciphertext_length )
3934{
mohammad16035955c982018-04-26 00:53:03 +03003935 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003936 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003937 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003938
mohammad1603f08a5502018-06-03 15:05:47 +03003939 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003940
Gilles Peskinec5487a82018-12-03 18:08:14 +01003941 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003942 if( status != PSA_SUCCESS )
3943 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003944
Gilles Peskineedf9a652018-08-17 18:11:56 +02003945 /* For all currently supported modes, the tag is at the end of the
3946 * ciphertext. */
3947 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3948 {
3949 status = PSA_ERROR_BUFFER_TOO_SMALL;
3950 goto exit;
3951 }
3952 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003953
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003954#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003955 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003956 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003957 status = mbedtls_to_psa_error(
3958 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3959 MBEDTLS_GCM_ENCRYPT,
3960 plaintext_length,
3961 nonce, nonce_length,
3962 additional_data, additional_data_length,
3963 plaintext, ciphertext,
3964 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003965 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003966 else
3967#endif /* MBEDTLS_GCM_C */
3968#if defined(MBEDTLS_CCM_C)
3969 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003970 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003971 status = mbedtls_to_psa_error(
3972 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3973 plaintext_length,
3974 nonce, nonce_length,
3975 additional_data,
3976 additional_data_length,
3977 plaintext, ciphertext,
3978 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003979 }
mohammad16035c8845f2018-05-09 05:40:09 -07003980 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003981#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003982#if defined(MBEDTLS_CHACHAPOLY_C)
3983 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3984 {
3985 if( nonce_length != 12 || operation.tag_length != 16 )
3986 {
3987 status = PSA_ERROR_NOT_SUPPORTED;
3988 goto exit;
3989 }
3990 status = mbedtls_to_psa_error(
3991 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3992 plaintext_length,
3993 nonce,
3994 additional_data,
3995 additional_data_length,
3996 plaintext,
3997 ciphertext,
3998 tag ) );
3999 }
4000 else
4001#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004002 {
mohammad1603554faad2018-06-03 15:07:38 +03004003 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004004 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004005
Gilles Peskineedf9a652018-08-17 18:11:56 +02004006 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4007 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004008
Gilles Peskineedf9a652018-08-17 18:11:56 +02004009exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004010 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004011 if( status == PSA_SUCCESS )
4012 *ciphertext_length = plaintext_length + operation.tag_length;
4013 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004014}
4015
Gilles Peskineee652a32018-06-01 19:23:52 +02004016/* Locate the tag in a ciphertext buffer containing the encrypted data
4017 * followed by the tag. Return the length of the part preceding the tag in
4018 * *plaintext_length. This is the size of the plaintext in modes where
4019 * the encrypted data has the same size as the plaintext, such as
4020 * CCM and GCM. */
4021static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4022 const uint8_t *ciphertext,
4023 size_t ciphertext_length,
4024 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004025 const uint8_t **p_tag )
4026{
4027 size_t payload_length;
4028 if( tag_length > ciphertext_length )
4029 return( PSA_ERROR_INVALID_ARGUMENT );
4030 payload_length = ciphertext_length - tag_length;
4031 if( payload_length > plaintext_size )
4032 return( PSA_ERROR_BUFFER_TOO_SMALL );
4033 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004034 return( PSA_SUCCESS );
4035}
4036
Gilles Peskinec5487a82018-12-03 18:08:14 +01004037psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004038 psa_algorithm_t alg,
4039 const uint8_t *nonce,
4040 size_t nonce_length,
4041 const uint8_t *additional_data,
4042 size_t additional_data_length,
4043 const uint8_t *ciphertext,
4044 size_t ciphertext_length,
4045 uint8_t *plaintext,
4046 size_t plaintext_size,
4047 size_t *plaintext_length )
4048{
mohammad16035955c982018-04-26 00:53:03 +03004049 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004050 aead_operation_t operation;
4051 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004052
Gilles Peskineee652a32018-06-01 19:23:52 +02004053 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004054
Gilles Peskinec5487a82018-12-03 18:08:14 +01004055 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004056 if( status != PSA_SUCCESS )
4057 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004058
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004059 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4060 ciphertext, ciphertext_length,
4061 plaintext_size, &tag );
4062 if( status != PSA_SUCCESS )
4063 goto exit;
4064
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004065#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004066 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004067 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004068 status = mbedtls_to_psa_error(
4069 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4070 ciphertext_length - operation.tag_length,
4071 nonce, nonce_length,
4072 additional_data,
4073 additional_data_length,
4074 tag, operation.tag_length,
4075 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004076 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004077 else
4078#endif /* MBEDTLS_GCM_C */
4079#if defined(MBEDTLS_CCM_C)
4080 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004081 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004082 status = mbedtls_to_psa_error(
4083 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4084 ciphertext_length - operation.tag_length,
4085 nonce, nonce_length,
4086 additional_data,
4087 additional_data_length,
4088 ciphertext, plaintext,
4089 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004090 }
mohammad160339574652018-06-01 04:39:53 -07004091 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004092#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004093#if defined(MBEDTLS_CHACHAPOLY_C)
4094 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4095 {
4096 if( nonce_length != 12 || operation.tag_length != 16 )
4097 {
4098 status = PSA_ERROR_NOT_SUPPORTED;
4099 goto exit;
4100 }
4101 status = mbedtls_to_psa_error(
4102 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4103 ciphertext_length - operation.tag_length,
4104 nonce,
4105 additional_data,
4106 additional_data_length,
4107 tag,
4108 ciphertext,
4109 plaintext ) );
4110 }
4111 else
4112#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004113 {
mohammad1603554faad2018-06-03 15:07:38 +03004114 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004115 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004116
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 if( status != PSA_SUCCESS && plaintext_size != 0 )
4118 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004119
Gilles Peskineedf9a652018-08-17 18:11:56 +02004120exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004121 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 if( status == PSA_SUCCESS )
4123 *plaintext_length = ciphertext_length - operation.tag_length;
4124 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004125}
4126
Gilles Peskinea0655c32018-04-30 17:06:50 +02004127
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004128
Gilles Peskine20035e32018-02-03 22:44:14 +01004129/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004130/* Generators */
4131/****************************************************************/
4132
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004133#define HKDF_STATE_INIT 0 /* no input yet */
4134#define HKDF_STATE_STARTED 1 /* got salt */
4135#define HKDF_STATE_KEYED 2 /* got key */
4136#define HKDF_STATE_OUTPUT 3 /* output started */
4137
Gilles Peskinecbe66502019-05-16 16:59:18 +02004138static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004139 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004140{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004141 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4142 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004143 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004145}
4146
4147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004148psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004149{
4150 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004152 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004153 {
4154 /* The object has (apparently) been initialized but it is not
4155 * in use. It's ok to call abort on such an object, and there's
4156 * nothing to do. */
4157 }
4158 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004159#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004160 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004161 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004162 mbedtls_free( operation->ctx.hkdf.info );
4163 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004164 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004165 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004166 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004167 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004168 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004169#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004171 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4173 operation->ctx.tls12_prf.key_len );
4174 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004175 }
Hanno Becker580fba12018-11-13 20:50:45 +00004176
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004178 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4180 operation->ctx.tls12_prf.Ai_with_seed_len );
4181 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004182 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004183#else
4184 if( operation->ctx.tls12_prf.seed != NULL )
4185 {
4186 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4187 operation->ctx.tls12_prf.seed_length );
4188 mbedtls_free( operation->ctx.tls12_prf.seed );
4189 }
4190
4191 if( operation->ctx.tls12_prf.label != NULL )
4192 {
4193 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4194 operation->ctx.tls12_prf.label_length );
4195 mbedtls_free( operation->ctx.tls12_prf.label );
4196 }
4197
4198 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4199
4200 /* We leave the fields Ai and output_block to be erased safely by the
4201 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004202#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004203 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004204 else
4205#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004206 {
4207 status = PSA_ERROR_BAD_STATE;
4208 }
Janos Follath083036a2019-06-11 10:22:26 +01004209 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210 return( status );
4211}
4212
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004213psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004214 size_t *capacity)
4215{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004216 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004217 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004219 return PSA_ERROR_BAD_STATE;
4220 }
4221
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004222 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004223 return( PSA_SUCCESS );
4224}
4225
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004226psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004227 size_t capacity )
4228{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004229 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004230 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004231 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004232 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004233 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004234 return( PSA_SUCCESS );
4235}
4236
Gilles Peskinebef7f142018-07-12 17:22:21 +02004237#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004238/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004239 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004240static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004241 psa_algorithm_t hash_alg,
4242 uint8_t *output,
4243 size_t output_length )
4244{
4245 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4246 psa_status_t status;
4247
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004248 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4249 return( PSA_ERROR_BAD_STATE );
4250 hkdf->state = HKDF_STATE_OUTPUT;
4251
Gilles Peskinebef7f142018-07-12 17:22:21 +02004252 while( output_length != 0 )
4253 {
4254 /* Copy what remains of the current block */
4255 uint8_t n = hash_length - hkdf->offset_in_block;
4256 if( n > output_length )
4257 n = (uint8_t) output_length;
4258 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4259 output += n;
4260 output_length -= n;
4261 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004262 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004263 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004264 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004265 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004266 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004267 * object was corrupted or if this function is called directly
4268 * inside the library. */
4269 if( hkdf->block_number == 0xff )
4270 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004271
4272 /* We need a new block */
4273 ++hkdf->block_number;
4274 hkdf->offset_in_block = 0;
4275 status = psa_hmac_setup_internal( &hkdf->hmac,
4276 hkdf->prk, hash_length,
4277 hash_alg );
4278 if( status != PSA_SUCCESS )
4279 return( status );
4280 if( hkdf->block_number != 1 )
4281 {
4282 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4283 hkdf->output_block,
4284 hash_length );
4285 if( status != PSA_SUCCESS )
4286 return( status );
4287 }
4288 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4289 hkdf->info,
4290 hkdf->info_length );
4291 if( status != PSA_SUCCESS )
4292 return( status );
4293 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4294 &hkdf->block_number, 1 );
4295 if( status != PSA_SUCCESS )
4296 return( status );
4297 status = psa_hmac_finish_internal( &hkdf->hmac,
4298 hkdf->output_block,
4299 sizeof( hkdf->output_block ) );
4300 if( status != PSA_SUCCESS )
4301 return( status );
4302 }
4303
4304 return( PSA_SUCCESS );
4305}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004306
Janos Follath999f6482019-06-11 12:04:10 +01004307#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004308static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4309 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004310 psa_algorithm_t alg )
4311{
4312 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4313 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4314 psa_hmac_internal_data hmac;
4315 psa_status_t status, cleanup_status;
4316
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004317 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004318 size_t Ai_len;
4319
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004320 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004321 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004323 * object was corrupted or if this function is called directly
4324 * inside the library. */
4325 if( tls12_prf->block_number == 0xff )
4326 return( PSA_ERROR_BAD_STATE );
4327
4328 /* We need a new block */
4329 ++tls12_prf->block_number;
4330 tls12_prf->offset_in_block = 0;
4331
4332 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4333 *
4334 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4335 *
4336 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4337 * HMAC_hash(secret, A(2) + seed) +
4338 * HMAC_hash(secret, A(3) + seed) + ...
4339 *
4340 * A(0) = seed
4341 * A(i) = HMAC_hash( secret, A(i-1) )
4342 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004343 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004344 * `HMAC_hash(secret, A(i) + seed)` from which the output
4345 * is currently extracted as `output_block`, while
4346 * `A(i) + seed` is stored in `Ai_with_seed`.
4347 *
4348 * Generating a new block means recalculating `Ai_with_seed`
4349 * from the A(i)-part of it, and afterwards recalculating
4350 * `output_block`.
4351 *
4352 * A(0) is computed at setup time.
4353 *
4354 */
4355
4356 psa_hmac_init_internal( &hmac );
4357
4358 /* We must distinguish the calculation of A(1) from those
4359 * of A(2) and higher, because A(0)=seed has a different
4360 * length than the other A(i). */
4361 if( tls12_prf->block_number == 1 )
4362 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004363 Ai = tls12_prf->Ai_with_seed + hash_length;
4364 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004365 }
4366 else
4367 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004368 Ai = tls12_prf->Ai_with_seed;
4369 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370 }
4371
Hanno Becker3b339e22018-11-13 20:56:14 +00004372 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4373 status = psa_hmac_setup_internal( &hmac,
4374 tls12_prf->key,
4375 tls12_prf->key_len,
4376 hash_alg );
4377 if( status != PSA_SUCCESS )
4378 goto cleanup;
4379
4380 status = psa_hash_update( &hmac.hash_ctx,
4381 Ai, Ai_len );
4382 if( status != PSA_SUCCESS )
4383 goto cleanup;
4384
4385 status = psa_hmac_finish_internal( &hmac,
4386 tls12_prf->Ai_with_seed,
4387 hash_length );
4388 if( status != PSA_SUCCESS )
4389 goto cleanup;
4390
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004391 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4392 status = psa_hmac_setup_internal( &hmac,
4393 tls12_prf->key,
4394 tls12_prf->key_len,
4395 hash_alg );
4396 if( status != PSA_SUCCESS )
4397 goto cleanup;
4398
4399 status = psa_hash_update( &hmac.hash_ctx,
4400 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004401 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004402 if( status != PSA_SUCCESS )
4403 goto cleanup;
4404
4405 status = psa_hmac_finish_internal( &hmac,
4406 tls12_prf->output_block,
4407 hash_length );
4408 if( status != PSA_SUCCESS )
4409 goto cleanup;
4410
4411cleanup:
4412
4413 cleanup_status = psa_hmac_abort_internal( &hmac );
4414 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4415 status = cleanup_status;
4416
4417 return( status );
4418}
Janos Follath7742fee2019-06-17 12:58:10 +01004419#else
4420static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4421 psa_tls12_prf_key_derivation_t *tls12_prf,
4422 psa_algorithm_t alg )
4423{
4424 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4425 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004426 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4427 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004428
Janos Follath7742fee2019-06-17 12:58:10 +01004429 /* We can't be wanting more output after block 0xff, otherwise
4430 * the capacity check in psa_key_derivation_output_bytes() would have
4431 * prevented this call. It could happen only if the operation
4432 * object was corrupted or if this function is called directly
4433 * inside the library. */
4434 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004435 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004436
4437 /* We need a new block */
4438 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004439 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004440
4441 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4442 *
4443 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4444 *
4445 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4446 * HMAC_hash(secret, A(2) + seed) +
4447 * HMAC_hash(secret, A(3) + seed) + ...
4448 *
4449 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004450 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004451 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004452 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004453 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004454 * is currently extracted as `output_block` and where i is
4455 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004456 */
4457
Janos Follathea29bfb2019-06-19 12:21:20 +01004458 /* Save the hash context before using it, to preserve the hash state with
4459 * only the inner padding in it. We need this, because inner padding depends
4460 * on the key (secret in the RFC's terminology). */
4461 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4462 if( status != PSA_SUCCESS )
4463 goto cleanup;
4464
4465 /* Calculate A(i) where i = tls12_prf->block_number. */
4466 if( tls12_prf->block_number == 1 )
4467 {
4468 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4469 * the variable seed and in this instance means it in the context of the
4470 * P_hash function, where seed = label + seed.) */
4471 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4472 tls12_prf->label, tls12_prf->label_length );
4473 if( status != PSA_SUCCESS )
4474 goto cleanup;
4475 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4476 tls12_prf->seed, tls12_prf->seed_length );
4477 if( status != PSA_SUCCESS )
4478 goto cleanup;
4479 }
4480 else
4481 {
4482 /* A(i) = HMAC_hash(secret, A(i-1)) */
4483 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4484 tls12_prf->Ai, hash_length );
4485 if( status != PSA_SUCCESS )
4486 goto cleanup;
4487 }
4488
4489 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4490 tls12_prf->Ai, hash_length );
4491 if( status != PSA_SUCCESS )
4492 goto cleanup;
4493 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4494 if( status != PSA_SUCCESS )
4495 goto cleanup;
4496
4497 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4498 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4499 tls12_prf->Ai, hash_length );
4500 if( status != PSA_SUCCESS )
4501 goto cleanup;
4502 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4503 tls12_prf->label, tls12_prf->label_length );
4504 if( status != PSA_SUCCESS )
4505 goto cleanup;
4506 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4507 tls12_prf->seed, tls12_prf->seed_length );
4508 if( status != PSA_SUCCESS )
4509 goto cleanup;
4510 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4511 tls12_prf->output_block, hash_length );
4512 if( status != PSA_SUCCESS )
4513 goto cleanup;
4514 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4515 if( status != PSA_SUCCESS )
4516 goto cleanup;
4517
Janos Follath7742fee2019-06-17 12:58:10 +01004518
4519cleanup:
4520
Janos Follathea29bfb2019-06-19 12:21:20 +01004521 cleanup_status = psa_hash_abort( &backup );
4522 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4523 status = cleanup_status;
4524
4525 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004526}
Janos Follath999f6482019-06-11 12:04:10 +01004527#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004528
Janos Follath999f6482019-06-11 12:04:10 +01004529#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004531 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004532static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004533 psa_tls12_prf_key_derivation_t *tls12_prf,
4534 psa_algorithm_t alg,
4535 uint8_t *output,
4536 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004537{
4538 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4539 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4540 psa_status_t status;
4541
4542 while( output_length != 0 )
4543 {
4544 /* Copy what remains of the current block */
4545 uint8_t n = hash_length - tls12_prf->offset_in_block;
4546
4547 /* Check if we have fully processed the current block. */
4548 if( n == 0 )
4549 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004550 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004551 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004552 if( status != PSA_SUCCESS )
4553 return( status );
4554
4555 continue;
4556 }
4557
4558 if( n > output_length )
4559 n = (uint8_t) output_length;
4560 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4561 n );
4562 output += n;
4563 output_length -= n;
4564 tls12_prf->offset_in_block += n;
4565 }
4566
4567 return( PSA_SUCCESS );
4568}
Janos Follath844eb0e2019-06-19 12:10:49 +01004569#else
4570static psa_status_t psa_key_derivation_tls12_prf_read(
4571 psa_tls12_prf_key_derivation_t *tls12_prf,
4572 psa_algorithm_t alg,
4573 uint8_t *output,
4574 size_t output_length )
4575{
4576 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4577 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4578 psa_status_t status;
4579 uint8_t offset, length;
4580
4581 while( output_length != 0 )
4582 {
4583 /* Check if we have fully processed the current block. */
4584 if( tls12_prf->left_in_block == 0 )
4585 {
4586 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4587 alg );
4588 if( status != PSA_SUCCESS )
4589 return( status );
4590
4591 continue;
4592 }
4593
4594 if( tls12_prf->left_in_block > output_length )
4595 length = (uint8_t) output_length;
4596 else
4597 length = tls12_prf->left_in_block;
4598
4599 offset = hash_length - tls12_prf->left_in_block;
4600 memcpy( output, tls12_prf->output_block + offset, length );
4601 output += length;
4602 output_length -= length;
4603 tls12_prf->left_in_block -= length;
4604 }
4605
4606 return( PSA_SUCCESS );
4607}
Janos Follath999f6482019-06-11 12:04:10 +01004608#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004609#endif /* MBEDTLS_MD_C */
4610
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004611psa_status_t psa_key_derivation_output_bytes(
4612 psa_key_derivation_operation_t *operation,
4613 uint8_t *output,
4614 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004615{
4616 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004618
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004620 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004621 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004622 return PSA_ERROR_BAD_STATE;
4623 }
4624
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004626 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004628 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004629 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004630 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004631 goto exit;
4632 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004634 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004636 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004637 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4638 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004640 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004641 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004642 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004643 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004644
Gilles Peskinebef7f142018-07-12 17:22:21 +02004645#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004646 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004647 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004648 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004650 output, output_length );
4651 }
Janos Follathadbec812019-06-14 11:05:39 +01004652 else
Janos Follathadbec812019-06-14 11:05:39 +01004653 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004654 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004655 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004657 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004658 output_length );
4659 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004660 else
4661#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004662 {
4663 return( PSA_ERROR_BAD_STATE );
4664 }
4665
4666exit:
4667 if( status != PSA_SUCCESS )
4668 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004669 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 * This allows us to differentiate between exhausted operations and
4671 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4672 * operations. */
4673 psa_algorithm_t alg = operation->alg;
4674 psa_key_derivation_abort( operation );
4675 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004676 memset( output, '!', output_length );
4677 }
4678 return( status );
4679}
4680
Gilles Peskine08542d82018-07-19 17:05:42 +02004681#if defined(MBEDTLS_DES_C)
4682static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4683{
4684 if( data_size >= 8 )
4685 mbedtls_des_key_set_parity( data );
4686 if( data_size >= 16 )
4687 mbedtls_des_key_set_parity( data + 8 );
4688 if( data_size >= 24 )
4689 mbedtls_des_key_set_parity( data + 16 );
4690}
4691#endif /* MBEDTLS_DES_C */
4692
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004693static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004694 psa_key_slot_t *slot,
4695 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004697{
4698 uint8_t *data = NULL;
4699 size_t bytes = PSA_BITS_TO_BYTES( bits );
4700 psa_status_t status;
4701
Gilles Peskine8e338702019-07-30 20:06:31 +02004702 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004703 return( PSA_ERROR_INVALID_ARGUMENT );
4704 if( bits % 8 != 0 )
4705 return( PSA_ERROR_INVALID_ARGUMENT );
4706 data = mbedtls_calloc( 1, bytes );
4707 if( data == NULL )
4708 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004711 if( status != PSA_SUCCESS )
4712 goto exit;
4713#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004714 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004715 psa_des_set_key_parity( data, bytes );
4716#endif /* MBEDTLS_DES_C */
4717 status = psa_import_key_into_slot( slot, data, bytes );
4718
4719exit:
4720 mbedtls_free( data );
4721 return( status );
4722}
4723
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004724psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004726 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004727{
4728 psa_status_t status;
4729 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004730 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004731 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004732#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4733 if( driver != NULL )
4734 {
4735 /* Deriving a key in a secure element is not implemented yet. */
4736 status = PSA_ERROR_NOT_SUPPORTED;
4737 }
4738#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004739 if( status == PSA_SUCCESS )
4740 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004741 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004742 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004744 }
4745 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004746 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004747 if( status != PSA_SUCCESS )
4748 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004749 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004750 *handle = 0;
4751 }
4752 return( status );
4753}
4754
Gilles Peskineeab56e42018-07-12 17:12:33 +02004755
4756
4757/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004758/* Key derivation */
4759/****************************************************************/
4760
Gilles Peskinea05219c2018-11-16 16:02:56 +01004761#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004762#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004764 * of the HKDF algorithm.
4765 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004766 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004767 * to potentially free embedded data structures and wipe confidential data.
4768 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004769static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004770 const uint8_t *secret,
4771 size_t secret_length,
4772 psa_algorithm_t hash_alg,
4773 const uint8_t *salt,
4774 size_t salt_length,
4775 const uint8_t *label,
4776 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004777{
4778 psa_status_t status;
4779 status = psa_hmac_setup_internal( &hkdf->hmac,
4780 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004781 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004782 if( status != PSA_SUCCESS )
4783 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004784 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004785 if( status != PSA_SUCCESS )
4786 return( status );
4787 status = psa_hmac_finish_internal( &hkdf->hmac,
4788 hkdf->prk,
4789 sizeof( hkdf->prk ) );
4790 if( status != PSA_SUCCESS )
4791 return( status );
4792 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4793 hkdf->block_number = 0;
4794 hkdf->info_length = label_length;
4795 if( label_length != 0 )
4796 {
4797 hkdf->info = mbedtls_calloc( 1, label_length );
4798 if( hkdf->info == NULL )
4799 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4800 memcpy( hkdf->info, label, label_length );
4801 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004802 hkdf->state = HKDF_STATE_KEYED;
4803 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004804 return( PSA_SUCCESS );
4805}
Janos Follath71a4c912019-06-11 09:14:47 +01004806#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004807#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004808
Gilles Peskinea05219c2018-11-16 16:02:56 +01004809#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004810#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004812 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004813 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004814 * to potentially free embedded data structures and wipe confidential data.
4815 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004816static psa_status_t psa_key_derivation_tls12_prf_setup(
4817 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004818 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004819 size_t key_len,
4820 psa_algorithm_t hash_alg,
4821 const uint8_t *salt,
4822 size_t salt_length,
4823 const uint8_t *label,
4824 size_t label_length )
4825{
4826 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004827 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4828 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004829
4830 tls12_prf->key = mbedtls_calloc( 1, key_len );
4831 if( tls12_prf->key == NULL )
4832 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4833 tls12_prf->key_len = key_len;
4834 memcpy( tls12_prf->key, key, key_len );
4835
Hanno Becker580fba12018-11-13 20:50:45 +00004836 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004837 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004838 if( overflow )
4839 return( PSA_ERROR_INVALID_ARGUMENT );
4840
4841 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4842 if( tls12_prf->Ai_with_seed == NULL )
4843 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4844 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4845
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004846 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4847 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004848 if( label_length != 0 )
4849 {
4850 memcpy( tls12_prf->Ai_with_seed + hash_length,
4851 label, label_length );
4852 }
4853
4854 if( salt_length != 0 )
4855 {
4856 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4857 salt, salt_length );
4858 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004859
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004860 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004861 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004862 tls12_prf->block_number = 0;
4863 tls12_prf->offset_in_block = hash_length;
4864
4865 return( PSA_SUCCESS );
4866}
Janos Follath71a4c912019-06-11 09:14:47 +01004867#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004868
Janos Follath71a4c912019-06-11 09:14:47 +01004869#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004871static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4872 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004873 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004874 size_t psk_len,
4875 psa_algorithm_t hash_alg,
4876 const uint8_t *salt,
4877 size_t salt_length,
4878 const uint8_t *label,
4879 size_t label_length )
4880{
4881 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004882 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004883
4884 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4885 return( PSA_ERROR_INVALID_ARGUMENT );
4886
4887 /* Quoting RFC 4279, Section 2:
4888 *
4889 * The premaster secret is formed as follows: if the PSK is N octets
4890 * long, concatenate a uint16 with the value N, N zero octets, a second
4891 * uint16 with the value N, and the PSK itself.
4892 */
4893
4894 pms[0] = ( psk_len >> 8 ) & 0xff;
4895 pms[1] = ( psk_len >> 0 ) & 0xff;
4896 memset( pms + 2, 0, psk_len );
4897 pms[2 + psk_len + 0] = pms[0];
4898 pms[2 + psk_len + 1] = pms[1];
4899 memcpy( pms + 4 + psk_len, psk, psk_len );
4900
Gilles Peskinecbe66502019-05-16 16:59:18 +02004901 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004902 pms, 4 + 2 * psk_len,
4903 hash_alg,
4904 salt, salt_length,
4905 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004906
Gilles Peskine3f108122018-12-07 18:14:53 +01004907 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004908 return( status );
4909}
Janos Follath71a4c912019-06-11 09:14:47 +01004910#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004911#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004912
Janos Follath71a4c912019-06-11 09:14:47 +01004913#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004914/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004915 * to potentially free embedded data structures and wipe confidential data.
4916 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004917static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004919 const uint8_t *secret, size_t secret_length,
4920 psa_algorithm_t alg,
4921 const uint8_t *salt, size_t salt_length,
4922 const uint8_t *label, size_t label_length,
4923 size_t capacity )
4924{
4925 psa_status_t status;
4926 size_t max_capacity;
4927
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 /* Set operation->alg even on failure so that abort knows what to do. */
4929 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004930
4931#if defined(MBEDTLS_MD_C)
4932 if( PSA_ALG_IS_HKDF( alg ) )
4933 {
4934 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4935 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4936 if( hash_size == 0 )
4937 return( PSA_ERROR_NOT_SUPPORTED );
4938 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004940 secret, secret_length,
4941 hash_alg,
4942 salt, salt_length,
4943 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004944 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004945 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4946 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4947 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004948 {
4949 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4950 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4951
4952 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4953 if( hash_alg != PSA_ALG_SHA_256 &&
4954 hash_alg != PSA_ALG_SHA_384 )
4955 {
4956 return( PSA_ERROR_NOT_SUPPORTED );
4957 }
4958
4959 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004960
4961 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4962 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004964 secret, secret_length,
4965 hash_alg, salt, salt_length,
4966 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004967 }
4968 else
4969 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004970 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004972 secret, secret_length,
4973 hash_alg, salt, salt_length,
4974 label, label_length );
4975 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004976 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004977 else
4978#endif
4979 {
4980 return( PSA_ERROR_NOT_SUPPORTED );
4981 }
4982
4983 if( status != PSA_SUCCESS )
4984 return( status );
4985
4986 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004988 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004990 else
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992
4993 return( PSA_SUCCESS );
4994}
Janos Follath71a4c912019-06-11 09:14:47 +01004995#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004996
Janos Follath71a4c912019-06-11 09:14:47 +01004997#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004999 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005000 psa_algorithm_t alg,
5001 const uint8_t *salt,
5002 size_t salt_length,
5003 const uint8_t *label,
5004 size_t label_length,
5005 size_t capacity )
5006{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005007 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005008 psa_status_t status;
5009
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005010 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005011 return( PSA_ERROR_BAD_STATE );
5012
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005013 /* Make sure that alg is a key derivation algorithm. This prevents
5014 * key selection algorithms, which psa_key_derivation_internal
5015 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005016 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5017 return( PSA_ERROR_INVALID_ARGUMENT );
5018
Gilles Peskine28f8f302019-07-24 13:30:31 +02005019 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005020 if( status != PSA_SUCCESS )
5021 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005022
Gilles Peskine8e338702019-07-30 20:06:31 +02005023 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005024 return( PSA_ERROR_INVALID_ARGUMENT );
5025
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005027 slot->data.raw.data,
5028 slot->data.raw.bytes,
5029 alg,
5030 salt, salt_length,
5031 label, label_length,
5032 capacity );
5033 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005035 return( status );
5036}
Janos Follath71a4c912019-06-11 09:14:47 +01005037#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005038
Gilles Peskine969c5d62019-01-16 15:53:06 +01005039static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005040 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005042{
Janos Follath5fe19732019-06-20 15:09:30 +01005043 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5044 * initialisers for this union leave some bytes unspecified.) */
5045 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5046
Gilles Peskine969c5d62019-01-16 15:53:06 +01005047 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005048#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005049 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5050 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5051 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005052 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005053 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005054 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5055 if( hash_size == 0 )
5056 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005057 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5058 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005059 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005060 {
5061 return( PSA_ERROR_NOT_SUPPORTED );
5062 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005064 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065 }
5066#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005067 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005068 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005069}
5070
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 psa_algorithm_t alg )
5073{
5074 psa_status_t status;
5075
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005076 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005077 return( PSA_ERROR_BAD_STATE );
5078
Gilles Peskine6843c292019-01-18 16:44:49 +01005079 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5080 return( PSA_ERROR_INVALID_ARGUMENT );
5081 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005082 {
5083 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085 }
5086 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5087 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005089 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005090 else
5091 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005092
5093 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005095 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096}
5097
5098#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005099static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005100 psa_algorithm_t hash_alg,
5101 psa_key_derivation_step_t step,
5102 const uint8_t *data,
5103 size_t data_length )
5104{
5105 psa_status_t status;
5106 switch( step )
5107 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005108 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005109 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005110 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005111 status = psa_hmac_setup_internal( &hkdf->hmac,
5112 data, data_length,
5113 hash_alg );
5114 if( status != PSA_SUCCESS )
5115 return( status );
5116 hkdf->state = HKDF_STATE_STARTED;
5117 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005118 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005119 /* If no salt was provided, use an empty salt. */
5120 if( hkdf->state == HKDF_STATE_INIT )
5121 {
5122 status = psa_hmac_setup_internal( &hkdf->hmac,
5123 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005124 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005125 if( status != PSA_SUCCESS )
5126 return( status );
5127 hkdf->state = HKDF_STATE_STARTED;
5128 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005129 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005131 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5132 data, data_length );
5133 if( status != PSA_SUCCESS )
5134 return( status );
5135 status = psa_hmac_finish_internal( &hkdf->hmac,
5136 hkdf->prk,
5137 sizeof( hkdf->prk ) );
5138 if( status != PSA_SUCCESS )
5139 return( status );
5140 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5141 hkdf->block_number = 0;
5142 hkdf->state = HKDF_STATE_KEYED;
5143 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005144 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 if( hkdf->state == HKDF_STATE_OUTPUT )
5146 return( PSA_ERROR_BAD_STATE );
5147 if( hkdf->info_set )
5148 return( PSA_ERROR_BAD_STATE );
5149 hkdf->info_length = data_length;
5150 if( data_length != 0 )
5151 {
5152 hkdf->info = mbedtls_calloc( 1, data_length );
5153 if( hkdf->info == NULL )
5154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5155 memcpy( hkdf->info, data, data_length );
5156 }
5157 hkdf->info_set = 1;
5158 return( PSA_SUCCESS );
5159 default:
5160 return( PSA_ERROR_INVALID_ARGUMENT );
5161 }
5162}
Janos Follathb03233e2019-06-11 15:30:30 +01005163
5164#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5165static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5166 psa_algorithm_t hash_alg,
5167 psa_key_derivation_step_t step,
5168 const uint8_t *data,
5169 size_t data_length )
5170{
5171 (void) prf;
5172 (void) hash_alg;
5173 (void) step;
5174 (void) data;
5175 (void) data_length;
5176
5177 return( PSA_ERROR_INVALID_ARGUMENT );
5178}
Janos Follath6660f0e2019-06-17 08:44:03 +01005179
5180static psa_status_t psa_tls12_prf_psk_to_ms_input(
5181 psa_tls12_prf_key_derivation_t *prf,
5182 psa_algorithm_t hash_alg,
5183 psa_key_derivation_step_t step,
5184 const uint8_t *data,
5185 size_t data_length )
5186{
5187 (void) prf;
5188 (void) hash_alg;
5189 (void) step;
5190 (void) data;
5191 (void) data_length;
5192
5193 return( PSA_ERROR_INVALID_ARGUMENT );
5194}
Janos Follathb03233e2019-06-11 15:30:30 +01005195#else
Janos Follathf08e2652019-06-13 09:05:41 +01005196static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5197 const uint8_t *data,
5198 size_t data_length )
5199{
5200 if( prf->state != TLS12_PRF_STATE_INIT )
5201 return( PSA_ERROR_BAD_STATE );
5202
Janos Follathd6dce9f2019-07-04 09:11:38 +01005203 if( data_length != 0 )
5204 {
5205 prf->seed = mbedtls_calloc( 1, data_length );
5206 if( prf->seed == NULL )
5207 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005208
Janos Follathd6dce9f2019-07-04 09:11:38 +01005209 memcpy( prf->seed, data, data_length );
5210 prf->seed_length = data_length;
5211 }
Janos Follathf08e2652019-06-13 09:05:41 +01005212
5213 prf->state = TLS12_PRF_STATE_SEED_SET;
5214
5215 return( PSA_SUCCESS );
5216}
5217
Janos Follath81550542019-06-13 14:26:34 +01005218static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5219 psa_algorithm_t hash_alg,
5220 const uint8_t *data,
5221 size_t data_length )
5222{
5223 psa_status_t status;
5224 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5225 return( PSA_ERROR_BAD_STATE );
5226
5227 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5228 if( status != PSA_SUCCESS )
5229 return( status );
5230
5231 prf->state = TLS12_PRF_STATE_KEY_SET;
5232
5233 return( PSA_SUCCESS );
5234}
5235
Janos Follath6660f0e2019-06-17 08:44:03 +01005236static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5237 psa_tls12_prf_key_derivation_t *prf,
5238 psa_algorithm_t hash_alg,
5239 const uint8_t *data,
5240 size_t data_length )
5241{
5242 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005243 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5244 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005245
5246 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5247 return( PSA_ERROR_INVALID_ARGUMENT );
5248
5249 /* Quoting RFC 4279, Section 2:
5250 *
5251 * The premaster secret is formed as follows: if the PSK is N octets
5252 * long, concatenate a uint16 with the value N, N zero octets, a second
5253 * uint16 with the value N, and the PSK itself.
5254 */
5255
Janos Follath40e13932019-06-26 13:22:29 +01005256 *cur++ = ( data_length >> 8 ) & 0xff;
5257 *cur++ = ( data_length >> 0 ) & 0xff;
5258 memset( cur, 0, data_length );
5259 cur += data_length;
5260 *cur++ = pms[0];
5261 *cur++ = pms[1];
5262 memcpy( cur, data, data_length );
5263 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005264
Janos Follath40e13932019-06-26 13:22:29 +01005265 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005266
5267 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5268 return( status );
5269}
5270
Janos Follath63028dd2019-06-13 09:15:47 +01005271static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5272 const uint8_t *data,
5273 size_t data_length )
5274{
5275 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5276 return( PSA_ERROR_BAD_STATE );
5277
Janos Follathd6dce9f2019-07-04 09:11:38 +01005278 if( data_length != 0 )
5279 {
5280 prf->label = mbedtls_calloc( 1, data_length );
5281 if( prf->label == NULL )
5282 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005283
Janos Follathd6dce9f2019-07-04 09:11:38 +01005284 memcpy( prf->label, data, data_length );
5285 prf->label_length = data_length;
5286 }
Janos Follath63028dd2019-06-13 09:15:47 +01005287
5288 prf->state = TLS12_PRF_STATE_LABEL_SET;
5289
5290 return( PSA_SUCCESS );
5291}
5292
Janos Follathb03233e2019-06-11 15:30:30 +01005293static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5294 psa_algorithm_t hash_alg,
5295 psa_key_derivation_step_t step,
5296 const uint8_t *data,
5297 size_t data_length )
5298{
Janos Follathb03233e2019-06-11 15:30:30 +01005299 switch( step )
5300 {
Janos Follathf08e2652019-06-13 09:05:41 +01005301 case PSA_KEY_DERIVATION_INPUT_SEED:
5302 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005303 case PSA_KEY_DERIVATION_INPUT_SECRET:
5304 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005305 case PSA_KEY_DERIVATION_INPUT_LABEL:
5306 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005307 default:
5308 return( PSA_ERROR_INVALID_ARGUMENT );
5309 }
5310}
Janos Follath6660f0e2019-06-17 08:44:03 +01005311
5312static psa_status_t psa_tls12_prf_psk_to_ms_input(
5313 psa_tls12_prf_key_derivation_t *prf,
5314 psa_algorithm_t hash_alg,
5315 psa_key_derivation_step_t step,
5316 const uint8_t *data,
5317 size_t data_length )
5318{
5319 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005320 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005321 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5322 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005323 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005324
5325 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5326}
Janos Follathb03233e2019-06-11 15:30:30 +01005327#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005328#endif /* MBEDTLS_MD_C */
5329
Janos Follathb80a94e2019-06-12 15:54:46 +01005330static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005332 psa_key_derivation_step_t step,
5333 const uint8_t *data,
5334 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335{
5336 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338
5339#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005340 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005341 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005343 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005344 step, data, data_length );
5345 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005346 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005347#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005348#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005349 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350 {
Janos Follathb03233e2019-06-11 15:30:30 +01005351 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5352 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5353 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005354 }
5355 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5356 {
5357 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5358 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5359 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005360 }
5361 else
5362#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005364 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365 return( PSA_ERROR_BAD_STATE );
5366 }
5367
5368 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005370 return( status );
5371}
5372
Janos Follath51f4a0f2019-06-14 11:35:55 +01005373psa_status_t psa_key_derivation_input_bytes(
5374 psa_key_derivation_operation_t *operation,
5375 psa_key_derivation_step_t step,
5376 const uint8_t *data,
5377 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005378{
Janos Follathc5621512019-06-14 11:27:57 +01005379 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5380 return( PSA_ERROR_INVALID_ARGUMENT );
5381
5382 return( psa_key_derivation_input_internal( operation, step,
5383 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005384}
5385
Janos Follath51f4a0f2019-06-14 11:35:55 +01005386psa_status_t psa_key_derivation_input_key(
5387 psa_key_derivation_operation_t *operation,
5388 psa_key_derivation_step_t step,
5389 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005390{
5391 psa_key_slot_t *slot;
5392 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005393 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005394 PSA_KEY_USAGE_DERIVE,
5395 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005396 if( status != PSA_SUCCESS )
5397 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005398 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005399 return( PSA_ERROR_INVALID_ARGUMENT );
5400 /* Don't allow a key to be used as an input that is usually public.
5401 * This is debatable. It's ok from a cryptographic perspective to
5402 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005403 * the material should be dedicated to a particular input step,
5404 * otherwise this may allow the key to be used in an unintended way
5405 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005406 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005407 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005408 return( psa_key_derivation_input_internal( operation,
5409 step,
5410 slot->data.raw.data,
5411 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005412}
5413
Gilles Peskineea0fb492018-07-12 17:17:20 +02005414
5415
5416/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005417/* Key agreement */
5418/****************************************************************/
5419
Gilles Peskinea05219c2018-11-16 16:02:56 +01005420#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005421static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5422 size_t peer_key_length,
5423 const mbedtls_ecp_keypair *our_key,
5424 uint8_t *shared_secret,
5425 size_t shared_secret_size,
5426 size_t *shared_secret_length )
5427{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005428 mbedtls_ecp_keypair *their_key = NULL;
5429 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005430 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005431 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005432
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005433 status = psa_import_ec_public_key(
5434 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5435 peer_key, peer_key_length,
5436 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005437 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005438 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005439
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005440 status = mbedtls_to_psa_error(
5441 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5442 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005443 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005444 status = mbedtls_to_psa_error(
5445 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5446 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005447 goto exit;
5448
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005449 status = mbedtls_to_psa_error(
5450 mbedtls_ecdh_calc_secret( &ecdh,
5451 shared_secret_length,
5452 shared_secret, shared_secret_size,
5453 mbedtls_ctr_drbg_random,
5454 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005455
5456exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005457 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005458 mbedtls_ecp_keypair_free( their_key );
5459 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005460 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005461}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005462#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005463
Gilles Peskine01d718c2018-09-18 12:01:02 +02005464#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5465
Gilles Peskine0216fe12019-04-11 21:23:21 +02005466static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5467 psa_key_slot_t *private_key,
5468 const uint8_t *peer_key,
5469 size_t peer_key_length,
5470 uint8_t *shared_secret,
5471 size_t shared_secret_size,
5472 size_t *shared_secret_length )
5473{
5474 switch( alg )
5475 {
5476#if defined(MBEDTLS_ECDH_C)
5477 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005478 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005479 return( PSA_ERROR_INVALID_ARGUMENT );
5480 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5481 private_key->data.ecp,
5482 shared_secret, shared_secret_size,
5483 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005484#endif /* MBEDTLS_ECDH_C */
5485 default:
5486 (void) private_key;
5487 (void) peer_key;
5488 (void) peer_key_length;
5489 (void) shared_secret;
5490 (void) shared_secret_size;
5491 (void) shared_secret_length;
5492 return( PSA_ERROR_NOT_SUPPORTED );
5493 }
5494}
5495
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005496/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005497 * to potentially free embedded data structures and wipe confidential data.
5498 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005499static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005500 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005501 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005502 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005503 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005504{
5505 psa_status_t status;
5506 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5507 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005509
5510 /* Step 1: run the secret agreement algorithm to generate the shared
5511 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005512 status = psa_key_agreement_raw_internal( ka_alg,
5513 private_key,
5514 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005515 shared_secret,
5516 sizeof( shared_secret ),
5517 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005518 if( status != PSA_SUCCESS )
5519 goto exit;
5520
5521 /* Step 2: set up the key derivation to generate key material from
5522 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005523 status = psa_key_derivation_input_internal( operation, step,
5524 shared_secret,
5525 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005526
Gilles Peskine01d718c2018-09-18 12:01:02 +02005527exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005528 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005529 return( status );
5530}
5531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005533 psa_key_derivation_step_t step,
5534 psa_key_handle_t private_key,
5535 const uint8_t *peer_key,
5536 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005537{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005538 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005539 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005541 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005542 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005543 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005544 if( status != PSA_SUCCESS )
5545 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005547 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005548 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005549 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005551 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005552}
5553
Gilles Peskinebe697d82019-05-16 18:00:41 +02005554psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5555 psa_key_handle_t private_key,
5556 const uint8_t *peer_key,
5557 size_t peer_key_length,
5558 uint8_t *output,
5559 size_t output_size,
5560 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005561{
5562 psa_key_slot_t *slot;
5563 psa_status_t status;
5564
5565 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5566 {
5567 status = PSA_ERROR_INVALID_ARGUMENT;
5568 goto exit;
5569 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005570 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005571 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005572 if( status != PSA_SUCCESS )
5573 goto exit;
5574
5575 status = psa_key_agreement_raw_internal( alg, slot,
5576 peer_key, peer_key_length,
5577 output, output_size,
5578 output_length );
5579
5580exit:
5581 if( status != PSA_SUCCESS )
5582 {
5583 /* If an error happens and is not handled properly, the output
5584 * may be used as a key to protect sensitive data. Arrange for such
5585 * a key to be random, which is likely to result in decryption or
5586 * verification errors. This is better than filling the buffer with
5587 * some constant data such as zeros, which would result in the data
5588 * being protected with a reproducible, easily knowable key.
5589 */
5590 psa_generate_random( output, output_size );
5591 *output_length = output_size;
5592 }
5593 return( status );
5594}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005595
5596
5597/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005598/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005599/****************************************************************/
5600
5601psa_status_t psa_generate_random( uint8_t *output,
5602 size_t output_size )
5603{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005604 int ret;
5605 GUARD_MODULE_INITIALIZED;
5606
5607 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005608 return( mbedtls_to_psa_error( ret ) );
5609}
5610
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005611#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5612#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005613
Gilles Peskine7228da22019-07-15 11:06:15 +02005614psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005615 size_t seed_size )
5616{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005617 if( global_data.initialized )
5618 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005619
5620 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5621 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5622 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5623 return( PSA_ERROR_INVALID_ARGUMENT );
5624
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005625 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005626}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005627#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005628
Gilles Peskinee56e8782019-04-26 17:34:02 +02005629#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5630static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5631 size_t domain_parameters_size,
5632 int *exponent )
5633{
5634 size_t i;
5635 uint32_t acc = 0;
5636
5637 if( domain_parameters_size == 0 )
5638 {
5639 *exponent = 65537;
5640 return( PSA_SUCCESS );
5641 }
5642
5643 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5644 * support values that fit in a 32-bit integer, which is larger than
5645 * int on just about every platform anyway. */
5646 if( domain_parameters_size > sizeof( acc ) )
5647 return( PSA_ERROR_NOT_SUPPORTED );
5648 for( i = 0; i < domain_parameters_size; i++ )
5649 acc = ( acc << 8 ) | domain_parameters[i];
5650 if( acc > INT_MAX )
5651 return( PSA_ERROR_NOT_SUPPORTED );
5652 *exponent = acc;
5653 return( PSA_SUCCESS );
5654}
5655#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5656
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005657static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005658 psa_key_slot_t *slot, size_t bits,
5659 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005660{
Gilles Peskine8e338702019-07-30 20:06:31 +02005661 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005662
Gilles Peskinee56e8782019-04-26 17:34:02 +02005663 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005664 return( PSA_ERROR_INVALID_ARGUMENT );
5665
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005666 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005667 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005668 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005669 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005670 if( status != PSA_SUCCESS )
5671 return( status );
5672 status = psa_generate_random( slot->data.raw.data,
5673 slot->data.raw.bytes );
5674 if( status != PSA_SUCCESS )
5675 {
5676 mbedtls_free( slot->data.raw.data );
5677 return( status );
5678 }
5679#if defined(MBEDTLS_DES_C)
5680 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005681 psa_des_set_key_parity( slot->data.raw.data,
5682 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005683#endif /* MBEDTLS_DES_C */
5684 }
5685 else
5686
5687#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005688 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005689 {
5690 mbedtls_rsa_context *rsa;
5691 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005692 int exponent;
5693 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005694 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5695 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005696 /* Accept only byte-aligned keys, for the same reasons as
5697 * in psa_import_rsa_key(). */
5698 if( bits % 8 != 0 )
5699 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005700 status = psa_read_rsa_exponent( domain_parameters,
5701 domain_parameters_size,
5702 &exponent );
5703 if( status != PSA_SUCCESS )
5704 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005705 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5706 if( rsa == NULL )
5707 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5708 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5709 ret = mbedtls_rsa_gen_key( rsa,
5710 mbedtls_ctr_drbg_random,
5711 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005712 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005713 exponent );
5714 if( ret != 0 )
5715 {
5716 mbedtls_rsa_free( rsa );
5717 mbedtls_free( rsa );
5718 return( mbedtls_to_psa_error( ret ) );
5719 }
5720 slot->data.rsa = rsa;
5721 }
5722 else
5723#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5724
5725#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005726 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005727 {
5728 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5729 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5730 const mbedtls_ecp_curve_info *curve_info =
5731 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5732 mbedtls_ecp_keypair *ecp;
5733 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005734 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005735 return( PSA_ERROR_NOT_SUPPORTED );
5736 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5737 return( PSA_ERROR_NOT_SUPPORTED );
5738 if( curve_info->bit_size != bits )
5739 return( PSA_ERROR_INVALID_ARGUMENT );
5740 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5741 if( ecp == NULL )
5742 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5743 mbedtls_ecp_keypair_init( ecp );
5744 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5745 mbedtls_ctr_drbg_random,
5746 &global_data.ctr_drbg );
5747 if( ret != 0 )
5748 {
5749 mbedtls_ecp_keypair_free( ecp );
5750 mbedtls_free( ecp );
5751 return( mbedtls_to_psa_error( ret ) );
5752 }
5753 slot->data.ecp = ecp;
5754 }
5755 else
5756#endif /* MBEDTLS_ECP_C */
5757
5758 return( PSA_ERROR_NOT_SUPPORTED );
5759
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005760 return( PSA_SUCCESS );
5761}
5762
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005763psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005764 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005765{
5766 psa_status_t status;
5767 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005768 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005769 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005770#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5771 if( driver != NULL )
5772 {
5773 /* Generating a key in a secure element is not implemented yet. */
5774 status = PSA_ERROR_NOT_SUPPORTED;
5775 }
5776#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005777 if( status == PSA_SUCCESS )
5778 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005779 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005780 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005781 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005782 }
5783 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005784 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005785 if( status != PSA_SUCCESS )
5786 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005787 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005788 *handle = 0;
5789 }
5790 return( status );
5791}
5792
5793
Gilles Peskine05d69892018-06-19 22:00:52 +02005794
5795/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005796/* Module setup */
5797/****************************************************************/
5798
Gilles Peskine5e769522018-11-20 21:59:56 +01005799psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5800 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5801 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5802{
5803 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5804 return( PSA_ERROR_BAD_STATE );
5805 global_data.entropy_init = entropy_init;
5806 global_data.entropy_free = entropy_free;
5807 return( PSA_SUCCESS );
5808}
5809
Gilles Peskinee59236f2018-01-27 23:32:46 +01005810void mbedtls_psa_crypto_free( void )
5811{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005812 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005813 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5814 {
5815 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005816 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005817 }
5818 /* Wipe all remaining data, including configuration.
5819 * In particular, this sets all state indicator to the value
5820 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005821 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005823 /* Unregister all secure element drivers, so that we restart from
5824 * a pristine state. */
5825 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005826#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005827}
5828
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005829#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5830/** Recover a transaction that was interrupted by a power failure.
5831 *
5832 * This function is called during initialization, before psa_crypto_init()
5833 * returns. If this function returns a failure status, the initialization
5834 * fails.
5835 */
5836static psa_status_t psa_crypto_recover_transaction(
5837 const psa_crypto_transaction_t *transaction )
5838{
5839 switch( transaction->unknown.type )
5840 {
5841 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5842 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5843 /* TOnogrepDO - fall through to the failure case until this
5844 * is implemented */
5845 default:
5846 /* We found an unsupported transaction in the storage.
5847 * We don't know what state the storage is in. Give up. */
5848 return( PSA_ERROR_STORAGE_FAILURE );
5849 }
5850}
5851#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5852
Gilles Peskinee59236f2018-01-27 23:32:46 +01005853psa_status_t psa_crypto_init( void )
5854{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005855 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005856 const unsigned char drbg_seed[] = "PSA";
5857
Gilles Peskinec6b69072018-11-20 21:42:52 +01005858 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005859 if( global_data.initialized != 0 )
5860 return( PSA_SUCCESS );
5861
Gilles Peskine5e769522018-11-20 21:59:56 +01005862 /* Set default configuration if
5863 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5864 if( global_data.entropy_init == NULL )
5865 global_data.entropy_init = mbedtls_entropy_init;
5866 if( global_data.entropy_free == NULL )
5867 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005868
Gilles Peskinec6b69072018-11-20 21:42:52 +01005869 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005870 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005871 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005872 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005873 status = mbedtls_to_psa_error(
5874 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5875 mbedtls_entropy_func,
5876 &global_data.entropy,
5877 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5878 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005879 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005880 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005881
Gilles Peskine66fb1262018-12-10 16:29:04 +01005882 status = psa_initialize_key_slots( );
5883 if( status != PSA_SUCCESS )
5884 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005885
Gilles Peskine4b734222019-07-24 15:56:31 +02005886#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005887 status = psa_crypto_load_transaction( );
5888 if( status == PSA_SUCCESS )
5889 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005890 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5891 if( status != PSA_SUCCESS )
5892 goto exit;
5893 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005894 }
5895 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5896 {
5897 /* There's no transaction to complete. It's all good. */
5898 status = PSA_SUCCESS;
5899 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005900#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005901
Gilles Peskinec6b69072018-11-20 21:42:52 +01005902 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005903 global_data.initialized = 1;
5904
Gilles Peskinee59236f2018-01-27 23:32:46 +01005905exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005906 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005907 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005908 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005909}
5910
5911#endif /* MBEDTLS_PSA_CRYPTO_C */