blob: 733a2e46c4dc74d3802a9227c7a1fb2bf7a9e12c [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 Peskineda8191d2018-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 ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003152 if( status != PSA_SUCCESS )
3153 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003154
3155 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3156 status = PSA_ERROR_INVALID_SIGNATURE;
3157
3158cleanup:
3159 if( status == PSA_SUCCESS )
3160 status = psa_mac_abort( operation );
3161 else
3162 psa_mac_abort( operation );
3163
Gilles Peskine3f108122018-12-07 18:14:53 +01003164 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003165
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003166 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003167}
3168
3169
Gilles Peskine20035e32018-02-03 22:44:14 +01003170
Gilles Peskine20035e32018-02-03 22:44:14 +01003171/****************************************************************/
3172/* Asymmetric cryptography */
3173/****************************************************************/
3174
Gilles Peskine2b450e32018-06-27 15:42:46 +02003175#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003176/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003177 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003178static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3179 size_t hash_length,
3180 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003181{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003182 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003183 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003184 *md_alg = mbedtls_md_get_type( md_info );
3185
3186 /* The Mbed TLS RSA module uses an unsigned int for hash length
3187 * parameters. Validate that it fits so that we don't risk an
3188 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003189#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003190 if( hash_length > UINT_MAX )
3191 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003192#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003193
3194#if defined(MBEDTLS_PKCS1_V15)
3195 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3196 * must be correct. */
3197 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3198 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003199 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003200 if( md_info == NULL )
3201 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003202 if( mbedtls_md_get_size( md_info ) != hash_length )
3203 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003204 }
3205#endif /* MBEDTLS_PKCS1_V15 */
3206
3207#if defined(MBEDTLS_PKCS1_V21)
3208 /* PSS requires a hash internally. */
3209 if( PSA_ALG_IS_RSA_PSS( alg ) )
3210 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003211 if( md_info == NULL )
3212 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003213 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003214#endif /* MBEDTLS_PKCS1_V21 */
3215
Gilles Peskine61b91d42018-06-08 16:09:36 +02003216 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003217}
3218
Gilles Peskine2b450e32018-06-27 15:42:46 +02003219static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3220 psa_algorithm_t alg,
3221 const uint8_t *hash,
3222 size_t hash_length,
3223 uint8_t *signature,
3224 size_t signature_size,
3225 size_t *signature_length )
3226{
3227 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003229 mbedtls_md_type_t md_alg;
3230
3231 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3232 if( status != PSA_SUCCESS )
3233 return( status );
3234
Gilles Peskine630a18a2018-06-29 17:49:35 +02003235 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003236 return( PSA_ERROR_BUFFER_TOO_SMALL );
3237
3238#if defined(MBEDTLS_PKCS1_V15)
3239 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3240 {
3241 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3242 MBEDTLS_MD_NONE );
3243 ret = mbedtls_rsa_pkcs1_sign( rsa,
3244 mbedtls_ctr_drbg_random,
3245 &global_data.ctr_drbg,
3246 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003247 md_alg,
3248 (unsigned int) hash_length,
3249 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003250 signature );
3251 }
3252 else
3253#endif /* MBEDTLS_PKCS1_V15 */
3254#if defined(MBEDTLS_PKCS1_V21)
3255 if( PSA_ALG_IS_RSA_PSS( alg ) )
3256 {
3257 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3258 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3259 mbedtls_ctr_drbg_random,
3260 &global_data.ctr_drbg,
3261 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003262 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003263 (unsigned int) hash_length,
3264 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003265 signature );
3266 }
3267 else
3268#endif /* MBEDTLS_PKCS1_V21 */
3269 {
3270 return( PSA_ERROR_INVALID_ARGUMENT );
3271 }
3272
3273 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003274 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003275 return( mbedtls_to_psa_error( ret ) );
3276}
3277
3278static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3279 psa_algorithm_t alg,
3280 const uint8_t *hash,
3281 size_t hash_length,
3282 const uint8_t *signature,
3283 size_t signature_length )
3284{
3285 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003287 mbedtls_md_type_t md_alg;
3288
3289 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3290 if( status != PSA_SUCCESS )
3291 return( status );
3292
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003293 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003295
3296#if defined(MBEDTLS_PKCS1_V15)
3297 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3298 {
3299 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3300 MBEDTLS_MD_NONE );
3301 ret = mbedtls_rsa_pkcs1_verify( rsa,
3302 mbedtls_ctr_drbg_random,
3303 &global_data.ctr_drbg,
3304 MBEDTLS_RSA_PUBLIC,
3305 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003306 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003307 hash,
3308 signature );
3309 }
3310 else
3311#endif /* MBEDTLS_PKCS1_V15 */
3312#if defined(MBEDTLS_PKCS1_V21)
3313 if( PSA_ALG_IS_RSA_PSS( alg ) )
3314 {
3315 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3316 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3317 mbedtls_ctr_drbg_random,
3318 &global_data.ctr_drbg,
3319 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003320 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003321 (unsigned int) hash_length,
3322 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003323 signature );
3324 }
3325 else
3326#endif /* MBEDTLS_PKCS1_V21 */
3327 {
3328 return( PSA_ERROR_INVALID_ARGUMENT );
3329 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003330
3331 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3332 * the rest of the signature is invalid". This has little use in
3333 * practice and PSA doesn't report this distinction. */
3334 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3335 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003336 return( mbedtls_to_psa_error( ret ) );
3337}
3338#endif /* MBEDTLS_RSA_C */
3339
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003340#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003341/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3342 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3343 * (even though these functions don't modify it). */
3344static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3345 psa_algorithm_t alg,
3346 const uint8_t *hash,
3347 size_t hash_length,
3348 uint8_t *signature,
3349 size_t signature_size,
3350 size_t *signature_length )
3351{
Janos Follath24eed8d2019-11-22 13:21:35 +00003352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003353 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003354 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003355 mbedtls_mpi_init( &r );
3356 mbedtls_mpi_init( &s );
3357
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003358 if( signature_size < 2 * curve_bytes )
3359 {
3360 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3361 goto cleanup;
3362 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003363
Gilles Peskinea05219c2018-11-16 16:02:56 +01003364#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003365 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3366 {
3367 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3368 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3369 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003370 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3371 &ecp->d, hash,
3372 hash_length, md_alg,
3373 mbedtls_ctr_drbg_random,
3374 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003375 }
3376 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003377#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003379 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003380 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3381 hash, hash_length,
3382 mbedtls_ctr_drbg_random,
3383 &global_data.ctr_drbg ) );
3384 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003385
3386 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3387 signature,
3388 curve_bytes ) );
3389 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3390 signature + curve_bytes,
3391 curve_bytes ) );
3392
3393cleanup:
3394 mbedtls_mpi_free( &r );
3395 mbedtls_mpi_free( &s );
3396 if( ret == 0 )
3397 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003398 return( mbedtls_to_psa_error( ret ) );
3399}
3400
3401static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3402 const uint8_t *hash,
3403 size_t hash_length,
3404 const uint8_t *signature,
3405 size_t signature_length )
3406{
Janos Follath24eed8d2019-11-22 13:21:35 +00003407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003408 mbedtls_mpi r, s;
3409 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3410 mbedtls_mpi_init( &r );
3411 mbedtls_mpi_init( &s );
3412
3413 if( signature_length != 2 * curve_bytes )
3414 return( PSA_ERROR_INVALID_SIGNATURE );
3415
3416 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3417 signature,
3418 curve_bytes ) );
3419 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3420 signature + curve_bytes,
3421 curve_bytes ) );
3422
3423 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3424 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003425
3426cleanup:
3427 mbedtls_mpi_free( &r );
3428 mbedtls_mpi_free( &s );
3429 return( mbedtls_to_psa_error( ret ) );
3430}
3431#endif /* MBEDTLS_ECDSA_C */
3432
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003433psa_status_t psa_sign_hash( psa_key_handle_t handle,
3434 psa_algorithm_t alg,
3435 const uint8_t *hash,
3436 size_t hash_length,
3437 uint8_t *signature,
3438 size_t signature_size,
3439 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003440{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003441 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003442 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003443#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3444 const psa_drv_se_t *drv;
3445 psa_drv_se_context_t *drv_context;
3446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003447
3448 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003449 /* Immediately reject a zero-length signature buffer. This guarantees
3450 * that signature must be a valid pointer. (On the other hand, the hash
3451 * buffer can in principle be empty since it doesn't actually have
3452 * to be a hash.) */
3453 if( signature_size == 0 )
3454 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003455
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003456 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003457 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003458 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003459 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003460 {
3461 status = PSA_ERROR_INVALID_ARGUMENT;
3462 goto exit;
3463 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003464
Gilles Peskineedc64242019-08-07 21:05:07 +02003465#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3466 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3467 {
3468 if( drv->asymmetric == NULL ||
3469 drv->asymmetric->p_sign == NULL )
3470 {
3471 status = PSA_ERROR_NOT_SUPPORTED;
3472 goto exit;
3473 }
3474 status = drv->asymmetric->p_sign( drv_context,
3475 slot->data.se.slot_number,
3476 alg,
3477 hash, hash_length,
3478 signature, signature_size,
3479 signature_length );
3480 }
3481 else
3482#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003483#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003484 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003485 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003486 status = psa_rsa_sign( slot->data.rsa,
3487 alg,
3488 hash, hash_length,
3489 signature, signature_size,
3490 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003491 }
3492 else
3493#endif /* defined(MBEDTLS_RSA_C) */
3494#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003495 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003496 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003497#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003498 if(
3499#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3500 PSA_ALG_IS_ECDSA( alg )
3501#else
3502 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3503#endif
3504 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003505 status = psa_ecdsa_sign( slot->data.ecp,
3506 alg,
3507 hash, hash_length,
3508 signature, signature_size,
3509 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003510 else
3511#endif /* defined(MBEDTLS_ECDSA_C) */
3512 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003513 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003514 }
itayzafrir5c753392018-05-08 11:18:38 +03003515 }
3516 else
3517#endif /* defined(MBEDTLS_ECP_C) */
3518 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003519 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003520 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003521
3522exit:
3523 /* Fill the unused part of the output buffer (the whole buffer on error,
3524 * the trailing part on success) with something that isn't a valid mac
3525 * (barring an attack on the mac and deliberately-crafted input),
3526 * in case the caller doesn't check the return status properly. */
3527 if( status == PSA_SUCCESS )
3528 memset( signature + *signature_length, '!',
3529 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003530 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003531 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003532 /* If signature_size is 0 then we have nothing to do. We must not call
3533 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003534 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003535}
3536
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003537psa_status_t psa_verify_hash( psa_key_handle_t handle,
3538 psa_algorithm_t alg,
3539 const uint8_t *hash,
3540 size_t hash_length,
3541 const uint8_t *signature,
3542 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003543{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003544 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003545 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003546#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3547 const psa_drv_se_t *drv;
3548 psa_drv_se_context_t *drv_context;
3549#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003550
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003551 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003552 if( status != PSA_SUCCESS )
3553 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003554
Gilles Peskineedc64242019-08-07 21:05:07 +02003555#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3556 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3557 {
3558 if( drv->asymmetric == NULL ||
3559 drv->asymmetric->p_verify == NULL )
3560 return( PSA_ERROR_NOT_SUPPORTED );
3561 return( drv->asymmetric->p_verify( drv_context,
3562 slot->data.se.slot_number,
3563 alg,
3564 hash, hash_length,
3565 signature, signature_length ) );
3566 }
3567 else
3568#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003569#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003570 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003572 return( psa_rsa_verify( slot->data.rsa,
3573 alg,
3574 hash, hash_length,
3575 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576 }
3577 else
3578#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003579#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003580 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003581 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003582#if defined(MBEDTLS_ECDSA_C)
3583 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003584 return( psa_ecdsa_verify( slot->data.ecp,
3585 hash, hash_length,
3586 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003587 else
3588#endif /* defined(MBEDTLS_ECDSA_C) */
3589 {
3590 return( PSA_ERROR_INVALID_ARGUMENT );
3591 }
itayzafrir5c753392018-05-08 11:18:38 +03003592 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003593 else
3594#endif /* defined(MBEDTLS_ECP_C) */
3595 {
3596 return( PSA_ERROR_NOT_SUPPORTED );
3597 }
3598}
3599
Gilles Peskine072ac562018-06-30 00:21:29 +02003600#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3601static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3602 mbedtls_rsa_context *rsa )
3603{
3604 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3605 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3606 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3607 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3608}
3609#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3610
Gilles Peskinec5487a82018-12-03 18:08:14 +01003611psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003612 psa_algorithm_t alg,
3613 const uint8_t *input,
3614 size_t input_length,
3615 const uint8_t *salt,
3616 size_t salt_length,
3617 uint8_t *output,
3618 size_t output_size,
3619 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003621 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003622 psa_status_t status;
3623
Darryl Green5cc689a2018-07-24 15:34:10 +01003624 (void) input;
3625 (void) input_length;
3626 (void) salt;
3627 (void) output;
3628 (void) output_size;
3629
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003630 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003632 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3633 return( PSA_ERROR_INVALID_ARGUMENT );
3634
Gilles Peskine28f8f302019-07-24 13:30:31 +02003635 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003636 if( status != PSA_SUCCESS )
3637 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003638 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3639 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641
3642#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003643 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 {
3645 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003647 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003648 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003650 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003652 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3653 mbedtls_ctr_drbg_random,
3654 &global_data.ctr_drbg,
3655 MBEDTLS_RSA_PUBLIC,
3656 input_length,
3657 input,
3658 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659 }
3660 else
3661#endif /* MBEDTLS_PKCS1_V15 */
3662#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003663 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003665 psa_rsa_oaep_set_padding_mode( alg, rsa );
3666 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3667 mbedtls_ctr_drbg_random,
3668 &global_data.ctr_drbg,
3669 MBEDTLS_RSA_PUBLIC,
3670 salt, salt_length,
3671 input_length,
3672 input,
3673 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 }
3675 else
3676#endif /* MBEDTLS_PKCS1_V21 */
3677 {
3678 return( PSA_ERROR_INVALID_ARGUMENT );
3679 }
3680 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003681 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682 return( mbedtls_to_psa_error( ret ) );
3683 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003685#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003686 {
3687 return( PSA_ERROR_NOT_SUPPORTED );
3688 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003689}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690
Gilles Peskinec5487a82018-12-03 18:08:14 +01003691psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003692 psa_algorithm_t alg,
3693 const uint8_t *input,
3694 size_t input_length,
3695 const uint8_t *salt,
3696 size_t salt_length,
3697 uint8_t *output,
3698 size_t output_size,
3699 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003700{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003701 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003702 psa_status_t status;
3703
Darryl Green5cc689a2018-07-24 15:34:10 +01003704 (void) input;
3705 (void) input_length;
3706 (void) salt;
3707 (void) output;
3708 (void) output_size;
3709
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003710 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003712 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3713 return( PSA_ERROR_INVALID_ARGUMENT );
3714
Gilles Peskine28f8f302019-07-24 13:30:31 +02003715 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003716 if( status != PSA_SUCCESS )
3717 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003718 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719 return( PSA_ERROR_INVALID_ARGUMENT );
3720
3721#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003722 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 {
3724 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726
Gilles Peskine630a18a2018-06-29 17:49:35 +02003727 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 return( PSA_ERROR_INVALID_ARGUMENT );
3729
3730#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003731 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003732 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003733 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3734 mbedtls_ctr_drbg_random,
3735 &global_data.ctr_drbg,
3736 MBEDTLS_RSA_PRIVATE,
3737 output_length,
3738 input,
3739 output,
3740 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003741 }
3742 else
3743#endif /* MBEDTLS_PKCS1_V15 */
3744#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003745 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003746 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003747 psa_rsa_oaep_set_padding_mode( alg, rsa );
3748 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3749 mbedtls_ctr_drbg_random,
3750 &global_data.ctr_drbg,
3751 MBEDTLS_RSA_PRIVATE,
3752 salt, salt_length,
3753 output_length,
3754 input,
3755 output,
3756 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003757 }
3758 else
3759#endif /* MBEDTLS_PKCS1_V21 */
3760 {
3761 return( PSA_ERROR_INVALID_ARGUMENT );
3762 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003763
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003764 return( mbedtls_to_psa_error( ret ) );
3765 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003767#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 {
3769 return( PSA_ERROR_NOT_SUPPORTED );
3770 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003771}
Gilles Peskine20035e32018-02-03 22:44:14 +01003772
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003773
3774
mohammad1603503973b2018-03-12 15:59:30 +02003775/****************************************************************/
3776/* Symmetric cryptography */
3777/****************************************************************/
3778
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003779/* Initialize the cipher operation structure. Once this function has been
3780 * called, psa_cipher_abort can run and will do the right thing. */
3781static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3782 psa_algorithm_t alg )
3783{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003784 if( ! PSA_ALG_IS_CIPHER( alg ) )
3785 {
3786 memset( operation, 0, sizeof( *operation ) );
3787 return( PSA_ERROR_INVALID_ARGUMENT );
3788 }
3789
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003790 operation->alg = alg;
3791 operation->key_set = 0;
3792 operation->iv_set = 0;
3793 operation->iv_required = 1;
3794 operation->iv_size = 0;
3795 operation->block_size = 0;
3796 mbedtls_cipher_init( &operation->ctx.cipher );
3797 return( PSA_SUCCESS );
3798}
3799
Gilles Peskinee553c652018-06-04 16:22:46 +02003800static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003801 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003802 psa_algorithm_t alg,
3803 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003804{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003805 int ret = 0;
3806 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003807 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003808 size_t key_bits;
3809 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003810 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3811 PSA_KEY_USAGE_ENCRYPT :
3812 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003813
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003814 /* A context must be freshly initialized before it can be set up. */
3815 if( operation->alg != 0 )
3816 {
3817 return( PSA_ERROR_BAD_STATE );
3818 }
3819
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003820 status = psa_cipher_init( operation, alg );
3821 if( status != PSA_SUCCESS )
3822 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003823
Gilles Peskine28f8f302019-07-24 13:30:31 +02003824 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003825 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003826 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003827 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003828
Gilles Peskine8e338702019-07-30 20:06:31 +02003829 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003830 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003831 {
3832 status = PSA_ERROR_NOT_SUPPORTED;
3833 goto exit;
3834 }
mohammad1603503973b2018-03-12 15:59:30 +02003835
mohammad1603503973b2018-03-12 15:59:30 +02003836 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003837 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003838 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003839
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003840#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003841 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003842 {
3843 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003844 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003845 memcpy( keys, slot->data.raw.data, 16 );
3846 memcpy( keys + 16, slot->data.raw.data, 8 );
3847 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3848 keys,
3849 192, cipher_operation );
3850 }
3851 else
3852#endif
3853 {
3854 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3855 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003856 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003857 }
Moran Peker41deec42018-04-04 15:43:05 +03003858 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003859 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003860
mohammad16038481e742018-03-18 13:57:31 +02003861#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003862 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003863 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003864 case PSA_ALG_CBC_NO_PADDING:
3865 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3866 MBEDTLS_PADDING_NONE );
3867 break;
3868 case PSA_ALG_CBC_PKCS7:
3869 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3870 MBEDTLS_PADDING_PKCS7 );
3871 break;
3872 default:
3873 /* The algorithm doesn't involve padding. */
3874 ret = 0;
3875 break;
3876 }
3877 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003878 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003879#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3880
mohammad1603503973b2018-03-12 15:59:30 +02003881 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003882 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003883 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003884 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003885 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003886 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003887 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003888#if defined(MBEDTLS_CHACHA20_C)
3889 else
3890 if( alg == PSA_ALG_CHACHA20 )
3891 operation->iv_size = 12;
3892#endif
mohammad1603503973b2018-03-12 15:59:30 +02003893
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003894exit:
3895 if( status == 0 )
3896 status = mbedtls_to_psa_error( ret );
3897 if( status != 0 )
3898 psa_cipher_abort( operation );
3899 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003900}
3901
Gilles Peskinefe119512018-07-08 21:39:34 +02003902psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003903 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003904 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003905{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003906 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003907}
3908
Gilles Peskinefe119512018-07-08 21:39:34 +02003909psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003910 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003911 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003912{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003913 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003914}
3915
Gilles Peskinefe119512018-07-08 21:39:34 +02003916psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003917 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003918 size_t iv_size,
3919 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003920{
itayzafrir534bd7c2018-08-02 13:56:32 +03003921 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003923 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003925 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003926 }
Moran Peker41deec42018-04-04 15:43:05 +03003927 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003928 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003929 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003930 goto exit;
3931 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003932 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3933 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003934 if( ret != 0 )
3935 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003936 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003937 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003938 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003939
mohammad16038481e742018-03-18 13:57:31 +02003940 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003941 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003942
Moran Peker395db872018-05-31 14:07:14 +03003943exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003944 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003945 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003946 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003947}
3948
Gilles Peskinefe119512018-07-08 21:39:34 +02003949psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003950 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003951 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003952{
itayzafrir534bd7c2018-08-02 13:56:32 +03003953 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003955 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003956 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003957 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003958 }
Moran Pekera28258c2018-05-29 16:25:04 +03003959 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003960 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003961 status = PSA_ERROR_INVALID_ARGUMENT;
3962 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003963 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003964 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3965 status = mbedtls_to_psa_error( ret );
3966exit:
3967 if( status == PSA_SUCCESS )
3968 operation->iv_set = 1;
3969 else
mohammad1603503973b2018-03-12 15:59:30 +02003970 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003971 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003972}
3973
Gilles Peskinee553c652018-06-04 16:22:46 +02003974psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3975 const uint8_t *input,
3976 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003977 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003978 size_t output_size,
3979 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003980{
itayzafrir534bd7c2018-08-02 13:56:32 +03003981 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003982 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003983 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003984
3985 if( operation->alg == 0 )
3986 {
3987 return( PSA_ERROR_BAD_STATE );
3988 }
3989
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003990 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003991 {
3992 /* Take the unprocessed partial block left over from previous
3993 * update calls, if any, plus the input to this call. Remove
3994 * the last partial block, if any. You get the data that will be
3995 * output in this call. */
3996 expected_output_size =
3997 ( operation->ctx.cipher.unprocessed_len + input_length )
3998 / operation->block_size * operation->block_size;
3999 }
4000 else
4001 {
4002 expected_output_size = input_length;
4003 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004004
Gilles Peskine89d789c2018-06-04 17:17:16 +02004005 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004006 {
4007 status = PSA_ERROR_BUFFER_TOO_SMALL;
4008 goto exit;
4009 }
mohammad160382759612018-03-12 18:16:40 +02004010
mohammad1603503973b2018-03-12 15:59:30 +02004011 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004012 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004013 status = mbedtls_to_psa_error( ret );
4014exit:
4015 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004016 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004017 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004018}
4019
Gilles Peskinee553c652018-06-04 16:22:46 +02004020psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4021 uint8_t *output,
4022 size_t output_size,
4023 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004024{
David Saadab4ecc272019-02-14 13:48:10 +02004025 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004026 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004027 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004028
mohammad1603503973b2018-03-12 15:59:30 +02004029 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004030 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004031 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004032 }
4033 if( operation->iv_required && ! operation->iv_set )
4034 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004035 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004036 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004037
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004038 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004039 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4040 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004041 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004042 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004043 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004044 }
4045
Janos Follath315b51c2018-07-09 16:04:51 +01004046 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4047 temp_output_buffer,
4048 output_length );
4049 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004050 {
Janos Follath315b51c2018-07-09 16:04:51 +01004051 status = mbedtls_to_psa_error( cipher_ret );
4052 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004053 }
Janos Follath315b51c2018-07-09 16:04:51 +01004054
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004055 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004056 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004057 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004058 memcpy( output, temp_output_buffer, *output_length );
4059 else
4060 {
Janos Follath315b51c2018-07-09 16:04:51 +01004061 status = PSA_ERROR_BUFFER_TOO_SMALL;
4062 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004063 }
mohammad1603503973b2018-03-12 15:59:30 +02004064
Gilles Peskine3f108122018-12-07 18:14:53 +01004065 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004066 status = psa_cipher_abort( operation );
4067
4068 return( status );
4069
4070error:
4071
4072 *output_length = 0;
4073
Gilles Peskine3f108122018-12-07 18:14:53 +01004074 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004075 (void) psa_cipher_abort( operation );
4076
4077 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004078}
4079
Gilles Peskinee553c652018-06-04 16:22:46 +02004080psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4081{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004082 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004083 {
4084 /* The object has (apparently) been initialized but it is not
4085 * in use. It's ok to call abort on such an object, and there's
4086 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004087 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004088 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004089
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004090 /* Sanity check (shouldn't happen: operation->alg should
4091 * always have been initialized to a valid value). */
4092 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4093 return( PSA_ERROR_BAD_STATE );
4094
mohammad1603503973b2018-03-12 15:59:30 +02004095 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004096
Moran Peker41deec42018-04-04 15:43:05 +03004097 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004098 operation->key_set = 0;
4099 operation->iv_set = 0;
4100 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004101 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004102 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004103
Moran Peker395db872018-05-31 14:07:14 +03004104 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004105}
4106
Gilles Peskinea0655c32018-04-30 17:06:50 +02004107
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004108
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004109
mohammad16035955c982018-04-26 00:53:03 +03004110/****************************************************************/
4111/* AEAD */
4112/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004113
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114typedef struct
4115{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004116 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 const mbedtls_cipher_info_t *cipher_info;
4118 union
4119 {
4120#if defined(MBEDTLS_CCM_C)
4121 mbedtls_ccm_context ccm;
4122#endif /* MBEDTLS_CCM_C */
4123#if defined(MBEDTLS_GCM_C)
4124 mbedtls_gcm_context gcm;
4125#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004126#if defined(MBEDTLS_CHACHAPOLY_C)
4127 mbedtls_chachapoly_context chachapoly;
4128#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004129 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004130 psa_algorithm_t core_alg;
4131 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004132 uint8_t tag_length;
4133} aead_operation_t;
4134
Gilles Peskine30a9e412019-01-14 18:36:12 +01004135static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004136{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004137 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004138 {
4139#if defined(MBEDTLS_CCM_C)
4140 case PSA_ALG_CCM:
4141 mbedtls_ccm_free( &operation->ctx.ccm );
4142 break;
4143#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004144#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004145 case PSA_ALG_GCM:
4146 mbedtls_gcm_free( &operation->ctx.gcm );
4147 break;
4148#endif /* MBEDTLS_GCM_C */
4149 }
4150}
4151
4152static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004153 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004154 psa_key_usage_t usage,
4155 psa_algorithm_t alg )
4156{
4157 psa_status_t status;
4158 size_t key_bits;
4159 mbedtls_cipher_id_t cipher_id;
4160
Gilles Peskine28f8f302019-07-24 13:30:31 +02004161 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004162 if( status != PSA_SUCCESS )
4163 return( status );
4164
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004165 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004166
4167 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004168 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004169 &cipher_id );
4170 if( operation->cipher_info == NULL )
4171 return( PSA_ERROR_NOT_SUPPORTED );
4172
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004173 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004174 {
4175#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004176 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4177 operation->core_alg = PSA_ALG_CCM;
4178 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004179 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4180 * The call to mbedtls_ccm_encrypt_and_tag or
4181 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004182 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004183 return( PSA_ERROR_INVALID_ARGUMENT );
4184 mbedtls_ccm_init( &operation->ctx.ccm );
4185 status = mbedtls_to_psa_error(
4186 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4187 operation->slot->data.raw.data,
4188 (unsigned int) key_bits ) );
4189 if( status != 0 )
4190 goto cleanup;
4191 break;
4192#endif /* MBEDTLS_CCM_C */
4193
4194#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004195 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4196 operation->core_alg = PSA_ALG_GCM;
4197 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004198 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4199 * The call to mbedtls_gcm_crypt_and_tag or
4200 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004201 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004202 return( PSA_ERROR_INVALID_ARGUMENT );
4203 mbedtls_gcm_init( &operation->ctx.gcm );
4204 status = mbedtls_to_psa_error(
4205 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4206 operation->slot->data.raw.data,
4207 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004208 if( status != 0 )
4209 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004210 break;
4211#endif /* MBEDTLS_GCM_C */
4212
Gilles Peskine26869f22019-05-06 15:25:00 +02004213#if defined(MBEDTLS_CHACHAPOLY_C)
4214 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4215 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4216 operation->full_tag_length = 16;
4217 /* We only support the default tag length. */
4218 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4219 return( PSA_ERROR_NOT_SUPPORTED );
4220 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4221 status = mbedtls_to_psa_error(
4222 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4223 operation->slot->data.raw.data ) );
4224 if( status != 0 )
4225 goto cleanup;
4226 break;
4227#endif /* MBEDTLS_CHACHAPOLY_C */
4228
Gilles Peskineedf9a652018-08-17 18:11:56 +02004229 default:
4230 return( PSA_ERROR_NOT_SUPPORTED );
4231 }
4232
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004233 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4234 {
4235 status = PSA_ERROR_INVALID_ARGUMENT;
4236 goto cleanup;
4237 }
4238 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004239
Gilles Peskineedf9a652018-08-17 18:11:56 +02004240 return( PSA_SUCCESS );
4241
4242cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004243 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004244 return( status );
4245}
4246
Gilles Peskinec5487a82018-12-03 18:08:14 +01004247psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004248 psa_algorithm_t alg,
4249 const uint8_t *nonce,
4250 size_t nonce_length,
4251 const uint8_t *additional_data,
4252 size_t additional_data_length,
4253 const uint8_t *plaintext,
4254 size_t plaintext_length,
4255 uint8_t *ciphertext,
4256 size_t ciphertext_size,
4257 size_t *ciphertext_length )
4258{
mohammad16035955c982018-04-26 00:53:03 +03004259 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004260 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004261 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004262
mohammad1603f08a5502018-06-03 15:05:47 +03004263 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004264
Gilles Peskinec5487a82018-12-03 18:08:14 +01004265 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004266 if( status != PSA_SUCCESS )
4267 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004268
Gilles Peskineedf9a652018-08-17 18:11:56 +02004269 /* For all currently supported modes, the tag is at the end of the
4270 * ciphertext. */
4271 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4272 {
4273 status = PSA_ERROR_BUFFER_TOO_SMALL;
4274 goto exit;
4275 }
4276 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004277
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004278#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004279 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004280 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004281 status = mbedtls_to_psa_error(
4282 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4283 MBEDTLS_GCM_ENCRYPT,
4284 plaintext_length,
4285 nonce, nonce_length,
4286 additional_data, additional_data_length,
4287 plaintext, ciphertext,
4288 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004289 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004290 else
4291#endif /* MBEDTLS_GCM_C */
4292#if defined(MBEDTLS_CCM_C)
4293 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004294 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004295 status = mbedtls_to_psa_error(
4296 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4297 plaintext_length,
4298 nonce, nonce_length,
4299 additional_data,
4300 additional_data_length,
4301 plaintext, ciphertext,
4302 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004303 }
mohammad16035c8845f2018-05-09 05:40:09 -07004304 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004305#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004306#if defined(MBEDTLS_CHACHAPOLY_C)
4307 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4308 {
4309 if( nonce_length != 12 || operation.tag_length != 16 )
4310 {
4311 status = PSA_ERROR_NOT_SUPPORTED;
4312 goto exit;
4313 }
4314 status = mbedtls_to_psa_error(
4315 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4316 plaintext_length,
4317 nonce,
4318 additional_data,
4319 additional_data_length,
4320 plaintext,
4321 ciphertext,
4322 tag ) );
4323 }
4324 else
4325#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004326 {
mohammad1603554faad2018-06-03 15:07:38 +03004327 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004328 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004329
Gilles Peskineedf9a652018-08-17 18:11:56 +02004330 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4331 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004332
Gilles Peskineedf9a652018-08-17 18:11:56 +02004333exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004334 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004335 if( status == PSA_SUCCESS )
4336 *ciphertext_length = plaintext_length + operation.tag_length;
4337 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004338}
4339
Gilles Peskineee652a32018-06-01 19:23:52 +02004340/* Locate the tag in a ciphertext buffer containing the encrypted data
4341 * followed by the tag. Return the length of the part preceding the tag in
4342 * *plaintext_length. This is the size of the plaintext in modes where
4343 * the encrypted data has the same size as the plaintext, such as
4344 * CCM and GCM. */
4345static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4346 const uint8_t *ciphertext,
4347 size_t ciphertext_length,
4348 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004349 const uint8_t **p_tag )
4350{
4351 size_t payload_length;
4352 if( tag_length > ciphertext_length )
4353 return( PSA_ERROR_INVALID_ARGUMENT );
4354 payload_length = ciphertext_length - tag_length;
4355 if( payload_length > plaintext_size )
4356 return( PSA_ERROR_BUFFER_TOO_SMALL );
4357 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004358 return( PSA_SUCCESS );
4359}
4360
Gilles Peskinec5487a82018-12-03 18:08:14 +01004361psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004362 psa_algorithm_t alg,
4363 const uint8_t *nonce,
4364 size_t nonce_length,
4365 const uint8_t *additional_data,
4366 size_t additional_data_length,
4367 const uint8_t *ciphertext,
4368 size_t ciphertext_length,
4369 uint8_t *plaintext,
4370 size_t plaintext_size,
4371 size_t *plaintext_length )
4372{
mohammad16035955c982018-04-26 00:53:03 +03004373 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004374 aead_operation_t operation;
4375 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004376
Gilles Peskineee652a32018-06-01 19:23:52 +02004377 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004378
Gilles Peskinec5487a82018-12-03 18:08:14 +01004379 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004380 if( status != PSA_SUCCESS )
4381 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004382
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004383 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4384 ciphertext, ciphertext_length,
4385 plaintext_size, &tag );
4386 if( status != PSA_SUCCESS )
4387 goto exit;
4388
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004389#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004390 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004391 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004392 status = mbedtls_to_psa_error(
4393 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4394 ciphertext_length - operation.tag_length,
4395 nonce, nonce_length,
4396 additional_data,
4397 additional_data_length,
4398 tag, operation.tag_length,
4399 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004400 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004401 else
4402#endif /* MBEDTLS_GCM_C */
4403#if defined(MBEDTLS_CCM_C)
4404 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004405 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004406 status = mbedtls_to_psa_error(
4407 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4408 ciphertext_length - operation.tag_length,
4409 nonce, nonce_length,
4410 additional_data,
4411 additional_data_length,
4412 ciphertext, plaintext,
4413 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004414 }
mohammad160339574652018-06-01 04:39:53 -07004415 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004416#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004417#if defined(MBEDTLS_CHACHAPOLY_C)
4418 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4419 {
4420 if( nonce_length != 12 || operation.tag_length != 16 )
4421 {
4422 status = PSA_ERROR_NOT_SUPPORTED;
4423 goto exit;
4424 }
4425 status = mbedtls_to_psa_error(
4426 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4427 ciphertext_length - operation.tag_length,
4428 nonce,
4429 additional_data,
4430 additional_data_length,
4431 tag,
4432 ciphertext,
4433 plaintext ) );
4434 }
4435 else
4436#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004437 {
mohammad1603554faad2018-06-03 15:07:38 +03004438 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004439 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004440
Gilles Peskineedf9a652018-08-17 18:11:56 +02004441 if( status != PSA_SUCCESS && plaintext_size != 0 )
4442 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004443
Gilles Peskineedf9a652018-08-17 18:11:56 +02004444exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004445 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446 if( status == PSA_SUCCESS )
4447 *plaintext_length = ciphertext_length - operation.tag_length;
4448 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004449}
4450
Gilles Peskinea0655c32018-04-30 17:06:50 +02004451
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004452
Gilles Peskine20035e32018-02-03 22:44:14 +01004453/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004454/* Generators */
4455/****************************************************************/
4456
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004457#define HKDF_STATE_INIT 0 /* no input yet */
4458#define HKDF_STATE_STARTED 1 /* got salt */
4459#define HKDF_STATE_KEYED 2 /* got key */
4460#define HKDF_STATE_OUTPUT 3 /* output started */
4461
Gilles Peskinecbe66502019-05-16 16:59:18 +02004462static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004463 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004464{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004465 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4466 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004467 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004468 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004469}
4470
4471
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004473{
4474 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004476 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004477 {
4478 /* The object has (apparently) been initialized but it is not
4479 * in use. It's ok to call abort on such an object, and there's
4480 * nothing to do. */
4481 }
4482 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004483#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004484 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004485 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 mbedtls_free( operation->ctx.hkdf.info );
4487 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004488 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004489 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004491 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004492 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004493 if( operation->ctx.tls12_prf.seed != NULL )
4494 {
4495 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4496 operation->ctx.tls12_prf.seed_length );
4497 mbedtls_free( operation->ctx.tls12_prf.seed );
4498 }
4499
4500 if( operation->ctx.tls12_prf.label != NULL )
4501 {
4502 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4503 operation->ctx.tls12_prf.label_length );
4504 mbedtls_free( operation->ctx.tls12_prf.label );
4505 }
4506
4507 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4508
4509 /* We leave the fields Ai and output_block to be erased safely by the
4510 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004511 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004512 else
4513#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004514 {
4515 status = PSA_ERROR_BAD_STATE;
4516 }
Janos Follath083036a2019-06-11 10:22:26 +01004517 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004518 return( status );
4519}
4520
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004521psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004522 size_t *capacity)
4523{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004525 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004527 return PSA_ERROR_BAD_STATE;
4528 }
4529
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004531 return( PSA_SUCCESS );
4532}
4533
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004535 size_t capacity )
4536{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004538 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004540 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004542 return( PSA_SUCCESS );
4543}
4544
Gilles Peskinebef7f142018-07-12 17:22:21 +02004545#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004546/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004547 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004548static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004549 psa_algorithm_t hash_alg,
4550 uint8_t *output,
4551 size_t output_length )
4552{
4553 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4554 psa_status_t status;
4555
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004556 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4557 return( PSA_ERROR_BAD_STATE );
4558 hkdf->state = HKDF_STATE_OUTPUT;
4559
Gilles Peskinebef7f142018-07-12 17:22:21 +02004560 while( output_length != 0 )
4561 {
4562 /* Copy what remains of the current block */
4563 uint8_t n = hash_length - hkdf->offset_in_block;
4564 if( n > output_length )
4565 n = (uint8_t) output_length;
4566 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4567 output += n;
4568 output_length -= n;
4569 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004570 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004571 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004572 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004573 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004574 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004575 * object was corrupted or if this function is called directly
4576 * inside the library. */
4577 if( hkdf->block_number == 0xff )
4578 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004579
4580 /* We need a new block */
4581 ++hkdf->block_number;
4582 hkdf->offset_in_block = 0;
4583 status = psa_hmac_setup_internal( &hkdf->hmac,
4584 hkdf->prk, hash_length,
4585 hash_alg );
4586 if( status != PSA_SUCCESS )
4587 return( status );
4588 if( hkdf->block_number != 1 )
4589 {
4590 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4591 hkdf->output_block,
4592 hash_length );
4593 if( status != PSA_SUCCESS )
4594 return( status );
4595 }
4596 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4597 hkdf->info,
4598 hkdf->info_length );
4599 if( status != PSA_SUCCESS )
4600 return( status );
4601 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4602 &hkdf->block_number, 1 );
4603 if( status != PSA_SUCCESS )
4604 return( status );
4605 status = psa_hmac_finish_internal( &hkdf->hmac,
4606 hkdf->output_block,
4607 sizeof( hkdf->output_block ) );
4608 if( status != PSA_SUCCESS )
4609 return( status );
4610 }
4611
4612 return( PSA_SUCCESS );
4613}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004614
Janos Follath7742fee2019-06-17 12:58:10 +01004615static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4616 psa_tls12_prf_key_derivation_t *tls12_prf,
4617 psa_algorithm_t alg )
4618{
4619 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4620 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004621 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4622 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004623
Janos Follath7742fee2019-06-17 12:58:10 +01004624 /* We can't be wanting more output after block 0xff, otherwise
4625 * the capacity check in psa_key_derivation_output_bytes() would have
4626 * prevented this call. It could happen only if the operation
4627 * object was corrupted or if this function is called directly
4628 * inside the library. */
4629 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004630 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004631
4632 /* We need a new block */
4633 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004634 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004635
4636 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4637 *
4638 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4639 *
4640 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4641 * HMAC_hash(secret, A(2) + seed) +
4642 * HMAC_hash(secret, A(3) + seed) + ...
4643 *
4644 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004645 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004646 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004647 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004648 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004649 * is currently extracted as `output_block` and where i is
4650 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004651 */
4652
Janos Follathea29bfb2019-06-19 12:21:20 +01004653 /* Save the hash context before using it, to preserve the hash state with
4654 * only the inner padding in it. We need this, because inner padding depends
4655 * on the key (secret in the RFC's terminology). */
4656 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4657 if( status != PSA_SUCCESS )
4658 goto cleanup;
4659
4660 /* Calculate A(i) where i = tls12_prf->block_number. */
4661 if( tls12_prf->block_number == 1 )
4662 {
4663 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4664 * the variable seed and in this instance means it in the context of the
4665 * P_hash function, where seed = label + seed.) */
4666 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4667 tls12_prf->label, tls12_prf->label_length );
4668 if( status != PSA_SUCCESS )
4669 goto cleanup;
4670 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4671 tls12_prf->seed, tls12_prf->seed_length );
4672 if( status != PSA_SUCCESS )
4673 goto cleanup;
4674 }
4675 else
4676 {
4677 /* A(i) = HMAC_hash(secret, A(i-1)) */
4678 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4679 tls12_prf->Ai, hash_length );
4680 if( status != PSA_SUCCESS )
4681 goto cleanup;
4682 }
4683
4684 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4685 tls12_prf->Ai, hash_length );
4686 if( status != PSA_SUCCESS )
4687 goto cleanup;
4688 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4689 if( status != PSA_SUCCESS )
4690 goto cleanup;
4691
4692 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4693 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4694 tls12_prf->Ai, hash_length );
4695 if( status != PSA_SUCCESS )
4696 goto cleanup;
4697 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4698 tls12_prf->label, tls12_prf->label_length );
4699 if( status != PSA_SUCCESS )
4700 goto cleanup;
4701 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4702 tls12_prf->seed, tls12_prf->seed_length );
4703 if( status != PSA_SUCCESS )
4704 goto cleanup;
4705 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4706 tls12_prf->output_block, hash_length );
4707 if( status != PSA_SUCCESS )
4708 goto cleanup;
4709 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4710 if( status != PSA_SUCCESS )
4711 goto cleanup;
4712
Janos Follath7742fee2019-06-17 12:58:10 +01004713
4714cleanup:
4715
Janos Follathea29bfb2019-06-19 12:21:20 +01004716 cleanup_status = psa_hash_abort( &backup );
4717 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4718 status = cleanup_status;
4719
4720 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004721}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004722
Janos Follath844eb0e2019-06-19 12:10:49 +01004723static psa_status_t psa_key_derivation_tls12_prf_read(
4724 psa_tls12_prf_key_derivation_t *tls12_prf,
4725 psa_algorithm_t alg,
4726 uint8_t *output,
4727 size_t output_length )
4728{
4729 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4730 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4731 psa_status_t status;
4732 uint8_t offset, length;
4733
4734 while( output_length != 0 )
4735 {
4736 /* Check if we have fully processed the current block. */
4737 if( tls12_prf->left_in_block == 0 )
4738 {
4739 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4740 alg );
4741 if( status != PSA_SUCCESS )
4742 return( status );
4743
4744 continue;
4745 }
4746
4747 if( tls12_prf->left_in_block > output_length )
4748 length = (uint8_t) output_length;
4749 else
4750 length = tls12_prf->left_in_block;
4751
4752 offset = hash_length - tls12_prf->left_in_block;
4753 memcpy( output, tls12_prf->output_block + offset, length );
4754 output += length;
4755 output_length -= length;
4756 tls12_prf->left_in_block -= length;
4757 }
4758
4759 return( PSA_SUCCESS );
4760}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004761#endif /* MBEDTLS_MD_C */
4762
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004763psa_status_t psa_key_derivation_output_bytes(
4764 psa_key_derivation_operation_t *operation,
4765 uint8_t *output,
4766 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004767{
4768 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004769 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004770
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004772 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004774 return PSA_ERROR_BAD_STATE;
4775 }
4776
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004778 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004780 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004782 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004783 goto exit;
4784 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004785 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004786 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004788 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004789 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4790 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004793 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004794 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004796
Gilles Peskinebef7f142018-07-12 17:22:21 +02004797#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004798 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004799 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004800 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004802 output, output_length );
4803 }
Janos Follathadbec812019-06-14 11:05:39 +01004804 else
Janos Follathadbec812019-06-14 11:05:39 +01004805 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004806 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004807 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004810 output_length );
4811 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004812 else
4813#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814 {
4815 return( PSA_ERROR_BAD_STATE );
4816 }
4817
4818exit:
4819 if( status != PSA_SUCCESS )
4820 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004821 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 * This allows us to differentiate between exhausted operations and
4823 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4824 * operations. */
4825 psa_algorithm_t alg = operation->alg;
4826 psa_key_derivation_abort( operation );
4827 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004828 memset( output, '!', output_length );
4829 }
4830 return( status );
4831}
4832
Gilles Peskine08542d82018-07-19 17:05:42 +02004833#if defined(MBEDTLS_DES_C)
4834static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4835{
4836 if( data_size >= 8 )
4837 mbedtls_des_key_set_parity( data );
4838 if( data_size >= 16 )
4839 mbedtls_des_key_set_parity( data + 8 );
4840 if( data_size >= 24 )
4841 mbedtls_des_key_set_parity( data + 16 );
4842}
4843#endif /* MBEDTLS_DES_C */
4844
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004845static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004846 psa_key_slot_t *slot,
4847 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004849{
4850 uint8_t *data = NULL;
4851 size_t bytes = PSA_BITS_TO_BYTES( bits );
4852 psa_status_t status;
4853
Gilles Peskine8e338702019-07-30 20:06:31 +02004854 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004855 return( PSA_ERROR_INVALID_ARGUMENT );
4856 if( bits % 8 != 0 )
4857 return( PSA_ERROR_INVALID_ARGUMENT );
4858 data = mbedtls_calloc( 1, bytes );
4859 if( data == NULL )
4860 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4861
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004863 if( status != PSA_SUCCESS )
4864 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004865#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004866 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004867 psa_des_set_key_parity( data, bytes );
4868#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004869 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004870
4871exit:
4872 mbedtls_free( data );
4873 return( status );
4874}
4875
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004876psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004878 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004879{
4880 psa_status_t status;
4881 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004882 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004883
4884 /* Reject any attempt to create a zero-length key so that we don't
4885 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4886 if( psa_get_key_bits( attributes ) == 0 )
4887 return( PSA_ERROR_INVALID_ARGUMENT );
4888
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004889 if( ! operation->can_output_key )
4890 return( PSA_ERROR_NOT_PERMITTED );
4891
Gilles Peskinedf179142019-07-15 22:02:14 +02004892 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4893 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004894#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4895 if( driver != NULL )
4896 {
4897 /* Deriving a key in a secure element is not implemented yet. */
4898 status = PSA_ERROR_NOT_SUPPORTED;
4899 }
4900#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004901 if( status == PSA_SUCCESS )
4902 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004903 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004904 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004906 }
4907 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004908 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004909 if( status != PSA_SUCCESS )
4910 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004911 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004912 *handle = 0;
4913 }
4914 return( status );
4915}
4916
Gilles Peskineeab56e42018-07-12 17:12:33 +02004917
4918
4919/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004920/* Key derivation */
4921/****************************************************************/
4922
Gilles Peskine969c5d62019-01-16 15:53:06 +01004923static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004926{
Janos Follath5fe19732019-06-20 15:09:30 +01004927 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4928 * initialisers for this union leave some bytes unspecified.) */
4929 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4930
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004932#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4934 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4935 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004936 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004938 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4939 if( hash_size == 0 )
4940 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004941 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4942 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004943 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944 {
4945 return( PSA_ERROR_NOT_SUPPORTED );
4946 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004948 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004949 }
4950#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004951 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004952 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004953}
4954
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004956 psa_algorithm_t alg )
4957{
4958 psa_status_t status;
4959
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004961 return( PSA_ERROR_BAD_STATE );
4962
Gilles Peskine6843c292019-01-18 16:44:49 +01004963 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4964 return( PSA_ERROR_INVALID_ARGUMENT );
4965 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004966 {
4967 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004969 }
4970 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4971 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004973 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 else
4975 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004976
4977 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004979 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980}
4981
4982#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004983static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004984 psa_algorithm_t hash_alg,
4985 psa_key_derivation_step_t step,
4986 const uint8_t *data,
4987 size_t data_length )
4988{
4989 psa_status_t status;
4990 switch( step )
4991 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004992 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004993 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004994 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004995 status = psa_hmac_setup_internal( &hkdf->hmac,
4996 data, data_length,
4997 hash_alg );
4998 if( status != PSA_SUCCESS )
4999 return( status );
5000 hkdf->state = HKDF_STATE_STARTED;
5001 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005002 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005003 /* If no salt was provided, use an empty salt. */
5004 if( hkdf->state == HKDF_STATE_INIT )
5005 {
5006 status = psa_hmac_setup_internal( &hkdf->hmac,
5007 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005008 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005009 if( status != PSA_SUCCESS )
5010 return( status );
5011 hkdf->state = HKDF_STATE_STARTED;
5012 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005013 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005014 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005015 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5016 data, data_length );
5017 if( status != PSA_SUCCESS )
5018 return( status );
5019 status = psa_hmac_finish_internal( &hkdf->hmac,
5020 hkdf->prk,
5021 sizeof( hkdf->prk ) );
5022 if( status != PSA_SUCCESS )
5023 return( status );
5024 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5025 hkdf->block_number = 0;
5026 hkdf->state = HKDF_STATE_KEYED;
5027 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005028 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 if( hkdf->state == HKDF_STATE_OUTPUT )
5030 return( PSA_ERROR_BAD_STATE );
5031 if( hkdf->info_set )
5032 return( PSA_ERROR_BAD_STATE );
5033 hkdf->info_length = data_length;
5034 if( data_length != 0 )
5035 {
5036 hkdf->info = mbedtls_calloc( 1, data_length );
5037 if( hkdf->info == NULL )
5038 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5039 memcpy( hkdf->info, data, data_length );
5040 }
5041 hkdf->info_set = 1;
5042 return( PSA_SUCCESS );
5043 default:
5044 return( PSA_ERROR_INVALID_ARGUMENT );
5045 }
5046}
Janos Follathb03233e2019-06-11 15:30:30 +01005047
Janos Follathf08e2652019-06-13 09:05:41 +01005048static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5049 const uint8_t *data,
5050 size_t data_length )
5051{
5052 if( prf->state != TLS12_PRF_STATE_INIT )
5053 return( PSA_ERROR_BAD_STATE );
5054
Janos Follathd6dce9f2019-07-04 09:11:38 +01005055 if( data_length != 0 )
5056 {
5057 prf->seed = mbedtls_calloc( 1, data_length );
5058 if( prf->seed == NULL )
5059 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005060
Janos Follathd6dce9f2019-07-04 09:11:38 +01005061 memcpy( prf->seed, data, data_length );
5062 prf->seed_length = data_length;
5063 }
Janos Follathf08e2652019-06-13 09:05:41 +01005064
5065 prf->state = TLS12_PRF_STATE_SEED_SET;
5066
5067 return( PSA_SUCCESS );
5068}
5069
Janos Follath81550542019-06-13 14:26:34 +01005070static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5071 psa_algorithm_t hash_alg,
5072 const uint8_t *data,
5073 size_t data_length )
5074{
5075 psa_status_t status;
5076 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5077 return( PSA_ERROR_BAD_STATE );
5078
5079 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5080 if( status != PSA_SUCCESS )
5081 return( status );
5082
5083 prf->state = TLS12_PRF_STATE_KEY_SET;
5084
5085 return( PSA_SUCCESS );
5086}
5087
Janos Follath6660f0e2019-06-17 08:44:03 +01005088static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5089 psa_tls12_prf_key_derivation_t *prf,
5090 psa_algorithm_t hash_alg,
5091 const uint8_t *data,
5092 size_t data_length )
5093{
5094 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005095 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5096 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005097
5098 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5099 return( PSA_ERROR_INVALID_ARGUMENT );
5100
5101 /* Quoting RFC 4279, Section 2:
5102 *
5103 * The premaster secret is formed as follows: if the PSK is N octets
5104 * long, concatenate a uint16 with the value N, N zero octets, a second
5105 * uint16 with the value N, and the PSK itself.
5106 */
5107
Janos Follath40e13932019-06-26 13:22:29 +01005108 *cur++ = ( data_length >> 8 ) & 0xff;
5109 *cur++ = ( data_length >> 0 ) & 0xff;
5110 memset( cur, 0, data_length );
5111 cur += data_length;
5112 *cur++ = pms[0];
5113 *cur++ = pms[1];
5114 memcpy( cur, data, data_length );
5115 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005116
Janos Follath40e13932019-06-26 13:22:29 +01005117 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005118
5119 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5120 return( status );
5121}
5122
Janos Follath63028dd2019-06-13 09:15:47 +01005123static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5124 const uint8_t *data,
5125 size_t data_length )
5126{
5127 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5128 return( PSA_ERROR_BAD_STATE );
5129
Janos Follathd6dce9f2019-07-04 09:11:38 +01005130 if( data_length != 0 )
5131 {
5132 prf->label = mbedtls_calloc( 1, data_length );
5133 if( prf->label == NULL )
5134 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005135
Janos Follathd6dce9f2019-07-04 09:11:38 +01005136 memcpy( prf->label, data, data_length );
5137 prf->label_length = data_length;
5138 }
Janos Follath63028dd2019-06-13 09:15:47 +01005139
5140 prf->state = TLS12_PRF_STATE_LABEL_SET;
5141
5142 return( PSA_SUCCESS );
5143}
5144
Janos Follathb03233e2019-06-11 15:30:30 +01005145static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5146 psa_algorithm_t hash_alg,
5147 psa_key_derivation_step_t step,
5148 const uint8_t *data,
5149 size_t data_length )
5150{
Janos Follathb03233e2019-06-11 15:30:30 +01005151 switch( step )
5152 {
Janos Follathf08e2652019-06-13 09:05:41 +01005153 case PSA_KEY_DERIVATION_INPUT_SEED:
5154 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005155 case PSA_KEY_DERIVATION_INPUT_SECRET:
5156 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005157 case PSA_KEY_DERIVATION_INPUT_LABEL:
5158 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005159 default:
5160 return( PSA_ERROR_INVALID_ARGUMENT );
5161 }
5162}
Janos Follath6660f0e2019-06-17 08:44:03 +01005163
5164static psa_status_t psa_tls12_prf_psk_to_ms_input(
5165 psa_tls12_prf_key_derivation_t *prf,
5166 psa_algorithm_t hash_alg,
5167 psa_key_derivation_step_t step,
5168 const uint8_t *data,
5169 size_t data_length )
5170{
5171 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005172 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005173 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5174 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005175 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005176
5177 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5178}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179#endif /* MBEDTLS_MD_C */
5180
Gilles Peskineb8965192019-09-24 16:21:10 +02005181/** Check whether the given key type is acceptable for the given
5182 * input step of a key derivation.
5183 *
5184 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5185 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5186 * Both secret and non-secret inputs can alternatively have the type
5187 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5188 * that the input was passed as a buffer rather than via a key object.
5189 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005190static int psa_key_derivation_check_input_type(
5191 psa_key_derivation_step_t step,
5192 psa_key_type_t key_type )
5193{
5194 switch( step )
5195 {
5196 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005197 if( key_type == PSA_KEY_TYPE_DERIVE )
5198 return( PSA_SUCCESS );
5199 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005200 return( PSA_SUCCESS );
5201 break;
5202 case PSA_KEY_DERIVATION_INPUT_LABEL:
5203 case PSA_KEY_DERIVATION_INPUT_SALT:
5204 case PSA_KEY_DERIVATION_INPUT_INFO:
5205 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005206 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5207 return( PSA_SUCCESS );
5208 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005209 return( PSA_SUCCESS );
5210 break;
5211 }
5212 return( PSA_ERROR_INVALID_ARGUMENT );
5213}
5214
Janos Follathb80a94e2019-06-12 15:54:46 +01005215static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005217 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005218 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005219 const uint8_t *data,
5220 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005222 psa_status_t status;
5223 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5224
5225 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005226 if( status != PSA_SUCCESS )
5227 goto exit;
5228
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005230 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005231 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005233 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 step, data, data_length );
5235 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005236 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 {
Janos Follathb03233e2019-06-11 15:30:30 +01005238 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5239 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5240 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005241 }
5242 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5243 {
5244 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5245 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5246 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247 }
5248 else
5249#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252 return( PSA_ERROR_BAD_STATE );
5253 }
5254
Gilles Peskine224b0d62019-09-23 18:13:17 +02005255exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005258 return( status );
5259}
5260
Janos Follath51f4a0f2019-06-14 11:35:55 +01005261psa_status_t psa_key_derivation_input_bytes(
5262 psa_key_derivation_operation_t *operation,
5263 psa_key_derivation_step_t step,
5264 const uint8_t *data,
5265 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005266{
Gilles Peskineb8965192019-09-24 16:21:10 +02005267 return( psa_key_derivation_input_internal( operation, step,
5268 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005269 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005270}
5271
Janos Follath51f4a0f2019-06-14 11:35:55 +01005272psa_status_t psa_key_derivation_input_key(
5273 psa_key_derivation_operation_t *operation,
5274 psa_key_derivation_step_t step,
5275 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276{
5277 psa_key_slot_t *slot;
5278 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005279
Gilles Peskine28f8f302019-07-24 13:30:31 +02005280 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005281 PSA_KEY_USAGE_DERIVE,
5282 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005284 {
5285 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005287 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005288
5289 /* Passing a key object as a SECRET input unlocks the permission
5290 * to output to a key object. */
5291 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5292 operation->can_output_key = 1;
5293
Janos Follathb80a94e2019-06-12 15:54:46 +01005294 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005295 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005296 slot->data.raw.data,
5297 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005299
5300
5301
5302/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005303/* Key agreement */
5304/****************************************************************/
5305
Gilles Peskinea05219c2018-11-16 16:02:56 +01005306#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005307static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5308 size_t peer_key_length,
5309 const mbedtls_ecp_keypair *our_key,
5310 uint8_t *shared_secret,
5311 size_t shared_secret_size,
5312 size_t *shared_secret_length )
5313{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005314 mbedtls_ecp_keypair *their_key = NULL;
5315 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005316 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005317 size_t bits = 0;
5318 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005319 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005321 status = psa_import_ec_public_key( curve,
5322 peer_key, peer_key_length,
5323 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005324 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005325 goto exit;
5326
Jaeden Amero97271b32019-01-10 19:38:51 +00005327 status = mbedtls_to_psa_error(
5328 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5329 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005330 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005331 status = mbedtls_to_psa_error(
5332 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5333 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005334 goto exit;
5335
Jaeden Amero97271b32019-01-10 19:38:51 +00005336 status = mbedtls_to_psa_error(
5337 mbedtls_ecdh_calc_secret( &ecdh,
5338 shared_secret_length,
5339 shared_secret, shared_secret_size,
5340 mbedtls_ctr_drbg_random,
5341 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005342 if( status != PSA_SUCCESS )
5343 goto exit;
5344 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5345 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005346
5347exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005348 if( status != PSA_SUCCESS )
5349 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005350 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005351 mbedtls_ecp_keypair_free( their_key );
5352 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005353 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005354}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005355#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005356
Gilles Peskine01d718c2018-09-18 12:01:02 +02005357#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5358
Gilles Peskine0216fe12019-04-11 21:23:21 +02005359static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5360 psa_key_slot_t *private_key,
5361 const uint8_t *peer_key,
5362 size_t peer_key_length,
5363 uint8_t *shared_secret,
5364 size_t shared_secret_size,
5365 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005367 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005368 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005369#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005370 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005371 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005372 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005373 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5374 private_key->data.ecp,
5375 shared_secret, shared_secret_size,
5376 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005377#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005378 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005379 (void) private_key;
5380 (void) peer_key;
5381 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005382 (void) shared_secret;
5383 (void) shared_secret_size;
5384 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005385 return( PSA_ERROR_NOT_SUPPORTED );
5386 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005387}
5388
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005389/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005390 * to potentially free embedded data structures and wipe confidential data.
5391 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005393 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394 psa_key_slot_t *private_key,
5395 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005396 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005397{
5398 psa_status_t status;
5399 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5400 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005402
5403 /* Step 1: run the secret agreement algorithm to generate the shared
5404 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005405 status = psa_key_agreement_raw_internal( ka_alg,
5406 private_key,
5407 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005408 shared_secret,
5409 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005410 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005411 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005412 goto exit;
5413
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005414 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005415 * the shared secret. A shared secret is permitted wherever a key
5416 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005417 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005418 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005419 shared_secret,
5420 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005421
Gilles Peskine12313cd2018-06-20 00:20:32 +02005422exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005423 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005424 return( status );
5425}
5426
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005427psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005428 psa_key_derivation_step_t step,
5429 psa_key_handle_t private_key,
5430 const uint8_t *peer_key,
5431 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005432{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005433 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005434 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005436 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005437 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005438 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005439 if( status != PSA_SUCCESS )
5440 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005442 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005443 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005444 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005446 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005447}
5448
Gilles Peskinebe697d82019-05-16 18:00:41 +02005449psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5450 psa_key_handle_t private_key,
5451 const uint8_t *peer_key,
5452 size_t peer_key_length,
5453 uint8_t *output,
5454 size_t output_size,
5455 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005456{
5457 psa_key_slot_t *slot;
5458 psa_status_t status;
5459
5460 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5461 {
5462 status = PSA_ERROR_INVALID_ARGUMENT;
5463 goto exit;
5464 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005465 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005466 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005467 if( status != PSA_SUCCESS )
5468 goto exit;
5469
5470 status = psa_key_agreement_raw_internal( alg, slot,
5471 peer_key, peer_key_length,
5472 output, output_size,
5473 output_length );
5474
5475exit:
5476 if( status != PSA_SUCCESS )
5477 {
5478 /* If an error happens and is not handled properly, the output
5479 * may be used as a key to protect sensitive data. Arrange for such
5480 * a key to be random, which is likely to result in decryption or
5481 * verification errors. This is better than filling the buffer with
5482 * some constant data such as zeros, which would result in the data
5483 * being protected with a reproducible, easily knowable key.
5484 */
5485 psa_generate_random( output, output_size );
5486 *output_length = output_size;
5487 }
5488 return( status );
5489}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005490
5491
5492/****************************************************************/
5493/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005494/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495
Gilles Peskine4c317f42018-07-12 01:24:09 +02005496psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005497 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005498{
Janos Follath24eed8d2019-11-22 13:21:35 +00005499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005500 GUARD_MODULE_INITIALIZED;
5501
Gilles Peskinef181eca2019-08-07 13:49:00 +02005502 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5503 {
5504 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5505 output,
5506 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5507 if( ret != 0 )
5508 return( mbedtls_to_psa_error( ret ) );
5509 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5510 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5511 }
5512
Gilles Peskinee59236f2018-01-27 23:32:46 +01005513 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5514 return( mbedtls_to_psa_error( ret ) );
5515}
5516
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005517#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5518#include "mbedtls/entropy_poll.h"
5519
Gilles Peskine7228da22019-07-15 11:06:15 +02005520psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005521 size_t seed_size )
5522{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005523 if( global_data.initialized )
5524 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005525
5526 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5527 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5528 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5529 return( PSA_ERROR_INVALID_ARGUMENT );
5530
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005531 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005532}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005533#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005534
Gilles Peskinee56e8782019-04-26 17:34:02 +02005535#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5536static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5537 size_t domain_parameters_size,
5538 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005539{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005540 size_t i;
5541 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005542
Gilles Peskinee56e8782019-04-26 17:34:02 +02005543 if( domain_parameters_size == 0 )
5544 {
5545 *exponent = 65537;
5546 return( PSA_SUCCESS );
5547 }
5548
5549 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5550 * support values that fit in a 32-bit integer, which is larger than
5551 * int on just about every platform anyway. */
5552 if( domain_parameters_size > sizeof( acc ) )
5553 return( PSA_ERROR_NOT_SUPPORTED );
5554 for( i = 0; i < domain_parameters_size; i++ )
5555 acc = ( acc << 8 ) | domain_parameters[i];
5556 if( acc > INT_MAX )
5557 return( PSA_ERROR_NOT_SUPPORTED );
5558 *exponent = acc;
5559 return( PSA_SUCCESS );
5560}
5561#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5562
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005563static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005564 psa_key_slot_t *slot, size_t bits,
5565 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005566{
Gilles Peskine8e338702019-07-30 20:06:31 +02005567 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005568
Gilles Peskinee56e8782019-04-26 17:34:02 +02005569 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005570 return( PSA_ERROR_INVALID_ARGUMENT );
5571
Gilles Peskinee59236f2018-01-27 23:32:46 +01005572 if( key_type_is_raw_bytes( type ) )
5573 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005574 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005575 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5576 if( status != PSA_SUCCESS )
5577 return( status );
5578 status = psa_generate_random( slot->data.raw.data,
5579 slot->data.raw.bytes );
5580 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005581 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005582#if defined(MBEDTLS_DES_C)
5583 if( type == PSA_KEY_TYPE_DES )
5584 psa_des_set_key_parity( slot->data.raw.data,
5585 slot->data.raw.bytes );
5586#endif /* MBEDTLS_DES_C */
5587 }
5588 else
5589
5590#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005591 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592 {
5593 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005595 int exponent;
5596 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005597 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5598 return( PSA_ERROR_NOT_SUPPORTED );
5599 /* Accept only byte-aligned keys, for the same reasons as
5600 * in psa_import_rsa_key(). */
5601 if( bits % 8 != 0 )
5602 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005603 status = psa_read_rsa_exponent( domain_parameters,
5604 domain_parameters_size,
5605 &exponent );
5606 if( status != PSA_SUCCESS )
5607 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005608 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5609 if( rsa == NULL )
5610 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5611 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5612 ret = mbedtls_rsa_gen_key( rsa,
5613 mbedtls_ctr_drbg_random,
5614 &global_data.ctr_drbg,
5615 (unsigned int) bits,
5616 exponent );
5617 if( ret != 0 )
5618 {
5619 mbedtls_rsa_free( rsa );
5620 mbedtls_free( rsa );
5621 return( mbedtls_to_psa_error( ret ) );
5622 }
5623 slot->data.rsa = rsa;
5624 }
5625 else
5626#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5627
5628#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005629 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005630 {
5631 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005632 mbedtls_ecp_group_id grp_id =
5633 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634 const mbedtls_ecp_curve_info *curve_info =
5635 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5636 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005638 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005639 return( PSA_ERROR_NOT_SUPPORTED );
5640 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5641 return( PSA_ERROR_NOT_SUPPORTED );
5642 if( curve_info->bit_size != bits )
5643 return( PSA_ERROR_INVALID_ARGUMENT );
5644 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5645 if( ecp == NULL )
5646 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5647 mbedtls_ecp_keypair_init( ecp );
5648 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5649 mbedtls_ctr_drbg_random,
5650 &global_data.ctr_drbg );
5651 if( ret != 0 )
5652 {
5653 mbedtls_ecp_keypair_free( ecp );
5654 mbedtls_free( ecp );
5655 return( mbedtls_to_psa_error( ret ) );
5656 }
5657 slot->data.ecp = ecp;
5658 }
5659 else
5660#endif /* MBEDTLS_ECP_C */
5661
5662 return( PSA_ERROR_NOT_SUPPORTED );
5663
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005664 return( PSA_SUCCESS );
5665}
Darryl Green0c6575a2018-11-07 16:05:30 +00005666
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005667psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005668 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005669{
5670 psa_status_t status;
5671 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005672 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005673
Gilles Peskine0f84d622019-09-12 19:03:13 +02005674 /* Reject any attempt to create a zero-length key so that we don't
5675 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5676 if( psa_get_key_bits( attributes ) == 0 )
5677 return( PSA_ERROR_INVALID_ARGUMENT );
5678
Gilles Peskinedf179142019-07-15 22:02:14 +02005679 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5680 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005681 if( status != PSA_SUCCESS )
5682 goto exit;
5683
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005684#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5685 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005686 {
Gilles Peskine11792082019-08-06 18:36:36 +02005687 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5688 size_t pubkey_length = 0; /* We don't support this feature yet */
5689 if( drv->key_management == NULL ||
5690 drv->key_management->p_generate == NULL )
5691 {
5692 status = PSA_ERROR_NOT_SUPPORTED;
5693 goto exit;
5694 }
5695 status = drv->key_management->p_generate(
5696 psa_get_se_driver_context( driver ),
5697 slot->data.se.slot_number, attributes,
5698 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005699 }
Gilles Peskine11792082019-08-06 18:36:36 +02005700 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005701#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005703 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005704 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005705 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005706 }
Gilles Peskine11792082019-08-06 18:36:36 +02005707
5708exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005709 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005710 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005711 if( status != PSA_SUCCESS )
5712 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005713 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005714 *handle = 0;
5715 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005716 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005717}
5718
5719
Gilles Peskinee59236f2018-01-27 23:32:46 +01005720
5721/****************************************************************/
5722/* Module setup */
5723/****************************************************************/
5724
Gilles Peskine5e769522018-11-20 21:59:56 +01005725psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5726 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5727 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5728{
5729 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5730 return( PSA_ERROR_BAD_STATE );
5731 global_data.entropy_init = entropy_init;
5732 global_data.entropy_free = entropy_free;
5733 return( PSA_SUCCESS );
5734}
5735
Gilles Peskinee59236f2018-01-27 23:32:46 +01005736void mbedtls_psa_crypto_free( void )
5737{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005738 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005739 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5740 {
5741 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005742 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005743 }
5744 /* Wipe all remaining data, including configuration.
5745 * In particular, this sets all state indicator to the value
5746 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005747 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005748#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005749 /* Unregister all secure element drivers, so that we restart from
5750 * a pristine state. */
5751 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005752#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005753}
5754
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005755#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5756/** Recover a transaction that was interrupted by a power failure.
5757 *
5758 * This function is called during initialization, before psa_crypto_init()
5759 * returns. If this function returns a failure status, the initialization
5760 * fails.
5761 */
5762static psa_status_t psa_crypto_recover_transaction(
5763 const psa_crypto_transaction_t *transaction )
5764{
5765 switch( transaction->unknown.type )
5766 {
5767 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5768 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005769 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005770 * is implemented.
5771 * https://github.com/ARMmbed/mbed-crypto/issues/218
5772 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005773 default:
5774 /* We found an unsupported transaction in the storage.
5775 * We don't know what state the storage is in. Give up. */
5776 return( PSA_ERROR_STORAGE_FAILURE );
5777 }
5778}
5779#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5780
Gilles Peskinee59236f2018-01-27 23:32:46 +01005781psa_status_t psa_crypto_init( void )
5782{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005783 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005784 const unsigned char drbg_seed[] = "PSA";
5785
Gilles Peskinec6b69072018-11-20 21:42:52 +01005786 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787 if( global_data.initialized != 0 )
5788 return( PSA_SUCCESS );
5789
Gilles Peskine5e769522018-11-20 21:59:56 +01005790 /* Set default configuration if
5791 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5792 if( global_data.entropy_init == NULL )
5793 global_data.entropy_init = mbedtls_entropy_init;
5794 if( global_data.entropy_free == NULL )
5795 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005796
Gilles Peskinec6b69072018-11-20 21:42:52 +01005797 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005798 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005799#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5800 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5801 /* The PSA entropy injection feature depends on using NV seed as an entropy
5802 * source. Add NV seed as an entropy source for PSA entropy injection. */
5803 mbedtls_entropy_add_source( &global_data.entropy,
5804 mbedtls_nv_seed_poll, NULL,
5805 MBEDTLS_ENTROPY_BLOCK_SIZE,
5806 MBEDTLS_ENTROPY_SOURCE_STRONG );
5807#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005808 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005809 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005810 status = mbedtls_to_psa_error(
5811 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5812 mbedtls_entropy_func,
5813 &global_data.entropy,
5814 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5815 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005816 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005817 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818
Gilles Peskine66fb1262018-12-10 16:29:04 +01005819 status = psa_initialize_key_slots( );
5820 if( status != PSA_SUCCESS )
5821 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005822
Gilles Peskined9348f22019-10-01 15:22:29 +02005823#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5824 status = psa_init_all_se_drivers( );
5825 if( status != PSA_SUCCESS )
5826 goto exit;
5827#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5828
Gilles Peskine4b734222019-07-24 15:56:31 +02005829#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005830 status = psa_crypto_load_transaction( );
5831 if( status == PSA_SUCCESS )
5832 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005833 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5834 if( status != PSA_SUCCESS )
5835 goto exit;
5836 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005837 }
5838 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5839 {
5840 /* There's no transaction to complete. It's all good. */
5841 status = PSA_SUCCESS;
5842 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005843#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005844
Gilles Peskinec6b69072018-11-20 21:42:52 +01005845 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005846 global_data.initialized = 1;
5847
5848exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005849 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005851 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005852}
5853
5854#endif /* MBEDTLS_PSA_CRYPTO_C */