blob: 72ecdde214e56de9fe3984f0712669111112a52b [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 +0100378psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
379 size_t *bits )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200380{
381 switch( grpid )
382 {
383 case MBEDTLS_ECP_DP_SECP192R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100384 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100385 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100388 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100391 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100394 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100397 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100400 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100403 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100406 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100409 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100412 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100415 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100418 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100421 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200422 default:
423 return( 0 );
424 }
425}
426
Gilles Peskine5055b232019-12-12 17:49:31 +0100427mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
428 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429{
430 switch( curve )
431 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 case PSA_ECC_CURVE_SECP_R1:
433 switch( byte_length )
434 {
435 case PSA_BITS_TO_BYTES( 192 ):
436 return( MBEDTLS_ECP_DP_SECP192R1 );
437 case PSA_BITS_TO_BYTES( 224 ):
438 return( MBEDTLS_ECP_DP_SECP224R1 );
439 case PSA_BITS_TO_BYTES( 256 ):
440 return( MBEDTLS_ECP_DP_SECP256R1 );
441 case PSA_BITS_TO_BYTES( 384 ):
442 return( MBEDTLS_ECP_DP_SECP384R1 );
443 case PSA_BITS_TO_BYTES( 521 ):
444 return( MBEDTLS_ECP_DP_SECP521R1 );
445 default:
446 return( MBEDTLS_ECP_DP_NONE );
447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
450 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
451 switch( byte_length )
452 {
453 case PSA_BITS_TO_BYTES( 256 ):
454 return( MBEDTLS_ECP_DP_BP256R1 );
455 case PSA_BITS_TO_BYTES( 384 ):
456 return( MBEDTLS_ECP_DP_BP384R1 );
457 case PSA_BITS_TO_BYTES( 512 ):
458 return( MBEDTLS_ECP_DP_BP512R1 );
459 default:
460 return( MBEDTLS_ECP_DP_NONE );
461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
464 case PSA_ECC_CURVE_MONTGOMERY:
465 switch( byte_length )
466 {
467 case PSA_BITS_TO_BYTES( 255 ):
468 return( MBEDTLS_ECP_DP_CURVE25519 );
469 case PSA_BITS_TO_BYTES( 448 ):
470 return( MBEDTLS_ECP_DP_CURVE448 );
471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
476 case PSA_ECC_CURVE_SECP_K1:
477 switch( byte_length )
478 {
479 case PSA_BITS_TO_BYTES( 192 ):
480 return( MBEDTLS_ECP_DP_SECP192K1 );
481 case PSA_BITS_TO_BYTES( 224 ):
482 return( MBEDTLS_ECP_DP_SECP224K1 );
483 case PSA_BITS_TO_BYTES( 256 ):
484 return( MBEDTLS_ECP_DP_SECP256K1 );
485 default:
486 return( MBEDTLS_ECP_DP_NONE );
487 }
488 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100489
Gilles Peskine12313cd2018-06-20 00:20:32 +0200490 default:
491 return( MBEDTLS_ECP_DP_NONE );
492 }
493}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100494#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200495
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
497 size_t bits,
498 struct raw_data *raw )
499{
500 /* Check that the bit size is acceptable for the key type */
501 switch( type )
502 {
503 case PSA_KEY_TYPE_RAW_DATA:
504#if defined(MBEDTLS_MD_C)
505 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200506#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200507 case PSA_KEY_TYPE_DERIVE:
508 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509#if defined(MBEDTLS_AES_C)
510 case PSA_KEY_TYPE_AES:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_CAMELLIA_C)
516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_DES_C)
522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_ARC4_C)
528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200533#if defined(MBEDTLS_CHACHA20_C)
534 case PSA_KEY_TYPE_CHACHA20:
535 if( bits != 256 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
545 /* Allocate memory for the key */
546 raw->bytes = PSA_BITS_TO_BYTES( bits );
547 raw->data = mbedtls_calloc( 1, raw->bytes );
548 if( raw->data == NULL )
549 {
550 raw->bytes = 0;
551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
552 }
553 return( PSA_SUCCESS );
554}
555
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100557/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
558 * that are not a multiple of 8) well. For example, there is only
559 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
560 * way to return the exact bit size of a key.
561 * To keep things simple, reject non-byte-aligned key sizes. */
562static psa_status_t psa_check_rsa_key_byte_aligned(
563 const mbedtls_rsa_context *rsa )
564{
565 mbedtls_mpi n;
566 psa_status_t status;
567 mbedtls_mpi_init( &n );
568 status = mbedtls_to_psa_error(
569 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
570 if( status == PSA_SUCCESS )
571 {
572 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
573 status = PSA_ERROR_NOT_SUPPORTED;
574 }
575 mbedtls_mpi_free( &n );
576 return( status );
577}
578
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000579static psa_status_t psa_import_rsa_key( psa_key_type_t type,
580 const uint8_t *data,
581 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582 mbedtls_rsa_context **p_rsa )
583{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000584 psa_status_t status;
585 mbedtls_pk_context pk;
586 mbedtls_rsa_context *rsa;
587 size_t bits;
588
589 mbedtls_pk_init( &pk );
590
591 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200592 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000593 status = mbedtls_to_psa_error(
594 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200595 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000596 status = mbedtls_to_psa_error(
597 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
598 if( status != PSA_SUCCESS )
599 goto exit;
600
601 /* We have something that the pkparse module recognizes. If it is a
602 * valid RSA key, store it. */
603 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000605 status = PSA_ERROR_INVALID_ARGUMENT;
606 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000608
609 rsa = mbedtls_pk_rsa( pk );
610 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
611 * supports non-byte-aligned key sizes, but not well. For example,
612 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
613 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
614 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
615 {
616 status = PSA_ERROR_NOT_SUPPORTED;
617 goto exit;
618 }
619 status = psa_check_rsa_key_byte_aligned( rsa );
620
621exit:
622 /* Free the content of the pk object only on error. */
623 if( status != PSA_SUCCESS )
624 {
625 mbedtls_pk_free( &pk );
626 return( status );
627 }
628
629 /* On success, store the content of the object in the RSA context. */
630 *p_rsa = rsa;
631
632 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
634#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
635
Jaeden Ameroccdce902019-01-10 11:42:27 +0000636#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100637static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100638 size_t data_length,
639 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100640 mbedtls_ecp_keypair **p_ecp )
641{
642 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
643 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
644 if( *p_ecp == NULL )
645 return( PSA_ERROR_INSUFFICIENT_MEMORY );
646 mbedtls_ecp_keypair_init( *p_ecp );
647
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100648 if( is_public )
649 {
650 /* A public key is represented as:
651 * - The byte 0x04;
652 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
653 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
654 * So its data length is 2m+1 where n is the key size in bits.
655 */
656 if( ( data_length & 1 ) == 0 )
657 return( PSA_ERROR_INVALID_ARGUMENT );
658 data_length = data_length / 2;
659 }
660
Gilles Peskine4cd32772019-12-02 20:49:42 +0100661 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100662 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
663 if( grp_id == MBEDTLS_ECP_DP_NONE )
664 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100665 return( mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
667}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000668
669/* Import a public key given as the uncompressed representation defined by SEC1
670 * 2.3.3 as the content of an ECPoint. */
671static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
672 const uint8_t *data,
673 size_t data_length,
674 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200675{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000677 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000678
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100679 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000680 if( status != PSA_SUCCESS )
681 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100682
Jaeden Ameroccdce902019-01-10 11:42:27 +0000683 /* Load the public value. */
684 status = mbedtls_to_psa_error(
685 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
686 data, data_length ) );
687 if( status != PSA_SUCCESS )
688 goto exit;
689
690 /* Check that the point is on the curve. */
691 status = mbedtls_to_psa_error(
692 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
693 if( status != PSA_SUCCESS )
694 goto exit;
695
696 *p_ecp = ecp;
697 return( PSA_SUCCESS );
698
699exit:
700 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000702 mbedtls_ecp_keypair_free( ecp );
703 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200704 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000705 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200706}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707
Gilles Peskinef76aa772018-10-29 19:24:33 +0100708/* Import a private key given as a byte string which is the private value
709 * in big-endian order. */
710static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
711 const uint8_t *data,
712 size_t data_length,
713 mbedtls_ecp_keypair **p_ecp )
714{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100716 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100718 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100719 if( status != PSA_SUCCESS )
720 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100721
Gilles Peskinef76aa772018-10-29 19:24:33 +0100722 /* Load the secret value. */
723 status = mbedtls_to_psa_error(
724 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
725 if( status != PSA_SUCCESS )
726 goto exit;
727 /* Validate the private key. */
728 status = mbedtls_to_psa_error(
729 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
730 if( status != PSA_SUCCESS )
731 goto exit;
732 /* Calculate the public key from the private key. */
733 status = mbedtls_to_psa_error(
734 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
735 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
736 if( status != PSA_SUCCESS )
737 goto exit;
738
739 *p_ecp = ecp;
740 return( PSA_SUCCESS );
741
742exit:
743 if( ecp != NULL )
744 {
745 mbedtls_ecp_keypair_free( ecp );
746 mbedtls_free( ecp );
747 }
748 return( status );
749}
750#endif /* defined(MBEDTLS_ECP_C) */
751
Gilles Peskineb46bef22019-07-30 21:32:04 +0200752
753/** Return the size of the key in the given slot, in bits.
754 *
755 * \param[in] slot A key slot.
756 *
757 * \return The key size in bits, read from the metadata in the slot.
758 */
759static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
760{
761 return( slot->attr.bits );
762}
763
764/** Calculate the size of the key in the given slot, in bits.
765 *
766 * \param[in] slot A key slot containing a transparent key.
767 *
768 * \return The key size in bits, calculated from the key data.
769 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200770static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200771{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200772 size_t bits = 0; /* return 0 on an empty slot */
773
Gilles Peskineb46bef22019-07-30 21:32:04 +0200774 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200775 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200776#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200777 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
778 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200779#endif /* defined(MBEDTLS_RSA_C) */
780#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200781 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
782 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200783#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200784
785 /* We know that the size fits in psa_key_bits_t thanks to checks
786 * when the key was created. */
787 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200788}
789
Gilles Peskine8e338702019-07-30 20:06:31 +0200790/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100791 * previously. This function assumes that the slot does not contain
792 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100793psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
794 const uint8_t *data,
795 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200797 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798
Gilles Peskine8e338702019-07-30 20:06:31 +0200799 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200801 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200802 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 if( data_length > SIZE_MAX / 8 )
804 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200805 /* Enforce a size limit, and in particular ensure that the bit
806 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200807 if( bit_size > PSA_MAX_KEY_BITS )
808 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200809 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200810 &slot->data.raw );
811 if( status != PSA_SUCCESS )
812 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200813 if( data_length != 0 )
814 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 }
816 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100817#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200818 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100819 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200820 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100821 data, data_length,
822 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100823 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200824 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100825 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000826 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200827 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000828 data, data_length,
829 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100830 }
831 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100832#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000833#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200834 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200836 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000837 data, data_length,
838 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839 }
840 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000841#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842 {
843 return( PSA_ERROR_NOT_SUPPORTED );
844 }
Darryl Green940d72c2018-07-13 13:18:51 +0100845
Gilles Peskineb46bef22019-07-30 21:32:04 +0200846 if( status == PSA_SUCCESS )
847 {
848 /* Write the actual key size to the slot.
849 * psa_start_key_creation() wrote the size declared by the
850 * caller, which may be 0 (meaning unspecified) or wrong. */
851 slot->attr.bits = psa_calculate_key_bits( slot );
852 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100853 return( status );
854}
855
Gilles Peskinef603c712019-01-19 13:40:11 +0100856/** Calculate the intersection of two algorithm usage policies.
857 *
858 * Return 0 (which allows no operation) on incompatibility.
859 */
860static psa_algorithm_t psa_key_policy_algorithm_intersection(
861 psa_algorithm_t alg1,
862 psa_algorithm_t alg2 )
863{
Gilles Peskine549ea862019-05-22 11:45:59 +0200864 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100865 if( alg1 == alg2 )
866 return( alg1 );
867 /* If the policies are from the same hash-and-sign family, check
868 * if one is a wildcard. If so the other has the specific algorithm. */
869 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
870 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
871 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
872 {
873 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
874 return( alg2 );
875 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
876 return( alg1 );
877 }
878 /* If the policies are incompatible, allow nothing. */
879 return( 0 );
880}
881
Gilles Peskined6f371b2019-05-10 19:33:38 +0200882static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
883 psa_algorithm_t requested_alg )
884{
Gilles Peskine549ea862019-05-22 11:45:59 +0200885 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 if( requested_alg == policy_alg )
887 return( 1 );
888 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200889 * and requested_alg is the same hash-and-sign family with any hash,
890 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
892 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
893 {
894 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
895 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
896 }
897 /* If it isn't permitted, it's forbidden. */
898 return( 0 );
899}
900
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100901/** Test whether a policy permits an algorithm.
902 *
903 * The caller must test usage flags separately.
904 */
905static int psa_key_policy_permits( const psa_key_policy_t *policy,
906 psa_algorithm_t alg )
907{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200908 return( psa_key_algorithm_permits( policy->alg, alg ) ||
909 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100910}
911
Gilles Peskinef603c712019-01-19 13:40:11 +0100912/** Restrict a key policy based on a constraint.
913 *
914 * \param[in,out] policy The policy to restrict.
915 * \param[in] constraint The policy constraint to apply.
916 *
917 * \retval #PSA_SUCCESS
918 * \c *policy contains the intersection of the original value of
919 * \c *policy and \c *constraint.
920 * \retval #PSA_ERROR_INVALID_ARGUMENT
921 * \c *policy and \c *constraint are incompatible.
922 * \c *policy is unchanged.
923 */
924static psa_status_t psa_restrict_key_policy(
925 psa_key_policy_t *policy,
926 const psa_key_policy_t *constraint )
927{
928 psa_algorithm_t intersection_alg =
929 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200930 psa_algorithm_t intersection_alg2 =
931 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100932 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
933 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200934 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
935 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 policy->usage &= constraint->usage;
937 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100939 return( PSA_SUCCESS );
940}
941
Darryl Green06fd18d2018-07-16 11:21:11 +0100942/** Retrieve a slot which must contain a key. The key must have allow all the
943 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
944 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100946 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100947 psa_key_usage_t usage,
948 psa_algorithm_t alg )
949{
950 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100952
953 *p_slot = NULL;
954
Gilles Peskinec5487a82018-12-03 18:08:14 +0100955 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100956 if( status != PSA_SUCCESS )
957 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100958
959 /* Enforce that usage policy for the key slot contains all the flags
960 * required by the usage parameter. There is one exception: public
961 * keys can always be exported, so we treat public key objects as
962 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100964 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200965 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100966 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100967
968 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200969 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100970 return( PSA_ERROR_NOT_PERMITTED );
971
972 *p_slot = slot;
973 return( PSA_SUCCESS );
974}
Darryl Green940d72c2018-07-13 13:18:51 +0100975
Gilles Peskine28f8f302019-07-24 13:30:31 +0200976/** Retrieve a slot which must contain a transparent key.
977 *
978 * A transparent key is a key for which the key material is directly
979 * available, as opposed to a key in a secure element.
980 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200981 * This is a temporary function to use instead of psa_get_key_from_slot()
982 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983 */
984#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
985static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
986 psa_key_slot_t **p_slot,
987 psa_key_usage_t usage,
988 psa_algorithm_t alg )
989{
990 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
991 if( status != PSA_SUCCESS )
992 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200993 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200994 {
995 *p_slot = NULL;
996 return( PSA_ERROR_NOT_SUPPORTED );
997 }
998 return( PSA_SUCCESS );
999}
1000#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1001/* With no secure element support, all keys are transparent. */
1002#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1003 psa_get_key_from_slot( handle, p_slot, usage, alg )
1004#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1005
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001007static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001008{
Gilles Peskine73167e12019-07-12 23:44:37 +02001009#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1010 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001011 {
1012 /* No key material to clean. */
1013 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001014 else
1015#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001016 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001017 {
1018 /* No key material to clean. */
1019 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001020 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001021 {
1022 mbedtls_free( slot->data.raw.data );
1023 }
1024 else
1025#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001026 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001027 {
1028 mbedtls_rsa_free( slot->data.rsa );
1029 mbedtls_free( slot->data.rsa );
1030 }
1031 else
1032#endif /* defined(MBEDTLS_RSA_C) */
1033#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001034 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001035 {
1036 mbedtls_ecp_keypair_free( slot->data.ecp );
1037 mbedtls_free( slot->data.ecp );
1038 }
1039 else
1040#endif /* defined(MBEDTLS_ECP_C) */
1041 {
1042 /* Shouldn't happen: the key type is not any type that we
1043 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001044 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001045 }
1046
1047 return( PSA_SUCCESS );
1048}
1049
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001050/** Completely wipe a slot in memory, including its policy.
1051 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001052psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001053{
1054 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001055 /* Multipart operations may still be using the key. This is safe
1056 * because all multipart operation objects are independent from
1057 * the key slot: if they need to access the key after the setup
1058 * phase, they have a copy of the key. Note that this means that
1059 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060 /* At this point, key material and other type-specific content has
1061 * been wiped. Clear remaining metadata. We can call memset and not
1062 * zeroize because the metadata is not particularly sensitive. */
1063 memset( slot, 0, sizeof( *slot ) );
1064 return( status );
1065}
1066
Gilles Peskinec5487a82018-12-03 18:08:14 +01001067psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001069 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001070 psa_status_t status; /* status of the last operation */
1071 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001072#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1073 psa_se_drv_table_entry_t *driver;
1074#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075
Gilles Peskine1841cf42019-10-08 15:48:25 +02001076 if( handle == 0 )
1077 return( PSA_SUCCESS );
1078
Gilles Peskinec5487a82018-12-03 18:08:14 +01001079 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080 if( status != PSA_SUCCESS )
1081 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001082
1083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001084 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001085 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001086 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001087 /* For a key in a secure element, we need to do three things:
1088 * remove the key file in internal storage, destroy the
1089 * key inside the secure element, and update the driver's
1090 * persistent data. Start a transaction that will encompass these
1091 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001092 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001093 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001094 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001095 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001096 status = psa_crypto_save_transaction( );
1097 if( status != PSA_SUCCESS )
1098 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001099 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001100 /* We should still try to destroy the key in the secure
1101 * element and the key metadata in storage. This is especially
1102 * important if the error is that the storage is full.
1103 * But how to do it exactly without risking an inconsistent
1104 * state after a reset?
1105 * https://github.com/ARMmbed/mbed-crypto/issues/215
1106 */
1107 overall_status = status;
1108 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001109 }
1110
Gilles Peskine354f7672019-07-12 23:46:38 +02001111 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001112 if( overall_status == PSA_SUCCESS )
1113 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001114 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001115#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1116
Darryl Greend49a4992018-06-18 17:27:26 +01001117#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001118 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001119 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001120 status = psa_destroy_persistent_key( slot->attr.id );
1121 if( overall_status == PSA_SUCCESS )
1122 overall_status = status;
1123
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001124 /* TODO: other slots may have a copy of the same key. We should
1125 * invalidate them.
1126 * https://github.com/ARMmbed/mbed-crypto/issues/214
1127 */
Darryl Greend49a4992018-06-18 17:27:26 +01001128 }
1129#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001130
Gilles Peskinefc762652019-07-22 19:30:34 +02001131#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1132 if( driver != NULL )
1133 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001134 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001135 if( overall_status == PSA_SUCCESS )
1136 overall_status = status;
1137 status = psa_crypto_stop_transaction( );
1138 if( overall_status == PSA_SUCCESS )
1139 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 }
1141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1142
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001143#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1144exit:
1145#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001146 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1148 if( overall_status == PSA_SUCCESS )
1149 overall_status = status;
1150 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001151}
1152
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001153void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001154{
Gilles Peskineb699f072019-04-26 16:06:02 +02001155 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001156 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001157}
1158
Gilles Peskineb699f072019-04-26 16:06:02 +02001159psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1160 psa_key_type_t type,
1161 const uint8_t *data,
1162 size_t data_length )
1163{
1164 uint8_t *copy = NULL;
1165
1166 if( data_length != 0 )
1167 {
1168 copy = mbedtls_calloc( 1, data_length );
1169 if( copy == NULL )
1170 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1171 memcpy( copy, data, data_length );
1172 }
1173 /* After this point, this function is guaranteed to succeed, so it
1174 * can start modifying `*attributes`. */
1175
1176 if( attributes->domain_parameters != NULL )
1177 {
1178 mbedtls_free( attributes->domain_parameters );
1179 attributes->domain_parameters = NULL;
1180 attributes->domain_parameters_size = 0;
1181 }
1182
1183 attributes->domain_parameters = copy;
1184 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001185 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 return( PSA_SUCCESS );
1187}
1188
1189psa_status_t psa_get_key_domain_parameters(
1190 const psa_key_attributes_t *attributes,
1191 uint8_t *data, size_t data_size, size_t *data_length )
1192{
1193 if( attributes->domain_parameters_size > data_size )
1194 return( PSA_ERROR_BUFFER_TOO_SMALL );
1195 *data_length = attributes->domain_parameters_size;
1196 if( attributes->domain_parameters_size != 0 )
1197 memcpy( data, attributes->domain_parameters,
1198 attributes->domain_parameters_size );
1199 return( PSA_SUCCESS );
1200}
1201
1202#if defined(MBEDTLS_RSA_C)
1203static psa_status_t psa_get_rsa_public_exponent(
1204 const mbedtls_rsa_context *rsa,
1205 psa_key_attributes_t *attributes )
1206{
1207 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 uint8_t *buffer = NULL;
1210 size_t buflen;
1211 mbedtls_mpi_init( &mpi );
1212
1213 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1214 if( ret != 0 )
1215 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001216 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1217 {
1218 /* It's the default value, which is reported as an empty string,
1219 * so there's nothing to do. */
1220 goto exit;
1221 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001222
1223 buflen = mbedtls_mpi_size( &mpi );
1224 buffer = mbedtls_calloc( 1, buflen );
1225 if( buffer == NULL )
1226 {
1227 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1228 goto exit;
1229 }
1230 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1231 if( ret != 0 )
1232 goto exit;
1233 attributes->domain_parameters = buffer;
1234 attributes->domain_parameters_size = buflen;
1235
1236exit:
1237 mbedtls_mpi_free( &mpi );
1238 if( ret != 0 )
1239 mbedtls_free( buffer );
1240 return( mbedtls_to_psa_error( ret ) );
1241}
1242#endif /* MBEDTLS_RSA_C */
1243
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001244/** Retrieve all the publicly-accessible attributes of a key.
1245 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001246psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1247 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001249 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001250 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001251
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001252 psa_reset_key_attributes( attributes );
1253
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001254 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001255 if( status != PSA_SUCCESS )
1256 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001257
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001258 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001259 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1260 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1261
1262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1263 if( psa_key_slot_is_external( slot ) )
1264 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001266
Gilles Peskine8e338702019-07-30 20:06:31 +02001267 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001268 {
1269#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001270 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001271 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001273 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001274 * is not yet implemented.
1275 * https://github.com/ARMmbed/mbed-crypto/issues/216
1276 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001278 break;
1279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1281 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001282#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001283 default:
1284 /* Nothing else to do. */
1285 break;
1286 }
1287
1288 if( status != PSA_SUCCESS )
1289 psa_reset_key_attributes( attributes );
1290 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291}
1292
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294psa_status_t psa_get_key_slot_number(
1295 const psa_key_attributes_t *attributes,
1296 psa_key_slot_number_t *slot_number )
1297{
1298 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1299 {
1300 *slot_number = attributes->slot_number;
1301 return( PSA_SUCCESS );
1302 }
1303 else
1304 return( PSA_ERROR_INVALID_ARGUMENT );
1305}
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Jaeden Ameroccdce902019-01-10 11:42:27 +00001308#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001309static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1310 unsigned char *buf, size_t size )
1311{
Janos Follath24eed8d2019-11-22 13:21:35 +00001312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001313 unsigned char *c;
1314 size_t len = 0;
1315
1316 c = buf + size;
1317
1318 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1319
1320 return( (int) len );
1321}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001322#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001323
Gilles Peskinef603c712019-01-19 13:40:11 +01001324static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1325 uint8_t *data,
1326 size_t data_size,
1327 size_t *data_length,
1328 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329{
Gilles Peskine5d309672019-07-12 23:47:28 +02001330#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1331 const psa_drv_se_t *drv;
1332 psa_drv_se_context_t *drv_context;
1333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1334
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001335 *data_length = 0;
1336
Gilles Peskine8e338702019-07-30 20:06:31 +02001337 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001338 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001339
Gilles Peskinef9168942019-09-12 19:20:29 +02001340 /* Reject a zero-length output buffer now, since this can never be a
1341 * valid key representation. This way we know that data must be a valid
1342 * pointer and we can do things like memset(data, ..., data_size). */
1343 if( data_size == 0 )
1344 return( PSA_ERROR_BUFFER_TOO_SMALL );
1345
Gilles Peskine5d309672019-07-12 23:47:28 +02001346#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001347 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001348 {
1349 psa_drv_se_export_key_t method;
1350 if( drv->key_management == NULL )
1351 return( PSA_ERROR_NOT_SUPPORTED );
1352 method = ( export_public_key ?
1353 drv->key_management->p_export_public :
1354 drv->key_management->p_export );
1355 if( method == NULL )
1356 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001357 return( method( drv_context,
1358 slot->data.se.slot_number,
1359 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001360 }
1361#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1362
Gilles Peskine8e338702019-07-30 20:06:31 +02001363 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364 {
1365 if( slot->data.raw.bytes > data_size )
1366 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001367 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1368 memset( data + slot->data.raw.bytes, 0,
1369 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370 *data_length = slot->data.raw.bytes;
1371 return( PSA_SUCCESS );
1372 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001373#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001374 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001375 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001376 psa_status_t status;
1377
Gilles Peskineb46bef22019-07-30 21:32:04 +02001378 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001379 if( bytes > data_size )
1380 return( PSA_ERROR_BUFFER_TOO_SMALL );
1381 status = mbedtls_to_psa_error(
1382 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1383 if( status != PSA_SUCCESS )
1384 return( status );
1385 memset( data + bytes, 0, data_size - bytes );
1386 *data_length = bytes;
1387 return( PSA_SUCCESS );
1388 }
1389#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001390 else
Moran Peker17e36e12018-05-02 12:55:20 +03001391 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001392#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001393 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1394 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001395 {
Moran Pekera998bc62018-04-16 18:16:20 +03001396 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001398 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001399 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001400#if defined(MBEDTLS_RSA_C)
1401 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001402 pk.pk_info = &mbedtls_rsa_info;
1403 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001404#else
1405 return( PSA_ERROR_NOT_SUPPORTED );
1406#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001407 }
1408 else
1409 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001410#if defined(MBEDTLS_ECP_C)
1411 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001412 pk.pk_info = &mbedtls_eckey_info;
1413 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001414#else
1415 return( PSA_ERROR_NOT_SUPPORTED );
1416#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001417 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001418 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001419 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001420 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001421 }
Moran Peker17e36e12018-05-02 12:55:20 +03001422 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001423 {
Moran Peker17e36e12018-05-02 12:55:20 +03001424 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001425 }
Moran Peker60364322018-04-29 11:34:58 +03001426 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001427 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001428 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001429 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001430 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001431 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1432 * Move the data to the beginning and erase remaining data
1433 * at the original location. */
1434 if( 2 * (size_t) ret <= data_size )
1435 {
1436 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001437 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001438 }
1439 else if( (size_t) ret < data_size )
1440 {
1441 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001442 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001443 }
Moran Pekera998bc62018-04-16 18:16:20 +03001444 *data_length = ret;
1445 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001446 }
1447 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001448#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001449 {
1450 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001451 it is valid for a special-purpose implementation to omit
1452 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001453 return( PSA_ERROR_NOT_SUPPORTED );
1454 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455 }
1456}
1457
Gilles Peskinec5487a82018-12-03 18:08:14 +01001458psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001459 uint8_t *data,
1460 size_t data_size,
1461 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001463 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001464 psa_status_t status;
1465
1466 /* Set the key to empty now, so that even when there are errors, we always
1467 * set data_length to a value between 0 and data_size. On error, setting
1468 * the key to empty is a good choice because an empty key representation is
1469 * unlikely to be accepted anywhere. */
1470 *data_length = 0;
1471
1472 /* Export requires the EXPORT flag. There is an exception for public keys,
1473 * which don't require any flag, but psa_get_key_from_slot takes
1474 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001475 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001476 if( status != PSA_SUCCESS )
1477 return( status );
1478 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001479 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481
Gilles Peskinec5487a82018-12-03 18:08:14 +01001482psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001483 uint8_t *data,
1484 size_t data_size,
1485 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001486{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001487 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001488 psa_status_t status;
1489
1490 /* Set the key to empty now, so that even when there are errors, we always
1491 * set data_length to a value between 0 and data_size. On error, setting
1492 * the key to empty is a good choice because an empty key representation is
1493 * unlikely to be accepted anywhere. */
1494 *data_length = 0;
1495
1496 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001497 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001498 if( status != PSA_SUCCESS )
1499 return( status );
1500 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001501 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001502}
1503
Gilles Peskine91e8c332019-08-02 19:19:39 +02001504#if defined(static_assert)
1505static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1506 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001507static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001508 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001509static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001510 "One or more key attribute flag is listed as both internal-only and external-only" );
1511#endif
1512
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513/** Validate that a key policy is internally well-formed.
1514 *
1515 * This function only rejects invalid policies. It does not validate the
1516 * consistency of the policy with respect to other attributes of the key
1517 * such as the key type.
1518 */
1519static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001520{
1521 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001522 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 PSA_KEY_USAGE_ENCRYPT |
1524 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001525 PSA_KEY_USAGE_SIGN_HASH |
1526 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001527 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1528 return( PSA_ERROR_INVALID_ARGUMENT );
1529
Gilles Peskine4747d192019-04-17 15:05:45 +02001530 return( PSA_SUCCESS );
1531}
1532
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001533/** Validate the internal consistency of key attributes.
1534 *
1535 * This function only rejects invalid attribute values. If does not
1536 * validate the consistency of the attributes with any key data that may
1537 * be involved in the creation of the key.
1538 *
1539 * Call this function early in the key creation process.
1540 *
1541 * \param[in] attributes Key attributes for the new key.
1542 * \param[out] p_drv On any return, the driver for the key, if any.
1543 * NULL for a transparent key.
1544 *
1545 */
1546static psa_status_t psa_validate_key_attributes(
1547 const psa_key_attributes_t *attributes,
1548 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001549{
1550 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001551
1552 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001553 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001554 status = psa_validate_persistent_key_parameters(
1555 attributes->core.lifetime, attributes->core.id,
1556 p_drv, 1 );
1557 if( status != PSA_SUCCESS )
1558 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001559 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001560
1561 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001562 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001563 return( status );
1564
1565 /* Refuse to create overly large keys.
1566 * Note that this doesn't trigger on import if the attributes don't
1567 * explicitly specify a size (so psa_get_key_bits returns 0), so
1568 * psa_import_key() needs its own checks. */
1569 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1570 return( PSA_ERROR_NOT_SUPPORTED );
1571
Gilles Peskine91e8c332019-08-02 19:19:39 +02001572 /* Reject invalid flags. These should not be reachable through the API. */
1573 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1574 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1575 return( PSA_ERROR_INVALID_ARGUMENT );
1576
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577 return( PSA_SUCCESS );
1578}
1579
Gilles Peskine4747d192019-04-17 15:05:45 +02001580/** Prepare a key slot to receive key material.
1581 *
1582 * This function allocates a key slot and sets its metadata.
1583 *
1584 * If this function fails, call psa_fail_key_creation().
1585 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001586 * This function is intended to be used as follows:
1587 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1588 * it with the specified attributes, and assign it a handle.
1589 * -# Populate the slot with the key material.
1590 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1591 * In case of failure at any step, stop the sequence and call
1592 * psa_fail_key_creation().
1593 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001594 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001595 * \param[in] attributes Key attributes for the new key.
1596 * \param[out] handle On success, a handle for the allocated slot.
1597 * \param[out] p_slot On success, a pointer to the prepared slot.
1598 * \param[out] p_drv On any return, the driver for the key, if any.
1599 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001600 *
1601 * \retval #PSA_SUCCESS
1602 * The key slot is ready to receive key material.
1603 * \return If this function fails, the key slot is an invalid state.
1604 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001605 */
1606static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001607 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001608 const psa_key_attributes_t *attributes,
1609 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001611 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001612{
1613 psa_status_t status;
1614 psa_key_slot_t *slot;
1615
Gilles Peskinedf179142019-07-15 22:02:14 +02001616 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 *p_drv = NULL;
1618
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001619 status = psa_validate_key_attributes( attributes, p_drv );
1620 if( status != PSA_SUCCESS )
1621 return( status );
1622
Gilles Peskineedbed562019-08-07 18:19:59 +02001623 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 if( status != PSA_SUCCESS )
1625 return( status );
1626 slot = *p_slot;
1627
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001628 /* We're storing the declared bit-size of the key. It's up to each
1629 * creation mechanism to verify that this information is correct.
1630 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001631 * an input (generate, device) but not for those where the bit-size
1632 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001633
1634 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001635
Gilles Peskine91e8c332019-08-02 19:19:39 +02001636 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001637 * external-only flags, query `attributes`. Thanks to the check
1638 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001639 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001640 * may have set. */
1641 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001642
Gilles Peskinecbaff462019-07-12 23:46:04 +02001643#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001644 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001645 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001646 * create the key file in internal storage, create the
1647 * key inside the secure element, and update the driver's
1648 * persistent data. Start a transaction that will encompass these
1649 * three actions. */
1650 /* The first thing to do is to find a slot number for the new key.
1651 * We save the slot number in persistent storage as part of the
1652 * transaction data. It will be needed to recover if the power
1653 * fails during the key creation process, to clean up on the secure
1654 * element side after restarting. Obtaining a slot number from the
1655 * secure element driver updates its persistent state, but we do not yet
1656 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001657 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001658 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001659 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001660 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001661 &slot->data.se.slot_number );
1662 if( status != PSA_SUCCESS )
1663 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001664 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001665 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001666 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001667 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001668 status = psa_crypto_save_transaction( );
1669 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001670 {
1671 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001672 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001673 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001674 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001675
1676 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1677 {
1678 /* Key registration only makes sense with a secure element. */
1679 return( PSA_ERROR_INVALID_ARGUMENT );
1680 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001681#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1682
Darryl Green0c6575a2018-11-07 16:05:30 +00001683 return( status );
1684}
Gilles Peskine4747d192019-04-17 15:05:45 +02001685
1686/** Finalize the creation of a key once its key material has been set.
1687 *
1688 * This entails writing the key to persistent storage.
1689 *
1690 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001691 * See the documentation of psa_start_key_creation() for the intended use
1692 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 * \param[in,out] slot Pointer to the slot with key material.
1695 * \param[in] driver The secure element driver for the key,
1696 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001697 *
1698 * \retval #PSA_SUCCESS
1699 * The key was successfully created. The handle is now valid.
1700 * \return If this function fails, the key slot is an invalid state.
1701 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001703static psa_status_t psa_finish_key_creation(
1704 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001705 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001706{
1707 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001708 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001710
1711#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001712 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001713 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001714#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1715 if( driver != NULL )
1716 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001717 psa_se_key_data_storage_t data;
1718#if defined(static_assert)
1719 static_assert( sizeof( slot->data.se.slot_number ) ==
1720 sizeof( data.slot_number ),
1721 "Slot number size does not match psa_se_key_data_storage_t" );
1722 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1723 "Bit-size size does not match psa_se_key_data_storage_t" );
1724#endif
1725 memcpy( &data.slot_number, &slot->data.se.slot_number,
1726 sizeof( slot->data.se.slot_number ) );
1727 memcpy( &data.bits, &slot->attr.bits,
1728 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001729 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001730 (uint8_t*) &data,
1731 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001732 }
1733 else
1734#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1735 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001736 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001737 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001738 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001739 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1740 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001741 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001742 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1743 status = psa_internal_export_key( slot,
1744 buffer, buffer_size, &length,
1745 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001746 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001747 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001748 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001749
Gilles Peskinef9168942019-09-12 19:20:29 +02001750 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001751 mbedtls_free( buffer );
1752 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001754#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1755
Gilles Peskinecbaff462019-07-12 23:46:04 +02001756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001757 /* Finish the transaction for a key creation. This does not
1758 * happen when registering an existing key. Detect this case
1759 * by checking whether a transaction is in progress (actual
1760 * creation of a key in a secure element requires a transaction,
1761 * but registration doesn't use one). */
1762 if( driver != NULL &&
1763 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001764 {
1765 status = psa_save_se_persistent_data( driver );
1766 if( status != PSA_SUCCESS )
1767 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001768 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001769 return( status );
1770 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001771 status = psa_crypto_stop_transaction( );
1772 if( status != PSA_SUCCESS )
1773 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001774 }
1775#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1776
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 return( status );
1778}
1779
1780/** Abort the creation of a key.
1781 *
1782 * You may call this function after calling psa_start_key_creation(),
1783 * or after psa_finish_key_creation() fails. In other circumstances, this
1784 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001785 * See the documentation of psa_start_key_creation() for the intended use
1786 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001787 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001788 * \param[in,out] slot Pointer to the slot with key material.
1789 * \param[in] driver The secure element driver for the key,
1790 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001792static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001793 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001794{
Gilles Peskine011e4282019-06-26 18:34:38 +02001795 (void) driver;
1796
Gilles Peskine4747d192019-04-17 15:05:45 +02001797 if( slot == NULL )
1798 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001799
1800#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001801 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001802 * element, and the failure happened later (when saving metadata
1803 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001804 * element.
1805 * https://github.com/ARMmbed/mbed-crypto/issues/217
1806 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001807
Gilles Peskined7729582019-08-05 15:55:54 +02001808 /* Abort the ongoing transaction if any (there may not be one if
1809 * the creation process failed before starting one, or if the
1810 * key creation is a registration of a key in a secure element).
1811 * Earlier functions must already have done what it takes to undo any
1812 * partial creation. All that's left is to update the transaction data
1813 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001814 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001815#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1816
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 psa_wipe_key_slot( slot );
1818}
1819
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001820/** Validate optional attributes during key creation.
1821 *
1822 * Some key attributes are optional during key creation. If they are
1823 * specified in the attributes structure, check that they are consistent
1824 * with the data in the slot.
1825 *
1826 * This function should be called near the end of key creation, after
1827 * the slot in memory is fully populated but before saving persistent data.
1828 */
1829static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001830 const psa_key_slot_t *slot,
1831 const psa_key_attributes_t *attributes )
1832{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001833 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001834 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001835 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 return( PSA_ERROR_INVALID_ARGUMENT );
1837 }
1838
1839 if( attributes->domain_parameters_size != 0 )
1840 {
1841#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001842 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001843 {
1844 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846 mbedtls_mpi_init( &actual );
1847 mbedtls_mpi_init( &required );
1848 ret = mbedtls_rsa_export( slot->data.rsa,
1849 NULL, NULL, NULL, NULL, &actual );
1850 if( ret != 0 )
1851 goto rsa_exit;
1852 ret = mbedtls_mpi_read_binary( &required,
1853 attributes->domain_parameters,
1854 attributes->domain_parameters_size );
1855 if( ret != 0 )
1856 goto rsa_exit;
1857 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1858 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1859 rsa_exit:
1860 mbedtls_mpi_free( &actual );
1861 mbedtls_mpi_free( &required );
1862 if( ret != 0)
1863 return( mbedtls_to_psa_error( ret ) );
1864 }
1865 else
1866#endif
1867 {
1868 return( PSA_ERROR_INVALID_ARGUMENT );
1869 }
1870 }
1871
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001872 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001873 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001874 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875 return( PSA_ERROR_INVALID_ARGUMENT );
1876 }
1877
1878 return( PSA_SUCCESS );
1879}
1880
Gilles Peskine4747d192019-04-17 15:05:45 +02001881psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001883 size_t data_length,
1884 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001885{
1886 psa_status_t status;
1887 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001888 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001889
Gilles Peskine0f84d622019-09-12 19:03:13 +02001890 /* Reject zero-length symmetric keys (including raw data key objects).
1891 * This also rejects any key which might be encoded as an empty string,
1892 * which is never valid. */
1893 if( data_length == 0 )
1894 return( PSA_ERROR_INVALID_ARGUMENT );
1895
Gilles Peskinedf179142019-07-15 22:02:14 +02001896 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1897 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 if( status != PSA_SUCCESS )
1899 goto exit;
1900
Gilles Peskine5d309672019-07-12 23:47:28 +02001901#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1902 if( driver != NULL )
1903 {
1904 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001905 /* The driver should set the number of key bits, however in
1906 * case it doesn't, we initialize bits to an invalid value. */
1907 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001908 if( drv->key_management == NULL ||
1909 drv->key_management->p_import == NULL )
1910 {
1911 status = PSA_ERROR_NOT_SUPPORTED;
1912 goto exit;
1913 }
1914 status = drv->key_management->p_import(
1915 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001916 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001917 &bits );
1918 if( status != PSA_SUCCESS )
1919 goto exit;
1920 if( bits > PSA_MAX_KEY_BITS )
1921 {
1922 status = PSA_ERROR_NOT_SUPPORTED;
1923 goto exit;
1924 }
1925 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001926 }
1927 else
1928#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1929 {
1930 status = psa_import_key_into_slot( slot, data, data_length );
1931 if( status != PSA_SUCCESS )
1932 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001933 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001934 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001935 if( status != PSA_SUCCESS )
1936 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001937
Gilles Peskine011e4282019-06-26 18:34:38 +02001938 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001940 if( status != PSA_SUCCESS )
1941 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001942 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001943 *handle = 0;
1944 }
1945 return( status );
1946}
1947
Gilles Peskined7729582019-08-05 15:55:54 +02001948#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1949psa_status_t mbedtls_psa_register_se_key(
1950 const psa_key_attributes_t *attributes )
1951{
1952 psa_status_t status;
1953 psa_key_slot_t *slot = NULL;
1954 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001955 psa_key_handle_t handle = 0;
1956
1957 /* Leaving attributes unspecified is not currently supported.
1958 * It could make sense to query the key type and size from the
1959 * secure element, but not all secure elements support this
1960 * and the driver HAL doesn't currently support it. */
1961 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1962 return( PSA_ERROR_NOT_SUPPORTED );
1963 if( psa_get_key_bits( attributes ) == 0 )
1964 return( PSA_ERROR_NOT_SUPPORTED );
1965
1966 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1967 &handle, &slot, &driver );
1968 if( status != PSA_SUCCESS )
1969 goto exit;
1970
Gilles Peskined7729582019-08-05 15:55:54 +02001971 status = psa_finish_key_creation( slot, driver );
1972
1973exit:
1974 if( status != PSA_SUCCESS )
1975 {
1976 psa_fail_key_creation( slot, driver );
1977 }
1978 /* Registration doesn't keep the key in RAM. */
1979 psa_close_key( handle );
1980 return( status );
1981}
1982#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1983
Gilles Peskinef603c712019-01-19 13:40:11 +01001984static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001986{
1987 psa_status_t status;
1988 uint8_t *buffer = NULL;
1989 size_t buffer_size = 0;
1990 size_t length;
1991
Gilles Peskine8e338702019-07-30 20:06:31 +02001992 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001993 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001994 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001995 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001996 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001997 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1998 if( status != PSA_SUCCESS )
1999 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002000 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002002
2003exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002004 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002005 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002006 return( status );
2007}
2008
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002009psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2010 const psa_key_attributes_t *specified_attributes,
2011 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002012{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002014 psa_key_slot_t *source_slot = NULL;
2015 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002017 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002018
Gilles Peskine28f8f302019-07-24 13:30:31 +02002019 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002020 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002021 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002022 goto exit;
2023
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002024 status = psa_validate_optional_attributes( source_slot,
2025 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002026 if( status != PSA_SUCCESS )
2027 goto exit;
2028
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002029 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002030 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002031 if( status != PSA_SUCCESS )
2032 goto exit;
2033
Gilles Peskinedf179142019-07-15 22:02:14 +02002034 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2035 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002036 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002037 if( status != PSA_SUCCESS )
2038 goto exit;
2039
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2041 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002042 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002043 /* Copying to a secure element is not implemented yet. */
2044 status = PSA_ERROR_NOT_SUPPORTED;
2045 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002046 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002047#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002048
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002049 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002050 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002052
Gilles Peskine011e4282019-06-26 18:34:38 +02002053 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002054exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002055 if( status != PSA_SUCCESS )
2056 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002057 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002058 *target_handle = 0;
2059 }
2060 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002061}
2062
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002063
2064
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002065/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002066/* Message digests */
2067/****************************************************************/
2068
Gilles Peskineb16841e2019-10-10 20:36:12 +02002069#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002070static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002071{
2072 switch( alg )
2073 {
2074#if defined(MBEDTLS_MD2_C)
2075 case PSA_ALG_MD2:
2076 return( &mbedtls_md2_info );
2077#endif
2078#if defined(MBEDTLS_MD4_C)
2079 case PSA_ALG_MD4:
2080 return( &mbedtls_md4_info );
2081#endif
2082#if defined(MBEDTLS_MD5_C)
2083 case PSA_ALG_MD5:
2084 return( &mbedtls_md5_info );
2085#endif
2086#if defined(MBEDTLS_RIPEMD160_C)
2087 case PSA_ALG_RIPEMD160:
2088 return( &mbedtls_ripemd160_info );
2089#endif
2090#if defined(MBEDTLS_SHA1_C)
2091 case PSA_ALG_SHA_1:
2092 return( &mbedtls_sha1_info );
2093#endif
2094#if defined(MBEDTLS_SHA256_C)
2095 case PSA_ALG_SHA_224:
2096 return( &mbedtls_sha224_info );
2097 case PSA_ALG_SHA_256:
2098 return( &mbedtls_sha256_info );
2099#endif
2100#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002101#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002102 case PSA_ALG_SHA_384:
2103 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002104#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002105 case PSA_ALG_SHA_512:
2106 return( &mbedtls_sha512_info );
2107#endif
2108 default:
2109 return( NULL );
2110 }
2111}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002112#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002113
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2115{
2116 switch( operation->alg )
2117 {
Gilles Peskine81736312018-06-26 15:04:31 +02002118 case 0:
2119 /* The object has (apparently) been initialized but it is not
2120 * in use. It's ok to call abort on such an object, and there's
2121 * nothing to do. */
2122 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123#if defined(MBEDTLS_MD2_C)
2124 case PSA_ALG_MD2:
2125 mbedtls_md2_free( &operation->ctx.md2 );
2126 break;
2127#endif
2128#if defined(MBEDTLS_MD4_C)
2129 case PSA_ALG_MD4:
2130 mbedtls_md4_free( &operation->ctx.md4 );
2131 break;
2132#endif
2133#if defined(MBEDTLS_MD5_C)
2134 case PSA_ALG_MD5:
2135 mbedtls_md5_free( &operation->ctx.md5 );
2136 break;
2137#endif
2138#if defined(MBEDTLS_RIPEMD160_C)
2139 case PSA_ALG_RIPEMD160:
2140 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2141 break;
2142#endif
2143#if defined(MBEDTLS_SHA1_C)
2144 case PSA_ALG_SHA_1:
2145 mbedtls_sha1_free( &operation->ctx.sha1 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_SHA256_C)
2149 case PSA_ALG_SHA_224:
2150 case PSA_ALG_SHA_256:
2151 mbedtls_sha256_free( &operation->ctx.sha256 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002155#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002156 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002157#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002158 case PSA_ALG_SHA_512:
2159 mbedtls_sha512_free( &operation->ctx.sha512 );
2160 break;
2161#endif
2162 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002163 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164 }
2165 operation->alg = 0;
2166 return( PSA_SUCCESS );
2167}
2168
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002169psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002170 psa_algorithm_t alg )
2171{
Janos Follath24eed8d2019-11-22 13:21:35 +00002172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002173
2174 /* A context must be freshly initialized before it can be set up. */
2175 if( operation->alg != 0 )
2176 {
2177 return( PSA_ERROR_BAD_STATE );
2178 }
2179
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180 switch( alg )
2181 {
2182#if defined(MBEDTLS_MD2_C)
2183 case PSA_ALG_MD2:
2184 mbedtls_md2_init( &operation->ctx.md2 );
2185 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2186 break;
2187#endif
2188#if defined(MBEDTLS_MD4_C)
2189 case PSA_ALG_MD4:
2190 mbedtls_md4_init( &operation->ctx.md4 );
2191 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2192 break;
2193#endif
2194#if defined(MBEDTLS_MD5_C)
2195 case PSA_ALG_MD5:
2196 mbedtls_md5_init( &operation->ctx.md5 );
2197 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2198 break;
2199#endif
2200#if defined(MBEDTLS_RIPEMD160_C)
2201 case PSA_ALG_RIPEMD160:
2202 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2203 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2204 break;
2205#endif
2206#if defined(MBEDTLS_SHA1_C)
2207 case PSA_ALG_SHA_1:
2208 mbedtls_sha1_init( &operation->ctx.sha1 );
2209 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2210 break;
2211#endif
2212#if defined(MBEDTLS_SHA256_C)
2213 case PSA_ALG_SHA_224:
2214 mbedtls_sha256_init( &operation->ctx.sha256 );
2215 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2216 break;
2217 case PSA_ALG_SHA_256:
2218 mbedtls_sha256_init( &operation->ctx.sha256 );
2219 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2220 break;
2221#endif
2222#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002223#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 case PSA_ALG_SHA_384:
2225 mbedtls_sha512_init( &operation->ctx.sha512 );
2226 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2227 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002228#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 case PSA_ALG_SHA_512:
2230 mbedtls_sha512_init( &operation->ctx.sha512 );
2231 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2232 break;
2233#endif
2234 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002235 return( PSA_ALG_IS_HASH( alg ) ?
2236 PSA_ERROR_NOT_SUPPORTED :
2237 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 }
2239 if( ret == 0 )
2240 operation->alg = alg;
2241 else
2242 psa_hash_abort( operation );
2243 return( mbedtls_to_psa_error( ret ) );
2244}
2245
2246psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2247 const uint8_t *input,
2248 size_t input_length )
2249{
Janos Follath24eed8d2019-11-22 13:21:35 +00002250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002251
2252 /* Don't require hash implementations to behave correctly on a
2253 * zero-length input, which may have an invalid pointer. */
2254 if( input_length == 0 )
2255 return( PSA_SUCCESS );
2256
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 switch( operation->alg )
2258 {
2259#if defined(MBEDTLS_MD2_C)
2260 case PSA_ALG_MD2:
2261 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2262 input, input_length );
2263 break;
2264#endif
2265#if defined(MBEDTLS_MD4_C)
2266 case PSA_ALG_MD4:
2267 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2268 input, input_length );
2269 break;
2270#endif
2271#if defined(MBEDTLS_MD5_C)
2272 case PSA_ALG_MD5:
2273 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2274 input, input_length );
2275 break;
2276#endif
2277#if defined(MBEDTLS_RIPEMD160_C)
2278 case PSA_ALG_RIPEMD160:
2279 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2280 input, input_length );
2281 break;
2282#endif
2283#if defined(MBEDTLS_SHA1_C)
2284 case PSA_ALG_SHA_1:
2285 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2286 input, input_length );
2287 break;
2288#endif
2289#if defined(MBEDTLS_SHA256_C)
2290 case PSA_ALG_SHA_224:
2291 case PSA_ALG_SHA_256:
2292 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2293 input, input_length );
2294 break;
2295#endif
2296#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002297#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002299#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002300 case PSA_ALG_SHA_512:
2301 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2302 input, input_length );
2303 break;
2304#endif
2305 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002306 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002307 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002308
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309 if( ret != 0 )
2310 psa_hash_abort( operation );
2311 return( mbedtls_to_psa_error( ret ) );
2312}
2313
2314psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2315 uint8_t *hash,
2316 size_t hash_size,
2317 size_t *hash_length )
2318{
itayzafrir40835d42018-08-02 13:14:17 +03002319 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002321 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322
2323 /* Fill the output buffer with something that isn't a valid hash
2324 * (barring an attack on the hash and deliberately-crafted input),
2325 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002326 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002327 /* If hash_size is 0 then hash may be NULL and then the
2328 * call to memset would have undefined behavior. */
2329 if( hash_size != 0 )
2330 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331
2332 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002333 {
2334 status = PSA_ERROR_BUFFER_TOO_SMALL;
2335 goto exit;
2336 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337
2338 switch( operation->alg )
2339 {
2340#if defined(MBEDTLS_MD2_C)
2341 case PSA_ALG_MD2:
2342 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2343 break;
2344#endif
2345#if defined(MBEDTLS_MD4_C)
2346 case PSA_ALG_MD4:
2347 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2348 break;
2349#endif
2350#if defined(MBEDTLS_MD5_C)
2351 case PSA_ALG_MD5:
2352 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2353 break;
2354#endif
2355#if defined(MBEDTLS_RIPEMD160_C)
2356 case PSA_ALG_RIPEMD160:
2357 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2358 break;
2359#endif
2360#if defined(MBEDTLS_SHA1_C)
2361 case PSA_ALG_SHA_1:
2362 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2363 break;
2364#endif
2365#if defined(MBEDTLS_SHA256_C)
2366 case PSA_ALG_SHA_224:
2367 case PSA_ALG_SHA_256:
2368 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2369 break;
2370#endif
2371#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002372#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002374#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 case PSA_ALG_SHA_512:
2376 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2377 break;
2378#endif
2379 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002380 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381 }
itayzafrir40835d42018-08-02 13:14:17 +03002382 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383
itayzafrir40835d42018-08-02 13:14:17 +03002384exit:
2385 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002387 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388 return( psa_hash_abort( operation ) );
2389 }
2390 else
2391 {
2392 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002393 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 }
2395}
2396
Gilles Peskine2d277862018-06-18 15:41:12 +02002397psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2398 const uint8_t *hash,
2399 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400{
2401 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2402 size_t actual_hash_length;
2403 psa_status_t status = psa_hash_finish( operation,
2404 actual_hash, sizeof( actual_hash ),
2405 &actual_hash_length );
2406 if( status != PSA_SUCCESS )
2407 return( status );
2408 if( actual_hash_length != hash_length )
2409 return( PSA_ERROR_INVALID_SIGNATURE );
2410 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2411 return( PSA_ERROR_INVALID_SIGNATURE );
2412 return( PSA_SUCCESS );
2413}
2414
Gilles Peskine0a749c82019-11-28 19:33:58 +01002415psa_status_t psa_hash_compute( psa_algorithm_t alg,
2416 const uint8_t *input, size_t input_length,
2417 uint8_t *hash, size_t hash_size,
2418 size_t *hash_length )
2419{
2420 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2422
2423 *hash_length = hash_size;
2424 status = psa_hash_setup( &operation, alg );
2425 if( status != PSA_SUCCESS )
2426 goto exit;
2427 status = psa_hash_update( &operation, input, input_length );
2428 if( status != PSA_SUCCESS )
2429 goto exit;
2430 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2431 if( status != PSA_SUCCESS )
2432 goto exit;
2433
2434exit:
2435 if( status == PSA_SUCCESS )
2436 status = psa_hash_abort( &operation );
2437 else
2438 psa_hash_abort( &operation );
2439 return( status );
2440}
2441
2442psa_status_t psa_hash_compare( psa_algorithm_t alg,
2443 const uint8_t *input, size_t input_length,
2444 const uint8_t *hash, size_t hash_length )
2445{
2446 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2447 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2448
2449 status = psa_hash_setup( &operation, alg );
2450 if( status != PSA_SUCCESS )
2451 goto exit;
2452 status = psa_hash_update( &operation, input, input_length );
2453 if( status != PSA_SUCCESS )
2454 goto exit;
2455 status = psa_hash_verify( &operation, hash, hash_length );
2456 if( status != PSA_SUCCESS )
2457 goto exit;
2458
2459exit:
2460 if( status == PSA_SUCCESS )
2461 status = psa_hash_abort( &operation );
2462 else
2463 psa_hash_abort( &operation );
2464 return( status );
2465}
2466
Gilles Peskineeb35d782019-01-22 17:56:16 +01002467psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2468 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469{
2470 if( target_operation->alg != 0 )
2471 return( PSA_ERROR_BAD_STATE );
2472
2473 switch( source_operation->alg )
2474 {
2475 case 0:
2476 return( PSA_ERROR_BAD_STATE );
2477#if defined(MBEDTLS_MD2_C)
2478 case PSA_ALG_MD2:
2479 mbedtls_md2_clone( &target_operation->ctx.md2,
2480 &source_operation->ctx.md2 );
2481 break;
2482#endif
2483#if defined(MBEDTLS_MD4_C)
2484 case PSA_ALG_MD4:
2485 mbedtls_md4_clone( &target_operation->ctx.md4,
2486 &source_operation->ctx.md4 );
2487 break;
2488#endif
2489#if defined(MBEDTLS_MD5_C)
2490 case PSA_ALG_MD5:
2491 mbedtls_md5_clone( &target_operation->ctx.md5,
2492 &source_operation->ctx.md5 );
2493 break;
2494#endif
2495#if defined(MBEDTLS_RIPEMD160_C)
2496 case PSA_ALG_RIPEMD160:
2497 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2498 &source_operation->ctx.ripemd160 );
2499 break;
2500#endif
2501#if defined(MBEDTLS_SHA1_C)
2502 case PSA_ALG_SHA_1:
2503 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2504 &source_operation->ctx.sha1 );
2505 break;
2506#endif
2507#if defined(MBEDTLS_SHA256_C)
2508 case PSA_ALG_SHA_224:
2509 case PSA_ALG_SHA_256:
2510 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2511 &source_operation->ctx.sha256 );
2512 break;
2513#endif
2514#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002515#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002516 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002517#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002518 case PSA_ALG_SHA_512:
2519 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2520 &source_operation->ctx.sha512 );
2521 break;
2522#endif
2523 default:
2524 return( PSA_ERROR_NOT_SUPPORTED );
2525 }
2526
2527 target_operation->alg = source_operation->alg;
2528 return( PSA_SUCCESS );
2529}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002530
2531
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002532/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533/* MAC */
2534/****************************************************************/
2535
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002536static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 psa_algorithm_t alg,
2538 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002539 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002540 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002543 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002545 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002546 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002547
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2549 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002550 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002552 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002553 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554 mode = MBEDTLS_MODE_STREAM;
2555 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 case PSA_ALG_CTR:
2557 mode = MBEDTLS_MODE_CTR;
2558 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002559 case PSA_ALG_CFB:
2560 mode = MBEDTLS_MODE_CFB;
2561 break;
2562 case PSA_ALG_OFB:
2563 mode = MBEDTLS_MODE_OFB;
2564 break;
2565 case PSA_ALG_CBC_NO_PADDING:
2566 mode = MBEDTLS_MODE_CBC;
2567 break;
2568 case PSA_ALG_CBC_PKCS7:
2569 mode = MBEDTLS_MODE_CBC;
2570 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002571 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 mode = MBEDTLS_MODE_CCM;
2573 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002574 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 mode = MBEDTLS_MODE_GCM;
2576 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002577 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2578 mode = MBEDTLS_MODE_CHACHAPOLY;
2579 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580 default:
2581 return( NULL );
2582 }
2583 }
2584 else if( alg == PSA_ALG_CMAC )
2585 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586 else
2587 return( NULL );
2588
2589 switch( key_type )
2590 {
2591 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002592 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593 break;
2594 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002595 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2596 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002598 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002600 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002601 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2602 * but two-key Triple-DES is functionally three-key Triple-DES
2603 * with K1=K3, so that's how we present it to mbedtls. */
2604 if( key_bits == 128 )
2605 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 break;
2607 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002608 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 break;
2610 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002611 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002613 case PSA_KEY_TYPE_CHACHA20:
2614 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2615 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616 default:
2617 return( NULL );
2618 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002619 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002620 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621
Jaeden Amero23bbb752018-06-26 14:16:54 +01002622 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2623 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624}
2625
Gilles Peskinea05219c2018-11-16 16:02:56 +01002626#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002627static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002628{
Gilles Peskine2d277862018-06-18 15:41:12 +02002629 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002630 {
2631 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002632 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002633 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002634 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002635 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002636 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002637 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002638 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002639 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002640 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002641 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002642 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002643 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002644 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002645 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002646 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002647 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002648 return( 128 );
2649 default:
2650 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002651 }
2652}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002653#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002654
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002655/* Initialize the MAC operation structure. Once this function has been
2656 * called, psa_mac_abort can run and will do the right thing. */
2657static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2658 psa_algorithm_t alg )
2659{
2660 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2661
2662 operation->alg = alg;
2663 operation->key_set = 0;
2664 operation->iv_set = 0;
2665 operation->iv_required = 0;
2666 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002667 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002668
2669#if defined(MBEDTLS_CMAC_C)
2670 if( alg == PSA_ALG_CMAC )
2671 {
2672 operation->iv_required = 0;
2673 mbedtls_cipher_init( &operation->ctx.cmac );
2674 status = PSA_SUCCESS;
2675 }
2676 else
2677#endif /* MBEDTLS_CMAC_C */
2678#if defined(MBEDTLS_MD_C)
2679 if( PSA_ALG_IS_HMAC( operation->alg ) )
2680 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002681 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2682 operation->ctx.hmac.hash_ctx.alg = 0;
2683 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684 }
2685 else
2686#endif /* MBEDTLS_MD_C */
2687 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002688 if( ! PSA_ALG_IS_MAC( alg ) )
2689 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002690 }
2691
2692 if( status != PSA_SUCCESS )
2693 memset( operation, 0, sizeof( *operation ) );
2694 return( status );
2695}
2696
Gilles Peskine01126fa2018-07-12 17:04:55 +02002697#if defined(MBEDTLS_MD_C)
2698static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2699{
Gilles Peskine3f108122018-12-07 18:14:53 +01002700 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 return( psa_hash_abort( &hmac->hash_ctx ) );
2702}
2703#endif /* MBEDTLS_MD_C */
2704
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2706{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002709 /* The object has (apparently) been initialized but it is not
2710 * in use. It's ok to call abort on such an object, and there's
2711 * nothing to do. */
2712 return( PSA_SUCCESS );
2713 }
2714 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002716 if( operation->alg == PSA_ALG_CMAC )
2717 {
2718 mbedtls_cipher_free( &operation->ctx.cmac );
2719 }
2720 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002723 if( PSA_ALG_IS_HMAC( operation->alg ) )
2724 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002725 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002726 }
2727 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002729 {
2730 /* Sanity check (shouldn't happen: operation->alg should
2731 * always have been initialized to a valid value). */
2732 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 }
Moran Peker41deec42018-04-04 15:43:05 +03002734
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735 operation->alg = 0;
2736 operation->key_set = 0;
2737 operation->iv_set = 0;
2738 operation->iv_required = 0;
2739 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002740 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002741
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002743
2744bad_state:
2745 /* If abort is called on an uninitialized object, we can't trust
2746 * anything. Wipe the object in case it contains confidential data.
2747 * This may result in a memory leak if a pointer gets overwritten,
2748 * but it's too late to do anything about this. */
2749 memset( operation, 0, sizeof( *operation ) );
2750 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751}
2752
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002753#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002754static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002755 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002756 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002757 const mbedtls_cipher_info_t *cipher_info )
2758{
Janos Follath24eed8d2019-11-22 13:21:35 +00002759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760
2761 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762
2763 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2764 if( ret != 0 )
2765 return( ret );
2766
2767 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2768 slot->data.raw.data,
2769 key_bits );
2770 return( ret );
2771}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002772#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773
Gilles Peskine248051a2018-06-20 16:09:38 +02002774#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002775static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2776 const uint8_t *key,
2777 size_t key_length,
2778 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002780 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002781 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002782 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002783 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002784 psa_status_t status;
2785
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002786 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2787 * overflow below. This should never trigger if the hash algorithm
2788 * is implemented correctly. */
2789 /* The size checks against the ipad and opad buffers cannot be written
2790 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2791 * because that triggers -Wlogical-op on GCC 7.3. */
2792 if( block_size > sizeof( ipad ) )
2793 return( PSA_ERROR_NOT_SUPPORTED );
2794 if( block_size > sizeof( hmac->opad ) )
2795 return( PSA_ERROR_NOT_SUPPORTED );
2796 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002797 return( PSA_ERROR_NOT_SUPPORTED );
2798
Gilles Peskined223b522018-06-11 18:12:58 +02002799 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002800 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002801 status = psa_hash_compute( hash_alg, key, key_length,
2802 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002803 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002804 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002805 }
Gilles Peskine96889972018-07-12 17:07:03 +02002806 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2807 * but it is permitted. It is common when HMAC is used in HKDF, for
2808 * example. Don't call `memcpy` in the 0-length because `key` could be
2809 * an invalid pointer which would make the behavior undefined. */
2810 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002811 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002812
Gilles Peskined223b522018-06-11 18:12:58 +02002813 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2814 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002816 ipad[i] ^= 0x36;
2817 memset( ipad + key_length, 0x36, block_size - key_length );
2818
2819 /* Copy the key material from ipad to opad, flipping the requisite bits,
2820 * and filling the rest of opad with the requisite constant. */
2821 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002822 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2823 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002824
Gilles Peskine01126fa2018-07-12 17:04:55 +02002825 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002826 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002827 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002828
Gilles Peskine01126fa2018-07-12 17:04:55 +02002829 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002830
2831cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002832 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002833
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834 return( status );
2835}
Gilles Peskine248051a2018-06-20 16:09:38 +02002836#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002837
Gilles Peskine89167cb2018-07-08 20:12:23 +02002838static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002839 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002840 psa_algorithm_t alg,
2841 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002844 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002846 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002847 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002848 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002849 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002851 /* A context must be freshly initialized before it can be set up. */
2852 if( operation->alg != 0 )
2853 {
2854 return( PSA_ERROR_BAD_STATE );
2855 }
2856
Gilles Peskined911eb72018-08-14 15:18:45 +02002857 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002858 if( status != PSA_SUCCESS )
2859 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002860 if( is_sign )
2861 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862
Gilles Peskine28f8f302019-07-24 13:30:31 +02002863 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002864 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002866 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002867
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002869 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002870 {
2871 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002872 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002873 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 if( cipher_info == NULL )
2876 {
2877 status = PSA_ERROR_NOT_SUPPORTED;
2878 goto exit;
2879 }
2880 operation->mac_size = cipher_info->block_size;
2881 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2882 status = mbedtls_to_psa_error( ret );
2883 }
2884 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002887 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002888 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002889 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002890 if( hash_alg == 0 )
2891 {
2892 status = PSA_ERROR_NOT_SUPPORTED;
2893 goto exit;
2894 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002895
2896 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2897 /* Sanity check. This shouldn't fail on a valid configuration. */
2898 if( operation->mac_size == 0 ||
2899 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2900 {
2901 status = PSA_ERROR_NOT_SUPPORTED;
2902 goto exit;
2903 }
2904
Gilles Peskine8e338702019-07-30 20:06:31 +02002905 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002906 {
2907 status = PSA_ERROR_INVALID_ARGUMENT;
2908 goto exit;
2909 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002910
Gilles Peskine01126fa2018-07-12 17:04:55 +02002911 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2912 slot->data.raw.data,
2913 slot->data.raw.bytes,
2914 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002915 }
2916 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002918 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002919 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002920 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002922
Gilles Peskined911eb72018-08-14 15:18:45 +02002923 if( truncated == 0 )
2924 {
2925 /* The "normal" case: untruncated algorithm. Nothing to do. */
2926 }
2927 else if( truncated < 4 )
2928 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002929 /* A very short MAC is too short for security since it can be
2930 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2931 * so we make this our minimum, even though 32 bits is still
2932 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002933 status = PSA_ERROR_NOT_SUPPORTED;
2934 }
2935 else if( truncated > operation->mac_size )
2936 {
2937 /* It's impossible to "truncate" to a larger length. */
2938 status = PSA_ERROR_INVALID_ARGUMENT;
2939 }
2940 else
2941 operation->mac_size = truncated;
2942
Gilles Peskinefbfac682018-07-08 20:51:54 +02002943exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002944 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002946 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002948 else
2949 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950 operation->key_set = 1;
2951 }
2952 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953}
2954
Gilles Peskine89167cb2018-07-08 20:12:23 +02002955psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002956 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002957 psa_algorithm_t alg )
2958{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002959 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002960}
2961
2962psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002963 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002964 psa_algorithm_t alg )
2965{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002966 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002967}
2968
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2970 const uint8_t *input,
2971 size_t input_length )
2972{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002973 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002975 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002977 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978 operation->has_input = 1;
2979
Gilles Peskine8c9def32018-02-08 10:02:12 +01002980#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002981 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002982 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002983 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2984 input, input_length );
2985 status = mbedtls_to_psa_error( ret );
2986 }
2987 else
2988#endif /* MBEDTLS_CMAC_C */
2989#if defined(MBEDTLS_MD_C)
2990 if( PSA_ALG_IS_HMAC( operation->alg ) )
2991 {
2992 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2993 input_length );
2994 }
2995 else
2996#endif /* MBEDTLS_MD_C */
2997 {
2998 /* This shouldn't happen if `operation` was initialized by
2999 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003000 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003001 }
3002
Gilles Peskinefbfac682018-07-08 20:51:54 +02003003 if( status != PSA_SUCCESS )
3004 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003005 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006}
3007
Gilles Peskine01126fa2018-07-12 17:04:55 +02003008#if defined(MBEDTLS_MD_C)
3009static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3010 uint8_t *mac,
3011 size_t mac_size )
3012{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003013 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003014 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3015 size_t hash_size = 0;
3016 size_t block_size = psa_get_hash_block_size( hash_alg );
3017 psa_status_t status;
3018
Gilles Peskine01126fa2018-07-12 17:04:55 +02003019 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3020 if( status != PSA_SUCCESS )
3021 return( status );
3022 /* From here on, tmp needs to be wiped. */
3023
3024 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3025 if( status != PSA_SUCCESS )
3026 goto exit;
3027
3028 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3029 if( status != PSA_SUCCESS )
3030 goto exit;
3031
3032 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3033 if( status != PSA_SUCCESS )
3034 goto exit;
3035
Gilles Peskined911eb72018-08-14 15:18:45 +02003036 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3037 if( status != PSA_SUCCESS )
3038 goto exit;
3039
3040 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003041
3042exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003043 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003044 return( status );
3045}
3046#endif /* MBEDTLS_MD_C */
3047
mohammad16036df908f2018-04-02 08:34:15 -07003048static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003049 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003050 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003052 if( ! operation->key_set )
3053 return( PSA_ERROR_BAD_STATE );
3054 if( operation->iv_required && ! operation->iv_set )
3055 return( PSA_ERROR_BAD_STATE );
3056
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057 if( mac_size < operation->mac_size )
3058 return( PSA_ERROR_BUFFER_TOO_SMALL );
3059
Gilles Peskine8c9def32018-02-08 10:02:12 +01003060#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003061 if( operation->alg == PSA_ALG_CMAC )
3062 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003063 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3064 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3065 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003066 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003067 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003068 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003070 else
3071#endif /* MBEDTLS_CMAC_C */
3072#if defined(MBEDTLS_MD_C)
3073 if( PSA_ALG_IS_HMAC( operation->alg ) )
3074 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003075 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003076 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003077 }
3078 else
3079#endif /* MBEDTLS_MD_C */
3080 {
3081 /* This shouldn't happen if `operation` was initialized by
3082 * a setup function. */
3083 return( PSA_ERROR_BAD_STATE );
3084 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085}
3086
Gilles Peskineacd4be32018-07-08 19:56:25 +02003087psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3088 uint8_t *mac,
3089 size_t mac_size,
3090 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003091{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003092 psa_status_t status;
3093
Jaeden Amero252ef282019-02-15 14:05:35 +00003094 if( operation->alg == 0 )
3095 {
3096 return( PSA_ERROR_BAD_STATE );
3097 }
3098
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003099 /* Fill the output buffer with something that isn't a valid mac
3100 * (barring an attack on the mac and deliberately-crafted input),
3101 * in case the caller doesn't check the return status properly. */
3102 *mac_length = mac_size;
3103 /* If mac_size is 0 then mac may be NULL and then the
3104 * call to memset would have undefined behavior. */
3105 if( mac_size != 0 )
3106 memset( mac, '!', mac_size );
3107
Gilles Peskine89167cb2018-07-08 20:12:23 +02003108 if( ! operation->is_sign )
3109 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003110 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003111 }
mohammad16036df908f2018-04-02 08:34:15 -07003112
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003113 status = psa_mac_finish_internal( operation, mac, mac_size );
3114
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003115 if( status == PSA_SUCCESS )
3116 {
3117 status = psa_mac_abort( operation );
3118 if( status == PSA_SUCCESS )
3119 *mac_length = operation->mac_size;
3120 else
3121 memset( mac, '!', mac_size );
3122 }
3123 else
3124 psa_mac_abort( operation );
3125 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003126}
3127
Gilles Peskineacd4be32018-07-08 19:56:25 +02003128psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3129 const uint8_t *mac,
3130 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003131{
Gilles Peskine828ed142018-06-18 23:25:51 +02003132 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003133 psa_status_t status;
3134
Jaeden Amero252ef282019-02-15 14:05:35 +00003135 if( operation->alg == 0 )
3136 {
3137 return( PSA_ERROR_BAD_STATE );
3138 }
3139
Gilles Peskine89167cb2018-07-08 20:12:23 +02003140 if( operation->is_sign )
3141 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003142 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003143 }
3144 if( operation->mac_size != mac_length )
3145 {
3146 status = PSA_ERROR_INVALID_SIGNATURE;
3147 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003148 }
mohammad16036df908f2018-04-02 08:34:15 -07003149
3150 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003151 actual_mac, sizeof( actual_mac ) );
3152
3153 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3154 status = PSA_ERROR_INVALID_SIGNATURE;
3155
3156cleanup:
3157 if( status == PSA_SUCCESS )
3158 status = psa_mac_abort( operation );
3159 else
3160 psa_mac_abort( operation );
3161
Gilles Peskine3f108122018-12-07 18:14:53 +01003162 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003163
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003164 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165}
3166
3167
Gilles Peskine20035e32018-02-03 22:44:14 +01003168
Gilles Peskine20035e32018-02-03 22:44:14 +01003169/****************************************************************/
3170/* Asymmetric cryptography */
3171/****************************************************************/
3172
Gilles Peskine2b450e32018-06-27 15:42:46 +02003173#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003174/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003175 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003176static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3177 size_t hash_length,
3178 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003179{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003180 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003181 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003182 *md_alg = mbedtls_md_get_type( md_info );
3183
3184 /* The Mbed TLS RSA module uses an unsigned int for hash length
3185 * parameters. Validate that it fits so that we don't risk an
3186 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003187#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003188 if( hash_length > UINT_MAX )
3189 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003190#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003191
3192#if defined(MBEDTLS_PKCS1_V15)
3193 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3194 * must be correct. */
3195 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3196 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003197 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003198 if( md_info == NULL )
3199 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003200 if( mbedtls_md_get_size( md_info ) != hash_length )
3201 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003202 }
3203#endif /* MBEDTLS_PKCS1_V15 */
3204
3205#if defined(MBEDTLS_PKCS1_V21)
3206 /* PSS requires a hash internally. */
3207 if( PSA_ALG_IS_RSA_PSS( alg ) )
3208 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003209 if( md_info == NULL )
3210 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003211 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003212#endif /* MBEDTLS_PKCS1_V21 */
3213
Gilles Peskine61b91d42018-06-08 16:09:36 +02003214 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003215}
3216
Gilles Peskine2b450e32018-06-27 15:42:46 +02003217static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3218 psa_algorithm_t alg,
3219 const uint8_t *hash,
3220 size_t hash_length,
3221 uint8_t *signature,
3222 size_t signature_size,
3223 size_t *signature_length )
3224{
3225 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003227 mbedtls_md_type_t md_alg;
3228
3229 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3230 if( status != PSA_SUCCESS )
3231 return( status );
3232
Gilles Peskine630a18a2018-06-29 17:49:35 +02003233 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003234 return( PSA_ERROR_BUFFER_TOO_SMALL );
3235
3236#if defined(MBEDTLS_PKCS1_V15)
3237 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3238 {
3239 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3240 MBEDTLS_MD_NONE );
3241 ret = mbedtls_rsa_pkcs1_sign( rsa,
3242 mbedtls_ctr_drbg_random,
3243 &global_data.ctr_drbg,
3244 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003245 md_alg,
3246 (unsigned int) hash_length,
3247 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003248 signature );
3249 }
3250 else
3251#endif /* MBEDTLS_PKCS1_V15 */
3252#if defined(MBEDTLS_PKCS1_V21)
3253 if( PSA_ALG_IS_RSA_PSS( alg ) )
3254 {
3255 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3256 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3257 mbedtls_ctr_drbg_random,
3258 &global_data.ctr_drbg,
3259 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003260 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003261 (unsigned int) hash_length,
3262 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003263 signature );
3264 }
3265 else
3266#endif /* MBEDTLS_PKCS1_V21 */
3267 {
3268 return( PSA_ERROR_INVALID_ARGUMENT );
3269 }
3270
3271 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003272 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003273 return( mbedtls_to_psa_error( ret ) );
3274}
3275
3276static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3277 psa_algorithm_t alg,
3278 const uint8_t *hash,
3279 size_t hash_length,
3280 const uint8_t *signature,
3281 size_t signature_length )
3282{
3283 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003285 mbedtls_md_type_t md_alg;
3286
3287 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3288 if( status != PSA_SUCCESS )
3289 return( status );
3290
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003291 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003293
3294#if defined(MBEDTLS_PKCS1_V15)
3295 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3296 {
3297 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3298 MBEDTLS_MD_NONE );
3299 ret = mbedtls_rsa_pkcs1_verify( rsa,
3300 mbedtls_ctr_drbg_random,
3301 &global_data.ctr_drbg,
3302 MBEDTLS_RSA_PUBLIC,
3303 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003304 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003305 hash,
3306 signature );
3307 }
3308 else
3309#endif /* MBEDTLS_PKCS1_V15 */
3310#if defined(MBEDTLS_PKCS1_V21)
3311 if( PSA_ALG_IS_RSA_PSS( alg ) )
3312 {
3313 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3314 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3315 mbedtls_ctr_drbg_random,
3316 &global_data.ctr_drbg,
3317 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003318 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003319 (unsigned int) hash_length,
3320 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003321 signature );
3322 }
3323 else
3324#endif /* MBEDTLS_PKCS1_V21 */
3325 {
3326 return( PSA_ERROR_INVALID_ARGUMENT );
3327 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003328
3329 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3330 * the rest of the signature is invalid". This has little use in
3331 * practice and PSA doesn't report this distinction. */
3332 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3333 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003334 return( mbedtls_to_psa_error( ret ) );
3335}
3336#endif /* MBEDTLS_RSA_C */
3337
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003338#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003339/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3340 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3341 * (even though these functions don't modify it). */
3342static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3343 psa_algorithm_t alg,
3344 const uint8_t *hash,
3345 size_t hash_length,
3346 uint8_t *signature,
3347 size_t signature_size,
3348 size_t *signature_length )
3349{
Janos Follath24eed8d2019-11-22 13:21:35 +00003350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003351 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003352 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003353 mbedtls_mpi_init( &r );
3354 mbedtls_mpi_init( &s );
3355
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003356 if( signature_size < 2 * curve_bytes )
3357 {
3358 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3359 goto cleanup;
3360 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003361
Gilles Peskinea05219c2018-11-16 16:02:56 +01003362#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003363 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3364 {
3365 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3366 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3367 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003368 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3369 &ecp->d, hash,
3370 hash_length, md_alg,
3371 mbedtls_ctr_drbg_random,
3372 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003373 }
3374 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003375#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003376 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003377 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3379 hash, hash_length,
3380 mbedtls_ctr_drbg_random,
3381 &global_data.ctr_drbg ) );
3382 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003383
3384 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3385 signature,
3386 curve_bytes ) );
3387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3388 signature + curve_bytes,
3389 curve_bytes ) );
3390
3391cleanup:
3392 mbedtls_mpi_free( &r );
3393 mbedtls_mpi_free( &s );
3394 if( ret == 0 )
3395 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003396 return( mbedtls_to_psa_error( ret ) );
3397}
3398
3399static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3400 const uint8_t *hash,
3401 size_t hash_length,
3402 const uint8_t *signature,
3403 size_t signature_length )
3404{
Janos Follath24eed8d2019-11-22 13:21:35 +00003405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003406 mbedtls_mpi r, s;
3407 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3408 mbedtls_mpi_init( &r );
3409 mbedtls_mpi_init( &s );
3410
3411 if( signature_length != 2 * curve_bytes )
3412 return( PSA_ERROR_INVALID_SIGNATURE );
3413
3414 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3415 signature,
3416 curve_bytes ) );
3417 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3418 signature + curve_bytes,
3419 curve_bytes ) );
3420
3421 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3422 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003423
3424cleanup:
3425 mbedtls_mpi_free( &r );
3426 mbedtls_mpi_free( &s );
3427 return( mbedtls_to_psa_error( ret ) );
3428}
3429#endif /* MBEDTLS_ECDSA_C */
3430
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003431psa_status_t psa_sign_hash( psa_key_handle_t handle,
3432 psa_algorithm_t alg,
3433 const uint8_t *hash,
3434 size_t hash_length,
3435 uint8_t *signature,
3436 size_t signature_size,
3437 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003438{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003439 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003440 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3442 const psa_drv_se_t *drv;
3443 psa_drv_se_context_t *drv_context;
3444#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003445
3446 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003447 /* Immediately reject a zero-length signature buffer. This guarantees
3448 * that signature must be a valid pointer. (On the other hand, the hash
3449 * buffer can in principle be empty since it doesn't actually have
3450 * to be a hash.) */
3451 if( signature_size == 0 )
3452 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003453
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003454 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003455 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003456 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003457 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003458 {
3459 status = PSA_ERROR_INVALID_ARGUMENT;
3460 goto exit;
3461 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003462
Gilles Peskineedc64242019-08-07 21:05:07 +02003463#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3464 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3465 {
3466 if( drv->asymmetric == NULL ||
3467 drv->asymmetric->p_sign == NULL )
3468 {
3469 status = PSA_ERROR_NOT_SUPPORTED;
3470 goto exit;
3471 }
3472 status = drv->asymmetric->p_sign( drv_context,
3473 slot->data.se.slot_number,
3474 alg,
3475 hash, hash_length,
3476 signature, signature_size,
3477 signature_length );
3478 }
3479 else
3480#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003481#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003482 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003483 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003484 status = psa_rsa_sign( slot->data.rsa,
3485 alg,
3486 hash, hash_length,
3487 signature, signature_size,
3488 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003489 }
3490 else
3491#endif /* defined(MBEDTLS_RSA_C) */
3492#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003493 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003494 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003495#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003496 if(
3497#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3498 PSA_ALG_IS_ECDSA( alg )
3499#else
3500 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3501#endif
3502 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003503 status = psa_ecdsa_sign( slot->data.ecp,
3504 alg,
3505 hash, hash_length,
3506 signature, signature_size,
3507 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003508 else
3509#endif /* defined(MBEDTLS_ECDSA_C) */
3510 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003511 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003512 }
itayzafrir5c753392018-05-08 11:18:38 +03003513 }
3514 else
3515#endif /* defined(MBEDTLS_ECP_C) */
3516 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003517 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003518 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003519
3520exit:
3521 /* Fill the unused part of the output buffer (the whole buffer on error,
3522 * the trailing part on success) with something that isn't a valid mac
3523 * (barring an attack on the mac and deliberately-crafted input),
3524 * in case the caller doesn't check the return status properly. */
3525 if( status == PSA_SUCCESS )
3526 memset( signature + *signature_length, '!',
3527 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003528 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003529 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003530 /* If signature_size is 0 then we have nothing to do. We must not call
3531 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003532 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003533}
3534
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003535psa_status_t psa_verify_hash( psa_key_handle_t handle,
3536 psa_algorithm_t alg,
3537 const uint8_t *hash,
3538 size_t hash_length,
3539 const uint8_t *signature,
3540 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003541{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003542 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003543 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003544#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3545 const psa_drv_se_t *drv;
3546 psa_drv_se_context_t *drv_context;
3547#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003548
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003549 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003550 if( status != PSA_SUCCESS )
3551 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003552
Gilles Peskineedc64242019-08-07 21:05:07 +02003553#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3554 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3555 {
3556 if( drv->asymmetric == NULL ||
3557 drv->asymmetric->p_verify == NULL )
3558 return( PSA_ERROR_NOT_SUPPORTED );
3559 return( drv->asymmetric->p_verify( drv_context,
3560 slot->data.se.slot_number,
3561 alg,
3562 hash, hash_length,
3563 signature, signature_length ) );
3564 }
3565 else
3566#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003567#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003568 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003570 return( psa_rsa_verify( slot->data.rsa,
3571 alg,
3572 hash, hash_length,
3573 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003574 }
3575 else
3576#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003577#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003578 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003579 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003580#if defined(MBEDTLS_ECDSA_C)
3581 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003582 return( psa_ecdsa_verify( slot->data.ecp,
3583 hash, hash_length,
3584 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003585 else
3586#endif /* defined(MBEDTLS_ECDSA_C) */
3587 {
3588 return( PSA_ERROR_INVALID_ARGUMENT );
3589 }
itayzafrir5c753392018-05-08 11:18:38 +03003590 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003591 else
3592#endif /* defined(MBEDTLS_ECP_C) */
3593 {
3594 return( PSA_ERROR_NOT_SUPPORTED );
3595 }
3596}
3597
Gilles Peskine072ac562018-06-30 00:21:29 +02003598#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3599static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3600 mbedtls_rsa_context *rsa )
3601{
3602 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3603 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3604 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3605 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3606}
3607#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3608
Gilles Peskinec5487a82018-12-03 18:08:14 +01003609psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003610 psa_algorithm_t alg,
3611 const uint8_t *input,
3612 size_t input_length,
3613 const uint8_t *salt,
3614 size_t salt_length,
3615 uint8_t *output,
3616 size_t output_size,
3617 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003619 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003620 psa_status_t status;
3621
Darryl Green5cc689a2018-07-24 15:34:10 +01003622 (void) input;
3623 (void) input_length;
3624 (void) salt;
3625 (void) output;
3626 (void) output_size;
3627
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003628 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003630 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3631 return( PSA_ERROR_INVALID_ARGUMENT );
3632
Gilles Peskine28f8f302019-07-24 13:30:31 +02003633 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003634 if( status != PSA_SUCCESS )
3635 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003636 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3637 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639
3640#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003641 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 {
3643 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003645 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003646 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003648 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003650 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3651 mbedtls_ctr_drbg_random,
3652 &global_data.ctr_drbg,
3653 MBEDTLS_RSA_PUBLIC,
3654 input_length,
3655 input,
3656 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003657 }
3658 else
3659#endif /* MBEDTLS_PKCS1_V15 */
3660#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003661 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003663 psa_rsa_oaep_set_padding_mode( alg, rsa );
3664 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3665 mbedtls_ctr_drbg_random,
3666 &global_data.ctr_drbg,
3667 MBEDTLS_RSA_PUBLIC,
3668 salt, salt_length,
3669 input_length,
3670 input,
3671 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672 }
3673 else
3674#endif /* MBEDTLS_PKCS1_V21 */
3675 {
3676 return( PSA_ERROR_INVALID_ARGUMENT );
3677 }
3678 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003679 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680 return( mbedtls_to_psa_error( ret ) );
3681 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003683#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684 {
3685 return( PSA_ERROR_NOT_SUPPORTED );
3686 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003687}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003688
Gilles Peskinec5487a82018-12-03 18:08:14 +01003689psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003690 psa_algorithm_t alg,
3691 const uint8_t *input,
3692 size_t input_length,
3693 const uint8_t *salt,
3694 size_t salt_length,
3695 uint8_t *output,
3696 size_t output_size,
3697 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003699 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003700 psa_status_t status;
3701
Darryl Green5cc689a2018-07-24 15:34:10 +01003702 (void) input;
3703 (void) input_length;
3704 (void) salt;
3705 (void) output;
3706 (void) output_size;
3707
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003708 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003709
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003710 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3711 return( PSA_ERROR_INVALID_ARGUMENT );
3712
Gilles Peskine28f8f302019-07-24 13:30:31 +02003713 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003714 if( status != PSA_SUCCESS )
3715 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003716 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 return( PSA_ERROR_INVALID_ARGUMENT );
3718
3719#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003720 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721 {
3722 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724
Gilles Peskine630a18a2018-06-29 17:49:35 +02003725 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726 return( PSA_ERROR_INVALID_ARGUMENT );
3727
3728#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003729 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003731 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3732 mbedtls_ctr_drbg_random,
3733 &global_data.ctr_drbg,
3734 MBEDTLS_RSA_PRIVATE,
3735 output_length,
3736 input,
3737 output,
3738 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003739 }
3740 else
3741#endif /* MBEDTLS_PKCS1_V15 */
3742#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003743 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003745 psa_rsa_oaep_set_padding_mode( alg, rsa );
3746 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3747 mbedtls_ctr_drbg_random,
3748 &global_data.ctr_drbg,
3749 MBEDTLS_RSA_PRIVATE,
3750 salt, salt_length,
3751 output_length,
3752 input,
3753 output,
3754 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003755 }
3756 else
3757#endif /* MBEDTLS_PKCS1_V21 */
3758 {
3759 return( PSA_ERROR_INVALID_ARGUMENT );
3760 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003761
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003762 return( mbedtls_to_psa_error( ret ) );
3763 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003764 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003765#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766 {
3767 return( PSA_ERROR_NOT_SUPPORTED );
3768 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003769}
Gilles Peskine20035e32018-02-03 22:44:14 +01003770
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003771
3772
mohammad1603503973b2018-03-12 15:59:30 +02003773/****************************************************************/
3774/* Symmetric cryptography */
3775/****************************************************************/
3776
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003777/* Initialize the cipher operation structure. Once this function has been
3778 * called, psa_cipher_abort can run and will do the right thing. */
3779static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3780 psa_algorithm_t alg )
3781{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003782 if( ! PSA_ALG_IS_CIPHER( alg ) )
3783 {
3784 memset( operation, 0, sizeof( *operation ) );
3785 return( PSA_ERROR_INVALID_ARGUMENT );
3786 }
3787
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003788 operation->alg = alg;
3789 operation->key_set = 0;
3790 operation->iv_set = 0;
3791 operation->iv_required = 1;
3792 operation->iv_size = 0;
3793 operation->block_size = 0;
3794 mbedtls_cipher_init( &operation->ctx.cipher );
3795 return( PSA_SUCCESS );
3796}
3797
Gilles Peskinee553c652018-06-04 16:22:46 +02003798static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003799 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003800 psa_algorithm_t alg,
3801 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003802{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003803 int ret = 0;
3804 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003805 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003806 size_t key_bits;
3807 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003808 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3809 PSA_KEY_USAGE_ENCRYPT :
3810 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003811
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003812 /* A context must be freshly initialized before it can be set up. */
3813 if( operation->alg != 0 )
3814 {
3815 return( PSA_ERROR_BAD_STATE );
3816 }
3817
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003818 status = psa_cipher_init( operation, alg );
3819 if( status != PSA_SUCCESS )
3820 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003821
Gilles Peskine28f8f302019-07-24 13:30:31 +02003822 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003823 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003824 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003825 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003826
Gilles Peskine8e338702019-07-30 20:06:31 +02003827 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003828 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003829 {
3830 status = PSA_ERROR_NOT_SUPPORTED;
3831 goto exit;
3832 }
mohammad1603503973b2018-03-12 15:59:30 +02003833
mohammad1603503973b2018-03-12 15:59:30 +02003834 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003835 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003836 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003837
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003838#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003839 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003840 {
3841 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003842 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003843 memcpy( keys, slot->data.raw.data, 16 );
3844 memcpy( keys + 16, slot->data.raw.data, 8 );
3845 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3846 keys,
3847 192, cipher_operation );
3848 }
3849 else
3850#endif
3851 {
3852 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3853 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003854 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003855 }
Moran Peker41deec42018-04-04 15:43:05 +03003856 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003857 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003858
mohammad16038481e742018-03-18 13:57:31 +02003859#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003860 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003861 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003862 case PSA_ALG_CBC_NO_PADDING:
3863 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3864 MBEDTLS_PADDING_NONE );
3865 break;
3866 case PSA_ALG_CBC_PKCS7:
3867 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3868 MBEDTLS_PADDING_PKCS7 );
3869 break;
3870 default:
3871 /* The algorithm doesn't involve padding. */
3872 ret = 0;
3873 break;
3874 }
3875 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003876 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003877#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3878
mohammad1603503973b2018-03-12 15:59:30 +02003879 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003880 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003881 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003882 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003883 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003884 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003885 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003886#if defined(MBEDTLS_CHACHA20_C)
3887 else
3888 if( alg == PSA_ALG_CHACHA20 )
3889 operation->iv_size = 12;
3890#endif
mohammad1603503973b2018-03-12 15:59:30 +02003891
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003892exit:
3893 if( status == 0 )
3894 status = mbedtls_to_psa_error( ret );
3895 if( status != 0 )
3896 psa_cipher_abort( operation );
3897 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003898}
3899
Gilles Peskinefe119512018-07-08 21:39:34 +02003900psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003901 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003902 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003903{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003904 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003905}
3906
Gilles Peskinefe119512018-07-08 21:39:34 +02003907psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003908 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003909 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003910{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003911 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003912}
3913
Gilles Peskinefe119512018-07-08 21:39:34 +02003914psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003915 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003916 size_t iv_size,
3917 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003918{
itayzafrir534bd7c2018-08-02 13:56:32 +03003919 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003921 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003923 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 }
Moran Peker41deec42018-04-04 15:43:05 +03003925 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003926 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003928 goto exit;
3929 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003930 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3931 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003932 if( ret != 0 )
3933 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003934 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003935 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003936 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003937
mohammad16038481e742018-03-18 13:57:31 +02003938 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003939 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003940
Moran Peker395db872018-05-31 14:07:14 +03003941exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003942 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003943 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003944 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003945}
3946
Gilles Peskinefe119512018-07-08 21:39:34 +02003947psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003948 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003949 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003950{
itayzafrir534bd7c2018-08-02 13:56:32 +03003951 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003953 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003954 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003955 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003956 }
Moran Pekera28258c2018-05-29 16:25:04 +03003957 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003958 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003959 status = PSA_ERROR_INVALID_ARGUMENT;
3960 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003961 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003962 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3963 status = mbedtls_to_psa_error( ret );
3964exit:
3965 if( status == PSA_SUCCESS )
3966 operation->iv_set = 1;
3967 else
mohammad1603503973b2018-03-12 15:59:30 +02003968 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003969 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003970}
3971
Gilles Peskinee553c652018-06-04 16:22:46 +02003972psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3973 const uint8_t *input,
3974 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003975 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003976 size_t output_size,
3977 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003978{
itayzafrir534bd7c2018-08-02 13:56:32 +03003979 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003980 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003981 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003982
3983 if( operation->alg == 0 )
3984 {
3985 return( PSA_ERROR_BAD_STATE );
3986 }
3987
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003988 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003989 {
3990 /* Take the unprocessed partial block left over from previous
3991 * update calls, if any, plus the input to this call. Remove
3992 * the last partial block, if any. You get the data that will be
3993 * output in this call. */
3994 expected_output_size =
3995 ( operation->ctx.cipher.unprocessed_len + input_length )
3996 / operation->block_size * operation->block_size;
3997 }
3998 else
3999 {
4000 expected_output_size = input_length;
4001 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004002
Gilles Peskine89d789c2018-06-04 17:17:16 +02004003 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004004 {
4005 status = PSA_ERROR_BUFFER_TOO_SMALL;
4006 goto exit;
4007 }
mohammad160382759612018-03-12 18:16:40 +02004008
mohammad1603503973b2018-03-12 15:59:30 +02004009 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004010 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004011 status = mbedtls_to_psa_error( ret );
4012exit:
4013 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004014 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004015 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004016}
4017
Gilles Peskinee553c652018-06-04 16:22:46 +02004018psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4019 uint8_t *output,
4020 size_t output_size,
4021 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004022{
David Saadab4ecc272019-02-14 13:48:10 +02004023 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004024 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004025 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004026
mohammad1603503973b2018-03-12 15:59:30 +02004027 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004028 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004029 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004030 }
4031 if( operation->iv_required && ! operation->iv_set )
4032 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004033 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004034 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004035
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004036 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004037 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4038 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004039 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004040 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004041 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004042 }
4043
Janos Follath315b51c2018-07-09 16:04:51 +01004044 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4045 temp_output_buffer,
4046 output_length );
4047 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004048 {
Janos Follath315b51c2018-07-09 16:04:51 +01004049 status = mbedtls_to_psa_error( cipher_ret );
4050 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004051 }
Janos Follath315b51c2018-07-09 16:04:51 +01004052
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004053 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004054 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004055 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004056 memcpy( output, temp_output_buffer, *output_length );
4057 else
4058 {
Janos Follath315b51c2018-07-09 16:04:51 +01004059 status = PSA_ERROR_BUFFER_TOO_SMALL;
4060 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004061 }
mohammad1603503973b2018-03-12 15:59:30 +02004062
Gilles Peskine3f108122018-12-07 18:14:53 +01004063 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004064 status = psa_cipher_abort( operation );
4065
4066 return( status );
4067
4068error:
4069
4070 *output_length = 0;
4071
Gilles Peskine3f108122018-12-07 18:14:53 +01004072 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004073 (void) psa_cipher_abort( operation );
4074
4075 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004076}
4077
Gilles Peskinee553c652018-06-04 16:22:46 +02004078psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4079{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004080 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004081 {
4082 /* The object has (apparently) been initialized but it is not
4083 * in use. It's ok to call abort on such an object, and there's
4084 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004085 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004086 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004087
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004088 /* Sanity check (shouldn't happen: operation->alg should
4089 * always have been initialized to a valid value). */
4090 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4091 return( PSA_ERROR_BAD_STATE );
4092
mohammad1603503973b2018-03-12 15:59:30 +02004093 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004094
Moran Peker41deec42018-04-04 15:43:05 +03004095 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004096 operation->key_set = 0;
4097 operation->iv_set = 0;
4098 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004099 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004100 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004101
Moran Peker395db872018-05-31 14:07:14 +03004102 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004103}
4104
Gilles Peskinea0655c32018-04-30 17:06:50 +02004105
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004106
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004107
mohammad16035955c982018-04-26 00:53:03 +03004108/****************************************************************/
4109/* AEAD */
4110/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004111
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112typedef struct
4113{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004114 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115 const mbedtls_cipher_info_t *cipher_info;
4116 union
4117 {
4118#if defined(MBEDTLS_CCM_C)
4119 mbedtls_ccm_context ccm;
4120#endif /* MBEDTLS_CCM_C */
4121#if defined(MBEDTLS_GCM_C)
4122 mbedtls_gcm_context gcm;
4123#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004124#if defined(MBEDTLS_CHACHAPOLY_C)
4125 mbedtls_chachapoly_context chachapoly;
4126#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004128 psa_algorithm_t core_alg;
4129 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004130 uint8_t tag_length;
4131} aead_operation_t;
4132
Gilles Peskine30a9e412019-01-14 18:36:12 +01004133static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004134{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004135 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004136 {
4137#if defined(MBEDTLS_CCM_C)
4138 case PSA_ALG_CCM:
4139 mbedtls_ccm_free( &operation->ctx.ccm );
4140 break;
4141#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004142#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143 case PSA_ALG_GCM:
4144 mbedtls_gcm_free( &operation->ctx.gcm );
4145 break;
4146#endif /* MBEDTLS_GCM_C */
4147 }
4148}
4149
4150static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004151 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004152 psa_key_usage_t usage,
4153 psa_algorithm_t alg )
4154{
4155 psa_status_t status;
4156 size_t key_bits;
4157 mbedtls_cipher_id_t cipher_id;
4158
Gilles Peskine28f8f302019-07-24 13:30:31 +02004159 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004160 if( status != PSA_SUCCESS )
4161 return( status );
4162
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004163 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004164
4165 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004166 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 &cipher_id );
4168 if( operation->cipher_info == NULL )
4169 return( PSA_ERROR_NOT_SUPPORTED );
4170
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004171 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004172 {
4173#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004174 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4175 operation->core_alg = PSA_ALG_CCM;
4176 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004177 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4178 * The call to mbedtls_ccm_encrypt_and_tag or
4179 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004180 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004181 return( PSA_ERROR_INVALID_ARGUMENT );
4182 mbedtls_ccm_init( &operation->ctx.ccm );
4183 status = mbedtls_to_psa_error(
4184 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4185 operation->slot->data.raw.data,
4186 (unsigned int) key_bits ) );
4187 if( status != 0 )
4188 goto cleanup;
4189 break;
4190#endif /* MBEDTLS_CCM_C */
4191
4192#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004193 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4194 operation->core_alg = PSA_ALG_GCM;
4195 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004196 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4197 * The call to mbedtls_gcm_crypt_and_tag or
4198 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004199 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004200 return( PSA_ERROR_INVALID_ARGUMENT );
4201 mbedtls_gcm_init( &operation->ctx.gcm );
4202 status = mbedtls_to_psa_error(
4203 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4204 operation->slot->data.raw.data,
4205 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004206 if( status != 0 )
4207 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208 break;
4209#endif /* MBEDTLS_GCM_C */
4210
Gilles Peskine26869f22019-05-06 15:25:00 +02004211#if defined(MBEDTLS_CHACHAPOLY_C)
4212 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4213 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4214 operation->full_tag_length = 16;
4215 /* We only support the default tag length. */
4216 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4217 return( PSA_ERROR_NOT_SUPPORTED );
4218 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4219 status = mbedtls_to_psa_error(
4220 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4221 operation->slot->data.raw.data ) );
4222 if( status != 0 )
4223 goto cleanup;
4224 break;
4225#endif /* MBEDTLS_CHACHAPOLY_C */
4226
Gilles Peskineedf9a652018-08-17 18:11:56 +02004227 default:
4228 return( PSA_ERROR_NOT_SUPPORTED );
4229 }
4230
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004231 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4232 {
4233 status = PSA_ERROR_INVALID_ARGUMENT;
4234 goto cleanup;
4235 }
4236 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004237
Gilles Peskineedf9a652018-08-17 18:11:56 +02004238 return( PSA_SUCCESS );
4239
4240cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004241 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004242 return( status );
4243}
4244
Gilles Peskinec5487a82018-12-03 18:08:14 +01004245psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004246 psa_algorithm_t alg,
4247 const uint8_t *nonce,
4248 size_t nonce_length,
4249 const uint8_t *additional_data,
4250 size_t additional_data_length,
4251 const uint8_t *plaintext,
4252 size_t plaintext_length,
4253 uint8_t *ciphertext,
4254 size_t ciphertext_size,
4255 size_t *ciphertext_length )
4256{
mohammad16035955c982018-04-26 00:53:03 +03004257 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004258 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004259 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004260
mohammad1603f08a5502018-06-03 15:05:47 +03004261 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004262
Gilles Peskinec5487a82018-12-03 18:08:14 +01004263 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004264 if( status != PSA_SUCCESS )
4265 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004266
Gilles Peskineedf9a652018-08-17 18:11:56 +02004267 /* For all currently supported modes, the tag is at the end of the
4268 * ciphertext. */
4269 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4270 {
4271 status = PSA_ERROR_BUFFER_TOO_SMALL;
4272 goto exit;
4273 }
4274 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004275
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004276#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004277 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004278 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004279 status = mbedtls_to_psa_error(
4280 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4281 MBEDTLS_GCM_ENCRYPT,
4282 plaintext_length,
4283 nonce, nonce_length,
4284 additional_data, additional_data_length,
4285 plaintext, ciphertext,
4286 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004287 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004288 else
4289#endif /* MBEDTLS_GCM_C */
4290#if defined(MBEDTLS_CCM_C)
4291 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004292 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004293 status = mbedtls_to_psa_error(
4294 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4295 plaintext_length,
4296 nonce, nonce_length,
4297 additional_data,
4298 additional_data_length,
4299 plaintext, ciphertext,
4300 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004301 }
mohammad16035c8845f2018-05-09 05:40:09 -07004302 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004303#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004304#if defined(MBEDTLS_CHACHAPOLY_C)
4305 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4306 {
4307 if( nonce_length != 12 || operation.tag_length != 16 )
4308 {
4309 status = PSA_ERROR_NOT_SUPPORTED;
4310 goto exit;
4311 }
4312 status = mbedtls_to_psa_error(
4313 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4314 plaintext_length,
4315 nonce,
4316 additional_data,
4317 additional_data_length,
4318 plaintext,
4319 ciphertext,
4320 tag ) );
4321 }
4322 else
4323#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004324 {
mohammad1603554faad2018-06-03 15:07:38 +03004325 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004326 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004327
Gilles Peskineedf9a652018-08-17 18:11:56 +02004328 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4329 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004330
Gilles Peskineedf9a652018-08-17 18:11:56 +02004331exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004332 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004333 if( status == PSA_SUCCESS )
4334 *ciphertext_length = plaintext_length + operation.tag_length;
4335 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004336}
4337
Gilles Peskineee652a32018-06-01 19:23:52 +02004338/* Locate the tag in a ciphertext buffer containing the encrypted data
4339 * followed by the tag. Return the length of the part preceding the tag in
4340 * *plaintext_length. This is the size of the plaintext in modes where
4341 * the encrypted data has the same size as the plaintext, such as
4342 * CCM and GCM. */
4343static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4344 const uint8_t *ciphertext,
4345 size_t ciphertext_length,
4346 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004347 const uint8_t **p_tag )
4348{
4349 size_t payload_length;
4350 if( tag_length > ciphertext_length )
4351 return( PSA_ERROR_INVALID_ARGUMENT );
4352 payload_length = ciphertext_length - tag_length;
4353 if( payload_length > plaintext_size )
4354 return( PSA_ERROR_BUFFER_TOO_SMALL );
4355 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004356 return( PSA_SUCCESS );
4357}
4358
Gilles Peskinec5487a82018-12-03 18:08:14 +01004359psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004360 psa_algorithm_t alg,
4361 const uint8_t *nonce,
4362 size_t nonce_length,
4363 const uint8_t *additional_data,
4364 size_t additional_data_length,
4365 const uint8_t *ciphertext,
4366 size_t ciphertext_length,
4367 uint8_t *plaintext,
4368 size_t plaintext_size,
4369 size_t *plaintext_length )
4370{
mohammad16035955c982018-04-26 00:53:03 +03004371 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004372 aead_operation_t operation;
4373 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004374
Gilles Peskineee652a32018-06-01 19:23:52 +02004375 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004376
Gilles Peskinec5487a82018-12-03 18:08:14 +01004377 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004378 if( status != PSA_SUCCESS )
4379 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004380
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004381 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4382 ciphertext, ciphertext_length,
4383 plaintext_size, &tag );
4384 if( status != PSA_SUCCESS )
4385 goto exit;
4386
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004387#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004388 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004389 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004390 status = mbedtls_to_psa_error(
4391 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4392 ciphertext_length - operation.tag_length,
4393 nonce, nonce_length,
4394 additional_data,
4395 additional_data_length,
4396 tag, operation.tag_length,
4397 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004398 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004399 else
4400#endif /* MBEDTLS_GCM_C */
4401#if defined(MBEDTLS_CCM_C)
4402 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004403 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404 status = mbedtls_to_psa_error(
4405 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4406 ciphertext_length - operation.tag_length,
4407 nonce, nonce_length,
4408 additional_data,
4409 additional_data_length,
4410 ciphertext, plaintext,
4411 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004412 }
mohammad160339574652018-06-01 04:39:53 -07004413 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004414#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004415#if defined(MBEDTLS_CHACHAPOLY_C)
4416 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4417 {
4418 if( nonce_length != 12 || operation.tag_length != 16 )
4419 {
4420 status = PSA_ERROR_NOT_SUPPORTED;
4421 goto exit;
4422 }
4423 status = mbedtls_to_psa_error(
4424 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4425 ciphertext_length - operation.tag_length,
4426 nonce,
4427 additional_data,
4428 additional_data_length,
4429 tag,
4430 ciphertext,
4431 plaintext ) );
4432 }
4433 else
4434#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004435 {
mohammad1603554faad2018-06-03 15:07:38 +03004436 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004437 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004438
Gilles Peskineedf9a652018-08-17 18:11:56 +02004439 if( status != PSA_SUCCESS && plaintext_size != 0 )
4440 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004441
Gilles Peskineedf9a652018-08-17 18:11:56 +02004442exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004443 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004444 if( status == PSA_SUCCESS )
4445 *plaintext_length = ciphertext_length - operation.tag_length;
4446 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004447}
4448
Gilles Peskinea0655c32018-04-30 17:06:50 +02004449
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004450
Gilles Peskine20035e32018-02-03 22:44:14 +01004451/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004452/* Generators */
4453/****************************************************************/
4454
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004455#define HKDF_STATE_INIT 0 /* no input yet */
4456#define HKDF_STATE_STARTED 1 /* got salt */
4457#define HKDF_STATE_KEYED 2 /* got key */
4458#define HKDF_STATE_OUTPUT 3 /* output started */
4459
Gilles Peskinecbe66502019-05-16 16:59:18 +02004460static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004461 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004462{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004463 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4464 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004465 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004466 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004467}
4468
4469
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004470psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004471{
4472 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004474 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004475 {
4476 /* The object has (apparently) been initialized but it is not
4477 * in use. It's ok to call abort on such an object, and there's
4478 * nothing to do. */
4479 }
4480 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004481#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004482 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004483 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 mbedtls_free( operation->ctx.hkdf.info );
4485 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004486 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004487 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004489 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004490 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004491 if( operation->ctx.tls12_prf.seed != NULL )
4492 {
4493 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4494 operation->ctx.tls12_prf.seed_length );
4495 mbedtls_free( operation->ctx.tls12_prf.seed );
4496 }
4497
4498 if( operation->ctx.tls12_prf.label != NULL )
4499 {
4500 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4501 operation->ctx.tls12_prf.label_length );
4502 mbedtls_free( operation->ctx.tls12_prf.label );
4503 }
4504
4505 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4506
4507 /* We leave the fields Ai and output_block to be erased safely by the
4508 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004509 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004510 else
4511#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004512 {
4513 status = PSA_ERROR_BAD_STATE;
4514 }
Janos Follath083036a2019-06-11 10:22:26 +01004515 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004516 return( status );
4517}
4518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004520 size_t *capacity)
4521{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004523 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004525 return PSA_ERROR_BAD_STATE;
4526 }
4527
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004528 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004529 return( PSA_SUCCESS );
4530}
4531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004533 size_t capacity )
4534{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004536 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004538 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004540 return( PSA_SUCCESS );
4541}
4542
Gilles Peskinebef7f142018-07-12 17:22:21 +02004543#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004545 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004546static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004547 psa_algorithm_t hash_alg,
4548 uint8_t *output,
4549 size_t output_length )
4550{
4551 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4552 psa_status_t status;
4553
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004554 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4555 return( PSA_ERROR_BAD_STATE );
4556 hkdf->state = HKDF_STATE_OUTPUT;
4557
Gilles Peskinebef7f142018-07-12 17:22:21 +02004558 while( output_length != 0 )
4559 {
4560 /* Copy what remains of the current block */
4561 uint8_t n = hash_length - hkdf->offset_in_block;
4562 if( n > output_length )
4563 n = (uint8_t) output_length;
4564 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4565 output += n;
4566 output_length -= n;
4567 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004568 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004569 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004570 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004571 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004573 * object was corrupted or if this function is called directly
4574 * inside the library. */
4575 if( hkdf->block_number == 0xff )
4576 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004577
4578 /* We need a new block */
4579 ++hkdf->block_number;
4580 hkdf->offset_in_block = 0;
4581 status = psa_hmac_setup_internal( &hkdf->hmac,
4582 hkdf->prk, hash_length,
4583 hash_alg );
4584 if( status != PSA_SUCCESS )
4585 return( status );
4586 if( hkdf->block_number != 1 )
4587 {
4588 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4589 hkdf->output_block,
4590 hash_length );
4591 if( status != PSA_SUCCESS )
4592 return( status );
4593 }
4594 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4595 hkdf->info,
4596 hkdf->info_length );
4597 if( status != PSA_SUCCESS )
4598 return( status );
4599 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4600 &hkdf->block_number, 1 );
4601 if( status != PSA_SUCCESS )
4602 return( status );
4603 status = psa_hmac_finish_internal( &hkdf->hmac,
4604 hkdf->output_block,
4605 sizeof( hkdf->output_block ) );
4606 if( status != PSA_SUCCESS )
4607 return( status );
4608 }
4609
4610 return( PSA_SUCCESS );
4611}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004612
Janos Follath7742fee2019-06-17 12:58:10 +01004613static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4614 psa_tls12_prf_key_derivation_t *tls12_prf,
4615 psa_algorithm_t alg )
4616{
4617 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4618 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004619 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4620 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004621
Janos Follath7742fee2019-06-17 12:58:10 +01004622 /* We can't be wanting more output after block 0xff, otherwise
4623 * the capacity check in psa_key_derivation_output_bytes() would have
4624 * prevented this call. It could happen only if the operation
4625 * object was corrupted or if this function is called directly
4626 * inside the library. */
4627 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004628 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004629
4630 /* We need a new block */
4631 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004632 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004633
4634 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4635 *
4636 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4637 *
4638 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4639 * HMAC_hash(secret, A(2) + seed) +
4640 * HMAC_hash(secret, A(3) + seed) + ...
4641 *
4642 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004643 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004644 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004645 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004646 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004647 * is currently extracted as `output_block` and where i is
4648 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004649 */
4650
Janos Follathea29bfb2019-06-19 12:21:20 +01004651 /* Save the hash context before using it, to preserve the hash state with
4652 * only the inner padding in it. We need this, because inner padding depends
4653 * on the key (secret in the RFC's terminology). */
4654 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4655 if( status != PSA_SUCCESS )
4656 goto cleanup;
4657
4658 /* Calculate A(i) where i = tls12_prf->block_number. */
4659 if( tls12_prf->block_number == 1 )
4660 {
4661 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4662 * the variable seed and in this instance means it in the context of the
4663 * P_hash function, where seed = label + seed.) */
4664 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4665 tls12_prf->label, tls12_prf->label_length );
4666 if( status != PSA_SUCCESS )
4667 goto cleanup;
4668 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4669 tls12_prf->seed, tls12_prf->seed_length );
4670 if( status != PSA_SUCCESS )
4671 goto cleanup;
4672 }
4673 else
4674 {
4675 /* A(i) = HMAC_hash(secret, A(i-1)) */
4676 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4677 tls12_prf->Ai, hash_length );
4678 if( status != PSA_SUCCESS )
4679 goto cleanup;
4680 }
4681
4682 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4683 tls12_prf->Ai, hash_length );
4684 if( status != PSA_SUCCESS )
4685 goto cleanup;
4686 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4687 if( status != PSA_SUCCESS )
4688 goto cleanup;
4689
4690 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4691 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4692 tls12_prf->Ai, hash_length );
4693 if( status != PSA_SUCCESS )
4694 goto cleanup;
4695 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4696 tls12_prf->label, tls12_prf->label_length );
4697 if( status != PSA_SUCCESS )
4698 goto cleanup;
4699 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4700 tls12_prf->seed, tls12_prf->seed_length );
4701 if( status != PSA_SUCCESS )
4702 goto cleanup;
4703 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4704 tls12_prf->output_block, hash_length );
4705 if( status != PSA_SUCCESS )
4706 goto cleanup;
4707 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4708 if( status != PSA_SUCCESS )
4709 goto cleanup;
4710
Janos Follath7742fee2019-06-17 12:58:10 +01004711
4712cleanup:
4713
Janos Follathea29bfb2019-06-19 12:21:20 +01004714 cleanup_status = psa_hash_abort( &backup );
4715 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4716 status = cleanup_status;
4717
4718 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004719}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004720
Janos Follath844eb0e2019-06-19 12:10:49 +01004721static psa_status_t psa_key_derivation_tls12_prf_read(
4722 psa_tls12_prf_key_derivation_t *tls12_prf,
4723 psa_algorithm_t alg,
4724 uint8_t *output,
4725 size_t output_length )
4726{
4727 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4728 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4729 psa_status_t status;
4730 uint8_t offset, length;
4731
4732 while( output_length != 0 )
4733 {
4734 /* Check if we have fully processed the current block. */
4735 if( tls12_prf->left_in_block == 0 )
4736 {
4737 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4738 alg );
4739 if( status != PSA_SUCCESS )
4740 return( status );
4741
4742 continue;
4743 }
4744
4745 if( tls12_prf->left_in_block > output_length )
4746 length = (uint8_t) output_length;
4747 else
4748 length = tls12_prf->left_in_block;
4749
4750 offset = hash_length - tls12_prf->left_in_block;
4751 memcpy( output, tls12_prf->output_block + offset, length );
4752 output += length;
4753 output_length -= length;
4754 tls12_prf->left_in_block -= length;
4755 }
4756
4757 return( PSA_SUCCESS );
4758}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004759#endif /* MBEDTLS_MD_C */
4760
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004761psa_status_t psa_key_derivation_output_bytes(
4762 psa_key_derivation_operation_t *operation,
4763 uint8_t *output,
4764 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004765{
4766 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004768
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004769 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004770 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004772 return PSA_ERROR_BAD_STATE;
4773 }
4774
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004776 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004778 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004780 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004781 goto exit;
4782 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004784 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004785 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004786 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004787 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4788 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004791 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004794
Gilles Peskinebef7f142018-07-12 17:22:21 +02004795#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004796 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004797 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004798 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004800 output, output_length );
4801 }
Janos Follathadbec812019-06-14 11:05:39 +01004802 else
Janos Follathadbec812019-06-14 11:05:39 +01004803 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004804 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004805 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004807 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004808 output_length );
4809 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004810 else
4811#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 {
4813 return( PSA_ERROR_BAD_STATE );
4814 }
4815
4816exit:
4817 if( status != PSA_SUCCESS )
4818 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004819 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 * This allows us to differentiate between exhausted operations and
4821 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4822 * operations. */
4823 psa_algorithm_t alg = operation->alg;
4824 psa_key_derivation_abort( operation );
4825 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004826 memset( output, '!', output_length );
4827 }
4828 return( status );
4829}
4830
Gilles Peskine08542d82018-07-19 17:05:42 +02004831#if defined(MBEDTLS_DES_C)
4832static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4833{
4834 if( data_size >= 8 )
4835 mbedtls_des_key_set_parity( data );
4836 if( data_size >= 16 )
4837 mbedtls_des_key_set_parity( data + 8 );
4838 if( data_size >= 24 )
4839 mbedtls_des_key_set_parity( data + 16 );
4840}
4841#endif /* MBEDTLS_DES_C */
4842
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004843static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004844 psa_key_slot_t *slot,
4845 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004847{
4848 uint8_t *data = NULL;
4849 size_t bytes = PSA_BITS_TO_BYTES( bits );
4850 psa_status_t status;
4851
Gilles Peskine8e338702019-07-30 20:06:31 +02004852 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004853 return( PSA_ERROR_INVALID_ARGUMENT );
4854 if( bits % 8 != 0 )
4855 return( PSA_ERROR_INVALID_ARGUMENT );
4856 data = mbedtls_calloc( 1, bytes );
4857 if( data == NULL )
4858 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4859
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004861 if( status != PSA_SUCCESS )
4862 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004863#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004864 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004865 psa_des_set_key_parity( data, bytes );
4866#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004867 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004868
4869exit:
4870 mbedtls_free( data );
4871 return( status );
4872}
4873
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004874psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004876 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004877{
4878 psa_status_t status;
4879 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004880 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004881
4882 /* Reject any attempt to create a zero-length key so that we don't
4883 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4884 if( psa_get_key_bits( attributes ) == 0 )
4885 return( PSA_ERROR_INVALID_ARGUMENT );
4886
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004887 if( ! operation->can_output_key )
4888 return( PSA_ERROR_NOT_PERMITTED );
4889
Gilles Peskinedf179142019-07-15 22:02:14 +02004890 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4891 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004892#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4893 if( driver != NULL )
4894 {
4895 /* Deriving a key in a secure element is not implemented yet. */
4896 status = PSA_ERROR_NOT_SUPPORTED;
4897 }
4898#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004899 if( status == PSA_SUCCESS )
4900 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004901 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004902 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004904 }
4905 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004906 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004907 if( status != PSA_SUCCESS )
4908 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004909 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004910 *handle = 0;
4911 }
4912 return( status );
4913}
4914
Gilles Peskineeab56e42018-07-12 17:12:33 +02004915
4916
4917/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004918/* Key derivation */
4919/****************************************************************/
4920
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004922 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004923 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004924{
Janos Follath5fe19732019-06-20 15:09:30 +01004925 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4926 * initialisers for this union leave some bytes unspecified.) */
4927 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4928
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004930#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4932 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4933 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004934 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004935 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004936 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4937 if( hash_size == 0 )
4938 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004939 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4940 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004941 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004942 {
4943 return( PSA_ERROR_NOT_SUPPORTED );
4944 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004946 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004947 }
4948#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004950 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004951}
4952
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954 psa_algorithm_t alg )
4955{
4956 psa_status_t status;
4957
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 return( PSA_ERROR_BAD_STATE );
4960
Gilles Peskine6843c292019-01-18 16:44:49 +01004961 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4962 return( PSA_ERROR_INVALID_ARGUMENT );
4963 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004964 {
4965 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004967 }
4968 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4969 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004971 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004972 else
4973 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974
4975 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004977 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004978}
4979
4980#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004981static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004982 psa_algorithm_t hash_alg,
4983 psa_key_derivation_step_t step,
4984 const uint8_t *data,
4985 size_t data_length )
4986{
4987 psa_status_t status;
4988 switch( step )
4989 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004990 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004991 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004992 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004993 status = psa_hmac_setup_internal( &hkdf->hmac,
4994 data, data_length,
4995 hash_alg );
4996 if( status != PSA_SUCCESS )
4997 return( status );
4998 hkdf->state = HKDF_STATE_STARTED;
4999 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005000 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005001 /* If no salt was provided, use an empty salt. */
5002 if( hkdf->state == HKDF_STATE_INIT )
5003 {
5004 status = psa_hmac_setup_internal( &hkdf->hmac,
5005 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005006 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005007 if( status != PSA_SUCCESS )
5008 return( status );
5009 hkdf->state = HKDF_STATE_STARTED;
5010 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005011 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005013 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5014 data, data_length );
5015 if( status != PSA_SUCCESS )
5016 return( status );
5017 status = psa_hmac_finish_internal( &hkdf->hmac,
5018 hkdf->prk,
5019 sizeof( hkdf->prk ) );
5020 if( status != PSA_SUCCESS )
5021 return( status );
5022 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5023 hkdf->block_number = 0;
5024 hkdf->state = HKDF_STATE_KEYED;
5025 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005026 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027 if( hkdf->state == HKDF_STATE_OUTPUT )
5028 return( PSA_ERROR_BAD_STATE );
5029 if( hkdf->info_set )
5030 return( PSA_ERROR_BAD_STATE );
5031 hkdf->info_length = data_length;
5032 if( data_length != 0 )
5033 {
5034 hkdf->info = mbedtls_calloc( 1, data_length );
5035 if( hkdf->info == NULL )
5036 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5037 memcpy( hkdf->info, data, data_length );
5038 }
5039 hkdf->info_set = 1;
5040 return( PSA_SUCCESS );
5041 default:
5042 return( PSA_ERROR_INVALID_ARGUMENT );
5043 }
5044}
Janos Follathb03233e2019-06-11 15:30:30 +01005045
Janos Follathf08e2652019-06-13 09:05:41 +01005046static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5047 const uint8_t *data,
5048 size_t data_length )
5049{
5050 if( prf->state != TLS12_PRF_STATE_INIT )
5051 return( PSA_ERROR_BAD_STATE );
5052
Janos Follathd6dce9f2019-07-04 09:11:38 +01005053 if( data_length != 0 )
5054 {
5055 prf->seed = mbedtls_calloc( 1, data_length );
5056 if( prf->seed == NULL )
5057 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005058
Janos Follathd6dce9f2019-07-04 09:11:38 +01005059 memcpy( prf->seed, data, data_length );
5060 prf->seed_length = data_length;
5061 }
Janos Follathf08e2652019-06-13 09:05:41 +01005062
5063 prf->state = TLS12_PRF_STATE_SEED_SET;
5064
5065 return( PSA_SUCCESS );
5066}
5067
Janos Follath81550542019-06-13 14:26:34 +01005068static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5069 psa_algorithm_t hash_alg,
5070 const uint8_t *data,
5071 size_t data_length )
5072{
5073 psa_status_t status;
5074 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5075 return( PSA_ERROR_BAD_STATE );
5076
5077 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5078 if( status != PSA_SUCCESS )
5079 return( status );
5080
5081 prf->state = TLS12_PRF_STATE_KEY_SET;
5082
5083 return( PSA_SUCCESS );
5084}
5085
Janos Follath6660f0e2019-06-17 08:44:03 +01005086static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5087 psa_tls12_prf_key_derivation_t *prf,
5088 psa_algorithm_t hash_alg,
5089 const uint8_t *data,
5090 size_t data_length )
5091{
5092 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005093 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5094 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005095
5096 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5097 return( PSA_ERROR_INVALID_ARGUMENT );
5098
5099 /* Quoting RFC 4279, Section 2:
5100 *
5101 * The premaster secret is formed as follows: if the PSK is N octets
5102 * long, concatenate a uint16 with the value N, N zero octets, a second
5103 * uint16 with the value N, and the PSK itself.
5104 */
5105
Janos Follath40e13932019-06-26 13:22:29 +01005106 *cur++ = ( data_length >> 8 ) & 0xff;
5107 *cur++ = ( data_length >> 0 ) & 0xff;
5108 memset( cur, 0, data_length );
5109 cur += data_length;
5110 *cur++ = pms[0];
5111 *cur++ = pms[1];
5112 memcpy( cur, data, data_length );
5113 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005114
Janos Follath40e13932019-06-26 13:22:29 +01005115 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005116
5117 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5118 return( status );
5119}
5120
Janos Follath63028dd2019-06-13 09:15:47 +01005121static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5122 const uint8_t *data,
5123 size_t data_length )
5124{
5125 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5126 return( PSA_ERROR_BAD_STATE );
5127
Janos Follathd6dce9f2019-07-04 09:11:38 +01005128 if( data_length != 0 )
5129 {
5130 prf->label = mbedtls_calloc( 1, data_length );
5131 if( prf->label == NULL )
5132 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005133
Janos Follathd6dce9f2019-07-04 09:11:38 +01005134 memcpy( prf->label, data, data_length );
5135 prf->label_length = data_length;
5136 }
Janos Follath63028dd2019-06-13 09:15:47 +01005137
5138 prf->state = TLS12_PRF_STATE_LABEL_SET;
5139
5140 return( PSA_SUCCESS );
5141}
5142
Janos Follathb03233e2019-06-11 15:30:30 +01005143static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5144 psa_algorithm_t hash_alg,
5145 psa_key_derivation_step_t step,
5146 const uint8_t *data,
5147 size_t data_length )
5148{
Janos Follathb03233e2019-06-11 15:30:30 +01005149 switch( step )
5150 {
Janos Follathf08e2652019-06-13 09:05:41 +01005151 case PSA_KEY_DERIVATION_INPUT_SEED:
5152 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005153 case PSA_KEY_DERIVATION_INPUT_SECRET:
5154 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005155 case PSA_KEY_DERIVATION_INPUT_LABEL:
5156 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005157 default:
5158 return( PSA_ERROR_INVALID_ARGUMENT );
5159 }
5160}
Janos Follath6660f0e2019-06-17 08:44:03 +01005161
5162static psa_status_t psa_tls12_prf_psk_to_ms_input(
5163 psa_tls12_prf_key_derivation_t *prf,
5164 psa_algorithm_t hash_alg,
5165 psa_key_derivation_step_t step,
5166 const uint8_t *data,
5167 size_t data_length )
5168{
5169 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005170 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005171 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5172 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005173 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005174
5175 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5176}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005177#endif /* MBEDTLS_MD_C */
5178
Gilles Peskineb8965192019-09-24 16:21:10 +02005179/** Check whether the given key type is acceptable for the given
5180 * input step of a key derivation.
5181 *
5182 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5183 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5184 * Both secret and non-secret inputs can alternatively have the type
5185 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5186 * that the input was passed as a buffer rather than via a key object.
5187 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005188static int psa_key_derivation_check_input_type(
5189 psa_key_derivation_step_t step,
5190 psa_key_type_t key_type )
5191{
5192 switch( step )
5193 {
5194 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005195 if( key_type == PSA_KEY_TYPE_DERIVE )
5196 return( PSA_SUCCESS );
5197 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005198 return( PSA_SUCCESS );
5199 break;
5200 case PSA_KEY_DERIVATION_INPUT_LABEL:
5201 case PSA_KEY_DERIVATION_INPUT_SALT:
5202 case PSA_KEY_DERIVATION_INPUT_INFO:
5203 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005204 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5205 return( PSA_SUCCESS );
5206 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005207 return( PSA_SUCCESS );
5208 break;
5209 }
5210 return( PSA_ERROR_INVALID_ARGUMENT );
5211}
5212
Janos Follathb80a94e2019-06-12 15:54:46 +01005213static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005215 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005216 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005217 const uint8_t *data,
5218 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005219{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005220 psa_status_t status;
5221 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5222
5223 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005224 if( status != PSA_SUCCESS )
5225 goto exit;
5226
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005228 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005231 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005232 step, data, data_length );
5233 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005234 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235 {
Janos Follathb03233e2019-06-11 15:30:30 +01005236 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5237 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5238 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005239 }
5240 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5241 {
5242 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5243 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5244 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005245 }
5246 else
5247#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005248 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250 return( PSA_ERROR_BAD_STATE );
5251 }
5252
Gilles Peskine224b0d62019-09-23 18:13:17 +02005253exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005254 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256 return( status );
5257}
5258
Janos Follath51f4a0f2019-06-14 11:35:55 +01005259psa_status_t psa_key_derivation_input_bytes(
5260 psa_key_derivation_operation_t *operation,
5261 psa_key_derivation_step_t step,
5262 const uint8_t *data,
5263 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005264{
Gilles Peskineb8965192019-09-24 16:21:10 +02005265 return( psa_key_derivation_input_internal( operation, step,
5266 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005267 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005268}
5269
Janos Follath51f4a0f2019-06-14 11:35:55 +01005270psa_status_t psa_key_derivation_input_key(
5271 psa_key_derivation_operation_t *operation,
5272 psa_key_derivation_step_t step,
5273 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005274{
5275 psa_key_slot_t *slot;
5276 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005277
Gilles Peskine28f8f302019-07-24 13:30:31 +02005278 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005279 PSA_KEY_USAGE_DERIVE,
5280 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005282 {
5283 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005284 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005285 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005286
5287 /* Passing a key object as a SECRET input unlocks the permission
5288 * to output to a key object. */
5289 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5290 operation->can_output_key = 1;
5291
Janos Follathb80a94e2019-06-12 15:54:46 +01005292 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005293 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005294 slot->data.raw.data,
5295 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005296}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005297
5298
5299
5300/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005301/* Key agreement */
5302/****************************************************************/
5303
Gilles Peskinea05219c2018-11-16 16:02:56 +01005304#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5306 size_t peer_key_length,
5307 const mbedtls_ecp_keypair *our_key,
5308 uint8_t *shared_secret,
5309 size_t shared_secret_size,
5310 size_t *shared_secret_length )
5311{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005312 mbedtls_ecp_keypair *their_key = NULL;
5313 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005314 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005315 size_t bits = 0;
5316 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005317 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005318
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005319 status = psa_import_ec_public_key( curve,
5320 peer_key, peer_key_length,
5321 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005322 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005323 goto exit;
5324
Jaeden Amero97271b32019-01-10 19:38:51 +00005325 status = mbedtls_to_psa_error(
5326 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5327 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005328 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005329 status = mbedtls_to_psa_error(
5330 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5331 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005332 goto exit;
5333
Jaeden Amero97271b32019-01-10 19:38:51 +00005334 status = mbedtls_to_psa_error(
5335 mbedtls_ecdh_calc_secret( &ecdh,
5336 shared_secret_length,
5337 shared_secret, shared_secret_size,
5338 mbedtls_ctr_drbg_random,
5339 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005340 if( status != PSA_SUCCESS )
5341 goto exit;
5342 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5343 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005344
5345exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005346 if( status != PSA_SUCCESS )
5347 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005348 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005349 mbedtls_ecp_keypair_free( their_key );
5350 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005351 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005352}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005353#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005354
Gilles Peskine01d718c2018-09-18 12:01:02 +02005355#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5356
Gilles Peskine0216fe12019-04-11 21:23:21 +02005357static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5358 psa_key_slot_t *private_key,
5359 const uint8_t *peer_key,
5360 size_t peer_key_length,
5361 uint8_t *shared_secret,
5362 size_t shared_secret_size,
5363 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005364{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005365 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005367#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005368 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005369 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005370 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005371 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5372 private_key->data.ecp,
5373 shared_secret, shared_secret_size,
5374 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005375#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005376 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005377 (void) private_key;
5378 (void) peer_key;
5379 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005380 (void) shared_secret;
5381 (void) shared_secret_size;
5382 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005383 return( PSA_ERROR_NOT_SUPPORTED );
5384 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005385}
5386
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005387/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005388 * to potentially free embedded data structures and wipe confidential data.
5389 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005391 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005392 psa_key_slot_t *private_key,
5393 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005394 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395{
5396 psa_status_t status;
5397 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5398 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005400
5401 /* Step 1: run the secret agreement algorithm to generate the shared
5402 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005403 status = psa_key_agreement_raw_internal( ka_alg,
5404 private_key,
5405 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005406 shared_secret,
5407 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005408 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005409 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005410 goto exit;
5411
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005412 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005413 * the shared secret. A shared secret is permitted wherever a key
5414 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005415 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005416 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005417 shared_secret,
5418 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005419
Gilles Peskine12313cd2018-06-20 00:20:32 +02005420exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005421 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005422 return( status );
5423}
5424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005425psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005426 psa_key_derivation_step_t step,
5427 psa_key_handle_t private_key,
5428 const uint8_t *peer_key,
5429 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005430{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005431 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005432 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005434 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005435 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005436 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005437 if( status != PSA_SUCCESS )
5438 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005440 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005441 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005442 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005444 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005445}
5446
Gilles Peskinebe697d82019-05-16 18:00:41 +02005447psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5448 psa_key_handle_t private_key,
5449 const uint8_t *peer_key,
5450 size_t peer_key_length,
5451 uint8_t *output,
5452 size_t output_size,
5453 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005454{
5455 psa_key_slot_t *slot;
5456 psa_status_t status;
5457
5458 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5459 {
5460 status = PSA_ERROR_INVALID_ARGUMENT;
5461 goto exit;
5462 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005463 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005464 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005465 if( status != PSA_SUCCESS )
5466 goto exit;
5467
5468 status = psa_key_agreement_raw_internal( alg, slot,
5469 peer_key, peer_key_length,
5470 output, output_size,
5471 output_length );
5472
5473exit:
5474 if( status != PSA_SUCCESS )
5475 {
5476 /* If an error happens and is not handled properly, the output
5477 * may be used as a key to protect sensitive data. Arrange for such
5478 * a key to be random, which is likely to result in decryption or
5479 * verification errors. This is better than filling the buffer with
5480 * some constant data such as zeros, which would result in the data
5481 * being protected with a reproducible, easily knowable key.
5482 */
5483 psa_generate_random( output, output_size );
5484 *output_length = output_size;
5485 }
5486 return( status );
5487}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005488
5489
5490/****************************************************************/
5491/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005492/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005493
Gilles Peskine4c317f42018-07-12 01:24:09 +02005494psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005495 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005496{
Janos Follath24eed8d2019-11-22 13:21:35 +00005497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005498 GUARD_MODULE_INITIALIZED;
5499
Gilles Peskinef181eca2019-08-07 13:49:00 +02005500 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5501 {
5502 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5503 output,
5504 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5505 if( ret != 0 )
5506 return( mbedtls_to_psa_error( ret ) );
5507 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5508 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5509 }
5510
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5512 return( mbedtls_to_psa_error( ret ) );
5513}
5514
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005515#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5516#include "mbedtls/entropy_poll.h"
5517
Gilles Peskine7228da22019-07-15 11:06:15 +02005518psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005519 size_t seed_size )
5520{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005521 if( global_data.initialized )
5522 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005523
5524 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5525 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5526 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5527 return( PSA_ERROR_INVALID_ARGUMENT );
5528
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005529 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005530}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005531#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005532
Gilles Peskinee56e8782019-04-26 17:34:02 +02005533#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5534static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5535 size_t domain_parameters_size,
5536 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005537{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005538 size_t i;
5539 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540
Gilles Peskinee56e8782019-04-26 17:34:02 +02005541 if( domain_parameters_size == 0 )
5542 {
5543 *exponent = 65537;
5544 return( PSA_SUCCESS );
5545 }
5546
5547 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5548 * support values that fit in a 32-bit integer, which is larger than
5549 * int on just about every platform anyway. */
5550 if( domain_parameters_size > sizeof( acc ) )
5551 return( PSA_ERROR_NOT_SUPPORTED );
5552 for( i = 0; i < domain_parameters_size; i++ )
5553 acc = ( acc << 8 ) | domain_parameters[i];
5554 if( acc > INT_MAX )
5555 return( PSA_ERROR_NOT_SUPPORTED );
5556 *exponent = acc;
5557 return( PSA_SUCCESS );
5558}
5559#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5560
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005561static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005562 psa_key_slot_t *slot, size_t bits,
5563 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005564{
Gilles Peskine8e338702019-07-30 20:06:31 +02005565 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005566
Gilles Peskinee56e8782019-04-26 17:34:02 +02005567 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005568 return( PSA_ERROR_INVALID_ARGUMENT );
5569
Gilles Peskinee59236f2018-01-27 23:32:46 +01005570 if( key_type_is_raw_bytes( type ) )
5571 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005572 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005573 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5574 if( status != PSA_SUCCESS )
5575 return( status );
5576 status = psa_generate_random( slot->data.raw.data,
5577 slot->data.raw.bytes );
5578 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005579 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005580#if defined(MBEDTLS_DES_C)
5581 if( type == PSA_KEY_TYPE_DES )
5582 psa_des_set_key_parity( slot->data.raw.data,
5583 slot->data.raw.bytes );
5584#endif /* MBEDTLS_DES_C */
5585 }
5586 else
5587
5588#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005589 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005590 {
5591 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005593 int exponent;
5594 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5596 return( PSA_ERROR_NOT_SUPPORTED );
5597 /* Accept only byte-aligned keys, for the same reasons as
5598 * in psa_import_rsa_key(). */
5599 if( bits % 8 != 0 )
5600 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005601 status = psa_read_rsa_exponent( domain_parameters,
5602 domain_parameters_size,
5603 &exponent );
5604 if( status != PSA_SUCCESS )
5605 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005606 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5607 if( rsa == NULL )
5608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5609 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5610 ret = mbedtls_rsa_gen_key( rsa,
5611 mbedtls_ctr_drbg_random,
5612 &global_data.ctr_drbg,
5613 (unsigned int) bits,
5614 exponent );
5615 if( ret != 0 )
5616 {
5617 mbedtls_rsa_free( rsa );
5618 mbedtls_free( rsa );
5619 return( mbedtls_to_psa_error( ret ) );
5620 }
5621 slot->data.rsa = rsa;
5622 }
5623 else
5624#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5625
5626#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005627 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005628 {
5629 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005630 mbedtls_ecp_group_id grp_id =
5631 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005632 const mbedtls_ecp_curve_info *curve_info =
5633 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5634 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005636 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005637 return( PSA_ERROR_NOT_SUPPORTED );
5638 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5639 return( PSA_ERROR_NOT_SUPPORTED );
5640 if( curve_info->bit_size != bits )
5641 return( PSA_ERROR_INVALID_ARGUMENT );
5642 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5643 if( ecp == NULL )
5644 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5645 mbedtls_ecp_keypair_init( ecp );
5646 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5647 mbedtls_ctr_drbg_random,
5648 &global_data.ctr_drbg );
5649 if( ret != 0 )
5650 {
5651 mbedtls_ecp_keypair_free( ecp );
5652 mbedtls_free( ecp );
5653 return( mbedtls_to_psa_error( ret ) );
5654 }
5655 slot->data.ecp = ecp;
5656 }
5657 else
5658#endif /* MBEDTLS_ECP_C */
5659
5660 return( PSA_ERROR_NOT_SUPPORTED );
5661
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 return( PSA_SUCCESS );
5663}
Darryl Green0c6575a2018-11-07 16:05:30 +00005664
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005665psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005666 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667{
5668 psa_status_t status;
5669 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005670 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005671
Gilles Peskine0f84d622019-09-12 19:03:13 +02005672 /* Reject any attempt to create a zero-length key so that we don't
5673 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5674 if( psa_get_key_bits( attributes ) == 0 )
5675 return( PSA_ERROR_INVALID_ARGUMENT );
5676
Gilles Peskinedf179142019-07-15 22:02:14 +02005677 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5678 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005679 if( status != PSA_SUCCESS )
5680 goto exit;
5681
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005682#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5683 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005684 {
Gilles Peskine11792082019-08-06 18:36:36 +02005685 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5686 size_t pubkey_length = 0; /* We don't support this feature yet */
5687 if( drv->key_management == NULL ||
5688 drv->key_management->p_generate == NULL )
5689 {
5690 status = PSA_ERROR_NOT_SUPPORTED;
5691 goto exit;
5692 }
5693 status = drv->key_management->p_generate(
5694 psa_get_se_driver_context( driver ),
5695 slot->data.se.slot_number, attributes,
5696 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005697 }
Gilles Peskine11792082019-08-06 18:36:36 +02005698 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005699#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005700 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005701 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005702 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005703 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005704 }
Gilles Peskine11792082019-08-06 18:36:36 +02005705
5706exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005708 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005709 if( status != PSA_SUCCESS )
5710 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005711 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005712 *handle = 0;
5713 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005714 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005715}
5716
5717
Gilles Peskinee59236f2018-01-27 23:32:46 +01005718
5719/****************************************************************/
5720/* Module setup */
5721/****************************************************************/
5722
Gilles Peskine5e769522018-11-20 21:59:56 +01005723psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5724 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5725 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5726{
5727 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5728 return( PSA_ERROR_BAD_STATE );
5729 global_data.entropy_init = entropy_init;
5730 global_data.entropy_free = entropy_free;
5731 return( PSA_SUCCESS );
5732}
5733
Gilles Peskinee59236f2018-01-27 23:32:46 +01005734void mbedtls_psa_crypto_free( void )
5735{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005736 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005737 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5738 {
5739 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005740 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005741 }
5742 /* Wipe all remaining data, including configuration.
5743 * In particular, this sets all state indicator to the value
5744 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005745 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005746#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005747 /* Unregister all secure element drivers, so that we restart from
5748 * a pristine state. */
5749 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005750#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005751}
5752
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005753#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5754/** Recover a transaction that was interrupted by a power failure.
5755 *
5756 * This function is called during initialization, before psa_crypto_init()
5757 * returns. If this function returns a failure status, the initialization
5758 * fails.
5759 */
5760static psa_status_t psa_crypto_recover_transaction(
5761 const psa_crypto_transaction_t *transaction )
5762{
5763 switch( transaction->unknown.type )
5764 {
5765 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5766 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005767 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005768 * is implemented.
5769 * https://github.com/ARMmbed/mbed-crypto/issues/218
5770 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005771 default:
5772 /* We found an unsupported transaction in the storage.
5773 * We don't know what state the storage is in. Give up. */
5774 return( PSA_ERROR_STORAGE_FAILURE );
5775 }
5776}
5777#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5778
Gilles Peskinee59236f2018-01-27 23:32:46 +01005779psa_status_t psa_crypto_init( void )
5780{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005781 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005782 const unsigned char drbg_seed[] = "PSA";
5783
Gilles Peskinec6b69072018-11-20 21:42:52 +01005784 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 if( global_data.initialized != 0 )
5786 return( PSA_SUCCESS );
5787
Gilles Peskine5e769522018-11-20 21:59:56 +01005788 /* Set default configuration if
5789 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5790 if( global_data.entropy_init == NULL )
5791 global_data.entropy_init = mbedtls_entropy_init;
5792 if( global_data.entropy_free == NULL )
5793 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005794
Gilles Peskinec6b69072018-11-20 21:42:52 +01005795 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005796 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005797#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5798 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5799 /* The PSA entropy injection feature depends on using NV seed as an entropy
5800 * source. Add NV seed as an entropy source for PSA entropy injection. */
5801 mbedtls_entropy_add_source( &global_data.entropy,
5802 mbedtls_nv_seed_poll, NULL,
5803 MBEDTLS_ENTROPY_BLOCK_SIZE,
5804 MBEDTLS_ENTROPY_SOURCE_STRONG );
5805#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005806 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005807 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005808 status = mbedtls_to_psa_error(
5809 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5810 mbedtls_entropy_func,
5811 &global_data.entropy,
5812 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5813 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005814 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005815 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005816
Gilles Peskine66fb1262018-12-10 16:29:04 +01005817 status = psa_initialize_key_slots( );
5818 if( status != PSA_SUCCESS )
5819 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005820
Gilles Peskined9348f22019-10-01 15:22:29 +02005821#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5822 status = psa_init_all_se_drivers( );
5823 if( status != PSA_SUCCESS )
5824 goto exit;
5825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5826
Gilles Peskine4b734222019-07-24 15:56:31 +02005827#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005828 status = psa_crypto_load_transaction( );
5829 if( status == PSA_SUCCESS )
5830 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005831 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5832 if( status != PSA_SUCCESS )
5833 goto exit;
5834 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005835 }
5836 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5837 {
5838 /* There's no transaction to complete. It's all good. */
5839 status = PSA_SUCCESS;
5840 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005841#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005842
Gilles Peskinec6b69072018-11-20 21:42:52 +01005843 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005844 global_data.initialized = 1;
5845
5846exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005847 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005848 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005849 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850}
5851
5852#endif /* MBEDTLS_PSA_CRYPTO_C */