blob: 69323184d0fc1b3343be747a66dce9476186f8b6 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132static psa_status_t mbedtls_to_psa_error( int ret )
133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
206 return( PSA_ERROR_BAD_STATE );
207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
379 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200380{
381 switch( curve )
382 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 case PSA_ECC_CURVE_SECP_R1:
384 switch( byte_length )
385 {
386 case PSA_BITS_TO_BYTES( 192 ):
387 return( MBEDTLS_ECP_DP_SECP192R1 );
388 case PSA_BITS_TO_BYTES( 224 ):
389 return( MBEDTLS_ECP_DP_SECP224R1 );
390 case PSA_BITS_TO_BYTES( 256 ):
391 return( MBEDTLS_ECP_DP_SECP256R1 );
392 case PSA_BITS_TO_BYTES( 384 ):
393 return( MBEDTLS_ECP_DP_SECP384R1 );
394 case PSA_BITS_TO_BYTES( 521 ):
395 return( MBEDTLS_ECP_DP_SECP521R1 );
396 default:
397 return( MBEDTLS_ECP_DP_NONE );
398 }
399 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100400
401 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
402 switch( byte_length )
403 {
404 case PSA_BITS_TO_BYTES( 256 ):
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_BITS_TO_BYTES( 384 ):
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_BITS_TO_BYTES( 512 ):
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 default:
411 return( MBEDTLS_ECP_DP_NONE );
412 }
413 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100414
415 case PSA_ECC_CURVE_MONTGOMERY:
416 switch( byte_length )
417 {
418 case PSA_BITS_TO_BYTES( 255 ):
419 return( MBEDTLS_ECP_DP_CURVE25519 );
420 case PSA_BITS_TO_BYTES( 448 ):
421 return( MBEDTLS_ECP_DP_CURVE448 );
422 default:
423 return( MBEDTLS_ECP_DP_NONE );
424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
427 case PSA_ECC_CURVE_SECP_K1:
428 switch( byte_length )
429 {
430 case PSA_BITS_TO_BYTES( 192 ):
431 return( MBEDTLS_ECP_DP_SECP192K1 );
432 case PSA_BITS_TO_BYTES( 224 ):
433 return( MBEDTLS_ECP_DP_SECP224K1 );
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_SECP256K1 );
436 default:
437 return( MBEDTLS_ECP_DP_NONE );
438 }
439 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100440
Gilles Peskine12313cd2018-06-20 00:20:32 +0200441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
455#if defined(MBEDTLS_MD_C)
456 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200457#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200458 case PSA_KEY_TYPE_DERIVE:
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_AES_C)
461 case PSA_KEY_TYPE_AES:
462 if( bits != 128 && bits != 192 && bits != 256 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466#if defined(MBEDTLS_CAMELLIA_C)
467 case PSA_KEY_TYPE_CAMELLIA:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_DES_C)
473 case PSA_KEY_TYPE_DES:
474 if( bits != 64 && bits != 128 && bits != 192 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_ARC4_C)
479 case PSA_KEY_TYPE_ARC4:
480 if( bits < 8 || bits > 2048 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200484#if defined(MBEDTLS_CHACHA20_C)
485 case PSA_KEY_TYPE_CHACHA20:
486 if( bits != 256 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 default:
491 return( PSA_ERROR_NOT_SUPPORTED );
492 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200493 if( bits % 8 != 0 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495
496 /* Allocate memory for the key */
497 raw->bytes = PSA_BITS_TO_BYTES( bits );
498 raw->data = mbedtls_calloc( 1, raw->bytes );
499 if( raw->data == NULL )
500 {
501 raw->bytes = 0;
502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
503 }
504 return( PSA_SUCCESS );
505}
506
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100508/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
509 * that are not a multiple of 8) well. For example, there is only
510 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
511 * way to return the exact bit size of a key.
512 * To keep things simple, reject non-byte-aligned key sizes. */
513static psa_status_t psa_check_rsa_key_byte_aligned(
514 const mbedtls_rsa_context *rsa )
515{
516 mbedtls_mpi n;
517 psa_status_t status;
518 mbedtls_mpi_init( &n );
519 status = mbedtls_to_psa_error(
520 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
521 if( status == PSA_SUCCESS )
522 {
523 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
524 status = PSA_ERROR_NOT_SUPPORTED;
525 }
526 mbedtls_mpi_free( &n );
527 return( status );
528}
529
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000530static psa_status_t psa_import_rsa_key( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 mbedtls_rsa_context **p_rsa )
534{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 psa_status_t status;
536 mbedtls_pk_context pk;
537 mbedtls_rsa_context *rsa;
538 size_t bits;
539
540 mbedtls_pk_init( &pk );
541
542 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
548 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
554 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
560 rsa = mbedtls_pk_rsa( pk );
561 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
562 * supports non-byte-aligned key sizes, but not well. For example,
563 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
564 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
565 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
566 {
567 status = PSA_ERROR_NOT_SUPPORTED;
568 goto exit;
569 }
570 status = psa_check_rsa_key_byte_aligned( rsa );
571
572exit:
573 /* Free the content of the pk object only on error. */
574 if( status != PSA_SUCCESS )
575 {
576 mbedtls_pk_free( &pk );
577 return( status );
578 }
579
580 /* On success, store the content of the object in the RSA context. */
581 *p_rsa = rsa;
582
583 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200584}
585#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Jaeden Ameroccdce902019-01-10 11:42:27 +0000587#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100589 size_t data_length,
590 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100591 mbedtls_ecp_keypair **p_ecp )
592{
593 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
594 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
595 if( *p_ecp == NULL )
596 return( PSA_ERROR_INSUFFICIENT_MEMORY );
597 mbedtls_ecp_keypair_init( *p_ecp );
598
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100599 if( is_public )
600 {
601 /* A public key is represented as:
602 * - The byte 0x04;
603 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
604 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
605 * So its data length is 2m+1 where n is the key size in bits.
606 */
607 if( ( data_length & 1 ) == 0 )
608 return( PSA_ERROR_INVALID_ARGUMENT );
609 data_length = data_length / 2;
610 }
611
Gilles Peskine4cd32772019-12-02 20:49:42 +0100612 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100613 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
614 if( grp_id == MBEDTLS_ECP_DP_NONE )
615 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100616 return( mbedtls_to_psa_error(
617 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
618}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619
620/* Import a public key given as the uncompressed representation defined by SEC1
621 * 2.3.3 as the content of an ECPoint. */
622static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
623 const uint8_t *data,
624 size_t data_length,
625 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100630 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 if( status != PSA_SUCCESS )
632 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100633
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634 /* Load the public value. */
635 status = mbedtls_to_psa_error(
636 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
637 data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640
641 /* Check that the point is on the curve. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646
647 *p_ecp = ecp;
648 return( PSA_SUCCESS );
649
650exit:
651 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 mbedtls_ecp_keypair_free( ecp );
654 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000656 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200657}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200658
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659/* Import a private key given as a byte string which is the private value
660 * in big-endian order. */
661static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
662 const uint8_t *data,
663 size_t data_length,
664 mbedtls_ecp_keypair **p_ecp )
665{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100669 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 if( status != PSA_SUCCESS )
671 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100672
Gilles Peskinef76aa772018-10-29 19:24:33 +0100673 /* Load the secret value. */
674 status = mbedtls_to_psa_error(
675 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Validate the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
681 if( status != PSA_SUCCESS )
682 goto exit;
683 /* Calculate the public key from the private key. */
684 status = mbedtls_to_psa_error(
685 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
686 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
687 if( status != PSA_SUCCESS )
688 goto exit;
689
690 *p_ecp = ecp;
691 return( PSA_SUCCESS );
692
693exit:
694 if( ecp != NULL )
695 {
696 mbedtls_ecp_keypair_free( ecp );
697 mbedtls_free( ecp );
698 }
699 return( status );
700}
701#endif /* defined(MBEDTLS_ECP_C) */
702
Gilles Peskineb46bef22019-07-30 21:32:04 +0200703
704/** Return the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot.
707 *
708 * \return The key size in bits, read from the metadata in the slot.
709 */
710static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
711{
712 return( slot->attr.bits );
713}
714
715/** Calculate the size of the key in the given slot, in bits.
716 *
717 * \param[in] slot A key slot containing a transparent key.
718 *
719 * \return The key size in bits, calculated from the key data.
720 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 size_t bits = 0; /* return 0 on an empty slot */
724
Gilles Peskineb46bef22019-07-30 21:32:04 +0200725 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200726 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
729 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_RSA_C) */
731#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200732 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
733 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200734#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200735
736 /* We know that the size fits in psa_key_bits_t thanks to checks
737 * when the key was created. */
738 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200739}
740
Gilles Peskine8e338702019-07-30 20:06:31 +0200741/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100742 * previously. This function assumes that the slot does not contain
743 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100744psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
745 const uint8_t *data,
746 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749
Gilles Peskine8e338702019-07-30 20:06:31 +0200750 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200752 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200753 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754 if( data_length > SIZE_MAX / 8 )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200756 /* Enforce a size limit, and in particular ensure that the bit
757 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200758 if( bit_size > PSA_MAX_KEY_BITS )
759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200761 &slot->data.raw );
762 if( status != PSA_SUCCESS )
763 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200764 if( data_length != 0 )
765 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 }
767 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200769 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100772 data, data_length,
773 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100774 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000777 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000779 data, data_length,
780 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 }
782 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100783#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200785 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200787 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000788 data, data_length,
789 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 }
791 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000792#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 {
794 return( PSA_ERROR_NOT_SUPPORTED );
795 }
Darryl Green940d72c2018-07-13 13:18:51 +0100796
Gilles Peskineb46bef22019-07-30 21:32:04 +0200797 if( status == PSA_SUCCESS )
798 {
799 /* Write the actual key size to the slot.
800 * psa_start_key_creation() wrote the size declared by the
801 * caller, which may be 0 (meaning unspecified) or wrong. */
802 slot->attr.bits = psa_calculate_key_bits( slot );
803 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100804 return( status );
805}
806
Gilles Peskinef603c712019-01-19 13:40:11 +0100807/** Calculate the intersection of two algorithm usage policies.
808 *
809 * Return 0 (which allows no operation) on incompatibility.
810 */
811static psa_algorithm_t psa_key_policy_algorithm_intersection(
812 psa_algorithm_t alg1,
813 psa_algorithm_t alg2 )
814{
Gilles Peskine549ea862019-05-22 11:45:59 +0200815 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100816 if( alg1 == alg2 )
817 return( alg1 );
818 /* If the policies are from the same hash-and-sign family, check
819 * if one is a wildcard. If so the other has the specific algorithm. */
820 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
821 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
822 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
823 {
824 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
825 return( alg2 );
826 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
827 return( alg1 );
828 }
829 /* If the policies are incompatible, allow nothing. */
830 return( 0 );
831}
832
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
834 psa_algorithm_t requested_alg )
835{
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837 if( requested_alg == policy_alg )
838 return( 1 );
839 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 * and requested_alg is the same hash-and-sign family with any hash,
841 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
843 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
844 {
845 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
846 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
847 }
848 /* If it isn't permitted, it's forbidden. */
849 return( 0 );
850}
851
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852/** Test whether a policy permits an algorithm.
853 *
854 * The caller must test usage flags separately.
855 */
856static int psa_key_policy_permits( const psa_key_policy_t *policy,
857 psa_algorithm_t alg )
858{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200859 return( psa_key_algorithm_permits( policy->alg, alg ) ||
860 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861}
862
Gilles Peskinef603c712019-01-19 13:40:11 +0100863/** Restrict a key policy based on a constraint.
864 *
865 * \param[in,out] policy The policy to restrict.
866 * \param[in] constraint The policy constraint to apply.
867 *
868 * \retval #PSA_SUCCESS
869 * \c *policy contains the intersection of the original value of
870 * \c *policy and \c *constraint.
871 * \retval #PSA_ERROR_INVALID_ARGUMENT
872 * \c *policy and \c *constraint are incompatible.
873 * \c *policy is unchanged.
874 */
875static psa_status_t psa_restrict_key_policy(
876 psa_key_policy_t *policy,
877 const psa_key_policy_t *constraint )
878{
879 psa_algorithm_t intersection_alg =
880 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 psa_algorithm_t intersection_alg2 =
882 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
884 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
886 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 policy->usage &= constraint->usage;
888 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200889 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100890 return( PSA_SUCCESS );
891}
892
Darryl Green06fd18d2018-07-16 11:21:11 +0100893/** Retrieve a slot which must contain a key. The key must have allow all the
894 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
895 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100896static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100897 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100898 psa_key_usage_t usage,
899 psa_algorithm_t alg )
900{
901 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100902 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100903
904 *p_slot = NULL;
905
Gilles Peskinec5487a82018-12-03 18:08:14 +0100906 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100907 if( status != PSA_SUCCESS )
908 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100909
910 /* Enforce that usage policy for the key slot contains all the flags
911 * required by the usage parameter. There is one exception: public
912 * keys can always be exported, so we treat public key objects as
913 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200914 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100915 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918
919 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200920 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100921 return( PSA_ERROR_NOT_PERMITTED );
922
923 *p_slot = slot;
924 return( PSA_SUCCESS );
925}
Darryl Green940d72c2018-07-13 13:18:51 +0100926
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927/** Retrieve a slot which must contain a transparent key.
928 *
929 * A transparent key is a key for which the key material is directly
930 * available, as opposed to a key in a secure element.
931 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200932 * This is a temporary function to use instead of psa_get_key_from_slot()
933 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 */
935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
936static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
937 psa_key_slot_t **p_slot,
938 psa_key_usage_t usage,
939 psa_algorithm_t alg )
940{
941 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
942 if( status != PSA_SUCCESS )
943 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200944 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 {
946 *p_slot = NULL;
947 return( PSA_ERROR_NOT_SUPPORTED );
948 }
949 return( PSA_SUCCESS );
950}
951#else /* MBEDTLS_PSA_CRYPTO_SE_C */
952/* With no secure element support, all keys are transparent. */
953#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
954 psa_get_key_from_slot( handle, p_slot, usage, alg )
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100958static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000959{
Gilles Peskine73167e12019-07-12 23:44:37 +0200960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
961 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000962 {
963 /* No key material to clean. */
964 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200965 else
966#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 /* No key material to clean. */
970 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200971 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000972 {
973 mbedtls_free( slot->data.raw.data );
974 }
975 else
976#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000978 {
979 mbedtls_rsa_free( slot->data.rsa );
980 mbedtls_free( slot->data.rsa );
981 }
982 else
983#endif /* defined(MBEDTLS_RSA_C) */
984#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000986 {
987 mbedtls_ecp_keypair_free( slot->data.ecp );
988 mbedtls_free( slot->data.ecp );
989 }
990 else
991#endif /* defined(MBEDTLS_ECP_C) */
992 {
993 /* Shouldn't happen: the key type is not any type that we
994 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200995 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000996 }
997
998 return( PSA_SUCCESS );
999}
1000
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001/** Completely wipe a slot in memory, including its policy.
1002 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001003psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001004{
1005 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001006 /* Multipart operations may still be using the key. This is safe
1007 * because all multipart operation objects are independent from
1008 * the key slot: if they need to access the key after the setup
1009 * phase, they have a copy of the key. Note that this means that
1010 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001011 /* At this point, key material and other type-specific content has
1012 * been wiped. Clear remaining metadata. We can call memset and not
1013 * zeroize because the metadata is not particularly sensitive. */
1014 memset( slot, 0, sizeof( *slot ) );
1015 return( status );
1016}
1017
Gilles Peskinec5487a82018-12-03 18:08:14 +01001018psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001020 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001021 psa_status_t status; /* status of the last operation */
1022 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1024 psa_se_drv_table_entry_t *driver;
1025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026
Gilles Peskine1841cf42019-10-08 15:48:25 +02001027 if( handle == 0 )
1028 return( PSA_SUCCESS );
1029
Gilles Peskinec5487a82018-12-03 18:08:14 +01001030 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
1032 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001033
1034#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001036 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001038 /* For a key in a secure element, we need to do three things:
1039 * remove the key file in internal storage, destroy the
1040 * key inside the secure element, and update the driver's
1041 * persistent data. Start a transaction that will encompass these
1042 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001044 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001045 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 status = psa_crypto_save_transaction( );
1048 if( status != PSA_SUCCESS )
1049 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001050 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001051 /* We should still try to destroy the key in the secure
1052 * element and the key metadata in storage. This is especially
1053 * important if the error is that the storage is full.
1054 * But how to do it exactly without risking an inconsistent
1055 * state after a reset?
1056 * https://github.com/ARMmbed/mbed-crypto/issues/215
1057 */
1058 overall_status = status;
1059 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001060 }
1061
Gilles Peskine354f7672019-07-12 23:46:38 +02001062 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001063 if( overall_status == PSA_SUCCESS )
1064 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1067
Darryl Greend49a4992018-06-18 17:27:26 +01001068#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001069 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001070 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 status = psa_destroy_persistent_key( slot->attr.id );
1072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001075 /* TODO: other slots may have a copy of the same key. We should
1076 * invalidate them.
1077 * https://github.com/ARMmbed/mbed-crypto/issues/214
1078 */
Darryl Greend49a4992018-06-18 17:27:26 +01001079 }
1080#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001081
Gilles Peskinefc762652019-07-22 19:30:34 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083 if( driver != NULL )
1084 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001085 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
1088 status = psa_crypto_stop_transaction( );
1089 if( overall_status == PSA_SUCCESS )
1090 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 }
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1093
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1095exit:
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001097 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1099 if( overall_status == PSA_SUCCESS )
1100 overall_status = status;
1101 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102}
1103
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001105{
Gilles Peskineb699f072019-04-26 16:06:02 +02001106 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001107 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001108}
1109
Gilles Peskineb699f072019-04-26 16:06:02 +02001110psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1111 psa_key_type_t type,
1112 const uint8_t *data,
1113 size_t data_length )
1114{
1115 uint8_t *copy = NULL;
1116
1117 if( data_length != 0 )
1118 {
1119 copy = mbedtls_calloc( 1, data_length );
1120 if( copy == NULL )
1121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1122 memcpy( copy, data, data_length );
1123 }
1124 /* After this point, this function is guaranteed to succeed, so it
1125 * can start modifying `*attributes`. */
1126
1127 if( attributes->domain_parameters != NULL )
1128 {
1129 mbedtls_free( attributes->domain_parameters );
1130 attributes->domain_parameters = NULL;
1131 attributes->domain_parameters_size = 0;
1132 }
1133
1134 attributes->domain_parameters = copy;
1135 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001136 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001137 return( PSA_SUCCESS );
1138}
1139
1140psa_status_t psa_get_key_domain_parameters(
1141 const psa_key_attributes_t *attributes,
1142 uint8_t *data, size_t data_size, size_t *data_length )
1143{
1144 if( attributes->domain_parameters_size > data_size )
1145 return( PSA_ERROR_BUFFER_TOO_SMALL );
1146 *data_length = attributes->domain_parameters_size;
1147 if( attributes->domain_parameters_size != 0 )
1148 memcpy( data, attributes->domain_parameters,
1149 attributes->domain_parameters_size );
1150 return( PSA_SUCCESS );
1151}
1152
1153#if defined(MBEDTLS_RSA_C)
1154static psa_status_t psa_get_rsa_public_exponent(
1155 const mbedtls_rsa_context *rsa,
1156 psa_key_attributes_t *attributes )
1157{
1158 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001160 uint8_t *buffer = NULL;
1161 size_t buflen;
1162 mbedtls_mpi_init( &mpi );
1163
1164 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1165 if( ret != 0 )
1166 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001167 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1168 {
1169 /* It's the default value, which is reported as an empty string,
1170 * so there's nothing to do. */
1171 goto exit;
1172 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
1174 buflen = mbedtls_mpi_size( &mpi );
1175 buffer = mbedtls_calloc( 1, buflen );
1176 if( buffer == NULL )
1177 {
1178 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1179 goto exit;
1180 }
1181 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1182 if( ret != 0 )
1183 goto exit;
1184 attributes->domain_parameters = buffer;
1185 attributes->domain_parameters_size = buflen;
1186
1187exit:
1188 mbedtls_mpi_free( &mpi );
1189 if( ret != 0 )
1190 mbedtls_free( buffer );
1191 return( mbedtls_to_psa_error( ret ) );
1192}
1193#endif /* MBEDTLS_RSA_C */
1194
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001195/** Retrieve all the publicly-accessible attributes of a key.
1196 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001197psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1198 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001201 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203 psa_reset_key_attributes( attributes );
1204
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001205 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001206 if( status != PSA_SUCCESS )
1207 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001208
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001209 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001210 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1211 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1212
1213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214 if( psa_key_slot_is_external( slot ) )
1215 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217
Gilles Peskine8e338702019-07-30 20:06:31 +02001218 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 {
1220#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001221 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001224 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001225 * is not yet implemented.
1226 * https://github.com/ARMmbed/mbed-crypto/issues/216
1227 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001228 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001229 break;
1230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1232 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001233#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 default:
1235 /* Nothing else to do. */
1236 break;
1237 }
1238
1239 if( status != PSA_SUCCESS )
1240 psa_reset_key_attributes( attributes );
1241 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242}
1243
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1245psa_status_t psa_get_key_slot_number(
1246 const psa_key_attributes_t *attributes,
1247 psa_key_slot_number_t *slot_number )
1248{
1249 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1250 {
1251 *slot_number = attributes->slot_number;
1252 return( PSA_SUCCESS );
1253 }
1254 else
1255 return( PSA_ERROR_INVALID_ARGUMENT );
1256}
1257#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1258
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001260static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1261 unsigned char *buf, size_t size )
1262{
Janos Follath24eed8d2019-11-22 13:21:35 +00001263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001264 unsigned char *c;
1265 size_t len = 0;
1266
1267 c = buf + size;
1268
1269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1270
1271 return( (int) len );
1272}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001273#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001274
Gilles Peskinef603c712019-01-19 13:40:11 +01001275static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1276 uint8_t *data,
1277 size_t data_size,
1278 size_t *data_length,
1279 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Gilles Peskine5d309672019-07-12 23:47:28 +02001281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1282 const psa_drv_se_t *drv;
1283 psa_drv_se_context_t *drv_context;
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001286 *data_length = 0;
1287
Gilles Peskine8e338702019-07-30 20:06:31 +02001288 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001289 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001290
Gilles Peskinef9168942019-09-12 19:20:29 +02001291 /* Reject a zero-length output buffer now, since this can never be a
1292 * valid key representation. This way we know that data must be a valid
1293 * pointer and we can do things like memset(data, ..., data_size). */
1294 if( data_size == 0 )
1295 return( PSA_ERROR_BUFFER_TOO_SMALL );
1296
Gilles Peskine5d309672019-07-12 23:47:28 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001299 {
1300 psa_drv_se_export_key_t method;
1301 if( drv->key_management == NULL )
1302 return( PSA_ERROR_NOT_SUPPORTED );
1303 method = ( export_public_key ?
1304 drv->key_management->p_export_public :
1305 drv->key_management->p_export );
1306 if( method == NULL )
1307 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001308 return( method( drv_context,
1309 slot->data.se.slot_number,
1310 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001311 }
1312#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1313
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 {
1316 if( slot->data.raw.bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001318 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1319 memset( data + slot->data.raw.bytes, 0,
1320 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321 *data_length = slot->data.raw.bytes;
1322 return( PSA_SUCCESS );
1323 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001324#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001325 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001326 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001327 psa_status_t status;
1328
Gilles Peskineb46bef22019-07-30 21:32:04 +02001329 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001330 if( bytes > data_size )
1331 return( PSA_ERROR_BUFFER_TOO_SMALL );
1332 status = mbedtls_to_psa_error(
1333 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1334 if( status != PSA_SUCCESS )
1335 return( status );
1336 memset( data + bytes, 0, data_size - bytes );
1337 *data_length = bytes;
1338 return( PSA_SUCCESS );
1339 }
1340#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341 else
Moran Peker17e36e12018-05-02 12:55:20 +03001342 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001343#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1345 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001346 {
Moran Pekera998bc62018-04-16 18:16:20 +03001347 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001349 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001350 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#if defined(MBEDTLS_RSA_C)
1352 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001353 pk.pk_info = &mbedtls_rsa_info;
1354 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001358 }
1359 else
1360 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001361#if defined(MBEDTLS_ECP_C)
1362 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001363 pk.pk_info = &mbedtls_eckey_info;
1364 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001365#else
1366 return( PSA_ERROR_NOT_SUPPORTED );
1367#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001368 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001369 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001371 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 }
Moran Peker17e36e12018-05-02 12:55:20 +03001373 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 {
Moran Peker17e36e12018-05-02 12:55:20 +03001375 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001376 }
Moran Peker60364322018-04-29 11:34:58 +03001377 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001379 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001380 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1383 * Move the data to the beginning and erase remaining data
1384 * at the original location. */
1385 if( 2 * (size_t) ret <= data_size )
1386 {
1387 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001388 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001389 }
1390 else if( (size_t) ret < data_size )
1391 {
1392 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001393 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001394 }
Moran Pekera998bc62018-04-16 18:16:20 +03001395 *data_length = ret;
1396 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001397 }
1398 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001399#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001400 {
1401 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001402 it is valid for a special-purpose implementation to omit
1403 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001404 return( PSA_ERROR_NOT_SUPPORTED );
1405 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406 }
1407}
1408
Gilles Peskinec5487a82018-12-03 18:08:14 +01001409psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 uint8_t *data,
1411 size_t data_size,
1412 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001413{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 psa_status_t status;
1416
1417 /* Set the key to empty now, so that even when there are errors, we always
1418 * set data_length to a value between 0 and data_size. On error, setting
1419 * the key to empty is a good choice because an empty key representation is
1420 * unlikely to be accepted anywhere. */
1421 *data_length = 0;
1422
1423 /* Export requires the EXPORT flag. There is an exception for public keys,
1424 * which don't require any flag, but psa_get_key_from_slot takes
1425 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
1429 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001430 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432
Gilles Peskinec5487a82018-12-03 18:08:14 +01001433psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 uint8_t *data,
1435 size_t data_size,
1436 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001437{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001438 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001439 psa_status_t status;
1440
1441 /* Set the key to empty now, so that even when there are errors, we always
1442 * set data_length to a value between 0 and data_size. On error, setting
1443 * the key to empty is a good choice because an empty key representation is
1444 * unlikely to be accepted anywhere. */
1445 *data_length = 0;
1446
1447 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001448 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001449 if( status != PSA_SUCCESS )
1450 return( status );
1451 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001452 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001453}
1454
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455#if defined(static_assert)
1456static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1457 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001458static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001459 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001460static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001461 "One or more key attribute flag is listed as both internal-only and external-only" );
1462#endif
1463
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001464/** Validate that a key policy is internally well-formed.
1465 *
1466 * This function only rejects invalid policies. It does not validate the
1467 * consistency of the policy with respect to other attributes of the key
1468 * such as the key type.
1469 */
1470static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001471{
1472 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001473 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 PSA_KEY_USAGE_ENCRYPT |
1475 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001476 PSA_KEY_USAGE_SIGN_HASH |
1477 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1479 return( PSA_ERROR_INVALID_ARGUMENT );
1480
Gilles Peskine4747d192019-04-17 15:05:45 +02001481 return( PSA_SUCCESS );
1482}
1483
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001484/** Validate the internal consistency of key attributes.
1485 *
1486 * This function only rejects invalid attribute values. If does not
1487 * validate the consistency of the attributes with any key data that may
1488 * be involved in the creation of the key.
1489 *
1490 * Call this function early in the key creation process.
1491 *
1492 * \param[in] attributes Key attributes for the new key.
1493 * \param[out] p_drv On any return, the driver for the key, if any.
1494 * NULL for a transparent key.
1495 *
1496 */
1497static psa_status_t psa_validate_key_attributes(
1498 const psa_key_attributes_t *attributes,
1499 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001500{
1501 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502
1503 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001504 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001505 status = psa_validate_persistent_key_parameters(
1506 attributes->core.lifetime, attributes->core.id,
1507 p_drv, 1 );
1508 if( status != PSA_SUCCESS )
1509 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001510 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001511
1512 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001513 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( status );
1515
1516 /* Refuse to create overly large keys.
1517 * Note that this doesn't trigger on import if the attributes don't
1518 * explicitly specify a size (so psa_get_key_bits returns 0), so
1519 * psa_import_key() needs its own checks. */
1520 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1521 return( PSA_ERROR_NOT_SUPPORTED );
1522
Gilles Peskine91e8c332019-08-02 19:19:39 +02001523 /* Reject invalid flags. These should not be reachable through the API. */
1524 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1525 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1526 return( PSA_ERROR_INVALID_ARGUMENT );
1527
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001528 return( PSA_SUCCESS );
1529}
1530
Gilles Peskine4747d192019-04-17 15:05:45 +02001531/** Prepare a key slot to receive key material.
1532 *
1533 * This function allocates a key slot and sets its metadata.
1534 *
1535 * If this function fails, call psa_fail_key_creation().
1536 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 * This function is intended to be used as follows:
1538 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1539 * it with the specified attributes, and assign it a handle.
1540 * -# Populate the slot with the key material.
1541 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1542 * In case of failure at any step, stop the sequence and call
1543 * psa_fail_key_creation().
1544 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001545 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001546 * \param[in] attributes Key attributes for the new key.
1547 * \param[out] handle On success, a handle for the allocated slot.
1548 * \param[out] p_slot On success, a pointer to the prepared slot.
1549 * \param[out] p_drv On any return, the driver for the key, if any.
1550 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001551 *
1552 * \retval #PSA_SUCCESS
1553 * The key slot is ready to receive key material.
1554 * \return If this function fails, the key slot is an invalid state.
1555 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 */
1557static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 const psa_key_attributes_t *attributes,
1560 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001561 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001562 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001563{
1564 psa_status_t status;
1565 psa_key_slot_t *slot;
1566
Gilles Peskinedf179142019-07-15 22:02:14 +02001567 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001568 *p_drv = NULL;
1569
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001570 status = psa_validate_key_attributes( attributes, p_drv );
1571 if( status != PSA_SUCCESS )
1572 return( status );
1573
Gilles Peskineedbed562019-08-07 18:19:59 +02001574 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001575 if( status != PSA_SUCCESS )
1576 return( status );
1577 slot = *p_slot;
1578
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001579 /* We're storing the declared bit-size of the key. It's up to each
1580 * creation mechanism to verify that this information is correct.
1581 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001582 * an input (generate, device) but not for those where the bit-size
1583 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001584
1585 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001586
Gilles Peskine91e8c332019-08-02 19:19:39 +02001587 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001588 * external-only flags, query `attributes`. Thanks to the check
1589 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001590 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001591 * may have set. */
1592 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001593
Gilles Peskinecbaff462019-07-12 23:46:04 +02001594#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001595 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001596 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001597 * create the key file in internal storage, create the
1598 * key inside the secure element, and update the driver's
1599 * persistent data. Start a transaction that will encompass these
1600 * three actions. */
1601 /* The first thing to do is to find a slot number for the new key.
1602 * We save the slot number in persistent storage as part of the
1603 * transaction data. It will be needed to recover if the power
1604 * fails during the key creation process, to clean up on the secure
1605 * element side after restarting. Obtaining a slot number from the
1606 * secure element driver updates its persistent state, but we do not yet
1607 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001608 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001609 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001610 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001611 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001612 &slot->data.se.slot_number );
1613 if( status != PSA_SUCCESS )
1614 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001615 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001616 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001617 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001618 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001619 status = psa_crypto_save_transaction( );
1620 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001621 {
1622 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001623 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001624 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001625 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001626
1627 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1628 {
1629 /* Key registration only makes sense with a secure element. */
1630 return( PSA_ERROR_INVALID_ARGUMENT );
1631 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001632#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1633
Darryl Green0c6575a2018-11-07 16:05:30 +00001634 return( status );
1635}
Gilles Peskine4747d192019-04-17 15:05:45 +02001636
1637/** Finalize the creation of a key once its key material has been set.
1638 *
1639 * This entails writing the key to persistent storage.
1640 *
1641 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001642 * See the documentation of psa_start_key_creation() for the intended use
1643 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001645 * \param[in,out] slot Pointer to the slot with key material.
1646 * \param[in] driver The secure element driver for the key,
1647 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001648 *
1649 * \retval #PSA_SUCCESS
1650 * The key was successfully created. The handle is now valid.
1651 * \return If this function fails, the key slot is an invalid state.
1652 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001654static psa_status_t psa_finish_key_creation(
1655 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001656 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001657{
1658 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001659 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001660 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001661
1662#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001663 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001664 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001665#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1666 if( driver != NULL )
1667 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001668 psa_se_key_data_storage_t data;
1669#if defined(static_assert)
1670 static_assert( sizeof( slot->data.se.slot_number ) ==
1671 sizeof( data.slot_number ),
1672 "Slot number size does not match psa_se_key_data_storage_t" );
1673 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1674 "Bit-size size does not match psa_se_key_data_storage_t" );
1675#endif
1676 memcpy( &data.slot_number, &slot->data.se.slot_number,
1677 sizeof( slot->data.se.slot_number ) );
1678 memcpy( &data.bits, &slot->attr.bits,
1679 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001680 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001681 (uint8_t*) &data,
1682 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001683 }
1684 else
1685#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1686 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001687 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001688 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001689 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001690 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1691 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001692 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001693 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1694 status = psa_internal_export_key( slot,
1695 buffer, buffer_size, &length,
1696 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001697 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001698 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001699 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001700
Gilles Peskinef9168942019-09-12 19:20:29 +02001701 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001702 mbedtls_free( buffer );
1703 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001704 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001705#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1706
Gilles Peskinecbaff462019-07-12 23:46:04 +02001707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001708 /* Finish the transaction for a key creation. This does not
1709 * happen when registering an existing key. Detect this case
1710 * by checking whether a transaction is in progress (actual
1711 * creation of a key in a secure element requires a transaction,
1712 * but registration doesn't use one). */
1713 if( driver != NULL &&
1714 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001715 {
1716 status = psa_save_se_persistent_data( driver );
1717 if( status != PSA_SUCCESS )
1718 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001719 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720 return( status );
1721 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001722 status = psa_crypto_stop_transaction( );
1723 if( status != PSA_SUCCESS )
1724 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001725 }
1726#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1727
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 return( status );
1729}
1730
1731/** Abort the creation of a key.
1732 *
1733 * You may call this function after calling psa_start_key_creation(),
1734 * or after psa_finish_key_creation() fails. In other circumstances, this
1735 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001736 * See the documentation of psa_start_key_creation() for the intended use
1737 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 * \param[in,out] slot Pointer to the slot with key material.
1740 * \param[in] driver The secure element driver for the key,
1741 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001742 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001743static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001744 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001745{
Gilles Peskine011e4282019-06-26 18:34:38 +02001746 (void) driver;
1747
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 if( slot == NULL )
1749 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001750
1751#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001752 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 * element, and the failure happened later (when saving metadata
1754 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001755 * element.
1756 * https://github.com/ARMmbed/mbed-crypto/issues/217
1757 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001758
Gilles Peskined7729582019-08-05 15:55:54 +02001759 /* Abort the ongoing transaction if any (there may not be one if
1760 * the creation process failed before starting one, or if the
1761 * key creation is a registration of a key in a secure element).
1762 * Earlier functions must already have done what it takes to undo any
1763 * partial creation. All that's left is to update the transaction data
1764 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001765 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001766#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1767
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 psa_wipe_key_slot( slot );
1769}
1770
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001771/** Validate optional attributes during key creation.
1772 *
1773 * Some key attributes are optional during key creation. If they are
1774 * specified in the attributes structure, check that they are consistent
1775 * with the data in the slot.
1776 *
1777 * This function should be called near the end of key creation, after
1778 * the slot in memory is fully populated but before saving persistent data.
1779 */
1780static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001781 const psa_key_slot_t *slot,
1782 const psa_key_attributes_t *attributes )
1783{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001784 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001785 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001786 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001787 return( PSA_ERROR_INVALID_ARGUMENT );
1788 }
1789
1790 if( attributes->domain_parameters_size != 0 )
1791 {
1792#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001793 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001794 {
1795 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001797 mbedtls_mpi_init( &actual );
1798 mbedtls_mpi_init( &required );
1799 ret = mbedtls_rsa_export( slot->data.rsa,
1800 NULL, NULL, NULL, NULL, &actual );
1801 if( ret != 0 )
1802 goto rsa_exit;
1803 ret = mbedtls_mpi_read_binary( &required,
1804 attributes->domain_parameters,
1805 attributes->domain_parameters_size );
1806 if( ret != 0 )
1807 goto rsa_exit;
1808 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1809 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1810 rsa_exit:
1811 mbedtls_mpi_free( &actual );
1812 mbedtls_mpi_free( &required );
1813 if( ret != 0)
1814 return( mbedtls_to_psa_error( ret ) );
1815 }
1816 else
1817#endif
1818 {
1819 return( PSA_ERROR_INVALID_ARGUMENT );
1820 }
1821 }
1822
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001823 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001824 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001825 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826 return( PSA_ERROR_INVALID_ARGUMENT );
1827 }
1828
1829 return( PSA_SUCCESS );
1830}
1831
Gilles Peskine4747d192019-04-17 15:05:45 +02001832psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001833 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001834 size_t data_length,
1835 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001836{
1837 psa_status_t status;
1838 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001839 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001840
Gilles Peskine0f84d622019-09-12 19:03:13 +02001841 /* Reject zero-length symmetric keys (including raw data key objects).
1842 * This also rejects any key which might be encoded as an empty string,
1843 * which is never valid. */
1844 if( data_length == 0 )
1845 return( PSA_ERROR_INVALID_ARGUMENT );
1846
Gilles Peskinedf179142019-07-15 22:02:14 +02001847 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1848 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849 if( status != PSA_SUCCESS )
1850 goto exit;
1851
Gilles Peskine5d309672019-07-12 23:47:28 +02001852#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1853 if( driver != NULL )
1854 {
1855 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001856 /* The driver should set the number of key bits, however in
1857 * case it doesn't, we initialize bits to an invalid value. */
1858 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001859 if( drv->key_management == NULL ||
1860 drv->key_management->p_import == NULL )
1861 {
1862 status = PSA_ERROR_NOT_SUPPORTED;
1863 goto exit;
1864 }
1865 status = drv->key_management->p_import(
1866 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001867 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001868 &bits );
1869 if( status != PSA_SUCCESS )
1870 goto exit;
1871 if( bits > PSA_MAX_KEY_BITS )
1872 {
1873 status = PSA_ERROR_NOT_SUPPORTED;
1874 goto exit;
1875 }
1876 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001877 }
1878 else
1879#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1880 {
1881 status = psa_import_key_into_slot( slot, data, data_length );
1882 if( status != PSA_SUCCESS )
1883 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001884 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001885 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001886 if( status != PSA_SUCCESS )
1887 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888
Gilles Peskine011e4282019-06-26 18:34:38 +02001889 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 if( status != PSA_SUCCESS )
1892 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001893 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001894 *handle = 0;
1895 }
1896 return( status );
1897}
1898
Gilles Peskined7729582019-08-05 15:55:54 +02001899#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1900psa_status_t mbedtls_psa_register_se_key(
1901 const psa_key_attributes_t *attributes )
1902{
1903 psa_status_t status;
1904 psa_key_slot_t *slot = NULL;
1905 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001906 psa_key_handle_t handle = 0;
1907
1908 /* Leaving attributes unspecified is not currently supported.
1909 * It could make sense to query the key type and size from the
1910 * secure element, but not all secure elements support this
1911 * and the driver HAL doesn't currently support it. */
1912 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1913 return( PSA_ERROR_NOT_SUPPORTED );
1914 if( psa_get_key_bits( attributes ) == 0 )
1915 return( PSA_ERROR_NOT_SUPPORTED );
1916
1917 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1918 &handle, &slot, &driver );
1919 if( status != PSA_SUCCESS )
1920 goto exit;
1921
Gilles Peskined7729582019-08-05 15:55:54 +02001922 status = psa_finish_key_creation( slot, driver );
1923
1924exit:
1925 if( status != PSA_SUCCESS )
1926 {
1927 psa_fail_key_creation( slot, driver );
1928 }
1929 /* Registration doesn't keep the key in RAM. */
1930 psa_close_key( handle );
1931 return( status );
1932}
1933#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1934
Gilles Peskinef603c712019-01-19 13:40:11 +01001935static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001936 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001937{
1938 psa_status_t status;
1939 uint8_t *buffer = NULL;
1940 size_t buffer_size = 0;
1941 size_t length;
1942
Gilles Peskine8e338702019-07-30 20:06:31 +02001943 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001944 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001945 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001946 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001947 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001948 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1949 if( status != PSA_SUCCESS )
1950 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001951 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001952 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001953
1954exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001955 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001956 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001957 return( status );
1958}
1959
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001960psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1961 const psa_key_attributes_t *specified_attributes,
1962 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001963{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001964 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001965 psa_key_slot_t *source_slot = NULL;
1966 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001967 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001968 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001969
Gilles Peskine28f8f302019-07-24 13:30:31 +02001970 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001971 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001972 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 goto exit;
1974
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001975 status = psa_validate_optional_attributes( source_slot,
1976 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001980 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001981 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
Gilles Peskinedf179142019-07-15 22:02:14 +02001985 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1986 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001987 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001988 if( status != PSA_SUCCESS )
1989 goto exit;
1990
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001991#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1992 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001993 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001994 /* Copying to a secure element is not implemented yet. */
1995 status = PSA_ERROR_NOT_SUPPORTED;
1996 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001997 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001998#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001999
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002001 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002003
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006 if( status != PSA_SUCCESS )
2007 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002008 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002009 *target_handle = 0;
2010 }
2011 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002012}
2013
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002014
2015
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002016/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002017/* Message digests */
2018/****************************************************************/
2019
Gilles Peskineb16841e2019-10-10 20:36:12 +02002020#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002021static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002022{
2023 switch( alg )
2024 {
2025#if defined(MBEDTLS_MD2_C)
2026 case PSA_ALG_MD2:
2027 return( &mbedtls_md2_info );
2028#endif
2029#if defined(MBEDTLS_MD4_C)
2030 case PSA_ALG_MD4:
2031 return( &mbedtls_md4_info );
2032#endif
2033#if defined(MBEDTLS_MD5_C)
2034 case PSA_ALG_MD5:
2035 return( &mbedtls_md5_info );
2036#endif
2037#if defined(MBEDTLS_RIPEMD160_C)
2038 case PSA_ALG_RIPEMD160:
2039 return( &mbedtls_ripemd160_info );
2040#endif
2041#if defined(MBEDTLS_SHA1_C)
2042 case PSA_ALG_SHA_1:
2043 return( &mbedtls_sha1_info );
2044#endif
2045#if defined(MBEDTLS_SHA256_C)
2046 case PSA_ALG_SHA_224:
2047 return( &mbedtls_sha224_info );
2048 case PSA_ALG_SHA_256:
2049 return( &mbedtls_sha256_info );
2050#endif
2051#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002052#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002053 case PSA_ALG_SHA_384:
2054 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002055#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002056 case PSA_ALG_SHA_512:
2057 return( &mbedtls_sha512_info );
2058#endif
2059 default:
2060 return( NULL );
2061 }
2062}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002063#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002064
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002065psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2066{
2067 switch( operation->alg )
2068 {
Gilles Peskine81736312018-06-26 15:04:31 +02002069 case 0:
2070 /* The object has (apparently) been initialized but it is not
2071 * in use. It's ok to call abort on such an object, and there's
2072 * nothing to do. */
2073 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002074#if defined(MBEDTLS_MD2_C)
2075 case PSA_ALG_MD2:
2076 mbedtls_md2_free( &operation->ctx.md2 );
2077 break;
2078#endif
2079#if defined(MBEDTLS_MD4_C)
2080 case PSA_ALG_MD4:
2081 mbedtls_md4_free( &operation->ctx.md4 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_MD5_C)
2085 case PSA_ALG_MD5:
2086 mbedtls_md5_free( &operation->ctx.md5 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_RIPEMD160_C)
2090 case PSA_ALG_RIPEMD160:
2091 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2092 break;
2093#endif
2094#if defined(MBEDTLS_SHA1_C)
2095 case PSA_ALG_SHA_1:
2096 mbedtls_sha1_free( &operation->ctx.sha1 );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA256_C)
2100 case PSA_ALG_SHA_224:
2101 case PSA_ALG_SHA_256:
2102 mbedtls_sha256_free( &operation->ctx.sha256 );
2103 break;
2104#endif
2105#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002106#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002108#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 case PSA_ALG_SHA_512:
2110 mbedtls_sha512_free( &operation->ctx.sha512 );
2111 break;
2112#endif
2113 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002114 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115 }
2116 operation->alg = 0;
2117 return( PSA_SUCCESS );
2118}
2119
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002120psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 psa_algorithm_t alg )
2122{
Janos Follath24eed8d2019-11-22 13:21:35 +00002123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002124
2125 /* A context must be freshly initialized before it can be set up. */
2126 if( operation->alg != 0 )
2127 {
2128 return( PSA_ERROR_BAD_STATE );
2129 }
2130
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002131 switch( alg )
2132 {
2133#if defined(MBEDTLS_MD2_C)
2134 case PSA_ALG_MD2:
2135 mbedtls_md2_init( &operation->ctx.md2 );
2136 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_MD4_C)
2140 case PSA_ALG_MD4:
2141 mbedtls_md4_init( &operation->ctx.md4 );
2142 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_MD5_C)
2146 case PSA_ALG_MD5:
2147 mbedtls_md5_init( &operation->ctx.md5 );
2148 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_RIPEMD160_C)
2152 case PSA_ALG_RIPEMD160:
2153 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2154 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2155 break;
2156#endif
2157#if defined(MBEDTLS_SHA1_C)
2158 case PSA_ALG_SHA_1:
2159 mbedtls_sha1_init( &operation->ctx.sha1 );
2160 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA256_C)
2164 case PSA_ALG_SHA_224:
2165 mbedtls_sha256_init( &operation->ctx.sha256 );
2166 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2167 break;
2168 case PSA_ALG_SHA_256:
2169 mbedtls_sha256_init( &operation->ctx.sha256 );
2170 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2171 break;
2172#endif
2173#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002174#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175 case PSA_ALG_SHA_384:
2176 mbedtls_sha512_init( &operation->ctx.sha512 );
2177 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2178 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002179#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180 case PSA_ALG_SHA_512:
2181 mbedtls_sha512_init( &operation->ctx.sha512 );
2182 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2183 break;
2184#endif
2185 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002186 return( PSA_ALG_IS_HASH( alg ) ?
2187 PSA_ERROR_NOT_SUPPORTED :
2188 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189 }
2190 if( ret == 0 )
2191 operation->alg = alg;
2192 else
2193 psa_hash_abort( operation );
2194 return( mbedtls_to_psa_error( ret ) );
2195}
2196
2197psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2198 const uint8_t *input,
2199 size_t input_length )
2200{
Janos Follath24eed8d2019-11-22 13:21:35 +00002201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002202
2203 /* Don't require hash implementations to behave correctly on a
2204 * zero-length input, which may have an invalid pointer. */
2205 if( input_length == 0 )
2206 return( PSA_SUCCESS );
2207
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208 switch( operation->alg )
2209 {
2210#if defined(MBEDTLS_MD2_C)
2211 case PSA_ALG_MD2:
2212 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2213 input, input_length );
2214 break;
2215#endif
2216#if defined(MBEDTLS_MD4_C)
2217 case PSA_ALG_MD4:
2218 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2219 input, input_length );
2220 break;
2221#endif
2222#if defined(MBEDTLS_MD5_C)
2223 case PSA_ALG_MD5:
2224 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2225 input, input_length );
2226 break;
2227#endif
2228#if defined(MBEDTLS_RIPEMD160_C)
2229 case PSA_ALG_RIPEMD160:
2230 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2231 input, input_length );
2232 break;
2233#endif
2234#if defined(MBEDTLS_SHA1_C)
2235 case PSA_ALG_SHA_1:
2236 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2237 input, input_length );
2238 break;
2239#endif
2240#if defined(MBEDTLS_SHA256_C)
2241 case PSA_ALG_SHA_224:
2242 case PSA_ALG_SHA_256:
2243 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2244 input, input_length );
2245 break;
2246#endif
2247#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002248#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002250#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 case PSA_ALG_SHA_512:
2252 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2253 input, input_length );
2254 break;
2255#endif
2256 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002257 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002259
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 if( ret != 0 )
2261 psa_hash_abort( operation );
2262 return( mbedtls_to_psa_error( ret ) );
2263}
2264
2265psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2266 uint8_t *hash,
2267 size_t hash_size,
2268 size_t *hash_length )
2269{
itayzafrir40835d42018-08-02 13:14:17 +03002270 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002272 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273
2274 /* Fill the output buffer with something that isn't a valid hash
2275 * (barring an attack on the hash and deliberately-crafted input),
2276 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002277 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002278 /* If hash_size is 0 then hash may be NULL and then the
2279 * call to memset would have undefined behavior. */
2280 if( hash_size != 0 )
2281 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282
2283 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002284 {
2285 status = PSA_ERROR_BUFFER_TOO_SMALL;
2286 goto exit;
2287 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288
2289 switch( operation->alg )
2290 {
2291#if defined(MBEDTLS_MD2_C)
2292 case PSA_ALG_MD2:
2293 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2294 break;
2295#endif
2296#if defined(MBEDTLS_MD4_C)
2297 case PSA_ALG_MD4:
2298 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2299 break;
2300#endif
2301#if defined(MBEDTLS_MD5_C)
2302 case PSA_ALG_MD5:
2303 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2304 break;
2305#endif
2306#if defined(MBEDTLS_RIPEMD160_C)
2307 case PSA_ALG_RIPEMD160:
2308 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2309 break;
2310#endif
2311#if defined(MBEDTLS_SHA1_C)
2312 case PSA_ALG_SHA_1:
2313 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2314 break;
2315#endif
2316#if defined(MBEDTLS_SHA256_C)
2317 case PSA_ALG_SHA_224:
2318 case PSA_ALG_SHA_256:
2319 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2320 break;
2321#endif
2322#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002323#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002325#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_SHA_512:
2327 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2328 break;
2329#endif
2330 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002331 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 }
itayzafrir40835d42018-08-02 13:14:17 +03002333 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334
itayzafrir40835d42018-08-02 13:14:17 +03002335exit:
2336 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002338 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 return( psa_hash_abort( operation ) );
2340 }
2341 else
2342 {
2343 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002344 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 }
2346}
2347
Gilles Peskine2d277862018-06-18 15:41:12 +02002348psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2349 const uint8_t *hash,
2350 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351{
2352 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2353 size_t actual_hash_length;
2354 psa_status_t status = psa_hash_finish( operation,
2355 actual_hash, sizeof( actual_hash ),
2356 &actual_hash_length );
2357 if( status != PSA_SUCCESS )
2358 return( status );
2359 if( actual_hash_length != hash_length )
2360 return( PSA_ERROR_INVALID_SIGNATURE );
2361 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2362 return( PSA_ERROR_INVALID_SIGNATURE );
2363 return( PSA_SUCCESS );
2364}
2365
Gilles Peskine0a749c82019-11-28 19:33:58 +01002366psa_status_t psa_hash_compute( psa_algorithm_t alg,
2367 const uint8_t *input, size_t input_length,
2368 uint8_t *hash, size_t hash_size,
2369 size_t *hash_length )
2370{
2371 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2373
2374 *hash_length = hash_size;
2375 status = psa_hash_setup( &operation, alg );
2376 if( status != PSA_SUCCESS )
2377 goto exit;
2378 status = psa_hash_update( &operation, input, input_length );
2379 if( status != PSA_SUCCESS )
2380 goto exit;
2381 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2382 if( status != PSA_SUCCESS )
2383 goto exit;
2384
2385exit:
2386 if( status == PSA_SUCCESS )
2387 status = psa_hash_abort( &operation );
2388 else
2389 psa_hash_abort( &operation );
2390 return( status );
2391}
2392
2393psa_status_t psa_hash_compare( psa_algorithm_t alg,
2394 const uint8_t *input, size_t input_length,
2395 const uint8_t *hash, size_t hash_length )
2396{
2397 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2399
2400 status = psa_hash_setup( &operation, alg );
2401 if( status != PSA_SUCCESS )
2402 goto exit;
2403 status = psa_hash_update( &operation, input, input_length );
2404 if( status != PSA_SUCCESS )
2405 goto exit;
2406 status = psa_hash_verify( &operation, hash, hash_length );
2407 if( status != PSA_SUCCESS )
2408 goto exit;
2409
2410exit:
2411 if( status == PSA_SUCCESS )
2412 status = psa_hash_abort( &operation );
2413 else
2414 psa_hash_abort( &operation );
2415 return( status );
2416}
2417
Gilles Peskineeb35d782019-01-22 17:56:16 +01002418psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2419 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002420{
2421 if( target_operation->alg != 0 )
2422 return( PSA_ERROR_BAD_STATE );
2423
2424 switch( source_operation->alg )
2425 {
2426 case 0:
2427 return( PSA_ERROR_BAD_STATE );
2428#if defined(MBEDTLS_MD2_C)
2429 case PSA_ALG_MD2:
2430 mbedtls_md2_clone( &target_operation->ctx.md2,
2431 &source_operation->ctx.md2 );
2432 break;
2433#endif
2434#if defined(MBEDTLS_MD4_C)
2435 case PSA_ALG_MD4:
2436 mbedtls_md4_clone( &target_operation->ctx.md4,
2437 &source_operation->ctx.md4 );
2438 break;
2439#endif
2440#if defined(MBEDTLS_MD5_C)
2441 case PSA_ALG_MD5:
2442 mbedtls_md5_clone( &target_operation->ctx.md5,
2443 &source_operation->ctx.md5 );
2444 break;
2445#endif
2446#if defined(MBEDTLS_RIPEMD160_C)
2447 case PSA_ALG_RIPEMD160:
2448 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2449 &source_operation->ctx.ripemd160 );
2450 break;
2451#endif
2452#if defined(MBEDTLS_SHA1_C)
2453 case PSA_ALG_SHA_1:
2454 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2455 &source_operation->ctx.sha1 );
2456 break;
2457#endif
2458#if defined(MBEDTLS_SHA256_C)
2459 case PSA_ALG_SHA_224:
2460 case PSA_ALG_SHA_256:
2461 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2462 &source_operation->ctx.sha256 );
2463 break;
2464#endif
2465#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002466#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002467 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002468#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469 case PSA_ALG_SHA_512:
2470 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2471 &source_operation->ctx.sha512 );
2472 break;
2473#endif
2474 default:
2475 return( PSA_ERROR_NOT_SUPPORTED );
2476 }
2477
2478 target_operation->alg = source_operation->alg;
2479 return( PSA_SUCCESS );
2480}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002481
2482
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484/* MAC */
2485/****************************************************************/
2486
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002487static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 psa_algorithm_t alg,
2489 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002490 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002491 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002494 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002496 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002497 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002498
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2500 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002501 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002503 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002504 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505 mode = MBEDTLS_MODE_STREAM;
2506 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 case PSA_ALG_CTR:
2508 mode = MBEDTLS_MODE_CTR;
2509 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002510 case PSA_ALG_CFB:
2511 mode = MBEDTLS_MODE_CFB;
2512 break;
2513 case PSA_ALG_OFB:
2514 mode = MBEDTLS_MODE_OFB;
2515 break;
2516 case PSA_ALG_CBC_NO_PADDING:
2517 mode = MBEDTLS_MODE_CBC;
2518 break;
2519 case PSA_ALG_CBC_PKCS7:
2520 mode = MBEDTLS_MODE_CBC;
2521 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002522 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523 mode = MBEDTLS_MODE_CCM;
2524 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002525 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526 mode = MBEDTLS_MODE_GCM;
2527 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002528 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2529 mode = MBEDTLS_MODE_CHACHAPOLY;
2530 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 default:
2532 return( NULL );
2533 }
2534 }
2535 else if( alg == PSA_ALG_CMAC )
2536 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 else
2538 return( NULL );
2539
2540 switch( key_type )
2541 {
2542 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002543 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 break;
2545 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002546 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2547 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002549 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002551 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002552 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2553 * but two-key Triple-DES is functionally three-key Triple-DES
2554 * with K1=K3, so that's how we present it to mbedtls. */
2555 if( key_bits == 128 )
2556 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 break;
2558 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002559 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002560 break;
2561 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002562 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002564 case PSA_KEY_TYPE_CHACHA20:
2565 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2566 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567 default:
2568 return( NULL );
2569 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002570 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002571 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572
Jaeden Amero23bbb752018-06-26 14:16:54 +01002573 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2574 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575}
2576
Gilles Peskinea05219c2018-11-16 16:02:56 +01002577#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002578static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002579{
Gilles Peskine2d277862018-06-18 15:41:12 +02002580 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002581 {
2582 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002583 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002584 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002585 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002588 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002597 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002598 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002599 return( 128 );
2600 default:
2601 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002602 }
2603}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002604#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002605
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002606/* Initialize the MAC operation structure. Once this function has been
2607 * called, psa_mac_abort can run and will do the right thing. */
2608static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2609 psa_algorithm_t alg )
2610{
2611 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2612
2613 operation->alg = alg;
2614 operation->key_set = 0;
2615 operation->iv_set = 0;
2616 operation->iv_required = 0;
2617 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002618 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619
2620#if defined(MBEDTLS_CMAC_C)
2621 if( alg == PSA_ALG_CMAC )
2622 {
2623 operation->iv_required = 0;
2624 mbedtls_cipher_init( &operation->ctx.cmac );
2625 status = PSA_SUCCESS;
2626 }
2627 else
2628#endif /* MBEDTLS_CMAC_C */
2629#if defined(MBEDTLS_MD_C)
2630 if( PSA_ALG_IS_HMAC( operation->alg ) )
2631 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002632 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2633 operation->ctx.hmac.hash_ctx.alg = 0;
2634 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635 }
2636 else
2637#endif /* MBEDTLS_MD_C */
2638 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002639 if( ! PSA_ALG_IS_MAC( alg ) )
2640 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641 }
2642
2643 if( status != PSA_SUCCESS )
2644 memset( operation, 0, sizeof( *operation ) );
2645 return( status );
2646}
2647
Gilles Peskine01126fa2018-07-12 17:04:55 +02002648#if defined(MBEDTLS_MD_C)
2649static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2650{
Gilles Peskine3f108122018-12-07 18:14:53 +01002651 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002652 return( psa_hash_abort( &hmac->hash_ctx ) );
2653}
2654#endif /* MBEDTLS_MD_C */
2655
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2657{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002658 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002660 /* The object has (apparently) been initialized but it is not
2661 * in use. It's ok to call abort on such an object, and there's
2662 * nothing to do. */
2663 return( PSA_SUCCESS );
2664 }
2665 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667 if( operation->alg == PSA_ALG_CMAC )
2668 {
2669 mbedtls_cipher_free( &operation->ctx.cmac );
2670 }
2671 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674 if( PSA_ALG_IS_HMAC( operation->alg ) )
2675 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002676 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002677 }
2678 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002680 {
2681 /* Sanity check (shouldn't happen: operation->alg should
2682 * always have been initialized to a valid value). */
2683 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 }
Moran Peker41deec42018-04-04 15:43:05 +03002685
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 operation->alg = 0;
2687 operation->key_set = 0;
2688 operation->iv_set = 0;
2689 operation->iv_required = 0;
2690 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002691 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002692
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002694
2695bad_state:
2696 /* If abort is called on an uninitialized object, we can't trust
2697 * anything. Wipe the object in case it contains confidential data.
2698 * This may result in a memory leak if a pointer gets overwritten,
2699 * but it's too late to do anything about this. */
2700 memset( operation, 0, sizeof( *operation ) );
2701 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702}
2703
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002704#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002705static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002707 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 const mbedtls_cipher_info_t *cipher_info )
2709{
Janos Follath24eed8d2019-11-22 13:21:35 +00002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711
2712 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713
2714 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2715 if( ret != 0 )
2716 return( ret );
2717
2718 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2719 slot->data.raw.data,
2720 key_bits );
2721 return( ret );
2722}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002723#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002724
Gilles Peskine248051a2018-06-20 16:09:38 +02002725#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002726static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2727 const uint8_t *key,
2728 size_t key_length,
2729 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002731 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002733 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002734 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002735 psa_status_t status;
2736
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002737 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2738 * overflow below. This should never trigger if the hash algorithm
2739 * is implemented correctly. */
2740 /* The size checks against the ipad and opad buffers cannot be written
2741 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2742 * because that triggers -Wlogical-op on GCC 7.3. */
2743 if( block_size > sizeof( ipad ) )
2744 return( PSA_ERROR_NOT_SUPPORTED );
2745 if( block_size > sizeof( hmac->opad ) )
2746 return( PSA_ERROR_NOT_SUPPORTED );
2747 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 return( PSA_ERROR_NOT_SUPPORTED );
2749
Gilles Peskined223b522018-06-11 18:12:58 +02002750 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002752 status = psa_hash_compute( hash_alg, key, key_length,
2753 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002754 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002755 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 }
Gilles Peskine96889972018-07-12 17:07:03 +02002757 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2758 * but it is permitted. It is common when HMAC is used in HKDF, for
2759 * example. Don't call `memcpy` in the 0-length because `key` could be
2760 * an invalid pointer which would make the behavior undefined. */
2761 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002762 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763
Gilles Peskined223b522018-06-11 18:12:58 +02002764 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2765 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002766 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002767 ipad[i] ^= 0x36;
2768 memset( ipad + key_length, 0x36, block_size - key_length );
2769
2770 /* Copy the key material from ipad to opad, flipping the requisite bits,
2771 * and filling the rest of opad with the requisite constant. */
2772 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002773 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2774 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002775
Gilles Peskine01126fa2018-07-12 17:04:55 +02002776 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002778 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779
Gilles Peskine01126fa2018-07-12 17:04:55 +02002780 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002781
2782cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002783 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002784
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785 return( status );
2786}
Gilles Peskine248051a2018-06-20 16:09:38 +02002787#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788
Gilles Peskine89167cb2018-07-08 20:12:23 +02002789static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002790 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002791 psa_algorithm_t alg,
2792 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002795 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002797 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002798 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002799 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002800 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002802 /* A context must be freshly initialized before it can be set up. */
2803 if( operation->alg != 0 )
2804 {
2805 return( PSA_ERROR_BAD_STATE );
2806 }
2807
Gilles Peskined911eb72018-08-14 15:18:45 +02002808 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002809 if( status != PSA_SUCCESS )
2810 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002811 if( is_sign )
2812 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813
Gilles Peskine28f8f302019-07-24 13:30:31 +02002814 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002815 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002816 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002817 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002818
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002820 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002821 {
2822 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002823 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002824 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002825 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002826 if( cipher_info == NULL )
2827 {
2828 status = PSA_ERROR_NOT_SUPPORTED;
2829 goto exit;
2830 }
2831 operation->mac_size = cipher_info->block_size;
2832 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2833 status = mbedtls_to_psa_error( ret );
2834 }
2835 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002836#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002838 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002839 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002840 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002841 if( hash_alg == 0 )
2842 {
2843 status = PSA_ERROR_NOT_SUPPORTED;
2844 goto exit;
2845 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002846
2847 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2848 /* Sanity check. This shouldn't fail on a valid configuration. */
2849 if( operation->mac_size == 0 ||
2850 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2851 {
2852 status = PSA_ERROR_NOT_SUPPORTED;
2853 goto exit;
2854 }
2855
Gilles Peskine8e338702019-07-30 20:06:31 +02002856 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002857 {
2858 status = PSA_ERROR_INVALID_ARGUMENT;
2859 goto exit;
2860 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002861
Gilles Peskine01126fa2018-07-12 17:04:55 +02002862 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2863 slot->data.raw.data,
2864 slot->data.raw.bytes,
2865 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 }
2867 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002869 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002870 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002873
Gilles Peskined911eb72018-08-14 15:18:45 +02002874 if( truncated == 0 )
2875 {
2876 /* The "normal" case: untruncated algorithm. Nothing to do. */
2877 }
2878 else if( truncated < 4 )
2879 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002880 /* A very short MAC is too short for security since it can be
2881 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2882 * so we make this our minimum, even though 32 bits is still
2883 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002884 status = PSA_ERROR_NOT_SUPPORTED;
2885 }
2886 else if( truncated > operation->mac_size )
2887 {
2888 /* It's impossible to "truncate" to a larger length. */
2889 status = PSA_ERROR_INVALID_ARGUMENT;
2890 }
2891 else
2892 operation->mac_size = truncated;
2893
Gilles Peskinefbfac682018-07-08 20:51:54 +02002894exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002895 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002897 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002899 else
2900 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 operation->key_set = 1;
2902 }
2903 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904}
2905
Gilles Peskine89167cb2018-07-08 20:12:23 +02002906psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002907 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002908 psa_algorithm_t alg )
2909{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002910 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002911}
2912
2913psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002914 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002915 psa_algorithm_t alg )
2916{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002917 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002918}
2919
Gilles Peskine8c9def32018-02-08 10:02:12 +01002920psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2921 const uint8_t *input,
2922 size_t input_length )
2923{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002924 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002926 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002928 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929 operation->has_input = 1;
2930
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002932 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002933 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002934 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2935 input, input_length );
2936 status = mbedtls_to_psa_error( ret );
2937 }
2938 else
2939#endif /* MBEDTLS_CMAC_C */
2940#if defined(MBEDTLS_MD_C)
2941 if( PSA_ALG_IS_HMAC( operation->alg ) )
2942 {
2943 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2944 input_length );
2945 }
2946 else
2947#endif /* MBEDTLS_MD_C */
2948 {
2949 /* This shouldn't happen if `operation` was initialized by
2950 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002951 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002952 }
2953
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 if( status != PSA_SUCCESS )
2955 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002956 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957}
2958
Gilles Peskine01126fa2018-07-12 17:04:55 +02002959#if defined(MBEDTLS_MD_C)
2960static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2961 uint8_t *mac,
2962 size_t mac_size )
2963{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002964 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002965 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2966 size_t hash_size = 0;
2967 size_t block_size = psa_get_hash_block_size( hash_alg );
2968 psa_status_t status;
2969
Gilles Peskine01126fa2018-07-12 17:04:55 +02002970 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2971 if( status != PSA_SUCCESS )
2972 return( status );
2973 /* From here on, tmp needs to be wiped. */
2974
2975 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2976 if( status != PSA_SUCCESS )
2977 goto exit;
2978
2979 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2980 if( status != PSA_SUCCESS )
2981 goto exit;
2982
2983 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2984 if( status != PSA_SUCCESS )
2985 goto exit;
2986
Gilles Peskined911eb72018-08-14 15:18:45 +02002987 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2988 if( status != PSA_SUCCESS )
2989 goto exit;
2990
2991 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002992
2993exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002994 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002995 return( status );
2996}
2997#endif /* MBEDTLS_MD_C */
2998
mohammad16036df908f2018-04-02 08:34:15 -07002999static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003000 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003001 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003003 if( ! operation->key_set )
3004 return( PSA_ERROR_BAD_STATE );
3005 if( operation->iv_required && ! operation->iv_set )
3006 return( PSA_ERROR_BAD_STATE );
3007
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 if( mac_size < operation->mac_size )
3009 return( PSA_ERROR_BUFFER_TOO_SMALL );
3010
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003012 if( operation->alg == PSA_ALG_CMAC )
3013 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003014 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3015 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3016 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003017 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003018 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003019 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003021 else
3022#endif /* MBEDTLS_CMAC_C */
3023#if defined(MBEDTLS_MD_C)
3024 if( PSA_ALG_IS_HMAC( operation->alg ) )
3025 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003026 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003027 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003028 }
3029 else
3030#endif /* MBEDTLS_MD_C */
3031 {
3032 /* This shouldn't happen if `operation` was initialized by
3033 * a setup function. */
3034 return( PSA_ERROR_BAD_STATE );
3035 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036}
3037
Gilles Peskineacd4be32018-07-08 19:56:25 +02003038psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3039 uint8_t *mac,
3040 size_t mac_size,
3041 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003042{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003043 psa_status_t status;
3044
Jaeden Amero252ef282019-02-15 14:05:35 +00003045 if( operation->alg == 0 )
3046 {
3047 return( PSA_ERROR_BAD_STATE );
3048 }
3049
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003050 /* Fill the output buffer with something that isn't a valid mac
3051 * (barring an attack on the mac and deliberately-crafted input),
3052 * in case the caller doesn't check the return status properly. */
3053 *mac_length = mac_size;
3054 /* If mac_size is 0 then mac may be NULL and then the
3055 * call to memset would have undefined behavior. */
3056 if( mac_size != 0 )
3057 memset( mac, '!', mac_size );
3058
Gilles Peskine89167cb2018-07-08 20:12:23 +02003059 if( ! operation->is_sign )
3060 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003061 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003062 }
mohammad16036df908f2018-04-02 08:34:15 -07003063
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003064 status = psa_mac_finish_internal( operation, mac, mac_size );
3065
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003066 if( status == PSA_SUCCESS )
3067 {
3068 status = psa_mac_abort( operation );
3069 if( status == PSA_SUCCESS )
3070 *mac_length = operation->mac_size;
3071 else
3072 memset( mac, '!', mac_size );
3073 }
3074 else
3075 psa_mac_abort( operation );
3076 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003077}
3078
Gilles Peskineacd4be32018-07-08 19:56:25 +02003079psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3080 const uint8_t *mac,
3081 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082{
Gilles Peskine828ed142018-06-18 23:25:51 +02003083 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003084 psa_status_t status;
3085
Jaeden Amero252ef282019-02-15 14:05:35 +00003086 if( operation->alg == 0 )
3087 {
3088 return( PSA_ERROR_BAD_STATE );
3089 }
3090
Gilles Peskine89167cb2018-07-08 20:12:23 +02003091 if( operation->is_sign )
3092 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003093 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003094 }
3095 if( operation->mac_size != mac_length )
3096 {
3097 status = PSA_ERROR_INVALID_SIGNATURE;
3098 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003099 }
mohammad16036df908f2018-04-02 08:34:15 -07003100
3101 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003102 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003103 if( status != PSA_SUCCESS )
3104 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003105
3106 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3107 status = PSA_ERROR_INVALID_SIGNATURE;
3108
3109cleanup:
3110 if( status == PSA_SUCCESS )
3111 status = psa_mac_abort( operation );
3112 else
3113 psa_mac_abort( operation );
3114
Gilles Peskine3f108122018-12-07 18:14:53 +01003115 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003116
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003117 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003118}
3119
3120
Gilles Peskine20035e32018-02-03 22:44:14 +01003121
Gilles Peskine20035e32018-02-03 22:44:14 +01003122/****************************************************************/
3123/* Asymmetric cryptography */
3124/****************************************************************/
3125
Gilles Peskine2b450e32018-06-27 15:42:46 +02003126#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003127/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003128 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003129static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3130 size_t hash_length,
3131 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003132{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003133 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003134 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003135 *md_alg = mbedtls_md_get_type( md_info );
3136
3137 /* The Mbed TLS RSA module uses an unsigned int for hash length
3138 * parameters. Validate that it fits so that we don't risk an
3139 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003140#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003141 if( hash_length > UINT_MAX )
3142 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003143#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003144
3145#if defined(MBEDTLS_PKCS1_V15)
3146 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3147 * must be correct. */
3148 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3149 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003150 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003151 if( md_info == NULL )
3152 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003153 if( mbedtls_md_get_size( md_info ) != hash_length )
3154 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003155 }
3156#endif /* MBEDTLS_PKCS1_V15 */
3157
3158#if defined(MBEDTLS_PKCS1_V21)
3159 /* PSS requires a hash internally. */
3160 if( PSA_ALG_IS_RSA_PSS( alg ) )
3161 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003162 if( md_info == NULL )
3163 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003164 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003165#endif /* MBEDTLS_PKCS1_V21 */
3166
Gilles Peskine61b91d42018-06-08 16:09:36 +02003167 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003168}
3169
Gilles Peskine2b450e32018-06-27 15:42:46 +02003170static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3171 psa_algorithm_t alg,
3172 const uint8_t *hash,
3173 size_t hash_length,
3174 uint8_t *signature,
3175 size_t signature_size,
3176 size_t *signature_length )
3177{
3178 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003180 mbedtls_md_type_t md_alg;
3181
3182 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3183 if( status != PSA_SUCCESS )
3184 return( status );
3185
Gilles Peskine630a18a2018-06-29 17:49:35 +02003186 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003187 return( PSA_ERROR_BUFFER_TOO_SMALL );
3188
3189#if defined(MBEDTLS_PKCS1_V15)
3190 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3191 {
3192 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3193 MBEDTLS_MD_NONE );
3194 ret = mbedtls_rsa_pkcs1_sign( rsa,
3195 mbedtls_ctr_drbg_random,
3196 &global_data.ctr_drbg,
3197 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003198 md_alg,
3199 (unsigned int) hash_length,
3200 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003201 signature );
3202 }
3203 else
3204#endif /* MBEDTLS_PKCS1_V15 */
3205#if defined(MBEDTLS_PKCS1_V21)
3206 if( PSA_ALG_IS_RSA_PSS( alg ) )
3207 {
3208 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3209 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3210 mbedtls_ctr_drbg_random,
3211 &global_data.ctr_drbg,
3212 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003213 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003214 (unsigned int) hash_length,
3215 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003216 signature );
3217 }
3218 else
3219#endif /* MBEDTLS_PKCS1_V21 */
3220 {
3221 return( PSA_ERROR_INVALID_ARGUMENT );
3222 }
3223
3224 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003225 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003226 return( mbedtls_to_psa_error( ret ) );
3227}
3228
3229static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3230 psa_algorithm_t alg,
3231 const uint8_t *hash,
3232 size_t hash_length,
3233 const uint8_t *signature,
3234 size_t signature_length )
3235{
3236 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003238 mbedtls_md_type_t md_alg;
3239
3240 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3241 if( status != PSA_SUCCESS )
3242 return( status );
3243
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003244 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3245 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003246
3247#if defined(MBEDTLS_PKCS1_V15)
3248 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3249 {
3250 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3251 MBEDTLS_MD_NONE );
3252 ret = mbedtls_rsa_pkcs1_verify( rsa,
3253 mbedtls_ctr_drbg_random,
3254 &global_data.ctr_drbg,
3255 MBEDTLS_RSA_PUBLIC,
3256 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003257 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003258 hash,
3259 signature );
3260 }
3261 else
3262#endif /* MBEDTLS_PKCS1_V15 */
3263#if defined(MBEDTLS_PKCS1_V21)
3264 if( PSA_ALG_IS_RSA_PSS( alg ) )
3265 {
3266 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3267 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3268 mbedtls_ctr_drbg_random,
3269 &global_data.ctr_drbg,
3270 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003271 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003272 (unsigned int) hash_length,
3273 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003274 signature );
3275 }
3276 else
3277#endif /* MBEDTLS_PKCS1_V21 */
3278 {
3279 return( PSA_ERROR_INVALID_ARGUMENT );
3280 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003281
3282 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3283 * the rest of the signature is invalid". This has little use in
3284 * practice and PSA doesn't report this distinction. */
3285 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3286 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003287 return( mbedtls_to_psa_error( ret ) );
3288}
3289#endif /* MBEDTLS_RSA_C */
3290
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003291#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003292/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3293 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3294 * (even though these functions don't modify it). */
3295static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3296 psa_algorithm_t alg,
3297 const uint8_t *hash,
3298 size_t hash_length,
3299 uint8_t *signature,
3300 size_t signature_size,
3301 size_t *signature_length )
3302{
Janos Follath24eed8d2019-11-22 13:21:35 +00003303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003304 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003305 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003306 mbedtls_mpi_init( &r );
3307 mbedtls_mpi_init( &s );
3308
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003309 if( signature_size < 2 * curve_bytes )
3310 {
3311 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3312 goto cleanup;
3313 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003314
Gilles Peskinea05219c2018-11-16 16:02:56 +01003315#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003316 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3317 {
3318 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3319 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3320 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003321 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3322 &ecp->d, hash,
3323 hash_length, md_alg,
3324 mbedtls_ctr_drbg_random,
3325 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003326 }
3327 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003328#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003329 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003330 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003331 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3332 hash, hash_length,
3333 mbedtls_ctr_drbg_random,
3334 &global_data.ctr_drbg ) );
3335 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003336
3337 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3338 signature,
3339 curve_bytes ) );
3340 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3341 signature + curve_bytes,
3342 curve_bytes ) );
3343
3344cleanup:
3345 mbedtls_mpi_free( &r );
3346 mbedtls_mpi_free( &s );
3347 if( ret == 0 )
3348 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003349 return( mbedtls_to_psa_error( ret ) );
3350}
3351
3352static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3353 const uint8_t *hash,
3354 size_t hash_length,
3355 const uint8_t *signature,
3356 size_t signature_length )
3357{
Janos Follath24eed8d2019-11-22 13:21:35 +00003358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003359 mbedtls_mpi r, s;
3360 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3361 mbedtls_mpi_init( &r );
3362 mbedtls_mpi_init( &s );
3363
3364 if( signature_length != 2 * curve_bytes )
3365 return( PSA_ERROR_INVALID_SIGNATURE );
3366
3367 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3368 signature,
3369 curve_bytes ) );
3370 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3371 signature + curve_bytes,
3372 curve_bytes ) );
3373
3374 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3375 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003376
3377cleanup:
3378 mbedtls_mpi_free( &r );
3379 mbedtls_mpi_free( &s );
3380 return( mbedtls_to_psa_error( ret ) );
3381}
3382#endif /* MBEDTLS_ECDSA_C */
3383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003384psa_status_t psa_sign_hash( psa_key_handle_t handle,
3385 psa_algorithm_t alg,
3386 const uint8_t *hash,
3387 size_t hash_length,
3388 uint8_t *signature,
3389 size_t signature_size,
3390 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003392 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003393 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3395 const psa_drv_se_t *drv;
3396 psa_drv_se_context_t *drv_context;
3397#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003398
3399 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003400 /* Immediately reject a zero-length signature buffer. This guarantees
3401 * that signature must be a valid pointer. (On the other hand, the hash
3402 * buffer can in principle be empty since it doesn't actually have
3403 * to be a hash.) */
3404 if( signature_size == 0 )
3405 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003407 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003408 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003409 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003410 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 {
3412 status = PSA_ERROR_INVALID_ARGUMENT;
3413 goto exit;
3414 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003415
Gilles Peskineedc64242019-08-07 21:05:07 +02003416#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3417 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3418 {
3419 if( drv->asymmetric == NULL ||
3420 drv->asymmetric->p_sign == NULL )
3421 {
3422 status = PSA_ERROR_NOT_SUPPORTED;
3423 goto exit;
3424 }
3425 status = drv->asymmetric->p_sign( drv_context,
3426 slot->data.se.slot_number,
3427 alg,
3428 hash, hash_length,
3429 signature, signature_size,
3430 signature_length );
3431 }
3432 else
3433#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003434#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003435 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003436 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003437 status = psa_rsa_sign( slot->data.rsa,
3438 alg,
3439 hash, hash_length,
3440 signature, signature_size,
3441 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003442 }
3443 else
3444#endif /* defined(MBEDTLS_RSA_C) */
3445#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003446 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003447 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003448#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003449 if(
3450#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3451 PSA_ALG_IS_ECDSA( alg )
3452#else
3453 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3454#endif
3455 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003456 status = psa_ecdsa_sign( slot->data.ecp,
3457 alg,
3458 hash, hash_length,
3459 signature, signature_size,
3460 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003461 else
3462#endif /* defined(MBEDTLS_ECDSA_C) */
3463 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003464 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003465 }
itayzafrir5c753392018-05-08 11:18:38 +03003466 }
3467 else
3468#endif /* defined(MBEDTLS_ECP_C) */
3469 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003470 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003471 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003472
3473exit:
3474 /* Fill the unused part of the output buffer (the whole buffer on error,
3475 * the trailing part on success) with something that isn't a valid mac
3476 * (barring an attack on the mac and deliberately-crafted input),
3477 * in case the caller doesn't check the return status properly. */
3478 if( status == PSA_SUCCESS )
3479 memset( signature + *signature_length, '!',
3480 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003481 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003482 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003483 /* If signature_size is 0 then we have nothing to do. We must not call
3484 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003485 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003486}
3487
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003488psa_status_t psa_verify_hash( psa_key_handle_t handle,
3489 psa_algorithm_t alg,
3490 const uint8_t *hash,
3491 size_t hash_length,
3492 const uint8_t *signature,
3493 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003494{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003495 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003496 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003497#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3498 const psa_drv_se_t *drv;
3499 psa_drv_se_context_t *drv_context;
3500#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003501
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003502 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003503 if( status != PSA_SUCCESS )
3504 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003505
Gilles Peskineedc64242019-08-07 21:05:07 +02003506#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3507 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3508 {
3509 if( drv->asymmetric == NULL ||
3510 drv->asymmetric->p_verify == NULL )
3511 return( PSA_ERROR_NOT_SUPPORTED );
3512 return( drv->asymmetric->p_verify( drv_context,
3513 slot->data.se.slot_number,
3514 alg,
3515 hash, hash_length,
3516 signature, signature_length ) );
3517 }
3518 else
3519#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003520#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003521 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003523 return( psa_rsa_verify( slot->data.rsa,
3524 alg,
3525 hash, hash_length,
3526 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527 }
3528 else
3529#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003530#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003531 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003532 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003533#if defined(MBEDTLS_ECDSA_C)
3534 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003535 return( psa_ecdsa_verify( slot->data.ecp,
3536 hash, hash_length,
3537 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538 else
3539#endif /* defined(MBEDTLS_ECDSA_C) */
3540 {
3541 return( PSA_ERROR_INVALID_ARGUMENT );
3542 }
itayzafrir5c753392018-05-08 11:18:38 +03003543 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003544 else
3545#endif /* defined(MBEDTLS_ECP_C) */
3546 {
3547 return( PSA_ERROR_NOT_SUPPORTED );
3548 }
3549}
3550
Gilles Peskine072ac562018-06-30 00:21:29 +02003551#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3552static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3553 mbedtls_rsa_context *rsa )
3554{
3555 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3556 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3557 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3558 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3559}
3560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3561
Gilles Peskinec5487a82018-12-03 18:08:14 +01003562psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003563 psa_algorithm_t alg,
3564 const uint8_t *input,
3565 size_t input_length,
3566 const uint8_t *salt,
3567 size_t salt_length,
3568 uint8_t *output,
3569 size_t output_size,
3570 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003572 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003573 psa_status_t status;
3574
Darryl Green5cc689a2018-07-24 15:34:10 +01003575 (void) input;
3576 (void) input_length;
3577 (void) salt;
3578 (void) output;
3579 (void) output_size;
3580
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003581 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003583 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3584 return( PSA_ERROR_INVALID_ARGUMENT );
3585
Gilles Peskine28f8f302019-07-24 13:30:31 +02003586 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003587 if( status != PSA_SUCCESS )
3588 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003589 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3590 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592
3593#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003594 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003595 {
3596 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003598 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003599 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003601 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003603 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3604 mbedtls_ctr_drbg_random,
3605 &global_data.ctr_drbg,
3606 MBEDTLS_RSA_PUBLIC,
3607 input_length,
3608 input,
3609 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 }
3611 else
3612#endif /* MBEDTLS_PKCS1_V15 */
3613#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003614 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003616 psa_rsa_oaep_set_padding_mode( alg, rsa );
3617 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3618 mbedtls_ctr_drbg_random,
3619 &global_data.ctr_drbg,
3620 MBEDTLS_RSA_PUBLIC,
3621 salt, salt_length,
3622 input_length,
3623 input,
3624 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003625 }
3626 else
3627#endif /* MBEDTLS_PKCS1_V21 */
3628 {
3629 return( PSA_ERROR_INVALID_ARGUMENT );
3630 }
3631 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003632 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 return( mbedtls_to_psa_error( ret ) );
3634 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003636#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637 {
3638 return( PSA_ERROR_NOT_SUPPORTED );
3639 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003640}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641
Gilles Peskinec5487a82018-12-03 18:08:14 +01003642psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003643 psa_algorithm_t alg,
3644 const uint8_t *input,
3645 size_t input_length,
3646 const uint8_t *salt,
3647 size_t salt_length,
3648 uint8_t *output,
3649 size_t output_size,
3650 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003652 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003653 psa_status_t status;
3654
Darryl Green5cc689a2018-07-24 15:34:10 +01003655 (void) input;
3656 (void) input_length;
3657 (void) salt;
3658 (void) output;
3659 (void) output_size;
3660
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003661 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003663 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3664 return( PSA_ERROR_INVALID_ARGUMENT );
3665
Gilles Peskine28f8f302019-07-24 13:30:31 +02003666 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003667 if( status != PSA_SUCCESS )
3668 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003669 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 return( PSA_ERROR_INVALID_ARGUMENT );
3671
3672#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003673 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 {
3675 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677
Gilles Peskine630a18a2018-06-29 17:49:35 +02003678 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 return( PSA_ERROR_INVALID_ARGUMENT );
3680
3681#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003682 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003684 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3685 mbedtls_ctr_drbg_random,
3686 &global_data.ctr_drbg,
3687 MBEDTLS_RSA_PRIVATE,
3688 output_length,
3689 input,
3690 output,
3691 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003692 }
3693 else
3694#endif /* MBEDTLS_PKCS1_V15 */
3695#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003696 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003698 psa_rsa_oaep_set_padding_mode( alg, rsa );
3699 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3700 mbedtls_ctr_drbg_random,
3701 &global_data.ctr_drbg,
3702 MBEDTLS_RSA_PRIVATE,
3703 salt, salt_length,
3704 output_length,
3705 input,
3706 output,
3707 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003708 }
3709 else
3710#endif /* MBEDTLS_PKCS1_V21 */
3711 {
3712 return( PSA_ERROR_INVALID_ARGUMENT );
3713 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003714
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 return( mbedtls_to_psa_error( ret ) );
3716 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003718#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719 {
3720 return( PSA_ERROR_NOT_SUPPORTED );
3721 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003722}
Gilles Peskine20035e32018-02-03 22:44:14 +01003723
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003724
3725
mohammad1603503973b2018-03-12 15:59:30 +02003726/****************************************************************/
3727/* Symmetric cryptography */
3728/****************************************************************/
3729
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003730/* Initialize the cipher operation structure. Once this function has been
3731 * called, psa_cipher_abort can run and will do the right thing. */
3732static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3733 psa_algorithm_t alg )
3734{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003735 if( ! PSA_ALG_IS_CIPHER( alg ) )
3736 {
3737 memset( operation, 0, sizeof( *operation ) );
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 }
3740
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003741 operation->alg = alg;
3742 operation->key_set = 0;
3743 operation->iv_set = 0;
3744 operation->iv_required = 1;
3745 operation->iv_size = 0;
3746 operation->block_size = 0;
3747 mbedtls_cipher_init( &operation->ctx.cipher );
3748 return( PSA_SUCCESS );
3749}
3750
Gilles Peskinee553c652018-06-04 16:22:46 +02003751static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003752 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003753 psa_algorithm_t alg,
3754 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003755{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003756 int ret = 0;
3757 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003758 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003759 size_t key_bits;
3760 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003761 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3762 PSA_KEY_USAGE_ENCRYPT :
3763 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003764
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003765 /* A context must be freshly initialized before it can be set up. */
3766 if( operation->alg != 0 )
3767 {
3768 return( PSA_ERROR_BAD_STATE );
3769 }
3770
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003771 status = psa_cipher_init( operation, alg );
3772 if( status != PSA_SUCCESS )
3773 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003774
Gilles Peskine28f8f302019-07-24 13:30:31 +02003775 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003776 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003777 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003778 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003779
Gilles Peskine8e338702019-07-30 20:06:31 +02003780 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003781 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003782 {
3783 status = PSA_ERROR_NOT_SUPPORTED;
3784 goto exit;
3785 }
mohammad1603503973b2018-03-12 15:59:30 +02003786
mohammad1603503973b2018-03-12 15:59:30 +02003787 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003788 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003789 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003790
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003791#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003792 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003793 {
3794 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003795 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003796 memcpy( keys, slot->data.raw.data, 16 );
3797 memcpy( keys + 16, slot->data.raw.data, 8 );
3798 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3799 keys,
3800 192, cipher_operation );
3801 }
3802 else
3803#endif
3804 {
3805 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3806 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003807 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003808 }
Moran Peker41deec42018-04-04 15:43:05 +03003809 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003810 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003811
mohammad16038481e742018-03-18 13:57:31 +02003812#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003813 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003814 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003815 case PSA_ALG_CBC_NO_PADDING:
3816 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3817 MBEDTLS_PADDING_NONE );
3818 break;
3819 case PSA_ALG_CBC_PKCS7:
3820 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3821 MBEDTLS_PADDING_PKCS7 );
3822 break;
3823 default:
3824 /* The algorithm doesn't involve padding. */
3825 ret = 0;
3826 break;
3827 }
3828 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003829 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003830#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3831
mohammad1603503973b2018-03-12 15:59:30 +02003832 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003833 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003834 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003835 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003836 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003837 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003838 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003839#if defined(MBEDTLS_CHACHA20_C)
3840 else
3841 if( alg == PSA_ALG_CHACHA20 )
3842 operation->iv_size = 12;
3843#endif
mohammad1603503973b2018-03-12 15:59:30 +02003844
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003845exit:
3846 if( status == 0 )
3847 status = mbedtls_to_psa_error( ret );
3848 if( status != 0 )
3849 psa_cipher_abort( operation );
3850 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003851}
3852
Gilles Peskinefe119512018-07-08 21:39:34 +02003853psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003854 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003855 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003856{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003857 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003858}
3859
Gilles Peskinefe119512018-07-08 21:39:34 +02003860psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003861 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003862 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003863{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003864 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003865}
3866
Gilles Peskinefe119512018-07-08 21:39:34 +02003867psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003868 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003869 size_t iv_size,
3870 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003871{
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003874 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003875 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003876 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003877 }
Moran Peker41deec42018-04-04 15:43:05 +03003878 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003879 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003880 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003881 goto exit;
3882 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003883 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3884 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003885 if( ret != 0 )
3886 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003887 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003888 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003889 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003890
mohammad16038481e742018-03-18 13:57:31 +02003891 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003893
Moran Peker395db872018-05-31 14:07:14 +03003894exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003896 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003898}
3899
Gilles Peskinefe119512018-07-08 21:39:34 +02003900psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003901 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003902 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003903{
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003906 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003908 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003909 }
Moran Pekera28258c2018-05-29 16:25:04 +03003910 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003911 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 status = PSA_ERROR_INVALID_ARGUMENT;
3913 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003914 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3916 status = mbedtls_to_psa_error( ret );
3917exit:
3918 if( status == PSA_SUCCESS )
3919 operation->iv_set = 1;
3920 else
mohammad1603503973b2018-03-12 15:59:30 +02003921 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003923}
3924
Gilles Peskinee553c652018-06-04 16:22:46 +02003925psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3926 const uint8_t *input,
3927 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003928 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003929 size_t output_size,
3930 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003931{
itayzafrir534bd7c2018-08-02 13:56:32 +03003932 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003934 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003935
3936 if( operation->alg == 0 )
3937 {
3938 return( PSA_ERROR_BAD_STATE );
3939 }
3940
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003941 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003942 {
3943 /* Take the unprocessed partial block left over from previous
3944 * update calls, if any, plus the input to this call. Remove
3945 * the last partial block, if any. You get the data that will be
3946 * output in this call. */
3947 expected_output_size =
3948 ( operation->ctx.cipher.unprocessed_len + input_length )
3949 / operation->block_size * operation->block_size;
3950 }
3951 else
3952 {
3953 expected_output_size = input_length;
3954 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003955
Gilles Peskine89d789c2018-06-04 17:17:16 +02003956 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003957 {
3958 status = PSA_ERROR_BUFFER_TOO_SMALL;
3959 goto exit;
3960 }
mohammad160382759612018-03-12 18:16:40 +02003961
mohammad1603503973b2018-03-12 15:59:30 +02003962 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003963 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003964 status = mbedtls_to_psa_error( ret );
3965exit:
3966 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003967 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003968 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003969}
3970
Gilles Peskinee553c652018-06-04 16:22:46 +02003971psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3972 uint8_t *output,
3973 size_t output_size,
3974 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003975{
David Saadab4ecc272019-02-14 13:48:10 +02003976 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003977 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003978 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003979
mohammad1603503973b2018-03-12 15:59:30 +02003980 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003981 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003982 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003983 }
3984 if( operation->iv_required && ! operation->iv_set )
3985 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003986 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003987 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003988
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003989 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003990 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3991 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003992 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003993 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003994 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003995 }
3996
Janos Follath315b51c2018-07-09 16:04:51 +01003997 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3998 temp_output_buffer,
3999 output_length );
4000 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004001 {
Janos Follath315b51c2018-07-09 16:04:51 +01004002 status = mbedtls_to_psa_error( cipher_ret );
4003 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004004 }
Janos Follath315b51c2018-07-09 16:04:51 +01004005
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004006 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004007 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004008 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004009 memcpy( output, temp_output_buffer, *output_length );
4010 else
4011 {
Janos Follath315b51c2018-07-09 16:04:51 +01004012 status = PSA_ERROR_BUFFER_TOO_SMALL;
4013 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004014 }
mohammad1603503973b2018-03-12 15:59:30 +02004015
Gilles Peskine3f108122018-12-07 18:14:53 +01004016 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004017 status = psa_cipher_abort( operation );
4018
4019 return( status );
4020
4021error:
4022
4023 *output_length = 0;
4024
Gilles Peskine3f108122018-12-07 18:14:53 +01004025 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004026 (void) psa_cipher_abort( operation );
4027
4028 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004029}
4030
Gilles Peskinee553c652018-06-04 16:22:46 +02004031psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4032{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004033 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004034 {
4035 /* The object has (apparently) been initialized but it is not
4036 * in use. It's ok to call abort on such an object, and there's
4037 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004038 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004039 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004040
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004041 /* Sanity check (shouldn't happen: operation->alg should
4042 * always have been initialized to a valid value). */
4043 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4044 return( PSA_ERROR_BAD_STATE );
4045
mohammad1603503973b2018-03-12 15:59:30 +02004046 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004047
Moran Peker41deec42018-04-04 15:43:05 +03004048 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004049 operation->key_set = 0;
4050 operation->iv_set = 0;
4051 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004052 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004053 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004054
Moran Peker395db872018-05-31 14:07:14 +03004055 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004056}
4057
Gilles Peskinea0655c32018-04-30 17:06:50 +02004058
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004059
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004060
mohammad16035955c982018-04-26 00:53:03 +03004061/****************************************************************/
4062/* AEAD */
4063/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004064
Gilles Peskineedf9a652018-08-17 18:11:56 +02004065typedef struct
4066{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004067 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004068 const mbedtls_cipher_info_t *cipher_info;
4069 union
4070 {
4071#if defined(MBEDTLS_CCM_C)
4072 mbedtls_ccm_context ccm;
4073#endif /* MBEDTLS_CCM_C */
4074#if defined(MBEDTLS_GCM_C)
4075 mbedtls_gcm_context gcm;
4076#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004077#if defined(MBEDTLS_CHACHAPOLY_C)
4078 mbedtls_chachapoly_context chachapoly;
4079#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004080 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004081 psa_algorithm_t core_alg;
4082 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004083 uint8_t tag_length;
4084} aead_operation_t;
4085
Gilles Peskine30a9e412019-01-14 18:36:12 +01004086static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004088 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089 {
4090#if defined(MBEDTLS_CCM_C)
4091 case PSA_ALG_CCM:
4092 mbedtls_ccm_free( &operation->ctx.ccm );
4093 break;
4094#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004095#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 case PSA_ALG_GCM:
4097 mbedtls_gcm_free( &operation->ctx.gcm );
4098 break;
4099#endif /* MBEDTLS_GCM_C */
4100 }
4101}
4102
4103static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004104 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004105 psa_key_usage_t usage,
4106 psa_algorithm_t alg )
4107{
4108 psa_status_t status;
4109 size_t key_bits;
4110 mbedtls_cipher_id_t cipher_id;
4111
Gilles Peskine28f8f302019-07-24 13:30:31 +02004112 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004113 if( status != PSA_SUCCESS )
4114 return( status );
4115
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004116 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117
4118 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004119 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004120 &cipher_id );
4121 if( operation->cipher_info == NULL )
4122 return( PSA_ERROR_NOT_SUPPORTED );
4123
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004124 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125 {
4126#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004127 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4128 operation->core_alg = PSA_ALG_CCM;
4129 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004130 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4131 * The call to mbedtls_ccm_encrypt_and_tag or
4132 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004133 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004134 return( PSA_ERROR_INVALID_ARGUMENT );
4135 mbedtls_ccm_init( &operation->ctx.ccm );
4136 status = mbedtls_to_psa_error(
4137 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4138 operation->slot->data.raw.data,
4139 (unsigned int) key_bits ) );
4140 if( status != 0 )
4141 goto cleanup;
4142 break;
4143#endif /* MBEDTLS_CCM_C */
4144
4145#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004146 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4147 operation->core_alg = PSA_ALG_GCM;
4148 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004149 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4150 * The call to mbedtls_gcm_crypt_and_tag or
4151 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004152 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004153 return( PSA_ERROR_INVALID_ARGUMENT );
4154 mbedtls_gcm_init( &operation->ctx.gcm );
4155 status = mbedtls_to_psa_error(
4156 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4157 operation->slot->data.raw.data,
4158 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004159 if( status != 0 )
4160 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 break;
4162#endif /* MBEDTLS_GCM_C */
4163
Gilles Peskine26869f22019-05-06 15:25:00 +02004164#if defined(MBEDTLS_CHACHAPOLY_C)
4165 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4166 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4167 operation->full_tag_length = 16;
4168 /* We only support the default tag length. */
4169 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4170 return( PSA_ERROR_NOT_SUPPORTED );
4171 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4172 status = mbedtls_to_psa_error(
4173 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4174 operation->slot->data.raw.data ) );
4175 if( status != 0 )
4176 goto cleanup;
4177 break;
4178#endif /* MBEDTLS_CHACHAPOLY_C */
4179
Gilles Peskineedf9a652018-08-17 18:11:56 +02004180 default:
4181 return( PSA_ERROR_NOT_SUPPORTED );
4182 }
4183
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004184 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4185 {
4186 status = PSA_ERROR_INVALID_ARGUMENT;
4187 goto cleanup;
4188 }
4189 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004190
Gilles Peskineedf9a652018-08-17 18:11:56 +02004191 return( PSA_SUCCESS );
4192
4193cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004194 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004195 return( status );
4196}
4197
Gilles Peskinec5487a82018-12-03 18:08:14 +01004198psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004199 psa_algorithm_t alg,
4200 const uint8_t *nonce,
4201 size_t nonce_length,
4202 const uint8_t *additional_data,
4203 size_t additional_data_length,
4204 const uint8_t *plaintext,
4205 size_t plaintext_length,
4206 uint8_t *ciphertext,
4207 size_t ciphertext_size,
4208 size_t *ciphertext_length )
4209{
mohammad16035955c982018-04-26 00:53:03 +03004210 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004211 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004212 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004213
mohammad1603f08a5502018-06-03 15:05:47 +03004214 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004215
Gilles Peskinec5487a82018-12-03 18:08:14 +01004216 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004217 if( status != PSA_SUCCESS )
4218 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004219
Gilles Peskineedf9a652018-08-17 18:11:56 +02004220 /* For all currently supported modes, the tag is at the end of the
4221 * ciphertext. */
4222 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4223 {
4224 status = PSA_ERROR_BUFFER_TOO_SMALL;
4225 goto exit;
4226 }
4227 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004228
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004229#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004230 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004231 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004232 status = mbedtls_to_psa_error(
4233 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4234 MBEDTLS_GCM_ENCRYPT,
4235 plaintext_length,
4236 nonce, nonce_length,
4237 additional_data, additional_data_length,
4238 plaintext, ciphertext,
4239 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004240 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004241 else
4242#endif /* MBEDTLS_GCM_C */
4243#if defined(MBEDTLS_CCM_C)
4244 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004245 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004246 status = mbedtls_to_psa_error(
4247 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4248 plaintext_length,
4249 nonce, nonce_length,
4250 additional_data,
4251 additional_data_length,
4252 plaintext, ciphertext,
4253 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004254 }
mohammad16035c8845f2018-05-09 05:40:09 -07004255 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004256#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004257#if defined(MBEDTLS_CHACHAPOLY_C)
4258 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4259 {
4260 if( nonce_length != 12 || operation.tag_length != 16 )
4261 {
4262 status = PSA_ERROR_NOT_SUPPORTED;
4263 goto exit;
4264 }
4265 status = mbedtls_to_psa_error(
4266 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4267 plaintext_length,
4268 nonce,
4269 additional_data,
4270 additional_data_length,
4271 plaintext,
4272 ciphertext,
4273 tag ) );
4274 }
4275 else
4276#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004277 {
mohammad1603554faad2018-06-03 15:07:38 +03004278 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004279 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004280
Gilles Peskineedf9a652018-08-17 18:11:56 +02004281 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4282 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004283
Gilles Peskineedf9a652018-08-17 18:11:56 +02004284exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004285 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004286 if( status == PSA_SUCCESS )
4287 *ciphertext_length = plaintext_length + operation.tag_length;
4288 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004289}
4290
Gilles Peskineee652a32018-06-01 19:23:52 +02004291/* Locate the tag in a ciphertext buffer containing the encrypted data
4292 * followed by the tag. Return the length of the part preceding the tag in
4293 * *plaintext_length. This is the size of the plaintext in modes where
4294 * the encrypted data has the same size as the plaintext, such as
4295 * CCM and GCM. */
4296static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4297 const uint8_t *ciphertext,
4298 size_t ciphertext_length,
4299 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004300 const uint8_t **p_tag )
4301{
4302 size_t payload_length;
4303 if( tag_length > ciphertext_length )
4304 return( PSA_ERROR_INVALID_ARGUMENT );
4305 payload_length = ciphertext_length - tag_length;
4306 if( payload_length > plaintext_size )
4307 return( PSA_ERROR_BUFFER_TOO_SMALL );
4308 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004309 return( PSA_SUCCESS );
4310}
4311
Gilles Peskinec5487a82018-12-03 18:08:14 +01004312psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004313 psa_algorithm_t alg,
4314 const uint8_t *nonce,
4315 size_t nonce_length,
4316 const uint8_t *additional_data,
4317 size_t additional_data_length,
4318 const uint8_t *ciphertext,
4319 size_t ciphertext_length,
4320 uint8_t *plaintext,
4321 size_t plaintext_size,
4322 size_t *plaintext_length )
4323{
mohammad16035955c982018-04-26 00:53:03 +03004324 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004325 aead_operation_t operation;
4326 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004327
Gilles Peskineee652a32018-06-01 19:23:52 +02004328 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004329
Gilles Peskinec5487a82018-12-03 18:08:14 +01004330 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004331 if( status != PSA_SUCCESS )
4332 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004333
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004334 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4335 ciphertext, ciphertext_length,
4336 plaintext_size, &tag );
4337 if( status != PSA_SUCCESS )
4338 goto exit;
4339
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004340#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004341 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004342 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004343 status = mbedtls_to_psa_error(
4344 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4345 ciphertext_length - operation.tag_length,
4346 nonce, nonce_length,
4347 additional_data,
4348 additional_data_length,
4349 tag, operation.tag_length,
4350 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004351 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004352 else
4353#endif /* MBEDTLS_GCM_C */
4354#if defined(MBEDTLS_CCM_C)
4355 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004356 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004357 status = mbedtls_to_psa_error(
4358 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4359 ciphertext_length - operation.tag_length,
4360 nonce, nonce_length,
4361 additional_data,
4362 additional_data_length,
4363 ciphertext, plaintext,
4364 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004365 }
mohammad160339574652018-06-01 04:39:53 -07004366 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004367#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004368#if defined(MBEDTLS_CHACHAPOLY_C)
4369 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4370 {
4371 if( nonce_length != 12 || operation.tag_length != 16 )
4372 {
4373 status = PSA_ERROR_NOT_SUPPORTED;
4374 goto exit;
4375 }
4376 status = mbedtls_to_psa_error(
4377 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4378 ciphertext_length - operation.tag_length,
4379 nonce,
4380 additional_data,
4381 additional_data_length,
4382 tag,
4383 ciphertext,
4384 plaintext ) );
4385 }
4386 else
4387#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004388 {
mohammad1603554faad2018-06-03 15:07:38 +03004389 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004390 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004391
Gilles Peskineedf9a652018-08-17 18:11:56 +02004392 if( status != PSA_SUCCESS && plaintext_size != 0 )
4393 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004394
Gilles Peskineedf9a652018-08-17 18:11:56 +02004395exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004396 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397 if( status == PSA_SUCCESS )
4398 *plaintext_length = ciphertext_length - operation.tag_length;
4399 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004400}
4401
Gilles Peskinea0655c32018-04-30 17:06:50 +02004402
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004403
Gilles Peskine20035e32018-02-03 22:44:14 +01004404/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004405/* Generators */
4406/****************************************************************/
4407
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004408#define HKDF_STATE_INIT 0 /* no input yet */
4409#define HKDF_STATE_STARTED 1 /* got salt */
4410#define HKDF_STATE_KEYED 2 /* got key */
4411#define HKDF_STATE_OUTPUT 3 /* output started */
4412
Gilles Peskinecbe66502019-05-16 16:59:18 +02004413static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004415{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4417 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004418 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004420}
4421
4422
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004424{
4425 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004428 {
4429 /* The object has (apparently) been initialized but it is not
4430 * in use. It's ok to call abort on such an object, and there's
4431 * nothing to do. */
4432 }
4433 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004434#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004435 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004436 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 mbedtls_free( operation->ctx.hkdf.info );
4438 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004439 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004442 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004443 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004444 if( operation->ctx.tls12_prf.seed != NULL )
4445 {
4446 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4447 operation->ctx.tls12_prf.seed_length );
4448 mbedtls_free( operation->ctx.tls12_prf.seed );
4449 }
4450
4451 if( operation->ctx.tls12_prf.label != NULL )
4452 {
4453 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4454 operation->ctx.tls12_prf.label_length );
4455 mbedtls_free( operation->ctx.tls12_prf.label );
4456 }
4457
4458 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4459
4460 /* We leave the fields Ai and output_block to be erased safely by the
4461 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004462 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004463 else
4464#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004465 {
4466 status = PSA_ERROR_BAD_STATE;
4467 }
Janos Follath083036a2019-06-11 10:22:26 +01004468 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004469 return( status );
4470}
4471
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004473 size_t *capacity)
4474{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004476 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004478 return PSA_ERROR_BAD_STATE;
4479 }
4480
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004481 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004482 return( PSA_SUCCESS );
4483}
4484
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004486 size_t capacity )
4487{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004489 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004491 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004493 return( PSA_SUCCESS );
4494}
4495
Gilles Peskinebef7f142018-07-12 17:22:21 +02004496#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004498 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004499static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004500 psa_algorithm_t hash_alg,
4501 uint8_t *output,
4502 size_t output_length )
4503{
4504 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4505 psa_status_t status;
4506
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004507 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4508 return( PSA_ERROR_BAD_STATE );
4509 hkdf->state = HKDF_STATE_OUTPUT;
4510
Gilles Peskinebef7f142018-07-12 17:22:21 +02004511 while( output_length != 0 )
4512 {
4513 /* Copy what remains of the current block */
4514 uint8_t n = hash_length - hkdf->offset_in_block;
4515 if( n > output_length )
4516 n = (uint8_t) output_length;
4517 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4518 output += n;
4519 output_length -= n;
4520 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004521 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004522 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004523 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004524 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004525 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004526 * object was corrupted or if this function is called directly
4527 * inside the library. */
4528 if( hkdf->block_number == 0xff )
4529 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004530
4531 /* We need a new block */
4532 ++hkdf->block_number;
4533 hkdf->offset_in_block = 0;
4534 status = psa_hmac_setup_internal( &hkdf->hmac,
4535 hkdf->prk, hash_length,
4536 hash_alg );
4537 if( status != PSA_SUCCESS )
4538 return( status );
4539 if( hkdf->block_number != 1 )
4540 {
4541 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4542 hkdf->output_block,
4543 hash_length );
4544 if( status != PSA_SUCCESS )
4545 return( status );
4546 }
4547 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4548 hkdf->info,
4549 hkdf->info_length );
4550 if( status != PSA_SUCCESS )
4551 return( status );
4552 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4553 &hkdf->block_number, 1 );
4554 if( status != PSA_SUCCESS )
4555 return( status );
4556 status = psa_hmac_finish_internal( &hkdf->hmac,
4557 hkdf->output_block,
4558 sizeof( hkdf->output_block ) );
4559 if( status != PSA_SUCCESS )
4560 return( status );
4561 }
4562
4563 return( PSA_SUCCESS );
4564}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004565
Janos Follath7742fee2019-06-17 12:58:10 +01004566static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4567 psa_tls12_prf_key_derivation_t *tls12_prf,
4568 psa_algorithm_t alg )
4569{
4570 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4571 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004572 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4573 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004574
Janos Follath7742fee2019-06-17 12:58:10 +01004575 /* We can't be wanting more output after block 0xff, otherwise
4576 * the capacity check in psa_key_derivation_output_bytes() would have
4577 * prevented this call. It could happen only if the operation
4578 * object was corrupted or if this function is called directly
4579 * inside the library. */
4580 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004581 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004582
4583 /* We need a new block */
4584 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004585 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004586
4587 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4588 *
4589 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4590 *
4591 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4592 * HMAC_hash(secret, A(2) + seed) +
4593 * HMAC_hash(secret, A(3) + seed) + ...
4594 *
4595 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004596 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004597 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004598 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004599 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004600 * is currently extracted as `output_block` and where i is
4601 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004602 */
4603
Janos Follathea29bfb2019-06-19 12:21:20 +01004604 /* Save the hash context before using it, to preserve the hash state with
4605 * only the inner padding in it. We need this, because inner padding depends
4606 * on the key (secret in the RFC's terminology). */
4607 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4608 if( status != PSA_SUCCESS )
4609 goto cleanup;
4610
4611 /* Calculate A(i) where i = tls12_prf->block_number. */
4612 if( tls12_prf->block_number == 1 )
4613 {
4614 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4615 * the variable seed and in this instance means it in the context of the
4616 * P_hash function, where seed = label + seed.) */
4617 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4618 tls12_prf->label, tls12_prf->label_length );
4619 if( status != PSA_SUCCESS )
4620 goto cleanup;
4621 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4622 tls12_prf->seed, tls12_prf->seed_length );
4623 if( status != PSA_SUCCESS )
4624 goto cleanup;
4625 }
4626 else
4627 {
4628 /* A(i) = HMAC_hash(secret, A(i-1)) */
4629 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4630 tls12_prf->Ai, hash_length );
4631 if( status != PSA_SUCCESS )
4632 goto cleanup;
4633 }
4634
4635 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4636 tls12_prf->Ai, hash_length );
4637 if( status != PSA_SUCCESS )
4638 goto cleanup;
4639 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4640 if( status != PSA_SUCCESS )
4641 goto cleanup;
4642
4643 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4644 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4645 tls12_prf->Ai, hash_length );
4646 if( status != PSA_SUCCESS )
4647 goto cleanup;
4648 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4649 tls12_prf->label, tls12_prf->label_length );
4650 if( status != PSA_SUCCESS )
4651 goto cleanup;
4652 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4653 tls12_prf->seed, tls12_prf->seed_length );
4654 if( status != PSA_SUCCESS )
4655 goto cleanup;
4656 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4657 tls12_prf->output_block, hash_length );
4658 if( status != PSA_SUCCESS )
4659 goto cleanup;
4660 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4661 if( status != PSA_SUCCESS )
4662 goto cleanup;
4663
Janos Follath7742fee2019-06-17 12:58:10 +01004664
4665cleanup:
4666
Janos Follathea29bfb2019-06-19 12:21:20 +01004667 cleanup_status = psa_hash_abort( &backup );
4668 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4669 status = cleanup_status;
4670
4671 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004672}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004673
Janos Follath844eb0e2019-06-19 12:10:49 +01004674static psa_status_t psa_key_derivation_tls12_prf_read(
4675 psa_tls12_prf_key_derivation_t *tls12_prf,
4676 psa_algorithm_t alg,
4677 uint8_t *output,
4678 size_t output_length )
4679{
4680 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4681 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4682 psa_status_t status;
4683 uint8_t offset, length;
4684
4685 while( output_length != 0 )
4686 {
4687 /* Check if we have fully processed the current block. */
4688 if( tls12_prf->left_in_block == 0 )
4689 {
4690 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4691 alg );
4692 if( status != PSA_SUCCESS )
4693 return( status );
4694
4695 continue;
4696 }
4697
4698 if( tls12_prf->left_in_block > output_length )
4699 length = (uint8_t) output_length;
4700 else
4701 length = tls12_prf->left_in_block;
4702
4703 offset = hash_length - tls12_prf->left_in_block;
4704 memcpy( output, tls12_prf->output_block + offset, length );
4705 output += length;
4706 output_length -= length;
4707 tls12_prf->left_in_block -= length;
4708 }
4709
4710 return( PSA_SUCCESS );
4711}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004712#endif /* MBEDTLS_MD_C */
4713
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004714psa_status_t psa_key_derivation_output_bytes(
4715 psa_key_derivation_operation_t *operation,
4716 uint8_t *output,
4717 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004718{
4719 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004723 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004725 return PSA_ERROR_BAD_STATE;
4726 }
4727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004733 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004734 goto exit;
4735 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004739 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4741 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004744 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004747
Gilles Peskinebef7f142018-07-12 17:22:21 +02004748#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004750 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004753 output, output_length );
4754 }
Janos Follathadbec812019-06-14 11:05:39 +01004755 else
Janos Follathadbec812019-06-14 11:05:39 +01004756 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004758 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004760 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004761 output_length );
4762 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004763 else
4764#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004765 {
4766 return( PSA_ERROR_BAD_STATE );
4767 }
4768
4769exit:
4770 if( status != PSA_SUCCESS )
4771 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004772 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 * This allows us to differentiate between exhausted operations and
4774 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4775 * operations. */
4776 psa_algorithm_t alg = operation->alg;
4777 psa_key_derivation_abort( operation );
4778 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004779 memset( output, '!', output_length );
4780 }
4781 return( status );
4782}
4783
Gilles Peskine08542d82018-07-19 17:05:42 +02004784#if defined(MBEDTLS_DES_C)
4785static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4786{
4787 if( data_size >= 8 )
4788 mbedtls_des_key_set_parity( data );
4789 if( data_size >= 16 )
4790 mbedtls_des_key_set_parity( data + 8 );
4791 if( data_size >= 24 )
4792 mbedtls_des_key_set_parity( data + 16 );
4793}
4794#endif /* MBEDTLS_DES_C */
4795
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004796static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004797 psa_key_slot_t *slot,
4798 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004800{
4801 uint8_t *data = NULL;
4802 size_t bytes = PSA_BITS_TO_BYTES( bits );
4803 psa_status_t status;
4804
Gilles Peskine8e338702019-07-30 20:06:31 +02004805 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 return( PSA_ERROR_INVALID_ARGUMENT );
4807 if( bits % 8 != 0 )
4808 return( PSA_ERROR_INVALID_ARGUMENT );
4809 data = mbedtls_calloc( 1, bytes );
4810 if( data == NULL )
4811 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4812
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814 if( status != PSA_SUCCESS )
4815 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004816#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004817 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004818 psa_des_set_key_parity( data, bytes );
4819#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004820 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004821
4822exit:
4823 mbedtls_free( data );
4824 return( status );
4825}
4826
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004827psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004829 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004830{
4831 psa_status_t status;
4832 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004833 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004834
4835 /* Reject any attempt to create a zero-length key so that we don't
4836 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4837 if( psa_get_key_bits( attributes ) == 0 )
4838 return( PSA_ERROR_INVALID_ARGUMENT );
4839
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004840 if( ! operation->can_output_key )
4841 return( PSA_ERROR_NOT_PERMITTED );
4842
Gilles Peskinedf179142019-07-15 22:02:14 +02004843 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4844 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004845#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4846 if( driver != NULL )
4847 {
4848 /* Deriving a key in a secure element is not implemented yet. */
4849 status = PSA_ERROR_NOT_SUPPORTED;
4850 }
4851#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004852 if( status == PSA_SUCCESS )
4853 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004854 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004855 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 }
4858 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004859 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 if( status != PSA_SUCCESS )
4861 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004862 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004863 *handle = 0;
4864 }
4865 return( status );
4866}
4867
Gilles Peskineeab56e42018-07-12 17:12:33 +02004868
4869
4870/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004871/* Key derivation */
4872/****************************************************************/
4873
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004876 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877{
Janos Follath5fe19732019-06-20 15:09:30 +01004878 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4879 * initialisers for this union leave some bytes unspecified.) */
4880 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4881
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004884 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4885 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4886 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4890 if( hash_size == 0 )
4891 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004892 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4893 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004894 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 {
4896 return( PSA_ERROR_NOT_SUPPORTED );
4897 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900 }
4901#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904}
4905
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 psa_algorithm_t alg )
4908{
4909 psa_status_t status;
4910
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 return( PSA_ERROR_BAD_STATE );
4913
Gilles Peskine6843c292019-01-18 16:44:49 +01004914 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4915 return( PSA_ERROR_INVALID_ARGUMENT );
4916 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917 {
4918 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004920 }
4921 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4922 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925 else
4926 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927
4928 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931}
4932
4933#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004934static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004935 psa_algorithm_t hash_alg,
4936 psa_key_derivation_step_t step,
4937 const uint8_t *data,
4938 size_t data_length )
4939{
4940 psa_status_t status;
4941 switch( step )
4942 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004943 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004944 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004946 status = psa_hmac_setup_internal( &hkdf->hmac,
4947 data, data_length,
4948 hash_alg );
4949 if( status != PSA_SUCCESS )
4950 return( status );
4951 hkdf->state = HKDF_STATE_STARTED;
4952 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004953 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004954 /* If no salt was provided, use an empty salt. */
4955 if( hkdf->state == HKDF_STATE_INIT )
4956 {
4957 status = psa_hmac_setup_internal( &hkdf->hmac,
4958 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004959 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004960 if( status != PSA_SUCCESS )
4961 return( status );
4962 hkdf->state = HKDF_STATE_STARTED;
4963 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004964 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004966 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4967 data, data_length );
4968 if( status != PSA_SUCCESS )
4969 return( status );
4970 status = psa_hmac_finish_internal( &hkdf->hmac,
4971 hkdf->prk,
4972 sizeof( hkdf->prk ) );
4973 if( status != PSA_SUCCESS )
4974 return( status );
4975 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4976 hkdf->block_number = 0;
4977 hkdf->state = HKDF_STATE_KEYED;
4978 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004979 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980 if( hkdf->state == HKDF_STATE_OUTPUT )
4981 return( PSA_ERROR_BAD_STATE );
4982 if( hkdf->info_set )
4983 return( PSA_ERROR_BAD_STATE );
4984 hkdf->info_length = data_length;
4985 if( data_length != 0 )
4986 {
4987 hkdf->info = mbedtls_calloc( 1, data_length );
4988 if( hkdf->info == NULL )
4989 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4990 memcpy( hkdf->info, data, data_length );
4991 }
4992 hkdf->info_set = 1;
4993 return( PSA_SUCCESS );
4994 default:
4995 return( PSA_ERROR_INVALID_ARGUMENT );
4996 }
4997}
Janos Follathb03233e2019-06-11 15:30:30 +01004998
Janos Follathf08e2652019-06-13 09:05:41 +01004999static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5000 const uint8_t *data,
5001 size_t data_length )
5002{
5003 if( prf->state != TLS12_PRF_STATE_INIT )
5004 return( PSA_ERROR_BAD_STATE );
5005
Janos Follathd6dce9f2019-07-04 09:11:38 +01005006 if( data_length != 0 )
5007 {
5008 prf->seed = mbedtls_calloc( 1, data_length );
5009 if( prf->seed == NULL )
5010 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005011
Janos Follathd6dce9f2019-07-04 09:11:38 +01005012 memcpy( prf->seed, data, data_length );
5013 prf->seed_length = data_length;
5014 }
Janos Follathf08e2652019-06-13 09:05:41 +01005015
5016 prf->state = TLS12_PRF_STATE_SEED_SET;
5017
5018 return( PSA_SUCCESS );
5019}
5020
Janos Follath81550542019-06-13 14:26:34 +01005021static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5022 psa_algorithm_t hash_alg,
5023 const uint8_t *data,
5024 size_t data_length )
5025{
5026 psa_status_t status;
5027 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5028 return( PSA_ERROR_BAD_STATE );
5029
5030 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5031 if( status != PSA_SUCCESS )
5032 return( status );
5033
5034 prf->state = TLS12_PRF_STATE_KEY_SET;
5035
5036 return( PSA_SUCCESS );
5037}
5038
Janos Follath6660f0e2019-06-17 08:44:03 +01005039static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5040 psa_tls12_prf_key_derivation_t *prf,
5041 psa_algorithm_t hash_alg,
5042 const uint8_t *data,
5043 size_t data_length )
5044{
5045 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005046 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5047 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005048
5049 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5050 return( PSA_ERROR_INVALID_ARGUMENT );
5051
5052 /* Quoting RFC 4279, Section 2:
5053 *
5054 * The premaster secret is formed as follows: if the PSK is N octets
5055 * long, concatenate a uint16 with the value N, N zero octets, a second
5056 * uint16 with the value N, and the PSK itself.
5057 */
5058
Janos Follath40e13932019-06-26 13:22:29 +01005059 *cur++ = ( data_length >> 8 ) & 0xff;
5060 *cur++ = ( data_length >> 0 ) & 0xff;
5061 memset( cur, 0, data_length );
5062 cur += data_length;
5063 *cur++ = pms[0];
5064 *cur++ = pms[1];
5065 memcpy( cur, data, data_length );
5066 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005067
Janos Follath40e13932019-06-26 13:22:29 +01005068 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005069
5070 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5071 return( status );
5072}
5073
Janos Follath63028dd2019-06-13 09:15:47 +01005074static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5075 const uint8_t *data,
5076 size_t data_length )
5077{
5078 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5079 return( PSA_ERROR_BAD_STATE );
5080
Janos Follathd6dce9f2019-07-04 09:11:38 +01005081 if( data_length != 0 )
5082 {
5083 prf->label = mbedtls_calloc( 1, data_length );
5084 if( prf->label == NULL )
5085 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005086
Janos Follathd6dce9f2019-07-04 09:11:38 +01005087 memcpy( prf->label, data, data_length );
5088 prf->label_length = data_length;
5089 }
Janos Follath63028dd2019-06-13 09:15:47 +01005090
5091 prf->state = TLS12_PRF_STATE_LABEL_SET;
5092
5093 return( PSA_SUCCESS );
5094}
5095
Janos Follathb03233e2019-06-11 15:30:30 +01005096static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5097 psa_algorithm_t hash_alg,
5098 psa_key_derivation_step_t step,
5099 const uint8_t *data,
5100 size_t data_length )
5101{
Janos Follathb03233e2019-06-11 15:30:30 +01005102 switch( step )
5103 {
Janos Follathf08e2652019-06-13 09:05:41 +01005104 case PSA_KEY_DERIVATION_INPUT_SEED:
5105 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005106 case PSA_KEY_DERIVATION_INPUT_SECRET:
5107 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005108 case PSA_KEY_DERIVATION_INPUT_LABEL:
5109 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005110 default:
5111 return( PSA_ERROR_INVALID_ARGUMENT );
5112 }
5113}
Janos Follath6660f0e2019-06-17 08:44:03 +01005114
5115static psa_status_t psa_tls12_prf_psk_to_ms_input(
5116 psa_tls12_prf_key_derivation_t *prf,
5117 psa_algorithm_t hash_alg,
5118 psa_key_derivation_step_t step,
5119 const uint8_t *data,
5120 size_t data_length )
5121{
5122 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005123 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005124 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5125 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005126 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005127
5128 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5129}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130#endif /* MBEDTLS_MD_C */
5131
Gilles Peskineb8965192019-09-24 16:21:10 +02005132/** Check whether the given key type is acceptable for the given
5133 * input step of a key derivation.
5134 *
5135 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5136 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5137 * Both secret and non-secret inputs can alternatively have the type
5138 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5139 * that the input was passed as a buffer rather than via a key object.
5140 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005141static int psa_key_derivation_check_input_type(
5142 psa_key_derivation_step_t step,
5143 psa_key_type_t key_type )
5144{
5145 switch( step )
5146 {
5147 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005148 if( key_type == PSA_KEY_TYPE_DERIVE )
5149 return( PSA_SUCCESS );
5150 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005151 return( PSA_SUCCESS );
5152 break;
5153 case PSA_KEY_DERIVATION_INPUT_LABEL:
5154 case PSA_KEY_DERIVATION_INPUT_SALT:
5155 case PSA_KEY_DERIVATION_INPUT_INFO:
5156 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005157 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5158 return( PSA_SUCCESS );
5159 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005160 return( PSA_SUCCESS );
5161 break;
5162 }
5163 return( PSA_ERROR_INVALID_ARGUMENT );
5164}
5165
Janos Follathb80a94e2019-06-12 15:54:46 +01005166static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005167 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005168 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005169 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005170 const uint8_t *data,
5171 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005172{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005173 psa_status_t status;
5174 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5175
5176 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005177 if( status != PSA_SUCCESS )
5178 goto exit;
5179
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005181 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005183 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005184 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 step, data, data_length );
5186 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005187 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188 {
Janos Follathb03233e2019-06-11 15:30:30 +01005189 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5190 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5191 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005192 }
5193 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5194 {
5195 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5196 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5197 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198 }
5199 else
5200#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203 return( PSA_ERROR_BAD_STATE );
5204 }
5205
Gilles Peskine224b0d62019-09-23 18:13:17 +02005206exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 return( status );
5210}
5211
Janos Follath51f4a0f2019-06-14 11:35:55 +01005212psa_status_t psa_key_derivation_input_bytes(
5213 psa_key_derivation_operation_t *operation,
5214 psa_key_derivation_step_t step,
5215 const uint8_t *data,
5216 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005217{
Gilles Peskineb8965192019-09-24 16:21:10 +02005218 return( psa_key_derivation_input_internal( operation, step,
5219 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005220 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005221}
5222
Janos Follath51f4a0f2019-06-14 11:35:55 +01005223psa_status_t psa_key_derivation_input_key(
5224 psa_key_derivation_operation_t *operation,
5225 psa_key_derivation_step_t step,
5226 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227{
5228 psa_key_slot_t *slot;
5229 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005230
Gilles Peskine28f8f302019-07-24 13:30:31 +02005231 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005232 PSA_KEY_USAGE_DERIVE,
5233 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005235 {
5236 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005238 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005239
5240 /* Passing a key object as a SECRET input unlocks the permission
5241 * to output to a key object. */
5242 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5243 operation->can_output_key = 1;
5244
Janos Follathb80a94e2019-06-12 15:54:46 +01005245 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005246 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005247 slot->data.raw.data,
5248 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005249}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005250
5251
5252
5253/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005254/* Key agreement */
5255/****************************************************************/
5256
Gilles Peskinea05219c2018-11-16 16:02:56 +01005257#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005258static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5259 size_t peer_key_length,
5260 const mbedtls_ecp_keypair *our_key,
5261 uint8_t *shared_secret,
5262 size_t shared_secret_size,
5263 size_t *shared_secret_length )
5264{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005265 mbedtls_ecp_keypair *their_key = NULL;
5266 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005267 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005268 size_t bits = 0;
5269 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005270 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005271
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005272 status = psa_import_ec_public_key( curve,
5273 peer_key, peer_key_length,
5274 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005275 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005276 goto exit;
5277
Jaeden Amero97271b32019-01-10 19:38:51 +00005278 status = mbedtls_to_psa_error(
5279 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5280 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005281 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005282 status = mbedtls_to_psa_error(
5283 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5284 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285 goto exit;
5286
Jaeden Amero97271b32019-01-10 19:38:51 +00005287 status = mbedtls_to_psa_error(
5288 mbedtls_ecdh_calc_secret( &ecdh,
5289 shared_secret_length,
5290 shared_secret, shared_secret_size,
5291 mbedtls_ctr_drbg_random,
5292 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005293 if( status != PSA_SUCCESS )
5294 goto exit;
5295 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5296 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005297
5298exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005299 if( status != PSA_SUCCESS )
5300 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005301 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005302 mbedtls_ecp_keypair_free( their_key );
5303 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005304 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005306#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005307
Gilles Peskine01d718c2018-09-18 12:01:02 +02005308#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5309
Gilles Peskine0216fe12019-04-11 21:23:21 +02005310static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5311 psa_key_slot_t *private_key,
5312 const uint8_t *peer_key,
5313 size_t peer_key_length,
5314 uint8_t *shared_secret,
5315 size_t shared_secret_size,
5316 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005317{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005318 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005319 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005321 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005322 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005323 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005324 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5325 private_key->data.ecp,
5326 shared_secret, shared_secret_size,
5327 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005328#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005329 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005330 (void) private_key;
5331 (void) peer_key;
5332 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005333 (void) shared_secret;
5334 (void) shared_secret_size;
5335 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005336 return( PSA_ERROR_NOT_SUPPORTED );
5337 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005338}
5339
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005340/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005341 * to potentially free embedded data structures and wipe confidential data.
5342 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005344 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005345 psa_key_slot_t *private_key,
5346 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005347 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005348{
5349 psa_status_t status;
5350 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5351 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005353
5354 /* Step 1: run the secret agreement algorithm to generate the shared
5355 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005356 status = psa_key_agreement_raw_internal( ka_alg,
5357 private_key,
5358 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005359 shared_secret,
5360 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005361 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005362 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005363 goto exit;
5364
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005365 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005366 * the shared secret. A shared secret is permitted wherever a key
5367 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005368 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005369 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005370 shared_secret,
5371 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005372
Gilles Peskine12313cd2018-06-20 00:20:32 +02005373exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005374 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005375 return( status );
5376}
5377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005379 psa_key_derivation_step_t step,
5380 psa_key_handle_t private_key,
5381 const uint8_t *peer_key,
5382 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005383{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005384 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005385 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005387 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005388 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005389 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005390 if( status != PSA_SUCCESS )
5391 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005393 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005394 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005395 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005396 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005397 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005398}
5399
Gilles Peskinebe697d82019-05-16 18:00:41 +02005400psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5401 psa_key_handle_t private_key,
5402 const uint8_t *peer_key,
5403 size_t peer_key_length,
5404 uint8_t *output,
5405 size_t output_size,
5406 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005407{
5408 psa_key_slot_t *slot;
5409 psa_status_t status;
5410
5411 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5412 {
5413 status = PSA_ERROR_INVALID_ARGUMENT;
5414 goto exit;
5415 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005416 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005417 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005418 if( status != PSA_SUCCESS )
5419 goto exit;
5420
5421 status = psa_key_agreement_raw_internal( alg, slot,
5422 peer_key, peer_key_length,
5423 output, output_size,
5424 output_length );
5425
5426exit:
5427 if( status != PSA_SUCCESS )
5428 {
5429 /* If an error happens and is not handled properly, the output
5430 * may be used as a key to protect sensitive data. Arrange for such
5431 * a key to be random, which is likely to result in decryption or
5432 * verification errors. This is better than filling the buffer with
5433 * some constant data such as zeros, which would result in the data
5434 * being protected with a reproducible, easily knowable key.
5435 */
5436 psa_generate_random( output, output_size );
5437 *output_length = output_size;
5438 }
5439 return( status );
5440}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005441
5442
5443/****************************************************************/
5444/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005445/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005446
Gilles Peskine4c317f42018-07-12 01:24:09 +02005447psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005448 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005449{
Janos Follath24eed8d2019-11-22 13:21:35 +00005450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005451 GUARD_MODULE_INITIALIZED;
5452
Gilles Peskinef181eca2019-08-07 13:49:00 +02005453 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5454 {
5455 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5456 output,
5457 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5458 if( ret != 0 )
5459 return( mbedtls_to_psa_error( ret ) );
5460 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5461 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5462 }
5463
Gilles Peskinee59236f2018-01-27 23:32:46 +01005464 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5465 return( mbedtls_to_psa_error( ret ) );
5466}
5467
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005468#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5469#include "mbedtls/entropy_poll.h"
5470
Gilles Peskine7228da22019-07-15 11:06:15 +02005471psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005472 size_t seed_size )
5473{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005474 if( global_data.initialized )
5475 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005476
5477 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5478 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5479 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5480 return( PSA_ERROR_INVALID_ARGUMENT );
5481
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005482 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005483}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005484#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005485
Gilles Peskinee56e8782019-04-26 17:34:02 +02005486#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5487static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5488 size_t domain_parameters_size,
5489 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005490{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005491 size_t i;
5492 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005493
Gilles Peskinee56e8782019-04-26 17:34:02 +02005494 if( domain_parameters_size == 0 )
5495 {
5496 *exponent = 65537;
5497 return( PSA_SUCCESS );
5498 }
5499
5500 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5501 * support values that fit in a 32-bit integer, which is larger than
5502 * int on just about every platform anyway. */
5503 if( domain_parameters_size > sizeof( acc ) )
5504 return( PSA_ERROR_NOT_SUPPORTED );
5505 for( i = 0; i < domain_parameters_size; i++ )
5506 acc = ( acc << 8 ) | domain_parameters[i];
5507 if( acc > INT_MAX )
5508 return( PSA_ERROR_NOT_SUPPORTED );
5509 *exponent = acc;
5510 return( PSA_SUCCESS );
5511}
5512#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5513
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005514static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005515 psa_key_slot_t *slot, size_t bits,
5516 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517{
Gilles Peskine8e338702019-07-30 20:06:31 +02005518 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519
Gilles Peskinee56e8782019-04-26 17:34:02 +02005520 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005521 return( PSA_ERROR_INVALID_ARGUMENT );
5522
Gilles Peskinee59236f2018-01-27 23:32:46 +01005523 if( key_type_is_raw_bytes( type ) )
5524 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005525 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005526 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5527 if( status != PSA_SUCCESS )
5528 return( status );
5529 status = psa_generate_random( slot->data.raw.data,
5530 slot->data.raw.bytes );
5531 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005532 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533#if defined(MBEDTLS_DES_C)
5534 if( type == PSA_KEY_TYPE_DES )
5535 psa_des_set_key_parity( slot->data.raw.data,
5536 slot->data.raw.bytes );
5537#endif /* MBEDTLS_DES_C */
5538 }
5539 else
5540
5541#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005542 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005543 {
5544 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005546 int exponent;
5547 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005548 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5549 return( PSA_ERROR_NOT_SUPPORTED );
5550 /* Accept only byte-aligned keys, for the same reasons as
5551 * in psa_import_rsa_key(). */
5552 if( bits % 8 != 0 )
5553 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005554 status = psa_read_rsa_exponent( domain_parameters,
5555 domain_parameters_size,
5556 &exponent );
5557 if( status != PSA_SUCCESS )
5558 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005559 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5560 if( rsa == NULL )
5561 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5562 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5563 ret = mbedtls_rsa_gen_key( rsa,
5564 mbedtls_ctr_drbg_random,
5565 &global_data.ctr_drbg,
5566 (unsigned int) bits,
5567 exponent );
5568 if( ret != 0 )
5569 {
5570 mbedtls_rsa_free( rsa );
5571 mbedtls_free( rsa );
5572 return( mbedtls_to_psa_error( ret ) );
5573 }
5574 slot->data.rsa = rsa;
5575 }
5576 else
5577#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5578
5579#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005580 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005581 {
5582 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005583 mbedtls_ecp_group_id grp_id =
5584 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005585 const mbedtls_ecp_curve_info *curve_info =
5586 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5587 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005589 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005590 return( PSA_ERROR_NOT_SUPPORTED );
5591 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5592 return( PSA_ERROR_NOT_SUPPORTED );
5593 if( curve_info->bit_size != bits )
5594 return( PSA_ERROR_INVALID_ARGUMENT );
5595 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5596 if( ecp == NULL )
5597 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5598 mbedtls_ecp_keypair_init( ecp );
5599 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5600 mbedtls_ctr_drbg_random,
5601 &global_data.ctr_drbg );
5602 if( ret != 0 )
5603 {
5604 mbedtls_ecp_keypair_free( ecp );
5605 mbedtls_free( ecp );
5606 return( mbedtls_to_psa_error( ret ) );
5607 }
5608 slot->data.ecp = ecp;
5609 }
5610 else
5611#endif /* MBEDTLS_ECP_C */
5612
5613 return( PSA_ERROR_NOT_SUPPORTED );
5614
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005615 return( PSA_SUCCESS );
5616}
Darryl Green0c6575a2018-11-07 16:05:30 +00005617
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005618psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005619 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005620{
5621 psa_status_t status;
5622 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005623 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005624
Gilles Peskine0f84d622019-09-12 19:03:13 +02005625 /* Reject any attempt to create a zero-length key so that we don't
5626 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5627 if( psa_get_key_bits( attributes ) == 0 )
5628 return( PSA_ERROR_INVALID_ARGUMENT );
5629
Gilles Peskinedf179142019-07-15 22:02:14 +02005630 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5631 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005632 if( status != PSA_SUCCESS )
5633 goto exit;
5634
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005635#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5636 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005637 {
Gilles Peskine11792082019-08-06 18:36:36 +02005638 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5639 size_t pubkey_length = 0; /* We don't support this feature yet */
5640 if( drv->key_management == NULL ||
5641 drv->key_management->p_generate == NULL )
5642 {
5643 status = PSA_ERROR_NOT_SUPPORTED;
5644 goto exit;
5645 }
5646 status = drv->key_management->p_generate(
5647 psa_get_se_driver_context( driver ),
5648 slot->data.se.slot_number, attributes,
5649 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005650 }
Gilles Peskine11792082019-08-06 18:36:36 +02005651 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005652#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005653 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005654 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005655 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005656 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005657 }
Gilles Peskine11792082019-08-06 18:36:36 +02005658
5659exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005661 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 if( status != PSA_SUCCESS )
5663 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005664 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 *handle = 0;
5666 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005667 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005668}
5669
5670
Gilles Peskinee59236f2018-01-27 23:32:46 +01005671
5672/****************************************************************/
5673/* Module setup */
5674/****************************************************************/
5675
Gilles Peskine5e769522018-11-20 21:59:56 +01005676psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5677 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5678 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5679{
5680 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5681 return( PSA_ERROR_BAD_STATE );
5682 global_data.entropy_init = entropy_init;
5683 global_data.entropy_free = entropy_free;
5684 return( PSA_SUCCESS );
5685}
5686
Gilles Peskinee59236f2018-01-27 23:32:46 +01005687void mbedtls_psa_crypto_free( void )
5688{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005689 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005690 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5691 {
5692 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005693 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005694 }
5695 /* Wipe all remaining data, including configuration.
5696 * In particular, this sets all state indicator to the value
5697 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005698 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005699#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005700 /* Unregister all secure element drivers, so that we restart from
5701 * a pristine state. */
5702 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005703#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005704}
5705
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005706#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5707/** Recover a transaction that was interrupted by a power failure.
5708 *
5709 * This function is called during initialization, before psa_crypto_init()
5710 * returns. If this function returns a failure status, the initialization
5711 * fails.
5712 */
5713static psa_status_t psa_crypto_recover_transaction(
5714 const psa_crypto_transaction_t *transaction )
5715{
5716 switch( transaction->unknown.type )
5717 {
5718 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5719 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005720 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005721 * is implemented.
5722 * https://github.com/ARMmbed/mbed-crypto/issues/218
5723 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005724 default:
5725 /* We found an unsupported transaction in the storage.
5726 * We don't know what state the storage is in. Give up. */
5727 return( PSA_ERROR_STORAGE_FAILURE );
5728 }
5729}
5730#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5731
Gilles Peskinee59236f2018-01-27 23:32:46 +01005732psa_status_t psa_crypto_init( void )
5733{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005734 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005735 const unsigned char drbg_seed[] = "PSA";
5736
Gilles Peskinec6b69072018-11-20 21:42:52 +01005737 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005738 if( global_data.initialized != 0 )
5739 return( PSA_SUCCESS );
5740
Gilles Peskine5e769522018-11-20 21:59:56 +01005741 /* Set default configuration if
5742 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5743 if( global_data.entropy_init == NULL )
5744 global_data.entropy_init = mbedtls_entropy_init;
5745 if( global_data.entropy_free == NULL )
5746 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005747
Gilles Peskinec6b69072018-11-20 21:42:52 +01005748 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005749 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005750#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5751 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5752 /* The PSA entropy injection feature depends on using NV seed as an entropy
5753 * source. Add NV seed as an entropy source for PSA entropy injection. */
5754 mbedtls_entropy_add_source( &global_data.entropy,
5755 mbedtls_nv_seed_poll, NULL,
5756 MBEDTLS_ENTROPY_BLOCK_SIZE,
5757 MBEDTLS_ENTROPY_SOURCE_STRONG );
5758#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005759 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005760 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005761 status = mbedtls_to_psa_error(
5762 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5763 mbedtls_entropy_func,
5764 &global_data.entropy,
5765 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5766 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005767 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005768 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005769
Gilles Peskine66fb1262018-12-10 16:29:04 +01005770 status = psa_initialize_key_slots( );
5771 if( status != PSA_SUCCESS )
5772 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005773
Gilles Peskined9348f22019-10-01 15:22:29 +02005774#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5775 status = psa_init_all_se_drivers( );
5776 if( status != PSA_SUCCESS )
5777 goto exit;
5778#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5779
Gilles Peskine4b734222019-07-24 15:56:31 +02005780#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005781 status = psa_crypto_load_transaction( );
5782 if( status == PSA_SUCCESS )
5783 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005784 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5785 if( status != PSA_SUCCESS )
5786 goto exit;
5787 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005788 }
5789 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5790 {
5791 /* There's no transaction to complete. It's all good. */
5792 status = PSA_SUCCESS;
5793 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005794#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005795
Gilles Peskinec6b69072018-11-20 21:42:52 +01005796 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797 global_data.initialized = 1;
5798
5799exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005800 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005801 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005802 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005803}
5804
5805#endif /* MBEDTLS_PSA_CRYPTO_C */