blob: f031654a606b9229260229bcae4ef974d90d7c25 [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 Peskinec7ef5b32019-12-12 16:58:00 +0100378static psa_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 Peskine34ef7f52018-06-18 20:47:51 +0200385 return( PSA_ECC_CURVE_SECP192R1 );
386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388 return( PSA_ECC_CURVE_SECP224R1 );
389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200391 return( PSA_ECC_CURVE_SECP256R1 );
392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200394 return( PSA_ECC_CURVE_SECP384R1 );
395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200397 return( PSA_ECC_CURVE_SECP521R1 );
398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200400 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200403 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200406 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200412 return( PSA_ECC_CURVE_SECP192K1 );
413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200415 return( PSA_ECC_CURVE_SECP224K1 );
416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200418 return( PSA_ECC_CURVE_SECP256K1 );
419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200421 return( PSA_ECC_CURVE_CURVE448 );
422 default:
423 return( 0 );
424 }
425}
426
Gilles Peskine12313cd2018-06-20 00:20:32 +0200427static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
428{
429 switch( curve )
430 {
431 case PSA_ECC_CURVE_SECP192R1:
432 return( MBEDTLS_ECP_DP_SECP192R1 );
433 case PSA_ECC_CURVE_SECP224R1:
434 return( MBEDTLS_ECP_DP_SECP224R1 );
435 case PSA_ECC_CURVE_SECP256R1:
436 return( MBEDTLS_ECP_DP_SECP256R1 );
437 case PSA_ECC_CURVE_SECP384R1:
438 return( MBEDTLS_ECP_DP_SECP384R1 );
439 case PSA_ECC_CURVE_SECP521R1:
440 return( MBEDTLS_ECP_DP_SECP521R1 );
441 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
442 return( MBEDTLS_ECP_DP_BP256R1 );
443 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
444 return( MBEDTLS_ECP_DP_BP384R1 );
445 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
446 return( MBEDTLS_ECP_DP_BP512R1 );
447 case PSA_ECC_CURVE_CURVE25519:
448 return( MBEDTLS_ECP_DP_CURVE25519 );
449 case PSA_ECC_CURVE_SECP192K1:
450 return( MBEDTLS_ECP_DP_SECP192K1 );
451 case PSA_ECC_CURVE_SECP224K1:
452 return( MBEDTLS_ECP_DP_SECP224K1 );
453 case PSA_ECC_CURVE_SECP256K1:
454 return( MBEDTLS_ECP_DP_SECP256K1 );
455 case PSA_ECC_CURVE_CURVE448:
456 return( MBEDTLS_ECP_DP_CURVE448 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100461#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200462
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
464 size_t bits,
465 struct raw_data *raw )
466{
467 /* Check that the bit size is acceptable for the key type */
468 switch( type )
469 {
470 case PSA_KEY_TYPE_RAW_DATA:
471#if defined(MBEDTLS_MD_C)
472 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200473#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200474 case PSA_KEY_TYPE_DERIVE:
475 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476#if defined(MBEDTLS_AES_C)
477 case PSA_KEY_TYPE_AES:
478 if( bits != 128 && bits != 192 && bits != 256 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
480 break;
481#endif
482#if defined(MBEDTLS_CAMELLIA_C)
483 case PSA_KEY_TYPE_CAMELLIA:
484 if( bits != 128 && bits != 192 && bits != 256 )
485 return( PSA_ERROR_INVALID_ARGUMENT );
486 break;
487#endif
488#if defined(MBEDTLS_DES_C)
489 case PSA_KEY_TYPE_DES:
490 if( bits != 64 && bits != 128 && bits != 192 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
492 break;
493#endif
494#if defined(MBEDTLS_ARC4_C)
495 case PSA_KEY_TYPE_ARC4:
496 if( bits < 8 || bits > 2048 )
497 return( PSA_ERROR_INVALID_ARGUMENT );
498 break;
499#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200500#if defined(MBEDTLS_CHACHA20_C)
501 case PSA_KEY_TYPE_CHACHA20:
502 if( bits != 256 )
503 return( PSA_ERROR_INVALID_ARGUMENT );
504 break;
505#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 default:
507 return( PSA_ERROR_NOT_SUPPORTED );
508 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200509 if( bits % 8 != 0 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511
512 /* Allocate memory for the key */
513 raw->bytes = PSA_BITS_TO_BYTES( bits );
514 raw->data = mbedtls_calloc( 1, raw->bytes );
515 if( raw->data == NULL )
516 {
517 raw->bytes = 0;
518 return( PSA_ERROR_INSUFFICIENT_MEMORY );
519 }
520 return( PSA_SUCCESS );
521}
522
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200523#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100524/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
525 * that are not a multiple of 8) well. For example, there is only
526 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
527 * way to return the exact bit size of a key.
528 * To keep things simple, reject non-byte-aligned key sizes. */
529static psa_status_t psa_check_rsa_key_byte_aligned(
530 const mbedtls_rsa_context *rsa )
531{
532 mbedtls_mpi n;
533 psa_status_t status;
534 mbedtls_mpi_init( &n );
535 status = mbedtls_to_psa_error(
536 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
537 if( status == PSA_SUCCESS )
538 {
539 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
540 status = PSA_ERROR_NOT_SUPPORTED;
541 }
542 mbedtls_mpi_free( &n );
543 return( status );
544}
545
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546static psa_status_t psa_import_rsa_key( psa_key_type_t type,
547 const uint8_t *data,
548 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200549 mbedtls_rsa_context **p_rsa )
550{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000551 psa_status_t status;
552 mbedtls_pk_context pk;
553 mbedtls_rsa_context *rsa;
554 size_t bits;
555
556 mbedtls_pk_init( &pk );
557
558 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200559 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 status = mbedtls_to_psa_error(
561 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200562 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 status = mbedtls_to_psa_error(
564 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
565 if( status != PSA_SUCCESS )
566 goto exit;
567
568 /* We have something that the pkparse module recognizes. If it is a
569 * valid RSA key, store it. */
570 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200571 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000572 status = PSA_ERROR_INVALID_ARGUMENT;
573 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200574 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575
576 rsa = mbedtls_pk_rsa( pk );
577 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
578 * supports non-byte-aligned key sizes, but not well. For example,
579 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
580 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
581 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
582 {
583 status = PSA_ERROR_NOT_SUPPORTED;
584 goto exit;
585 }
586 status = psa_check_rsa_key_byte_aligned( rsa );
587
588exit:
589 /* Free the content of the pk object only on error. */
590 if( status != PSA_SUCCESS )
591 {
592 mbedtls_pk_free( &pk );
593 return( status );
594 }
595
596 /* On success, store the content of the object in the RSA context. */
597 *p_rsa = rsa;
598
599 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600}
601#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
602
Jaeden Ameroccdce902019-01-10 11:42:27 +0000603#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100604static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
605 mbedtls_ecp_keypair **p_ecp )
606{
607 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
608 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
609 if( *p_ecp == NULL )
610 return( PSA_ERROR_INSUFFICIENT_MEMORY );
611 mbedtls_ecp_keypair_init( *p_ecp );
612
613 /* Load the group. */
614 grp_id = mbedtls_ecc_group_of_psa( curve );
615 return( mbedtls_to_psa_error(
616 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
617}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000618
619/* Import a public key given as the uncompressed representation defined by SEC1
620 * 2.3.3 as the content of an ECPoint. */
621static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
622 const uint8_t *data,
623 size_t data_length,
624 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200625{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000627 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628
Gilles Peskine4cd32772019-12-02 20:49:42 +0100629 status = psa_prepare_import_ec_key( curve, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000630 if( status != PSA_SUCCESS )
631 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100632
Jaeden Ameroccdce902019-01-10 11:42:27 +0000633 /* Load the public value. */
634 status = mbedtls_to_psa_error(
635 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
636 data, data_length ) );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
640 /* Check that the point is on the curve. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645
646 *p_ecp = ecp;
647 return( PSA_SUCCESS );
648
649exit:
650 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200651 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000652 mbedtls_ecp_keypair_free( ecp );
653 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200654 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000655 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200656}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658/* Import a private key given as a byte string which is the private value
659 * in big-endian order. */
660static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
661 const uint8_t *data,
662 size_t data_length,
663 mbedtls_ecp_keypair **p_ecp )
664{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100666 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200668 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
669 return( PSA_ERROR_INVALID_ARGUMENT );
670
Gilles Peskine4cd32772019-12-02 20:49:42 +0100671 status = psa_prepare_import_ec_key( curve, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100672 if( status != PSA_SUCCESS )
673 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100674
Gilles Peskinef76aa772018-10-29 19:24:33 +0100675 /* Load the secret value. */
676 status = mbedtls_to_psa_error(
677 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
678 if( status != PSA_SUCCESS )
679 goto exit;
680 /* Validate the private key. */
681 status = mbedtls_to_psa_error(
682 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685 /* Calculate the public key from the private key. */
686 status = mbedtls_to_psa_error(
687 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
688 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
689 if( status != PSA_SUCCESS )
690 goto exit;
691
692 *p_ecp = ecp;
693 return( PSA_SUCCESS );
694
695exit:
696 if( ecp != NULL )
697 {
698 mbedtls_ecp_keypair_free( ecp );
699 mbedtls_free( ecp );
700 }
701 return( status );
702}
703#endif /* defined(MBEDTLS_ECP_C) */
704
Gilles Peskineb46bef22019-07-30 21:32:04 +0200705
706/** Return the size of the key in the given slot, in bits.
707 *
708 * \param[in] slot A key slot.
709 *
710 * \return The key size in bits, read from the metadata in the slot.
711 */
712static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
713{
714 return( slot->attr.bits );
715}
716
717/** Calculate the size of the key in the given slot, in bits.
718 *
719 * \param[in] slot A key slot containing a transparent key.
720 *
721 * \return The key size in bits, calculated from the key data.
722 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200724{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200725 size_t bits = 0; /* return 0 on an empty slot */
726
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200729#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200730 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
731 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200732#endif /* defined(MBEDTLS_RSA_C) */
733#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200734 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
735 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200736#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200737
738 /* We know that the size fits in psa_key_bits_t thanks to checks
739 * when the key was created. */
740 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200741}
742
Gilles Peskine8e338702019-07-30 20:06:31 +0200743/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100744 * previously. This function assumes that the slot does not contain
745 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100746psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
747 const uint8_t *data,
748 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200750 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751
Gilles Peskine8e338702019-07-30 20:06:31 +0200752 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100753 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200755 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100756 if( data_length > SIZE_MAX / 8 )
757 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200758 /* Enforce a size limit, and in particular ensure that the bit
759 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200760 if( bit_size > PSA_MAX_KEY_BITS )
761 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200762 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200763 &slot->data.raw );
764 if( status != PSA_SUCCESS )
765 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200766 if( data_length != 0 )
767 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100768 }
769 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100772 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200773 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100774 data, data_length,
775 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100776 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200777 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100778 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000779 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200780 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781 data, data_length,
782 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 }
784 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100785#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000786#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200787 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200789 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000790 data, data_length,
791 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100792 }
793 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000794#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100795 {
796 return( PSA_ERROR_NOT_SUPPORTED );
797 }
Darryl Green940d72c2018-07-13 13:18:51 +0100798
Gilles Peskineb46bef22019-07-30 21:32:04 +0200799 if( status == PSA_SUCCESS )
800 {
801 /* Write the actual key size to the slot.
802 * psa_start_key_creation() wrote the size declared by the
803 * caller, which may be 0 (meaning unspecified) or wrong. */
804 slot->attr.bits = psa_calculate_key_bits( slot );
805 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100806 return( status );
807}
808
Gilles Peskinef603c712019-01-19 13:40:11 +0100809/** Calculate the intersection of two algorithm usage policies.
810 *
811 * Return 0 (which allows no operation) on incompatibility.
812 */
813static psa_algorithm_t psa_key_policy_algorithm_intersection(
814 psa_algorithm_t alg1,
815 psa_algorithm_t alg2 )
816{
Gilles Peskine549ea862019-05-22 11:45:59 +0200817 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100818 if( alg1 == alg2 )
819 return( alg1 );
820 /* If the policies are from the same hash-and-sign family, check
821 * if one is a wildcard. If so the other has the specific algorithm. */
822 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
823 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
824 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
825 {
826 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
827 return( alg2 );
828 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
829 return( alg1 );
830 }
831 /* If the policies are incompatible, allow nothing. */
832 return( 0 );
833}
834
Gilles Peskined6f371b2019-05-10 19:33:38 +0200835static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
836 psa_algorithm_t requested_alg )
837{
Gilles Peskine549ea862019-05-22 11:45:59 +0200838 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839 if( requested_alg == policy_alg )
840 return( 1 );
841 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200842 * and requested_alg is the same hash-and-sign family with any hash,
843 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200844 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
845 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
846 {
847 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
848 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
849 }
850 /* If it isn't permitted, it's forbidden. */
851 return( 0 );
852}
853
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100854/** Test whether a policy permits an algorithm.
855 *
856 * The caller must test usage flags separately.
857 */
858static int psa_key_policy_permits( const psa_key_policy_t *policy,
859 psa_algorithm_t alg )
860{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200861 return( psa_key_algorithm_permits( policy->alg, alg ) ||
862 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100863}
864
Gilles Peskinef603c712019-01-19 13:40:11 +0100865/** Restrict a key policy based on a constraint.
866 *
867 * \param[in,out] policy The policy to restrict.
868 * \param[in] constraint The policy constraint to apply.
869 *
870 * \retval #PSA_SUCCESS
871 * \c *policy contains the intersection of the original value of
872 * \c *policy and \c *constraint.
873 * \retval #PSA_ERROR_INVALID_ARGUMENT
874 * \c *policy and \c *constraint are incompatible.
875 * \c *policy is unchanged.
876 */
877static psa_status_t psa_restrict_key_policy(
878 psa_key_policy_t *policy,
879 const psa_key_policy_t *constraint )
880{
881 psa_algorithm_t intersection_alg =
882 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200883 psa_algorithm_t intersection_alg2 =
884 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100885 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
886 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200887 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
888 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100889 policy->usage &= constraint->usage;
890 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100892 return( PSA_SUCCESS );
893}
894
Darryl Green06fd18d2018-07-16 11:21:11 +0100895/** Retrieve a slot which must contain a key. The key must have allow all the
896 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
897 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100898static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100899 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100900 psa_key_usage_t usage,
901 psa_algorithm_t alg )
902{
903 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100904 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 *p_slot = NULL;
907
Gilles Peskinec5487a82018-12-03 18:08:14 +0100908 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100909 if( status != PSA_SUCCESS )
910 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100911
912 /* Enforce that usage policy for the key slot contains all the flags
913 * required by the usage parameter. There is one exception: public
914 * keys can always be exported, so we treat public key objects as
915 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200918 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100919 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100920
921 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200922 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100923 return( PSA_ERROR_NOT_PERMITTED );
924
925 *p_slot = slot;
926 return( PSA_SUCCESS );
927}
Darryl Green940d72c2018-07-13 13:18:51 +0100928
Gilles Peskine28f8f302019-07-24 13:30:31 +0200929/** Retrieve a slot which must contain a transparent key.
930 *
931 * A transparent key is a key for which the key material is directly
932 * available, as opposed to a key in a secure element.
933 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200934 * This is a temporary function to use instead of psa_get_key_from_slot()
935 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200936 */
937#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
938static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
939 psa_key_slot_t **p_slot,
940 psa_key_usage_t usage,
941 psa_algorithm_t alg )
942{
943 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
944 if( status != PSA_SUCCESS )
945 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200946 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200947 {
948 *p_slot = NULL;
949 return( PSA_ERROR_NOT_SUPPORTED );
950 }
951 return( PSA_SUCCESS );
952}
953#else /* MBEDTLS_PSA_CRYPTO_SE_C */
954/* With no secure element support, all keys are transparent. */
955#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
956 psa_get_key_from_slot( handle, p_slot, usage, alg )
957#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
958
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100959/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100960static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000961{
Gilles Peskine73167e12019-07-12 23:44:37 +0200962#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
963 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200967 else
968#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200969 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000970 {
971 /* No key material to clean. */
972 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_free( slot->data.raw.data );
976 }
977 else
978#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200979 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000980 {
981 mbedtls_rsa_free( slot->data.rsa );
982 mbedtls_free( slot->data.rsa );
983 }
984 else
985#endif /* defined(MBEDTLS_RSA_C) */
986#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200987 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000988 {
989 mbedtls_ecp_keypair_free( slot->data.ecp );
990 mbedtls_free( slot->data.ecp );
991 }
992 else
993#endif /* defined(MBEDTLS_ECP_C) */
994 {
995 /* Shouldn't happen: the key type is not any type that we
996 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200997 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000998 }
999
1000 return( PSA_SUCCESS );
1001}
1002
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003/** Completely wipe a slot in memory, including its policy.
1004 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001005psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006{
1007 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001008 /* Multipart operations may still be using the key. This is safe
1009 * because all multipart operation objects are independent from
1010 * the key slot: if they need to access the key after the setup
1011 * phase, they have a copy of the key. Note that this means that
1012 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001013 /* At this point, key material and other type-specific content has
1014 * been wiped. Clear remaining metadata. We can call memset and not
1015 * zeroize because the metadata is not particularly sensitive. */
1016 memset( slot, 0, sizeof( *slot ) );
1017 return( status );
1018}
1019
Gilles Peskinec5487a82018-12-03 18:08:14 +01001020psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001022 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001023 psa_status_t status; /* status of the last operation */
1024 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001025#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1026 psa_se_drv_table_entry_t *driver;
1027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028
Gilles Peskine1841cf42019-10-08 15:48:25 +02001029 if( handle == 0 )
1030 return( PSA_SUCCESS );
1031
Gilles Peskinec5487a82018-12-03 18:08:14 +01001032 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 if( status != PSA_SUCCESS )
1034 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001035
1036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001038 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001039 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001040 /* For a key in a secure element, we need to do three things:
1041 * remove the key file in internal storage, destroy the
1042 * key inside the secure element, and update the driver's
1043 * persistent data. Start a transaction that will encompass these
1044 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001045 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001048 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 status = psa_crypto_save_transaction( );
1050 if( status != PSA_SUCCESS )
1051 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001052 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001053 /* We should still try to destroy the key in the secure
1054 * element and the key metadata in storage. This is especially
1055 * important if the error is that the storage is full.
1056 * But how to do it exactly without risking an inconsistent
1057 * state after a reset?
1058 * https://github.com/ARMmbed/mbed-crypto/issues/215
1059 */
1060 overall_status = status;
1061 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001062 }
1063
Gilles Peskine354f7672019-07-12 23:46:38 +02001064 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001065 if( overall_status == PSA_SUCCESS )
1066 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001067 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001068#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1069
Darryl Greend49a4992018-06-18 17:27:26 +01001070#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001071 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001072 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001073 status = psa_destroy_persistent_key( slot->attr.id );
1074 if( overall_status == PSA_SUCCESS )
1075 overall_status = status;
1076
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001077 /* TODO: other slots may have a copy of the same key. We should
1078 * invalidate them.
1079 * https://github.com/ARMmbed/mbed-crypto/issues/214
1080 */
Darryl Greend49a4992018-06-18 17:27:26 +01001081 }
1082#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001083
Gilles Peskinefc762652019-07-22 19:30:34 +02001084#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1085 if( driver != NULL )
1086 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001087 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001088 if( overall_status == PSA_SUCCESS )
1089 overall_status = status;
1090 status = psa_crypto_stop_transaction( );
1091 if( overall_status == PSA_SUCCESS )
1092 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001093 }
1094#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1095
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001096#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1097exit:
1098#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001099 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001100 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1101 if( overall_status == PSA_SUCCESS )
1102 overall_status = status;
1103 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001104}
1105
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001106void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001107{
Gilles Peskineb699f072019-04-26 16:06:02 +02001108 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001109 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001110}
1111
Gilles Peskineb699f072019-04-26 16:06:02 +02001112psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1113 psa_key_type_t type,
1114 const uint8_t *data,
1115 size_t data_length )
1116{
1117 uint8_t *copy = NULL;
1118
1119 if( data_length != 0 )
1120 {
1121 copy = mbedtls_calloc( 1, data_length );
1122 if( copy == NULL )
1123 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1124 memcpy( copy, data, data_length );
1125 }
1126 /* After this point, this function is guaranteed to succeed, so it
1127 * can start modifying `*attributes`. */
1128
1129 if( attributes->domain_parameters != NULL )
1130 {
1131 mbedtls_free( attributes->domain_parameters );
1132 attributes->domain_parameters = NULL;
1133 attributes->domain_parameters_size = 0;
1134 }
1135
1136 attributes->domain_parameters = copy;
1137 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001138 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001139 return( PSA_SUCCESS );
1140}
1141
1142psa_status_t psa_get_key_domain_parameters(
1143 const psa_key_attributes_t *attributes,
1144 uint8_t *data, size_t data_size, size_t *data_length )
1145{
1146 if( attributes->domain_parameters_size > data_size )
1147 return( PSA_ERROR_BUFFER_TOO_SMALL );
1148 *data_length = attributes->domain_parameters_size;
1149 if( attributes->domain_parameters_size != 0 )
1150 memcpy( data, attributes->domain_parameters,
1151 attributes->domain_parameters_size );
1152 return( PSA_SUCCESS );
1153}
1154
1155#if defined(MBEDTLS_RSA_C)
1156static psa_status_t psa_get_rsa_public_exponent(
1157 const mbedtls_rsa_context *rsa,
1158 psa_key_attributes_t *attributes )
1159{
1160 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001162 uint8_t *buffer = NULL;
1163 size_t buflen;
1164 mbedtls_mpi_init( &mpi );
1165
1166 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1167 if( ret != 0 )
1168 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001169 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1170 {
1171 /* It's the default value, which is reported as an empty string,
1172 * so there's nothing to do. */
1173 goto exit;
1174 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001175
1176 buflen = mbedtls_mpi_size( &mpi );
1177 buffer = mbedtls_calloc( 1, buflen );
1178 if( buffer == NULL )
1179 {
1180 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1181 goto exit;
1182 }
1183 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1184 if( ret != 0 )
1185 goto exit;
1186 attributes->domain_parameters = buffer;
1187 attributes->domain_parameters_size = buflen;
1188
1189exit:
1190 mbedtls_mpi_free( &mpi );
1191 if( ret != 0 )
1192 mbedtls_free( buffer );
1193 return( mbedtls_to_psa_error( ret ) );
1194}
1195#endif /* MBEDTLS_RSA_C */
1196
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001197/** Retrieve all the publicly-accessible attributes of a key.
1198 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001199psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1200 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001201{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001202 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001203 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001205 psa_reset_key_attributes( attributes );
1206
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001207 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001208 if( status != PSA_SUCCESS )
1209 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001210
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001211 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001212 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1213 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1214
1215#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1216 if( psa_key_slot_is_external( slot ) )
1217 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1218#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001219
Gilles Peskine8e338702019-07-30 20:06:31 +02001220 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001221 {
1222#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001223 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001226 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001227 * is not yet implemented.
1228 * https://github.com/ARMmbed/mbed-crypto/issues/216
1229 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001230 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001231 break;
1232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001233 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1234 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001235#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001236 default:
1237 /* Nothing else to do. */
1238 break;
1239 }
1240
1241 if( status != PSA_SUCCESS )
1242 psa_reset_key_attributes( attributes );
1243 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244}
1245
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1247psa_status_t psa_get_key_slot_number(
1248 const psa_key_attributes_t *attributes,
1249 psa_key_slot_number_t *slot_number )
1250{
1251 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1252 {
1253 *slot_number = attributes->slot_number;
1254 return( PSA_SUCCESS );
1255 }
1256 else
1257 return( PSA_ERROR_INVALID_ARGUMENT );
1258}
1259#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1260
Jaeden Ameroccdce902019-01-10 11:42:27 +00001261#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001262static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1263 unsigned char *buf, size_t size )
1264{
Janos Follath24eed8d2019-11-22 13:21:35 +00001265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001266 unsigned char *c;
1267 size_t len = 0;
1268
1269 c = buf + size;
1270
1271 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1272
1273 return( (int) len );
1274}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001275#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001276
Gilles Peskinef603c712019-01-19 13:40:11 +01001277static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1278 uint8_t *data,
1279 size_t data_size,
1280 size_t *data_length,
1281 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282{
Gilles Peskine5d309672019-07-12 23:47:28 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1284 const psa_drv_se_t *drv;
1285 psa_drv_se_context_t *drv_context;
1286#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1287
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001288 *data_length = 0;
1289
Gilles Peskine8e338702019-07-30 20:06:31 +02001290 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001291 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001292
Gilles Peskinef9168942019-09-12 19:20:29 +02001293 /* Reject a zero-length output buffer now, since this can never be a
1294 * valid key representation. This way we know that data must be a valid
1295 * pointer and we can do things like memset(data, ..., data_size). */
1296 if( data_size == 0 )
1297 return( PSA_ERROR_BUFFER_TOO_SMALL );
1298
Gilles Peskine5d309672019-07-12 23:47:28 +02001299#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001301 {
1302 psa_drv_se_export_key_t method;
1303 if( drv->key_management == NULL )
1304 return( PSA_ERROR_NOT_SUPPORTED );
1305 method = ( export_public_key ?
1306 drv->key_management->p_export_public :
1307 drv->key_management->p_export );
1308 if( method == NULL )
1309 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001310 return( method( drv_context,
1311 slot->data.se.slot_number,
1312 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001313 }
1314#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1315
Gilles Peskine8e338702019-07-30 20:06:31 +02001316 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 {
1318 if( slot->data.raw.bytes > data_size )
1319 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001320 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1321 memset( data + slot->data.raw.bytes, 0,
1322 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001323 *data_length = slot->data.raw.bytes;
1324 return( PSA_SUCCESS );
1325 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001326#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001327 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001328 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001329 psa_status_t status;
1330
Gilles Peskineb46bef22019-07-30 21:32:04 +02001331 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001332 if( bytes > data_size )
1333 return( PSA_ERROR_BUFFER_TOO_SMALL );
1334 status = mbedtls_to_psa_error(
1335 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1336 if( status != PSA_SUCCESS )
1337 return( status );
1338 memset( data + bytes, 0, data_size - bytes );
1339 *data_length = bytes;
1340 return( PSA_SUCCESS );
1341 }
1342#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001343 else
Moran Peker17e36e12018-05-02 12:55:20 +03001344 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001345#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001346 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1347 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001348 {
Moran Pekera998bc62018-04-16 18:16:20 +03001349 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001351 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001352 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001353#if defined(MBEDTLS_RSA_C)
1354 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001355 pk.pk_info = &mbedtls_rsa_info;
1356 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001357#else
1358 return( PSA_ERROR_NOT_SUPPORTED );
1359#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001360 }
1361 else
1362 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001363#if defined(MBEDTLS_ECP_C)
1364 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001365 pk.pk_info = &mbedtls_eckey_info;
1366 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001367#else
1368 return( PSA_ERROR_NOT_SUPPORTED );
1369#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001370 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001371 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001373 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 }
Moran Peker17e36e12018-05-02 12:55:20 +03001375 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001376 {
Moran Peker17e36e12018-05-02 12:55:20 +03001377 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001378 }
Moran Peker60364322018-04-29 11:34:58 +03001379 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001380 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001381 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001382 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001383 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001384 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1385 * Move the data to the beginning and erase remaining data
1386 * at the original location. */
1387 if( 2 * (size_t) ret <= data_size )
1388 {
1389 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001390 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001391 }
1392 else if( (size_t) ret < data_size )
1393 {
1394 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001395 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001396 }
Moran Pekera998bc62018-04-16 18:16:20 +03001397 *data_length = ret;
1398 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001399 }
1400 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001401#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001402 {
1403 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001404 it is valid for a special-purpose implementation to omit
1405 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001406 return( PSA_ERROR_NOT_SUPPORTED );
1407 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001408 }
1409}
1410
Gilles Peskinec5487a82018-12-03 18:08:14 +01001411psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001412 uint8_t *data,
1413 size_t data_size,
1414 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001415{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001416 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001417 psa_status_t status;
1418
1419 /* Set the key to empty now, so that even when there are errors, we always
1420 * set data_length to a value between 0 and data_size. On error, setting
1421 * the key to empty is a good choice because an empty key representation is
1422 * unlikely to be accepted anywhere. */
1423 *data_length = 0;
1424
1425 /* Export requires the EXPORT flag. There is an exception for public keys,
1426 * which don't require any flag, but psa_get_key_from_slot takes
1427 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001428 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001429 if( status != PSA_SUCCESS )
1430 return( status );
1431 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001432 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001433}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001434
Gilles Peskinec5487a82018-12-03 18:08:14 +01001435psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001436 uint8_t *data,
1437 size_t data_size,
1438 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001439{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001440 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001441 psa_status_t status;
1442
1443 /* Set the key to empty now, so that even when there are errors, we always
1444 * set data_length to a value between 0 and data_size. On error, setting
1445 * the key to empty is a good choice because an empty key representation is
1446 * unlikely to be accepted anywhere. */
1447 *data_length = 0;
1448
1449 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001450 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001451 if( status != PSA_SUCCESS )
1452 return( status );
1453 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001454 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001455}
1456
Gilles Peskine91e8c332019-08-02 19:19:39 +02001457#if defined(static_assert)
1458static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1459 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001460static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001461 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001462static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001463 "One or more key attribute flag is listed as both internal-only and external-only" );
1464#endif
1465
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001466/** Validate that a key policy is internally well-formed.
1467 *
1468 * This function only rejects invalid policies. It does not validate the
1469 * consistency of the policy with respect to other attributes of the key
1470 * such as the key type.
1471 */
1472static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001473{
1474 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001475 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001476 PSA_KEY_USAGE_ENCRYPT |
1477 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001478 PSA_KEY_USAGE_SIGN_HASH |
1479 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001480 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1481 return( PSA_ERROR_INVALID_ARGUMENT );
1482
Gilles Peskine4747d192019-04-17 15:05:45 +02001483 return( PSA_SUCCESS );
1484}
1485
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001486/** Validate the internal consistency of key attributes.
1487 *
1488 * This function only rejects invalid attribute values. If does not
1489 * validate the consistency of the attributes with any key data that may
1490 * be involved in the creation of the key.
1491 *
1492 * Call this function early in the key creation process.
1493 *
1494 * \param[in] attributes Key attributes for the new key.
1495 * \param[out] p_drv On any return, the driver for the key, if any.
1496 * NULL for a transparent key.
1497 *
1498 */
1499static psa_status_t psa_validate_key_attributes(
1500 const psa_key_attributes_t *attributes,
1501 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001502{
1503 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001504
1505 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001506 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507 status = psa_validate_persistent_key_parameters(
1508 attributes->core.lifetime, attributes->core.id,
1509 p_drv, 1 );
1510 if( status != PSA_SUCCESS )
1511 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001512 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513
1514 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001515 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001516 return( status );
1517
1518 /* Refuse to create overly large keys.
1519 * Note that this doesn't trigger on import if the attributes don't
1520 * explicitly specify a size (so psa_get_key_bits returns 0), so
1521 * psa_import_key() needs its own checks. */
1522 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1523 return( PSA_ERROR_NOT_SUPPORTED );
1524
Gilles Peskine91e8c332019-08-02 19:19:39 +02001525 /* Reject invalid flags. These should not be reachable through the API. */
1526 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1527 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1528 return( PSA_ERROR_INVALID_ARGUMENT );
1529
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001530 return( PSA_SUCCESS );
1531}
1532
Gilles Peskine4747d192019-04-17 15:05:45 +02001533/** Prepare a key slot to receive key material.
1534 *
1535 * This function allocates a key slot and sets its metadata.
1536 *
1537 * If this function fails, call psa_fail_key_creation().
1538 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001539 * This function is intended to be used as follows:
1540 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1541 * it with the specified attributes, and assign it a handle.
1542 * -# Populate the slot with the key material.
1543 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1544 * In case of failure at any step, stop the sequence and call
1545 * psa_fail_key_creation().
1546 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001547 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 * \param[in] attributes Key attributes for the new key.
1549 * \param[out] handle On success, a handle for the allocated slot.
1550 * \param[out] p_slot On success, a pointer to the prepared slot.
1551 * \param[out] p_drv On any return, the driver for the key, if any.
1552 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001553 *
1554 * \retval #PSA_SUCCESS
1555 * The key slot is ready to receive key material.
1556 * \return If this function fails, the key slot is an invalid state.
1557 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 */
1559static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001560 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 const psa_key_attributes_t *attributes,
1562 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001563 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001564 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001565{
1566 psa_status_t status;
1567 psa_key_slot_t *slot;
1568
Gilles Peskinedf179142019-07-15 22:02:14 +02001569 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001570 *p_drv = NULL;
1571
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572 status = psa_validate_key_attributes( attributes, p_drv );
1573 if( status != PSA_SUCCESS )
1574 return( status );
1575
Gilles Peskineedbed562019-08-07 18:19:59 +02001576 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 if( status != PSA_SUCCESS )
1578 return( status );
1579 slot = *p_slot;
1580
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001581 /* We're storing the declared bit-size of the key. It's up to each
1582 * creation mechanism to verify that this information is correct.
1583 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001584 * an input (generate, device) but not for those where the bit-size
1585 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001586
1587 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001588
Gilles Peskine91e8c332019-08-02 19:19:39 +02001589 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001590 * external-only flags, query `attributes`. Thanks to the check
1591 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001592 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001593 * may have set. */
1594 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001595
Gilles Peskinecbaff462019-07-12 23:46:04 +02001596#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001597 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001598 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001599 * create the key file in internal storage, create the
1600 * key inside the secure element, and update the driver's
1601 * persistent data. Start a transaction that will encompass these
1602 * three actions. */
1603 /* The first thing to do is to find a slot number for the new key.
1604 * We save the slot number in persistent storage as part of the
1605 * transaction data. It will be needed to recover if the power
1606 * fails during the key creation process, to clean up on the secure
1607 * element side after restarting. Obtaining a slot number from the
1608 * secure element driver updates its persistent state, but we do not yet
1609 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001610 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001611 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001612 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001613 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001614 &slot->data.se.slot_number );
1615 if( status != PSA_SUCCESS )
1616 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001617 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001618 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001619 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001620 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001621 status = psa_crypto_save_transaction( );
1622 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001623 {
1624 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001625 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001626 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001627 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001628
1629 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1630 {
1631 /* Key registration only makes sense with a secure element. */
1632 return( PSA_ERROR_INVALID_ARGUMENT );
1633 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001634#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1635
Darryl Green0c6575a2018-11-07 16:05:30 +00001636 return( status );
1637}
Gilles Peskine4747d192019-04-17 15:05:45 +02001638
1639/** Finalize the creation of a key once its key material has been set.
1640 *
1641 * This entails writing the key to persistent storage.
1642 *
1643 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001644 * See the documentation of psa_start_key_creation() for the intended use
1645 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 * \param[in,out] slot Pointer to the slot with key material.
1648 * \param[in] driver The secure element driver for the key,
1649 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001650 *
1651 * \retval #PSA_SUCCESS
1652 * The key was successfully created. The handle is now valid.
1653 * \return If this function fails, the key slot is an invalid state.
1654 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001656static psa_status_t psa_finish_key_creation(
1657 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001658 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001659{
1660 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001661 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001662 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001663
1664#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001665 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001667#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1668 if( driver != NULL )
1669 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001670 psa_se_key_data_storage_t data;
1671#if defined(static_assert)
1672 static_assert( sizeof( slot->data.se.slot_number ) ==
1673 sizeof( data.slot_number ),
1674 "Slot number size does not match psa_se_key_data_storage_t" );
1675 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1676 "Bit-size size does not match psa_se_key_data_storage_t" );
1677#endif
1678 memcpy( &data.slot_number, &slot->data.se.slot_number,
1679 sizeof( slot->data.se.slot_number ) );
1680 memcpy( &data.bits, &slot->attr.bits,
1681 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001682 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001683 (uint8_t*) &data,
1684 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001685 }
1686 else
1687#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1688 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001689 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001690 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001691 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001692 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1693 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001694 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001695 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1696 status = psa_internal_export_key( slot,
1697 buffer, buffer_size, &length,
1698 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001699 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001700 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001701 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001702
Gilles Peskinef9168942019-09-12 19:20:29 +02001703 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001704 mbedtls_free( buffer );
1705 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001707#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1708
Gilles Peskinecbaff462019-07-12 23:46:04 +02001709#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001710 /* Finish the transaction for a key creation. This does not
1711 * happen when registering an existing key. Detect this case
1712 * by checking whether a transaction is in progress (actual
1713 * creation of a key in a secure element requires a transaction,
1714 * but registration doesn't use one). */
1715 if( driver != NULL &&
1716 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717 {
1718 status = psa_save_se_persistent_data( driver );
1719 if( status != PSA_SUCCESS )
1720 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001721 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001722 return( status );
1723 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001724 status = psa_crypto_stop_transaction( );
1725 if( status != PSA_SUCCESS )
1726 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001727 }
1728#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1729
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 return( status );
1731}
1732
1733/** Abort the creation of a key.
1734 *
1735 * You may call this function after calling psa_start_key_creation(),
1736 * or after psa_finish_key_creation() fails. In other circumstances, this
1737 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001738 * See the documentation of psa_start_key_creation() for the intended use
1739 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * \param[in,out] slot Pointer to the slot with key material.
1742 * \param[in] driver The secure element driver for the key,
1743 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001745static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001746 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001747{
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 (void) driver;
1749
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 if( slot == NULL )
1751 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001752
1753#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001754 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 * element, and the failure happened later (when saving metadata
1756 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001757 * element.
1758 * https://github.com/ARMmbed/mbed-crypto/issues/217
1759 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001760
Gilles Peskined7729582019-08-05 15:55:54 +02001761 /* Abort the ongoing transaction if any (there may not be one if
1762 * the creation process failed before starting one, or if the
1763 * key creation is a registration of a key in a secure element).
1764 * Earlier functions must already have done what it takes to undo any
1765 * partial creation. All that's left is to update the transaction data
1766 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001767 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001768#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1769
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 psa_wipe_key_slot( slot );
1771}
1772
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001773/** Validate optional attributes during key creation.
1774 *
1775 * Some key attributes are optional during key creation. If they are
1776 * specified in the attributes structure, check that they are consistent
1777 * with the data in the slot.
1778 *
1779 * This function should be called near the end of key creation, after
1780 * the slot in memory is fully populated but before saving persistent data.
1781 */
1782static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 const psa_key_slot_t *slot,
1784 const psa_key_attributes_t *attributes )
1785{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001786 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001787 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001788 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001789 return( PSA_ERROR_INVALID_ARGUMENT );
1790 }
1791
1792 if( attributes->domain_parameters_size != 0 )
1793 {
1794#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001795 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001796 {
1797 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001799 mbedtls_mpi_init( &actual );
1800 mbedtls_mpi_init( &required );
1801 ret = mbedtls_rsa_export( slot->data.rsa,
1802 NULL, NULL, NULL, NULL, &actual );
1803 if( ret != 0 )
1804 goto rsa_exit;
1805 ret = mbedtls_mpi_read_binary( &required,
1806 attributes->domain_parameters,
1807 attributes->domain_parameters_size );
1808 if( ret != 0 )
1809 goto rsa_exit;
1810 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1811 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1812 rsa_exit:
1813 mbedtls_mpi_free( &actual );
1814 mbedtls_mpi_free( &required );
1815 if( ret != 0)
1816 return( mbedtls_to_psa_error( ret ) );
1817 }
1818 else
1819#endif
1820 {
1821 return( PSA_ERROR_INVALID_ARGUMENT );
1822 }
1823 }
1824
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001825 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001827 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 return( PSA_ERROR_INVALID_ARGUMENT );
1829 }
1830
1831 return( PSA_SUCCESS );
1832}
1833
Gilles Peskine4747d192019-04-17 15:05:45 +02001834psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001836 size_t data_length,
1837 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001838{
1839 psa_status_t status;
1840 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001841 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001842
Gilles Peskine0f84d622019-09-12 19:03:13 +02001843 /* Reject zero-length symmetric keys (including raw data key objects).
1844 * This also rejects any key which might be encoded as an empty string,
1845 * which is never valid. */
1846 if( data_length == 0 )
1847 return( PSA_ERROR_INVALID_ARGUMENT );
1848
Gilles Peskinedf179142019-07-15 22:02:14 +02001849 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1850 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 if( status != PSA_SUCCESS )
1852 goto exit;
1853
Gilles Peskine5d309672019-07-12 23:47:28 +02001854#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1855 if( driver != NULL )
1856 {
1857 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001858 /* The driver should set the number of key bits, however in
1859 * case it doesn't, we initialize bits to an invalid value. */
1860 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001861 if( drv->key_management == NULL ||
1862 drv->key_management->p_import == NULL )
1863 {
1864 status = PSA_ERROR_NOT_SUPPORTED;
1865 goto exit;
1866 }
1867 status = drv->key_management->p_import(
1868 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001869 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001870 &bits );
1871 if( status != PSA_SUCCESS )
1872 goto exit;
1873 if( bits > PSA_MAX_KEY_BITS )
1874 {
1875 status = PSA_ERROR_NOT_SUPPORTED;
1876 goto exit;
1877 }
1878 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001879 }
1880 else
1881#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1882 {
1883 status = psa_import_key_into_slot( slot, data, data_length );
1884 if( status != PSA_SUCCESS )
1885 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001886 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001887 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001888 if( status != PSA_SUCCESS )
1889 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890
Gilles Peskine011e4282019-06-26 18:34:38 +02001891 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 if( status != PSA_SUCCESS )
1894 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 *handle = 0;
1897 }
1898 return( status );
1899}
1900
Gilles Peskined7729582019-08-05 15:55:54 +02001901#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1902psa_status_t mbedtls_psa_register_se_key(
1903 const psa_key_attributes_t *attributes )
1904{
1905 psa_status_t status;
1906 psa_key_slot_t *slot = NULL;
1907 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001908 psa_key_handle_t handle = 0;
1909
1910 /* Leaving attributes unspecified is not currently supported.
1911 * It could make sense to query the key type and size from the
1912 * secure element, but not all secure elements support this
1913 * and the driver HAL doesn't currently support it. */
1914 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1915 return( PSA_ERROR_NOT_SUPPORTED );
1916 if( psa_get_key_bits( attributes ) == 0 )
1917 return( PSA_ERROR_NOT_SUPPORTED );
1918
1919 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1920 &handle, &slot, &driver );
1921 if( status != PSA_SUCCESS )
1922 goto exit;
1923
Gilles Peskined7729582019-08-05 15:55:54 +02001924 status = psa_finish_key_creation( slot, driver );
1925
1926exit:
1927 if( status != PSA_SUCCESS )
1928 {
1929 psa_fail_key_creation( slot, driver );
1930 }
1931 /* Registration doesn't keep the key in RAM. */
1932 psa_close_key( handle );
1933 return( status );
1934}
1935#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1936
Gilles Peskinef603c712019-01-19 13:40:11 +01001937static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001938 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001939{
1940 psa_status_t status;
1941 uint8_t *buffer = NULL;
1942 size_t buffer_size = 0;
1943 size_t length;
1944
Gilles Peskine8e338702019-07-30 20:06:31 +02001945 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001946 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001947 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001948 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001949 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1951 if( status != PSA_SUCCESS )
1952 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001953 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001954 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001955
1956exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001957 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001958 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001959 return( status );
1960}
1961
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001962psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1963 const psa_key_attributes_t *specified_attributes,
1964 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001965{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001966 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001967 psa_key_slot_t *source_slot = NULL;
1968 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001970 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001971
Gilles Peskine28f8f302019-07-24 13:30:31 +02001972 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001973 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001974 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001975 goto exit;
1976
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001977 status = psa_validate_optional_attributes( source_slot,
1978 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001979 if( status != PSA_SUCCESS )
1980 goto exit;
1981
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001982 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001983 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001984 if( status != PSA_SUCCESS )
1985 goto exit;
1986
Gilles Peskinedf179142019-07-15 22:02:14 +02001987 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1988 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001989 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001993#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1994 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001995 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001996 /* Copying to a secure element is not implemented yet. */
1997 status = PSA_ERROR_NOT_SUPPORTED;
1998 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001999 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002001
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002003 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002005
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 if( status != PSA_SUCCESS )
2009 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002010 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 *target_handle = 0;
2012 }
2013 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002014}
2015
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002016
2017
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002018/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002019/* Message digests */
2020/****************************************************************/
2021
Gilles Peskineb16841e2019-10-10 20:36:12 +02002022#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002023static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002024{
2025 switch( alg )
2026 {
2027#if defined(MBEDTLS_MD2_C)
2028 case PSA_ALG_MD2:
2029 return( &mbedtls_md2_info );
2030#endif
2031#if defined(MBEDTLS_MD4_C)
2032 case PSA_ALG_MD4:
2033 return( &mbedtls_md4_info );
2034#endif
2035#if defined(MBEDTLS_MD5_C)
2036 case PSA_ALG_MD5:
2037 return( &mbedtls_md5_info );
2038#endif
2039#if defined(MBEDTLS_RIPEMD160_C)
2040 case PSA_ALG_RIPEMD160:
2041 return( &mbedtls_ripemd160_info );
2042#endif
2043#if defined(MBEDTLS_SHA1_C)
2044 case PSA_ALG_SHA_1:
2045 return( &mbedtls_sha1_info );
2046#endif
2047#if defined(MBEDTLS_SHA256_C)
2048 case PSA_ALG_SHA_224:
2049 return( &mbedtls_sha224_info );
2050 case PSA_ALG_SHA_256:
2051 return( &mbedtls_sha256_info );
2052#endif
2053#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002054#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002055 case PSA_ALG_SHA_384:
2056 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002057#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 case PSA_ALG_SHA_512:
2059 return( &mbedtls_sha512_info );
2060#endif
2061 default:
2062 return( NULL );
2063 }
2064}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002065#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002066
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2068{
2069 switch( operation->alg )
2070 {
Gilles Peskine81736312018-06-26 15:04:31 +02002071 case 0:
2072 /* The object has (apparently) been initialized but it is not
2073 * in use. It's ok to call abort on such an object, and there's
2074 * nothing to do. */
2075 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002076#if defined(MBEDTLS_MD2_C)
2077 case PSA_ALG_MD2:
2078 mbedtls_md2_free( &operation->ctx.md2 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_MD4_C)
2082 case PSA_ALG_MD4:
2083 mbedtls_md4_free( &operation->ctx.md4 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_MD5_C)
2087 case PSA_ALG_MD5:
2088 mbedtls_md5_free( &operation->ctx.md5 );
2089 break;
2090#endif
2091#if defined(MBEDTLS_RIPEMD160_C)
2092 case PSA_ALG_RIPEMD160:
2093 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2094 break;
2095#endif
2096#if defined(MBEDTLS_SHA1_C)
2097 case PSA_ALG_SHA_1:
2098 mbedtls_sha1_free( &operation->ctx.sha1 );
2099 break;
2100#endif
2101#if defined(MBEDTLS_SHA256_C)
2102 case PSA_ALG_SHA_224:
2103 case PSA_ALG_SHA_256:
2104 mbedtls_sha256_free( &operation->ctx.sha256 );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002108#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002110#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002111 case PSA_ALG_SHA_512:
2112 mbedtls_sha512_free( &operation->ctx.sha512 );
2113 break;
2114#endif
2115 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002116 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117 }
2118 operation->alg = 0;
2119 return( PSA_SUCCESS );
2120}
2121
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002122psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 psa_algorithm_t alg )
2124{
Janos Follath24eed8d2019-11-22 13:21:35 +00002125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002126
2127 /* A context must be freshly initialized before it can be set up. */
2128 if( operation->alg != 0 )
2129 {
2130 return( PSA_ERROR_BAD_STATE );
2131 }
2132
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133 switch( alg )
2134 {
2135#if defined(MBEDTLS_MD2_C)
2136 case PSA_ALG_MD2:
2137 mbedtls_md2_init( &operation->ctx.md2 );
2138 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2139 break;
2140#endif
2141#if defined(MBEDTLS_MD4_C)
2142 case PSA_ALG_MD4:
2143 mbedtls_md4_init( &operation->ctx.md4 );
2144 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2145 break;
2146#endif
2147#if defined(MBEDTLS_MD5_C)
2148 case PSA_ALG_MD5:
2149 mbedtls_md5_init( &operation->ctx.md5 );
2150 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_RIPEMD160_C)
2154 case PSA_ALG_RIPEMD160:
2155 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2156 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_SHA1_C)
2160 case PSA_ALG_SHA_1:
2161 mbedtls_sha1_init( &operation->ctx.sha1 );
2162 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2163 break;
2164#endif
2165#if defined(MBEDTLS_SHA256_C)
2166 case PSA_ALG_SHA_224:
2167 mbedtls_sha256_init( &operation->ctx.sha256 );
2168 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2169 break;
2170 case PSA_ALG_SHA_256:
2171 mbedtls_sha256_init( &operation->ctx.sha256 );
2172 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2173 break;
2174#endif
2175#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002176#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 case PSA_ALG_SHA_384:
2178 mbedtls_sha512_init( &operation->ctx.sha512 );
2179 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2180 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002181#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182 case PSA_ALG_SHA_512:
2183 mbedtls_sha512_init( &operation->ctx.sha512 );
2184 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2185 break;
2186#endif
2187 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002188 return( PSA_ALG_IS_HASH( alg ) ?
2189 PSA_ERROR_NOT_SUPPORTED :
2190 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002191 }
2192 if( ret == 0 )
2193 operation->alg = alg;
2194 else
2195 psa_hash_abort( operation );
2196 return( mbedtls_to_psa_error( ret ) );
2197}
2198
2199psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2200 const uint8_t *input,
2201 size_t input_length )
2202{
Janos Follath24eed8d2019-11-22 13:21:35 +00002203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002204
2205 /* Don't require hash implementations to behave correctly on a
2206 * zero-length input, which may have an invalid pointer. */
2207 if( input_length == 0 )
2208 return( PSA_SUCCESS );
2209
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210 switch( operation->alg )
2211 {
2212#if defined(MBEDTLS_MD2_C)
2213 case PSA_ALG_MD2:
2214 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2215 input, input_length );
2216 break;
2217#endif
2218#if defined(MBEDTLS_MD4_C)
2219 case PSA_ALG_MD4:
2220 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2221 input, input_length );
2222 break;
2223#endif
2224#if defined(MBEDTLS_MD5_C)
2225 case PSA_ALG_MD5:
2226 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2227 input, input_length );
2228 break;
2229#endif
2230#if defined(MBEDTLS_RIPEMD160_C)
2231 case PSA_ALG_RIPEMD160:
2232 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2233 input, input_length );
2234 break;
2235#endif
2236#if defined(MBEDTLS_SHA1_C)
2237 case PSA_ALG_SHA_1:
2238 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2239 input, input_length );
2240 break;
2241#endif
2242#if defined(MBEDTLS_SHA256_C)
2243 case PSA_ALG_SHA_224:
2244 case PSA_ALG_SHA_256:
2245 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2246 input, input_length );
2247 break;
2248#endif
2249#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002250#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002252#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253 case PSA_ALG_SHA_512:
2254 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2255 input, input_length );
2256 break;
2257#endif
2258 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002259 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002261
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 if( ret != 0 )
2263 psa_hash_abort( operation );
2264 return( mbedtls_to_psa_error( ret ) );
2265}
2266
2267psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2268 uint8_t *hash,
2269 size_t hash_size,
2270 size_t *hash_length )
2271{
itayzafrir40835d42018-08-02 13:14:17 +03002272 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002274 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275
2276 /* Fill the output buffer with something that isn't a valid hash
2277 * (barring an attack on the hash and deliberately-crafted input),
2278 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002279 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002280 /* If hash_size is 0 then hash may be NULL and then the
2281 * call to memset would have undefined behavior. */
2282 if( hash_size != 0 )
2283 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284
2285 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002286 {
2287 status = PSA_ERROR_BUFFER_TOO_SMALL;
2288 goto exit;
2289 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290
2291 switch( operation->alg )
2292 {
2293#if defined(MBEDTLS_MD2_C)
2294 case PSA_ALG_MD2:
2295 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_MD4_C)
2299 case PSA_ALG_MD4:
2300 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_MD5_C)
2304 case PSA_ALG_MD5:
2305 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_RIPEMD160_C)
2309 case PSA_ALG_RIPEMD160:
2310 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2311 break;
2312#endif
2313#if defined(MBEDTLS_SHA1_C)
2314 case PSA_ALG_SHA_1:
2315 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2316 break;
2317#endif
2318#if defined(MBEDTLS_SHA256_C)
2319 case PSA_ALG_SHA_224:
2320 case PSA_ALG_SHA_256:
2321 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002325#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002327#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328 case PSA_ALG_SHA_512:
2329 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2330 break;
2331#endif
2332 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002333 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 }
itayzafrir40835d42018-08-02 13:14:17 +03002335 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336
itayzafrir40835d42018-08-02 13:14:17 +03002337exit:
2338 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002340 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 return( psa_hash_abort( operation ) );
2342 }
2343 else
2344 {
2345 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002346 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 }
2348}
2349
Gilles Peskine2d277862018-06-18 15:41:12 +02002350psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2351 const uint8_t *hash,
2352 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353{
2354 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2355 size_t actual_hash_length;
2356 psa_status_t status = psa_hash_finish( operation,
2357 actual_hash, sizeof( actual_hash ),
2358 &actual_hash_length );
2359 if( status != PSA_SUCCESS )
2360 return( status );
2361 if( actual_hash_length != hash_length )
2362 return( PSA_ERROR_INVALID_SIGNATURE );
2363 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2364 return( PSA_ERROR_INVALID_SIGNATURE );
2365 return( PSA_SUCCESS );
2366}
2367
Gilles Peskine0a749c82019-11-28 19:33:58 +01002368psa_status_t psa_hash_compute( psa_algorithm_t alg,
2369 const uint8_t *input, size_t input_length,
2370 uint8_t *hash, size_t hash_size,
2371 size_t *hash_length )
2372{
2373 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2375
2376 *hash_length = hash_size;
2377 status = psa_hash_setup( &operation, alg );
2378 if( status != PSA_SUCCESS )
2379 goto exit;
2380 status = psa_hash_update( &operation, input, input_length );
2381 if( status != PSA_SUCCESS )
2382 goto exit;
2383 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
2386
2387exit:
2388 if( status == PSA_SUCCESS )
2389 status = psa_hash_abort( &operation );
2390 else
2391 psa_hash_abort( &operation );
2392 return( status );
2393}
2394
2395psa_status_t psa_hash_compare( psa_algorithm_t alg,
2396 const uint8_t *input, size_t input_length,
2397 const uint8_t *hash, size_t hash_length )
2398{
2399 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2401
2402 status = psa_hash_setup( &operation, alg );
2403 if( status != PSA_SUCCESS )
2404 goto exit;
2405 status = psa_hash_update( &operation, input, input_length );
2406 if( status != PSA_SUCCESS )
2407 goto exit;
2408 status = psa_hash_verify( &operation, hash, hash_length );
2409 if( status != PSA_SUCCESS )
2410 goto exit;
2411
2412exit:
2413 if( status == PSA_SUCCESS )
2414 status = psa_hash_abort( &operation );
2415 else
2416 psa_hash_abort( &operation );
2417 return( status );
2418}
2419
Gilles Peskineeb35d782019-01-22 17:56:16 +01002420psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2421 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002422{
2423 if( target_operation->alg != 0 )
2424 return( PSA_ERROR_BAD_STATE );
2425
2426 switch( source_operation->alg )
2427 {
2428 case 0:
2429 return( PSA_ERROR_BAD_STATE );
2430#if defined(MBEDTLS_MD2_C)
2431 case PSA_ALG_MD2:
2432 mbedtls_md2_clone( &target_operation->ctx.md2,
2433 &source_operation->ctx.md2 );
2434 break;
2435#endif
2436#if defined(MBEDTLS_MD4_C)
2437 case PSA_ALG_MD4:
2438 mbedtls_md4_clone( &target_operation->ctx.md4,
2439 &source_operation->ctx.md4 );
2440 break;
2441#endif
2442#if defined(MBEDTLS_MD5_C)
2443 case PSA_ALG_MD5:
2444 mbedtls_md5_clone( &target_operation->ctx.md5,
2445 &source_operation->ctx.md5 );
2446 break;
2447#endif
2448#if defined(MBEDTLS_RIPEMD160_C)
2449 case PSA_ALG_RIPEMD160:
2450 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2451 &source_operation->ctx.ripemd160 );
2452 break;
2453#endif
2454#if defined(MBEDTLS_SHA1_C)
2455 case PSA_ALG_SHA_1:
2456 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2457 &source_operation->ctx.sha1 );
2458 break;
2459#endif
2460#if defined(MBEDTLS_SHA256_C)
2461 case PSA_ALG_SHA_224:
2462 case PSA_ALG_SHA_256:
2463 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2464 &source_operation->ctx.sha256 );
2465 break;
2466#endif
2467#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002468#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002470#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002471 case PSA_ALG_SHA_512:
2472 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2473 &source_operation->ctx.sha512 );
2474 break;
2475#endif
2476 default:
2477 return( PSA_ERROR_NOT_SUPPORTED );
2478 }
2479
2480 target_operation->alg = source_operation->alg;
2481 return( PSA_SUCCESS );
2482}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483
2484
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002485/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486/* MAC */
2487/****************************************************************/
2488
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002489static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 psa_algorithm_t alg,
2491 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002492 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002493 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002496 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002498 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002499 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002500
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2502 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002503 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002505 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002506 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 mode = MBEDTLS_MODE_STREAM;
2508 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 case PSA_ALG_CTR:
2510 mode = MBEDTLS_MODE_CTR;
2511 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002512 case PSA_ALG_CFB:
2513 mode = MBEDTLS_MODE_CFB;
2514 break;
2515 case PSA_ALG_OFB:
2516 mode = MBEDTLS_MODE_OFB;
2517 break;
2518 case PSA_ALG_CBC_NO_PADDING:
2519 mode = MBEDTLS_MODE_CBC;
2520 break;
2521 case PSA_ALG_CBC_PKCS7:
2522 mode = MBEDTLS_MODE_CBC;
2523 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002524 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525 mode = MBEDTLS_MODE_CCM;
2526 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002527 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528 mode = MBEDTLS_MODE_GCM;
2529 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002530 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2531 mode = MBEDTLS_MODE_CHACHAPOLY;
2532 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533 default:
2534 return( NULL );
2535 }
2536 }
2537 else if( alg == PSA_ALG_CMAC )
2538 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 else
2540 return( NULL );
2541
2542 switch( key_type )
2543 {
2544 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002545 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 break;
2547 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002548 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2549 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002551 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002553 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002554 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2555 * but two-key Triple-DES is functionally three-key Triple-DES
2556 * with K1=K3, so that's how we present it to mbedtls. */
2557 if( key_bits == 128 )
2558 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 break;
2560 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002561 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562 break;
2563 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002564 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002566 case PSA_KEY_TYPE_CHACHA20:
2567 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2568 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 default:
2570 return( NULL );
2571 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002572 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002573 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574
Jaeden Amero23bbb752018-06-26 14:16:54 +01002575 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2576 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577}
2578
Gilles Peskinea05219c2018-11-16 16:02:56 +01002579#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002580static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002581{
Gilles Peskine2d277862018-06-18 15:41:12 +02002582 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002583 {
2584 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002585 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002588 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002598 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002599 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002601 return( 128 );
2602 default:
2603 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002604 }
2605}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002606#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002607
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002608/* Initialize the MAC operation structure. Once this function has been
2609 * called, psa_mac_abort can run and will do the right thing. */
2610static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2611 psa_algorithm_t alg )
2612{
2613 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2614
2615 operation->alg = alg;
2616 operation->key_set = 0;
2617 operation->iv_set = 0;
2618 operation->iv_required = 0;
2619 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002620 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621
2622#if defined(MBEDTLS_CMAC_C)
2623 if( alg == PSA_ALG_CMAC )
2624 {
2625 operation->iv_required = 0;
2626 mbedtls_cipher_init( &operation->ctx.cmac );
2627 status = PSA_SUCCESS;
2628 }
2629 else
2630#endif /* MBEDTLS_CMAC_C */
2631#if defined(MBEDTLS_MD_C)
2632 if( PSA_ALG_IS_HMAC( operation->alg ) )
2633 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002634 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2635 operation->ctx.hmac.hash_ctx.alg = 0;
2636 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637 }
2638 else
2639#endif /* MBEDTLS_MD_C */
2640 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002641 if( ! PSA_ALG_IS_MAC( alg ) )
2642 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643 }
2644
2645 if( status != PSA_SUCCESS )
2646 memset( operation, 0, sizeof( *operation ) );
2647 return( status );
2648}
2649
Gilles Peskine01126fa2018-07-12 17:04:55 +02002650#if defined(MBEDTLS_MD_C)
2651static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2652{
Gilles Peskine3f108122018-12-07 18:14:53 +01002653 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002654 return( psa_hash_abort( &hmac->hash_ctx ) );
2655}
2656#endif /* MBEDTLS_MD_C */
2657
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2659{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002660 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002662 /* The object has (apparently) been initialized but it is not
2663 * in use. It's ok to call abort on such an object, and there's
2664 * nothing to do. */
2665 return( PSA_SUCCESS );
2666 }
2667 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002669 if( operation->alg == PSA_ALG_CMAC )
2670 {
2671 mbedtls_cipher_free( &operation->ctx.cmac );
2672 }
2673 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002676 if( PSA_ALG_IS_HMAC( operation->alg ) )
2677 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002678 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002679 }
2680 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 {
2683 /* Sanity check (shouldn't happen: operation->alg should
2684 * always have been initialized to a valid value). */
2685 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 }
Moran Peker41deec42018-04-04 15:43:05 +03002687
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688 operation->alg = 0;
2689 operation->key_set = 0;
2690 operation->iv_set = 0;
2691 operation->iv_required = 0;
2692 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002693 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002694
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002696
2697bad_state:
2698 /* If abort is called on an uninitialized object, we can't trust
2699 * anything. Wipe the object in case it contains confidential data.
2700 * This may result in a memory leak if a pointer gets overwritten,
2701 * but it's too late to do anything about this. */
2702 memset( operation, 0, sizeof( *operation ) );
2703 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704}
2705
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002706#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002707static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002709 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710 const mbedtls_cipher_info_t *cipher_info )
2711{
Janos Follath24eed8d2019-11-22 13:21:35 +00002712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713
2714 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715
2716 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2717 if( ret != 0 )
2718 return( ret );
2719
2720 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2721 slot->data.raw.data,
2722 key_bits );
2723 return( ret );
2724}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002725#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002726
Gilles Peskine248051a2018-06-20 16:09:38 +02002727#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002728static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2729 const uint8_t *key,
2730 size_t key_length,
2731 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002733 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002735 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002736 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737 psa_status_t status;
2738
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002739 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2740 * overflow below. This should never trigger if the hash algorithm
2741 * is implemented correctly. */
2742 /* The size checks against the ipad and opad buffers cannot be written
2743 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2744 * because that triggers -Wlogical-op on GCC 7.3. */
2745 if( block_size > sizeof( ipad ) )
2746 return( PSA_ERROR_NOT_SUPPORTED );
2747 if( block_size > sizeof( hmac->opad ) )
2748 return( PSA_ERROR_NOT_SUPPORTED );
2749 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002750 return( PSA_ERROR_NOT_SUPPORTED );
2751
Gilles Peskined223b522018-06-11 18:12:58 +02002752 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002753 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002754 status = psa_hash_compute( hash_alg, key, key_length,
2755 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002757 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002758 }
Gilles Peskine96889972018-07-12 17:07:03 +02002759 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2760 * but it is permitted. It is common when HMAC is used in HKDF, for
2761 * example. Don't call `memcpy` in the 0-length because `key` could be
2762 * an invalid pointer which would make the behavior undefined. */
2763 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002764 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765
Gilles Peskined223b522018-06-11 18:12:58 +02002766 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2767 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002768 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002769 ipad[i] ^= 0x36;
2770 memset( ipad + key_length, 0x36, block_size - key_length );
2771
2772 /* Copy the key material from ipad to opad, flipping the requisite bits,
2773 * and filling the rest of opad with the requisite constant. */
2774 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002775 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2776 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777
Gilles Peskine01126fa2018-07-12 17:04:55 +02002778 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002780 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002781
Gilles Peskine01126fa2018-07-12 17:04:55 +02002782 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002783
2784cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002785 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002786
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002787 return( status );
2788}
Gilles Peskine248051a2018-06-20 16:09:38 +02002789#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002790
Gilles Peskine89167cb2018-07-08 20:12:23 +02002791static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002792 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002793 psa_algorithm_t alg,
2794 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002797 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002799 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002800 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002801 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002802 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002804 /* A context must be freshly initialized before it can be set up. */
2805 if( operation->alg != 0 )
2806 {
2807 return( PSA_ERROR_BAD_STATE );
2808 }
2809
Gilles Peskined911eb72018-08-14 15:18:45 +02002810 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002811 if( status != PSA_SUCCESS )
2812 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002813 if( is_sign )
2814 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815
Gilles Peskine28f8f302019-07-24 13:30:31 +02002816 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002817 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002819 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002820
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002822 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 {
2824 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002825 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002826 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002828 if( cipher_info == NULL )
2829 {
2830 status = PSA_ERROR_NOT_SUPPORTED;
2831 goto exit;
2832 }
2833 operation->mac_size = cipher_info->block_size;
2834 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2835 status = mbedtls_to_psa_error( ret );
2836 }
2837 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002840 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002841 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002842 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002843 if( hash_alg == 0 )
2844 {
2845 status = PSA_ERROR_NOT_SUPPORTED;
2846 goto exit;
2847 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002848
2849 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2850 /* Sanity check. This shouldn't fail on a valid configuration. */
2851 if( operation->mac_size == 0 ||
2852 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2853 {
2854 status = PSA_ERROR_NOT_SUPPORTED;
2855 goto exit;
2856 }
2857
Gilles Peskine8e338702019-07-30 20:06:31 +02002858 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002859 {
2860 status = PSA_ERROR_INVALID_ARGUMENT;
2861 goto exit;
2862 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002863
Gilles Peskine01126fa2018-07-12 17:04:55 +02002864 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2865 slot->data.raw.data,
2866 slot->data.raw.bytes,
2867 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 }
2869 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002872 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002873 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002875
Gilles Peskined911eb72018-08-14 15:18:45 +02002876 if( truncated == 0 )
2877 {
2878 /* The "normal" case: untruncated algorithm. Nothing to do. */
2879 }
2880 else if( truncated < 4 )
2881 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002882 /* A very short MAC is too short for security since it can be
2883 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2884 * so we make this our minimum, even though 32 bits is still
2885 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002886 status = PSA_ERROR_NOT_SUPPORTED;
2887 }
2888 else if( truncated > operation->mac_size )
2889 {
2890 /* It's impossible to "truncate" to a larger length. */
2891 status = PSA_ERROR_INVALID_ARGUMENT;
2892 }
2893 else
2894 operation->mac_size = truncated;
2895
Gilles Peskinefbfac682018-07-08 20:51:54 +02002896exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002899 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 else
2902 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 operation->key_set = 1;
2904 }
2905 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906}
2907
Gilles Peskine89167cb2018-07-08 20:12:23 +02002908psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002909 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002910 psa_algorithm_t alg )
2911{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002912 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002913}
2914
2915psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002916 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002917 psa_algorithm_t alg )
2918{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002919 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920}
2921
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2923 const uint8_t *input,
2924 size_t input_length )
2925{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002926 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002928 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002930 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 operation->has_input = 1;
2932
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002934 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002935 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002936 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2937 input, input_length );
2938 status = mbedtls_to_psa_error( ret );
2939 }
2940 else
2941#endif /* MBEDTLS_CMAC_C */
2942#if defined(MBEDTLS_MD_C)
2943 if( PSA_ALG_IS_HMAC( operation->alg ) )
2944 {
2945 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2946 input_length );
2947 }
2948 else
2949#endif /* MBEDTLS_MD_C */
2950 {
2951 /* This shouldn't happen if `operation` was initialized by
2952 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002953 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002954 }
2955
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 if( status != PSA_SUCCESS )
2957 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002958 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959}
2960
Gilles Peskine01126fa2018-07-12 17:04:55 +02002961#if defined(MBEDTLS_MD_C)
2962static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2963 uint8_t *mac,
2964 size_t mac_size )
2965{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002966 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2968 size_t hash_size = 0;
2969 size_t block_size = psa_get_hash_block_size( hash_alg );
2970 psa_status_t status;
2971
Gilles Peskine01126fa2018-07-12 17:04:55 +02002972 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2973 if( status != PSA_SUCCESS )
2974 return( status );
2975 /* From here on, tmp needs to be wiped. */
2976
2977 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2978 if( status != PSA_SUCCESS )
2979 goto exit;
2980
2981 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2982 if( status != PSA_SUCCESS )
2983 goto exit;
2984
2985 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2986 if( status != PSA_SUCCESS )
2987 goto exit;
2988
Gilles Peskined911eb72018-08-14 15:18:45 +02002989 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2990 if( status != PSA_SUCCESS )
2991 goto exit;
2992
2993 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002994
2995exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002996 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997 return( status );
2998}
2999#endif /* MBEDTLS_MD_C */
3000
mohammad16036df908f2018-04-02 08:34:15 -07003001static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003002 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003003 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003005 if( ! operation->key_set )
3006 return( PSA_ERROR_BAD_STATE );
3007 if( operation->iv_required && ! operation->iv_set )
3008 return( PSA_ERROR_BAD_STATE );
3009
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 if( mac_size < operation->mac_size )
3011 return( PSA_ERROR_BUFFER_TOO_SMALL );
3012
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003014 if( operation->alg == PSA_ALG_CMAC )
3015 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003016 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3017 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3018 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003019 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003020 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003021 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003023 else
3024#endif /* MBEDTLS_CMAC_C */
3025#if defined(MBEDTLS_MD_C)
3026 if( PSA_ALG_IS_HMAC( operation->alg ) )
3027 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003028 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003029 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003030 }
3031 else
3032#endif /* MBEDTLS_MD_C */
3033 {
3034 /* This shouldn't happen if `operation` was initialized by
3035 * a setup function. */
3036 return( PSA_ERROR_BAD_STATE );
3037 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038}
3039
Gilles Peskineacd4be32018-07-08 19:56:25 +02003040psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3041 uint8_t *mac,
3042 size_t mac_size,
3043 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003044{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003045 psa_status_t status;
3046
Jaeden Amero252ef282019-02-15 14:05:35 +00003047 if( operation->alg == 0 )
3048 {
3049 return( PSA_ERROR_BAD_STATE );
3050 }
3051
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003052 /* Fill the output buffer with something that isn't a valid mac
3053 * (barring an attack on the mac and deliberately-crafted input),
3054 * in case the caller doesn't check the return status properly. */
3055 *mac_length = mac_size;
3056 /* If mac_size is 0 then mac may be NULL and then the
3057 * call to memset would have undefined behavior. */
3058 if( mac_size != 0 )
3059 memset( mac, '!', mac_size );
3060
Gilles Peskine89167cb2018-07-08 20:12:23 +02003061 if( ! operation->is_sign )
3062 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003063 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003064 }
mohammad16036df908f2018-04-02 08:34:15 -07003065
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003066 status = psa_mac_finish_internal( operation, mac, mac_size );
3067
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003068 if( status == PSA_SUCCESS )
3069 {
3070 status = psa_mac_abort( operation );
3071 if( status == PSA_SUCCESS )
3072 *mac_length = operation->mac_size;
3073 else
3074 memset( mac, '!', mac_size );
3075 }
3076 else
3077 psa_mac_abort( operation );
3078 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003079}
3080
Gilles Peskineacd4be32018-07-08 19:56:25 +02003081psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3082 const uint8_t *mac,
3083 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084{
Gilles Peskine828ed142018-06-18 23:25:51 +02003085 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003086 psa_status_t status;
3087
Jaeden Amero252ef282019-02-15 14:05:35 +00003088 if( operation->alg == 0 )
3089 {
3090 return( PSA_ERROR_BAD_STATE );
3091 }
3092
Gilles Peskine89167cb2018-07-08 20:12:23 +02003093 if( operation->is_sign )
3094 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003095 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003096 }
3097 if( operation->mac_size != mac_length )
3098 {
3099 status = PSA_ERROR_INVALID_SIGNATURE;
3100 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003101 }
mohammad16036df908f2018-04-02 08:34:15 -07003102
3103 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003104 actual_mac, sizeof( actual_mac ) );
3105
3106 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3107 status = PSA_ERROR_INVALID_SIGNATURE;
3108
3109cleanup:
3110 if( status == PSA_SUCCESS )
3111 status = psa_mac_abort( operation );
3112 else
3113 psa_mac_abort( operation );
3114
Gilles Peskine3f108122018-12-07 18:14:53 +01003115 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003116
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003117 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003118}
3119
3120
Gilles Peskine20035e32018-02-03 22:44:14 +01003121
Gilles Peskine20035e32018-02-03 22:44:14 +01003122/****************************************************************/
3123/* Asymmetric cryptography */
3124/****************************************************************/
3125
Gilles Peskine2b450e32018-06-27 15:42:46 +02003126#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003127/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003128 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003129static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3130 size_t hash_length,
3131 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003132{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003133 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003134 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003135 *md_alg = mbedtls_md_get_type( md_info );
3136
3137 /* The Mbed TLS RSA module uses an unsigned int for hash length
3138 * parameters. Validate that it fits so that we don't risk an
3139 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003140#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003141 if( hash_length > UINT_MAX )
3142 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003143#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003144
3145#if defined(MBEDTLS_PKCS1_V15)
3146 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3147 * must be correct. */
3148 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3149 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003150 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003151 if( md_info == NULL )
3152 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003153 if( mbedtls_md_get_size( md_info ) != hash_length )
3154 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003155 }
3156#endif /* MBEDTLS_PKCS1_V15 */
3157
3158#if defined(MBEDTLS_PKCS1_V21)
3159 /* PSS requires a hash internally. */
3160 if( PSA_ALG_IS_RSA_PSS( alg ) )
3161 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003162 if( md_info == NULL )
3163 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003164 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003165#endif /* MBEDTLS_PKCS1_V21 */
3166
Gilles Peskine61b91d42018-06-08 16:09:36 +02003167 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003168}
3169
Gilles Peskine2b450e32018-06-27 15:42:46 +02003170static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3171 psa_algorithm_t alg,
3172 const uint8_t *hash,
3173 size_t hash_length,
3174 uint8_t *signature,
3175 size_t signature_size,
3176 size_t *signature_length )
3177{
3178 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003180 mbedtls_md_type_t md_alg;
3181
3182 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3183 if( status != PSA_SUCCESS )
3184 return( status );
3185
Gilles Peskine630a18a2018-06-29 17:49:35 +02003186 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003187 return( PSA_ERROR_BUFFER_TOO_SMALL );
3188
3189#if defined(MBEDTLS_PKCS1_V15)
3190 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3191 {
3192 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3193 MBEDTLS_MD_NONE );
3194 ret = mbedtls_rsa_pkcs1_sign( rsa,
3195 mbedtls_ctr_drbg_random,
3196 &global_data.ctr_drbg,
3197 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003198 md_alg,
3199 (unsigned int) hash_length,
3200 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003201 signature );
3202 }
3203 else
3204#endif /* MBEDTLS_PKCS1_V15 */
3205#if defined(MBEDTLS_PKCS1_V21)
3206 if( PSA_ALG_IS_RSA_PSS( alg ) )
3207 {
3208 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3209 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3210 mbedtls_ctr_drbg_random,
3211 &global_data.ctr_drbg,
3212 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003213 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003214 (unsigned int) hash_length,
3215 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003216 signature );
3217 }
3218 else
3219#endif /* MBEDTLS_PKCS1_V21 */
3220 {
3221 return( PSA_ERROR_INVALID_ARGUMENT );
3222 }
3223
3224 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003225 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003226 return( mbedtls_to_psa_error( ret ) );
3227}
3228
3229static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3230 psa_algorithm_t alg,
3231 const uint8_t *hash,
3232 size_t hash_length,
3233 const uint8_t *signature,
3234 size_t signature_length )
3235{
3236 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003238 mbedtls_md_type_t md_alg;
3239
3240 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3241 if( status != PSA_SUCCESS )
3242 return( status );
3243
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003244 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3245 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003246
3247#if defined(MBEDTLS_PKCS1_V15)
3248 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3249 {
3250 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3251 MBEDTLS_MD_NONE );
3252 ret = mbedtls_rsa_pkcs1_verify( rsa,
3253 mbedtls_ctr_drbg_random,
3254 &global_data.ctr_drbg,
3255 MBEDTLS_RSA_PUBLIC,
3256 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003257 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003258 hash,
3259 signature );
3260 }
3261 else
3262#endif /* MBEDTLS_PKCS1_V15 */
3263#if defined(MBEDTLS_PKCS1_V21)
3264 if( PSA_ALG_IS_RSA_PSS( alg ) )
3265 {
3266 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3267 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3268 mbedtls_ctr_drbg_random,
3269 &global_data.ctr_drbg,
3270 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003271 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003272 (unsigned int) hash_length,
3273 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003274 signature );
3275 }
3276 else
3277#endif /* MBEDTLS_PKCS1_V21 */
3278 {
3279 return( PSA_ERROR_INVALID_ARGUMENT );
3280 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003281
3282 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3283 * the rest of the signature is invalid". This has little use in
3284 * practice and PSA doesn't report this distinction. */
3285 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3286 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003287 return( mbedtls_to_psa_error( ret ) );
3288}
3289#endif /* MBEDTLS_RSA_C */
3290
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003291#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003292/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3293 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3294 * (even though these functions don't modify it). */
3295static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3296 psa_algorithm_t alg,
3297 const uint8_t *hash,
3298 size_t hash_length,
3299 uint8_t *signature,
3300 size_t signature_size,
3301 size_t *signature_length )
3302{
Janos Follath24eed8d2019-11-22 13:21:35 +00003303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003304 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003305 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003306 mbedtls_mpi_init( &r );
3307 mbedtls_mpi_init( &s );
3308
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003309 if( signature_size < 2 * curve_bytes )
3310 {
3311 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3312 goto cleanup;
3313 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003314
Gilles Peskinea05219c2018-11-16 16:02:56 +01003315#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003316 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3317 {
3318 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3319 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3320 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003321 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3322 &ecp->d, hash,
3323 hash_length, md_alg,
3324 mbedtls_ctr_drbg_random,
3325 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003326 }
3327 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003328#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003329 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003330 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003331 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3332 hash, hash_length,
3333 mbedtls_ctr_drbg_random,
3334 &global_data.ctr_drbg ) );
3335 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003336
3337 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3338 signature,
3339 curve_bytes ) );
3340 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3341 signature + curve_bytes,
3342 curve_bytes ) );
3343
3344cleanup:
3345 mbedtls_mpi_free( &r );
3346 mbedtls_mpi_free( &s );
3347 if( ret == 0 )
3348 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003349 return( mbedtls_to_psa_error( ret ) );
3350}
3351
3352static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3353 const uint8_t *hash,
3354 size_t hash_length,
3355 const uint8_t *signature,
3356 size_t signature_length )
3357{
Janos Follath24eed8d2019-11-22 13:21:35 +00003358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003359 mbedtls_mpi r, s;
3360 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3361 mbedtls_mpi_init( &r );
3362 mbedtls_mpi_init( &s );
3363
3364 if( signature_length != 2 * curve_bytes )
3365 return( PSA_ERROR_INVALID_SIGNATURE );
3366
3367 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3368 signature,
3369 curve_bytes ) );
3370 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3371 signature + curve_bytes,
3372 curve_bytes ) );
3373
3374 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3375 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003376
3377cleanup:
3378 mbedtls_mpi_free( &r );
3379 mbedtls_mpi_free( &s );
3380 return( mbedtls_to_psa_error( ret ) );
3381}
3382#endif /* MBEDTLS_ECDSA_C */
3383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003384psa_status_t psa_sign_hash( psa_key_handle_t handle,
3385 psa_algorithm_t alg,
3386 const uint8_t *hash,
3387 size_t hash_length,
3388 uint8_t *signature,
3389 size_t signature_size,
3390 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003392 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003393 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3395 const psa_drv_se_t *drv;
3396 psa_drv_se_context_t *drv_context;
3397#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003398
3399 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003400 /* Immediately reject a zero-length signature buffer. This guarantees
3401 * that signature must be a valid pointer. (On the other hand, the hash
3402 * buffer can in principle be empty since it doesn't actually have
3403 * to be a hash.) */
3404 if( signature_size == 0 )
3405 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003407 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003408 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003409 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003410 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 {
3412 status = PSA_ERROR_INVALID_ARGUMENT;
3413 goto exit;
3414 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003415
Gilles Peskineedc64242019-08-07 21:05:07 +02003416#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3417 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3418 {
3419 if( drv->asymmetric == NULL ||
3420 drv->asymmetric->p_sign == NULL )
3421 {
3422 status = PSA_ERROR_NOT_SUPPORTED;
3423 goto exit;
3424 }
3425 status = drv->asymmetric->p_sign( drv_context,
3426 slot->data.se.slot_number,
3427 alg,
3428 hash, hash_length,
3429 signature, signature_size,
3430 signature_length );
3431 }
3432 else
3433#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003434#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003435 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003436 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003437 status = psa_rsa_sign( slot->data.rsa,
3438 alg,
3439 hash, hash_length,
3440 signature, signature_size,
3441 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003442 }
3443 else
3444#endif /* defined(MBEDTLS_RSA_C) */
3445#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003446 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003447 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003448#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003449 if(
3450#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3451 PSA_ALG_IS_ECDSA( alg )
3452#else
3453 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3454#endif
3455 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003456 status = psa_ecdsa_sign( slot->data.ecp,
3457 alg,
3458 hash, hash_length,
3459 signature, signature_size,
3460 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003461 else
3462#endif /* defined(MBEDTLS_ECDSA_C) */
3463 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003464 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003465 }
itayzafrir5c753392018-05-08 11:18:38 +03003466 }
3467 else
3468#endif /* defined(MBEDTLS_ECP_C) */
3469 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003470 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003471 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003472
3473exit:
3474 /* Fill the unused part of the output buffer (the whole buffer on error,
3475 * the trailing part on success) with something that isn't a valid mac
3476 * (barring an attack on the mac and deliberately-crafted input),
3477 * in case the caller doesn't check the return status properly. */
3478 if( status == PSA_SUCCESS )
3479 memset( signature + *signature_length, '!',
3480 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003481 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003482 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003483 /* If signature_size is 0 then we have nothing to do. We must not call
3484 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003485 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003486}
3487
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003488psa_status_t psa_verify_hash( psa_key_handle_t handle,
3489 psa_algorithm_t alg,
3490 const uint8_t *hash,
3491 size_t hash_length,
3492 const uint8_t *signature,
3493 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003494{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003495 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003496 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003497#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3498 const psa_drv_se_t *drv;
3499 psa_drv_se_context_t *drv_context;
3500#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003501
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003502 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003503 if( status != PSA_SUCCESS )
3504 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003505
Gilles Peskineedc64242019-08-07 21:05:07 +02003506#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3507 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3508 {
3509 if( drv->asymmetric == NULL ||
3510 drv->asymmetric->p_verify == NULL )
3511 return( PSA_ERROR_NOT_SUPPORTED );
3512 return( drv->asymmetric->p_verify( drv_context,
3513 slot->data.se.slot_number,
3514 alg,
3515 hash, hash_length,
3516 signature, signature_length ) );
3517 }
3518 else
3519#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003520#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003521 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003523 return( psa_rsa_verify( slot->data.rsa,
3524 alg,
3525 hash, hash_length,
3526 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527 }
3528 else
3529#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003530#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003531 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003532 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003533#if defined(MBEDTLS_ECDSA_C)
3534 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003535 return( psa_ecdsa_verify( slot->data.ecp,
3536 hash, hash_length,
3537 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538 else
3539#endif /* defined(MBEDTLS_ECDSA_C) */
3540 {
3541 return( PSA_ERROR_INVALID_ARGUMENT );
3542 }
itayzafrir5c753392018-05-08 11:18:38 +03003543 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003544 else
3545#endif /* defined(MBEDTLS_ECP_C) */
3546 {
3547 return( PSA_ERROR_NOT_SUPPORTED );
3548 }
3549}
3550
Gilles Peskine072ac562018-06-30 00:21:29 +02003551#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3552static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3553 mbedtls_rsa_context *rsa )
3554{
3555 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3556 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3557 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3558 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3559}
3560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3561
Gilles Peskinec5487a82018-12-03 18:08:14 +01003562psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003563 psa_algorithm_t alg,
3564 const uint8_t *input,
3565 size_t input_length,
3566 const uint8_t *salt,
3567 size_t salt_length,
3568 uint8_t *output,
3569 size_t output_size,
3570 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003572 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003573 psa_status_t status;
3574
Darryl Green5cc689a2018-07-24 15:34:10 +01003575 (void) input;
3576 (void) input_length;
3577 (void) salt;
3578 (void) output;
3579 (void) output_size;
3580
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003581 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003583 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3584 return( PSA_ERROR_INVALID_ARGUMENT );
3585
Gilles Peskine28f8f302019-07-24 13:30:31 +02003586 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003587 if( status != PSA_SUCCESS )
3588 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003589 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3590 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592
3593#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003594 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003595 {
3596 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003598 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003599 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003601 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003603 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3604 mbedtls_ctr_drbg_random,
3605 &global_data.ctr_drbg,
3606 MBEDTLS_RSA_PUBLIC,
3607 input_length,
3608 input,
3609 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 }
3611 else
3612#endif /* MBEDTLS_PKCS1_V15 */
3613#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003614 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003616 psa_rsa_oaep_set_padding_mode( alg, rsa );
3617 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3618 mbedtls_ctr_drbg_random,
3619 &global_data.ctr_drbg,
3620 MBEDTLS_RSA_PUBLIC,
3621 salt, salt_length,
3622 input_length,
3623 input,
3624 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003625 }
3626 else
3627#endif /* MBEDTLS_PKCS1_V21 */
3628 {
3629 return( PSA_ERROR_INVALID_ARGUMENT );
3630 }
3631 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003632 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 return( mbedtls_to_psa_error( ret ) );
3634 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003636#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637 {
3638 return( PSA_ERROR_NOT_SUPPORTED );
3639 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003640}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641
Gilles Peskinec5487a82018-12-03 18:08:14 +01003642psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003643 psa_algorithm_t alg,
3644 const uint8_t *input,
3645 size_t input_length,
3646 const uint8_t *salt,
3647 size_t salt_length,
3648 uint8_t *output,
3649 size_t output_size,
3650 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003652 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003653 psa_status_t status;
3654
Darryl Green5cc689a2018-07-24 15:34:10 +01003655 (void) input;
3656 (void) input_length;
3657 (void) salt;
3658 (void) output;
3659 (void) output_size;
3660
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003661 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003663 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3664 return( PSA_ERROR_INVALID_ARGUMENT );
3665
Gilles Peskine28f8f302019-07-24 13:30:31 +02003666 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003667 if( status != PSA_SUCCESS )
3668 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003669 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 return( PSA_ERROR_INVALID_ARGUMENT );
3671
3672#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003673 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 {
3675 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677
Gilles Peskine630a18a2018-06-29 17:49:35 +02003678 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 return( PSA_ERROR_INVALID_ARGUMENT );
3680
3681#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003682 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003684 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3685 mbedtls_ctr_drbg_random,
3686 &global_data.ctr_drbg,
3687 MBEDTLS_RSA_PRIVATE,
3688 output_length,
3689 input,
3690 output,
3691 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003692 }
3693 else
3694#endif /* MBEDTLS_PKCS1_V15 */
3695#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003696 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003698 psa_rsa_oaep_set_padding_mode( alg, rsa );
3699 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3700 mbedtls_ctr_drbg_random,
3701 &global_data.ctr_drbg,
3702 MBEDTLS_RSA_PRIVATE,
3703 salt, salt_length,
3704 output_length,
3705 input,
3706 output,
3707 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003708 }
3709 else
3710#endif /* MBEDTLS_PKCS1_V21 */
3711 {
3712 return( PSA_ERROR_INVALID_ARGUMENT );
3713 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003714
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 return( mbedtls_to_psa_error( ret ) );
3716 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003718#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719 {
3720 return( PSA_ERROR_NOT_SUPPORTED );
3721 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003722}
Gilles Peskine20035e32018-02-03 22:44:14 +01003723
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003724
3725
mohammad1603503973b2018-03-12 15:59:30 +02003726/****************************************************************/
3727/* Symmetric cryptography */
3728/****************************************************************/
3729
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003730/* Initialize the cipher operation structure. Once this function has been
3731 * called, psa_cipher_abort can run and will do the right thing. */
3732static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3733 psa_algorithm_t alg )
3734{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003735 if( ! PSA_ALG_IS_CIPHER( alg ) )
3736 {
3737 memset( operation, 0, sizeof( *operation ) );
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 }
3740
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003741 operation->alg = alg;
3742 operation->key_set = 0;
3743 operation->iv_set = 0;
3744 operation->iv_required = 1;
3745 operation->iv_size = 0;
3746 operation->block_size = 0;
3747 mbedtls_cipher_init( &operation->ctx.cipher );
3748 return( PSA_SUCCESS );
3749}
3750
Gilles Peskinee553c652018-06-04 16:22:46 +02003751static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003752 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003753 psa_algorithm_t alg,
3754 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003755{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003756 int ret = 0;
3757 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003758 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003759 size_t key_bits;
3760 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003761 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3762 PSA_KEY_USAGE_ENCRYPT :
3763 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003764
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003765 /* A context must be freshly initialized before it can be set up. */
3766 if( operation->alg != 0 )
3767 {
3768 return( PSA_ERROR_BAD_STATE );
3769 }
3770
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003771 status = psa_cipher_init( operation, alg );
3772 if( status != PSA_SUCCESS )
3773 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003774
Gilles Peskine28f8f302019-07-24 13:30:31 +02003775 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003776 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003777 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003778 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003779
Gilles Peskine8e338702019-07-30 20:06:31 +02003780 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003781 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003782 {
3783 status = PSA_ERROR_NOT_SUPPORTED;
3784 goto exit;
3785 }
mohammad1603503973b2018-03-12 15:59:30 +02003786
mohammad1603503973b2018-03-12 15:59:30 +02003787 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003788 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003789 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003790
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003791#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003792 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003793 {
3794 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003795 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003796 memcpy( keys, slot->data.raw.data, 16 );
3797 memcpy( keys + 16, slot->data.raw.data, 8 );
3798 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3799 keys,
3800 192, cipher_operation );
3801 }
3802 else
3803#endif
3804 {
3805 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3806 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003807 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003808 }
Moran Peker41deec42018-04-04 15:43:05 +03003809 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003810 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003811
mohammad16038481e742018-03-18 13:57:31 +02003812#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003813 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003814 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003815 case PSA_ALG_CBC_NO_PADDING:
3816 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3817 MBEDTLS_PADDING_NONE );
3818 break;
3819 case PSA_ALG_CBC_PKCS7:
3820 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3821 MBEDTLS_PADDING_PKCS7 );
3822 break;
3823 default:
3824 /* The algorithm doesn't involve padding. */
3825 ret = 0;
3826 break;
3827 }
3828 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003829 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003830#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3831
mohammad1603503973b2018-03-12 15:59:30 +02003832 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003833 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003834 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003835 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003836 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003837 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003838 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003839#if defined(MBEDTLS_CHACHA20_C)
3840 else
3841 if( alg == PSA_ALG_CHACHA20 )
3842 operation->iv_size = 12;
3843#endif
mohammad1603503973b2018-03-12 15:59:30 +02003844
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003845exit:
3846 if( status == 0 )
3847 status = mbedtls_to_psa_error( ret );
3848 if( status != 0 )
3849 psa_cipher_abort( operation );
3850 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003851}
3852
Gilles Peskinefe119512018-07-08 21:39:34 +02003853psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003854 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003855 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003856{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003857 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003858}
3859
Gilles Peskinefe119512018-07-08 21:39:34 +02003860psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003861 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003862 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003863{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003864 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003865}
3866
Gilles Peskinefe119512018-07-08 21:39:34 +02003867psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003868 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003869 size_t iv_size,
3870 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003871{
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003874 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003875 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003876 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003877 }
Moran Peker41deec42018-04-04 15:43:05 +03003878 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003879 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003880 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003881 goto exit;
3882 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003883 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3884 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003885 if( ret != 0 )
3886 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003887 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003888 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003889 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003890
mohammad16038481e742018-03-18 13:57:31 +02003891 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003893
Moran Peker395db872018-05-31 14:07:14 +03003894exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003896 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003898}
3899
Gilles Peskinefe119512018-07-08 21:39:34 +02003900psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003901 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003902 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003903{
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003906 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003908 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003909 }
Moran Pekera28258c2018-05-29 16:25:04 +03003910 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003911 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 status = PSA_ERROR_INVALID_ARGUMENT;
3913 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003914 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3916 status = mbedtls_to_psa_error( ret );
3917exit:
3918 if( status == PSA_SUCCESS )
3919 operation->iv_set = 1;
3920 else
mohammad1603503973b2018-03-12 15:59:30 +02003921 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003923}
3924
Gilles Peskinee553c652018-06-04 16:22:46 +02003925psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3926 const uint8_t *input,
3927 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003928 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003929 size_t output_size,
3930 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003931{
itayzafrir534bd7c2018-08-02 13:56:32 +03003932 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003934 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003935
3936 if( operation->alg == 0 )
3937 {
3938 return( PSA_ERROR_BAD_STATE );
3939 }
3940
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003941 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003942 {
3943 /* Take the unprocessed partial block left over from previous
3944 * update calls, if any, plus the input to this call. Remove
3945 * the last partial block, if any. You get the data that will be
3946 * output in this call. */
3947 expected_output_size =
3948 ( operation->ctx.cipher.unprocessed_len + input_length )
3949 / operation->block_size * operation->block_size;
3950 }
3951 else
3952 {
3953 expected_output_size = input_length;
3954 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003955
Gilles Peskine89d789c2018-06-04 17:17:16 +02003956 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003957 {
3958 status = PSA_ERROR_BUFFER_TOO_SMALL;
3959 goto exit;
3960 }
mohammad160382759612018-03-12 18:16:40 +02003961
mohammad1603503973b2018-03-12 15:59:30 +02003962 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003963 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003964 status = mbedtls_to_psa_error( ret );
3965exit:
3966 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003967 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003968 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003969}
3970
Gilles Peskinee553c652018-06-04 16:22:46 +02003971psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3972 uint8_t *output,
3973 size_t output_size,
3974 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003975{
David Saadab4ecc272019-02-14 13:48:10 +02003976 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003977 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003978 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003979
mohammad1603503973b2018-03-12 15:59:30 +02003980 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003981 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003982 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003983 }
3984 if( operation->iv_required && ! operation->iv_set )
3985 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003986 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003987 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003988
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003989 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003990 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3991 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003992 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003993 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003994 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003995 }
3996
Janos Follath315b51c2018-07-09 16:04:51 +01003997 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3998 temp_output_buffer,
3999 output_length );
4000 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004001 {
Janos Follath315b51c2018-07-09 16:04:51 +01004002 status = mbedtls_to_psa_error( cipher_ret );
4003 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004004 }
Janos Follath315b51c2018-07-09 16:04:51 +01004005
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004006 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004007 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004008 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004009 memcpy( output, temp_output_buffer, *output_length );
4010 else
4011 {
Janos Follath315b51c2018-07-09 16:04:51 +01004012 status = PSA_ERROR_BUFFER_TOO_SMALL;
4013 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004014 }
mohammad1603503973b2018-03-12 15:59:30 +02004015
Gilles Peskine3f108122018-12-07 18:14:53 +01004016 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004017 status = psa_cipher_abort( operation );
4018
4019 return( status );
4020
4021error:
4022
4023 *output_length = 0;
4024
Gilles Peskine3f108122018-12-07 18:14:53 +01004025 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004026 (void) psa_cipher_abort( operation );
4027
4028 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004029}
4030
Gilles Peskinee553c652018-06-04 16:22:46 +02004031psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4032{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004033 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004034 {
4035 /* The object has (apparently) been initialized but it is not
4036 * in use. It's ok to call abort on such an object, and there's
4037 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004038 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004039 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004040
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004041 /* Sanity check (shouldn't happen: operation->alg should
4042 * always have been initialized to a valid value). */
4043 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4044 return( PSA_ERROR_BAD_STATE );
4045
mohammad1603503973b2018-03-12 15:59:30 +02004046 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004047
Moran Peker41deec42018-04-04 15:43:05 +03004048 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004049 operation->key_set = 0;
4050 operation->iv_set = 0;
4051 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004052 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004053 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004054
Moran Peker395db872018-05-31 14:07:14 +03004055 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004056}
4057
Gilles Peskinea0655c32018-04-30 17:06:50 +02004058
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004059
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004060
mohammad16035955c982018-04-26 00:53:03 +03004061/****************************************************************/
4062/* AEAD */
4063/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004064
Gilles Peskineedf9a652018-08-17 18:11:56 +02004065typedef struct
4066{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004067 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004068 const mbedtls_cipher_info_t *cipher_info;
4069 union
4070 {
4071#if defined(MBEDTLS_CCM_C)
4072 mbedtls_ccm_context ccm;
4073#endif /* MBEDTLS_CCM_C */
4074#if defined(MBEDTLS_GCM_C)
4075 mbedtls_gcm_context gcm;
4076#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004077#if defined(MBEDTLS_CHACHAPOLY_C)
4078 mbedtls_chachapoly_context chachapoly;
4079#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004080 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004081 psa_algorithm_t core_alg;
4082 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004083 uint8_t tag_length;
4084} aead_operation_t;
4085
Gilles Peskine30a9e412019-01-14 18:36:12 +01004086static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004088 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089 {
4090#if defined(MBEDTLS_CCM_C)
4091 case PSA_ALG_CCM:
4092 mbedtls_ccm_free( &operation->ctx.ccm );
4093 break;
4094#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004095#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 case PSA_ALG_GCM:
4097 mbedtls_gcm_free( &operation->ctx.gcm );
4098 break;
4099#endif /* MBEDTLS_GCM_C */
4100 }
4101}
4102
4103static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004104 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004105 psa_key_usage_t usage,
4106 psa_algorithm_t alg )
4107{
4108 psa_status_t status;
4109 size_t key_bits;
4110 mbedtls_cipher_id_t cipher_id;
4111
Gilles Peskine28f8f302019-07-24 13:30:31 +02004112 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004113 if( status != PSA_SUCCESS )
4114 return( status );
4115
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004116 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117
4118 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004119 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004120 &cipher_id );
4121 if( operation->cipher_info == NULL )
4122 return( PSA_ERROR_NOT_SUPPORTED );
4123
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004124 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125 {
4126#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004127 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4128 operation->core_alg = PSA_ALG_CCM;
4129 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004130 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4131 * The call to mbedtls_ccm_encrypt_and_tag or
4132 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004133 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004134 return( PSA_ERROR_INVALID_ARGUMENT );
4135 mbedtls_ccm_init( &operation->ctx.ccm );
4136 status = mbedtls_to_psa_error(
4137 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4138 operation->slot->data.raw.data,
4139 (unsigned int) key_bits ) );
4140 if( status != 0 )
4141 goto cleanup;
4142 break;
4143#endif /* MBEDTLS_CCM_C */
4144
4145#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004146 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4147 operation->core_alg = PSA_ALG_GCM;
4148 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004149 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4150 * The call to mbedtls_gcm_crypt_and_tag or
4151 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004152 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004153 return( PSA_ERROR_INVALID_ARGUMENT );
4154 mbedtls_gcm_init( &operation->ctx.gcm );
4155 status = mbedtls_to_psa_error(
4156 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4157 operation->slot->data.raw.data,
4158 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004159 if( status != 0 )
4160 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 break;
4162#endif /* MBEDTLS_GCM_C */
4163
Gilles Peskine26869f22019-05-06 15:25:00 +02004164#if defined(MBEDTLS_CHACHAPOLY_C)
4165 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4166 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4167 operation->full_tag_length = 16;
4168 /* We only support the default tag length. */
4169 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4170 return( PSA_ERROR_NOT_SUPPORTED );
4171 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4172 status = mbedtls_to_psa_error(
4173 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4174 operation->slot->data.raw.data ) );
4175 if( status != 0 )
4176 goto cleanup;
4177 break;
4178#endif /* MBEDTLS_CHACHAPOLY_C */
4179
Gilles Peskineedf9a652018-08-17 18:11:56 +02004180 default:
4181 return( PSA_ERROR_NOT_SUPPORTED );
4182 }
4183
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004184 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4185 {
4186 status = PSA_ERROR_INVALID_ARGUMENT;
4187 goto cleanup;
4188 }
4189 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004190
Gilles Peskineedf9a652018-08-17 18:11:56 +02004191 return( PSA_SUCCESS );
4192
4193cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004194 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004195 return( status );
4196}
4197
Gilles Peskinec5487a82018-12-03 18:08:14 +01004198psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004199 psa_algorithm_t alg,
4200 const uint8_t *nonce,
4201 size_t nonce_length,
4202 const uint8_t *additional_data,
4203 size_t additional_data_length,
4204 const uint8_t *plaintext,
4205 size_t plaintext_length,
4206 uint8_t *ciphertext,
4207 size_t ciphertext_size,
4208 size_t *ciphertext_length )
4209{
mohammad16035955c982018-04-26 00:53:03 +03004210 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004211 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004212 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004213
mohammad1603f08a5502018-06-03 15:05:47 +03004214 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004215
Gilles Peskinec5487a82018-12-03 18:08:14 +01004216 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004217 if( status != PSA_SUCCESS )
4218 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004219
Gilles Peskineedf9a652018-08-17 18:11:56 +02004220 /* For all currently supported modes, the tag is at the end of the
4221 * ciphertext. */
4222 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4223 {
4224 status = PSA_ERROR_BUFFER_TOO_SMALL;
4225 goto exit;
4226 }
4227 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004228
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004229#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004230 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004231 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004232 status = mbedtls_to_psa_error(
4233 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4234 MBEDTLS_GCM_ENCRYPT,
4235 plaintext_length,
4236 nonce, nonce_length,
4237 additional_data, additional_data_length,
4238 plaintext, ciphertext,
4239 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004240 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004241 else
4242#endif /* MBEDTLS_GCM_C */
4243#if defined(MBEDTLS_CCM_C)
4244 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004245 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004246 status = mbedtls_to_psa_error(
4247 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4248 plaintext_length,
4249 nonce, nonce_length,
4250 additional_data,
4251 additional_data_length,
4252 plaintext, ciphertext,
4253 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004254 }
mohammad16035c8845f2018-05-09 05:40:09 -07004255 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004256#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004257#if defined(MBEDTLS_CHACHAPOLY_C)
4258 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4259 {
4260 if( nonce_length != 12 || operation.tag_length != 16 )
4261 {
4262 status = PSA_ERROR_NOT_SUPPORTED;
4263 goto exit;
4264 }
4265 status = mbedtls_to_psa_error(
4266 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4267 plaintext_length,
4268 nonce,
4269 additional_data,
4270 additional_data_length,
4271 plaintext,
4272 ciphertext,
4273 tag ) );
4274 }
4275 else
4276#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004277 {
mohammad1603554faad2018-06-03 15:07:38 +03004278 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004279 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004280
Gilles Peskineedf9a652018-08-17 18:11:56 +02004281 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4282 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004283
Gilles Peskineedf9a652018-08-17 18:11:56 +02004284exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004285 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004286 if( status == PSA_SUCCESS )
4287 *ciphertext_length = plaintext_length + operation.tag_length;
4288 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004289}
4290
Gilles Peskineee652a32018-06-01 19:23:52 +02004291/* Locate the tag in a ciphertext buffer containing the encrypted data
4292 * followed by the tag. Return the length of the part preceding the tag in
4293 * *plaintext_length. This is the size of the plaintext in modes where
4294 * the encrypted data has the same size as the plaintext, such as
4295 * CCM and GCM. */
4296static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4297 const uint8_t *ciphertext,
4298 size_t ciphertext_length,
4299 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004300 const uint8_t **p_tag )
4301{
4302 size_t payload_length;
4303 if( tag_length > ciphertext_length )
4304 return( PSA_ERROR_INVALID_ARGUMENT );
4305 payload_length = ciphertext_length - tag_length;
4306 if( payload_length > plaintext_size )
4307 return( PSA_ERROR_BUFFER_TOO_SMALL );
4308 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004309 return( PSA_SUCCESS );
4310}
4311
Gilles Peskinec5487a82018-12-03 18:08:14 +01004312psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004313 psa_algorithm_t alg,
4314 const uint8_t *nonce,
4315 size_t nonce_length,
4316 const uint8_t *additional_data,
4317 size_t additional_data_length,
4318 const uint8_t *ciphertext,
4319 size_t ciphertext_length,
4320 uint8_t *plaintext,
4321 size_t plaintext_size,
4322 size_t *plaintext_length )
4323{
mohammad16035955c982018-04-26 00:53:03 +03004324 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004325 aead_operation_t operation;
4326 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004327
Gilles Peskineee652a32018-06-01 19:23:52 +02004328 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004329
Gilles Peskinec5487a82018-12-03 18:08:14 +01004330 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004331 if( status != PSA_SUCCESS )
4332 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004333
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004334 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4335 ciphertext, ciphertext_length,
4336 plaintext_size, &tag );
4337 if( status != PSA_SUCCESS )
4338 goto exit;
4339
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004340#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004341 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004342 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004343 status = mbedtls_to_psa_error(
4344 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4345 ciphertext_length - operation.tag_length,
4346 nonce, nonce_length,
4347 additional_data,
4348 additional_data_length,
4349 tag, operation.tag_length,
4350 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004351 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004352 else
4353#endif /* MBEDTLS_GCM_C */
4354#if defined(MBEDTLS_CCM_C)
4355 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004356 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004357 status = mbedtls_to_psa_error(
4358 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4359 ciphertext_length - operation.tag_length,
4360 nonce, nonce_length,
4361 additional_data,
4362 additional_data_length,
4363 ciphertext, plaintext,
4364 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004365 }
mohammad160339574652018-06-01 04:39:53 -07004366 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004367#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004368#if defined(MBEDTLS_CHACHAPOLY_C)
4369 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4370 {
4371 if( nonce_length != 12 || operation.tag_length != 16 )
4372 {
4373 status = PSA_ERROR_NOT_SUPPORTED;
4374 goto exit;
4375 }
4376 status = mbedtls_to_psa_error(
4377 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4378 ciphertext_length - operation.tag_length,
4379 nonce,
4380 additional_data,
4381 additional_data_length,
4382 tag,
4383 ciphertext,
4384 plaintext ) );
4385 }
4386 else
4387#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004388 {
mohammad1603554faad2018-06-03 15:07:38 +03004389 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004390 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004391
Gilles Peskineedf9a652018-08-17 18:11:56 +02004392 if( status != PSA_SUCCESS && plaintext_size != 0 )
4393 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004394
Gilles Peskineedf9a652018-08-17 18:11:56 +02004395exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004396 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397 if( status == PSA_SUCCESS )
4398 *plaintext_length = ciphertext_length - operation.tag_length;
4399 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004400}
4401
Gilles Peskinea0655c32018-04-30 17:06:50 +02004402
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004403
Gilles Peskine20035e32018-02-03 22:44:14 +01004404/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004405/* Generators */
4406/****************************************************************/
4407
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004408#define HKDF_STATE_INIT 0 /* no input yet */
4409#define HKDF_STATE_STARTED 1 /* got salt */
4410#define HKDF_STATE_KEYED 2 /* got key */
4411#define HKDF_STATE_OUTPUT 3 /* output started */
4412
Gilles Peskinecbe66502019-05-16 16:59:18 +02004413static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004415{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4417 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004418 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004420}
4421
4422
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004424{
4425 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004428 {
4429 /* The object has (apparently) been initialized but it is not
4430 * in use. It's ok to call abort on such an object, and there's
4431 * nothing to do. */
4432 }
4433 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004434#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004435 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004436 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 mbedtls_free( operation->ctx.hkdf.info );
4438 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004439 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004442 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004443 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004444 if( operation->ctx.tls12_prf.seed != NULL )
4445 {
4446 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4447 operation->ctx.tls12_prf.seed_length );
4448 mbedtls_free( operation->ctx.tls12_prf.seed );
4449 }
4450
4451 if( operation->ctx.tls12_prf.label != NULL )
4452 {
4453 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4454 operation->ctx.tls12_prf.label_length );
4455 mbedtls_free( operation->ctx.tls12_prf.label );
4456 }
4457
4458 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4459
4460 /* We leave the fields Ai and output_block to be erased safely by the
4461 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004462 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004463 else
4464#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004465 {
4466 status = PSA_ERROR_BAD_STATE;
4467 }
Janos Follath083036a2019-06-11 10:22:26 +01004468 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004469 return( status );
4470}
4471
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004473 size_t *capacity)
4474{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004476 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004478 return PSA_ERROR_BAD_STATE;
4479 }
4480
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004481 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004482 return( PSA_SUCCESS );
4483}
4484
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004486 size_t capacity )
4487{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004489 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004491 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004493 return( PSA_SUCCESS );
4494}
4495
Gilles Peskinebef7f142018-07-12 17:22:21 +02004496#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004498 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004499static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004500 psa_algorithm_t hash_alg,
4501 uint8_t *output,
4502 size_t output_length )
4503{
4504 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4505 psa_status_t status;
4506
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004507 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4508 return( PSA_ERROR_BAD_STATE );
4509 hkdf->state = HKDF_STATE_OUTPUT;
4510
Gilles Peskinebef7f142018-07-12 17:22:21 +02004511 while( output_length != 0 )
4512 {
4513 /* Copy what remains of the current block */
4514 uint8_t n = hash_length - hkdf->offset_in_block;
4515 if( n > output_length )
4516 n = (uint8_t) output_length;
4517 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4518 output += n;
4519 output_length -= n;
4520 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004521 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004522 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004523 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004524 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004525 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004526 * object was corrupted or if this function is called directly
4527 * inside the library. */
4528 if( hkdf->block_number == 0xff )
4529 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004530
4531 /* We need a new block */
4532 ++hkdf->block_number;
4533 hkdf->offset_in_block = 0;
4534 status = psa_hmac_setup_internal( &hkdf->hmac,
4535 hkdf->prk, hash_length,
4536 hash_alg );
4537 if( status != PSA_SUCCESS )
4538 return( status );
4539 if( hkdf->block_number != 1 )
4540 {
4541 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4542 hkdf->output_block,
4543 hash_length );
4544 if( status != PSA_SUCCESS )
4545 return( status );
4546 }
4547 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4548 hkdf->info,
4549 hkdf->info_length );
4550 if( status != PSA_SUCCESS )
4551 return( status );
4552 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4553 &hkdf->block_number, 1 );
4554 if( status != PSA_SUCCESS )
4555 return( status );
4556 status = psa_hmac_finish_internal( &hkdf->hmac,
4557 hkdf->output_block,
4558 sizeof( hkdf->output_block ) );
4559 if( status != PSA_SUCCESS )
4560 return( status );
4561 }
4562
4563 return( PSA_SUCCESS );
4564}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004565
Janos Follath7742fee2019-06-17 12:58:10 +01004566static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4567 psa_tls12_prf_key_derivation_t *tls12_prf,
4568 psa_algorithm_t alg )
4569{
4570 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4571 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004572 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4573 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004574
Janos Follath7742fee2019-06-17 12:58:10 +01004575 /* We can't be wanting more output after block 0xff, otherwise
4576 * the capacity check in psa_key_derivation_output_bytes() would have
4577 * prevented this call. It could happen only if the operation
4578 * object was corrupted or if this function is called directly
4579 * inside the library. */
4580 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004581 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004582
4583 /* We need a new block */
4584 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004585 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004586
4587 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4588 *
4589 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4590 *
4591 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4592 * HMAC_hash(secret, A(2) + seed) +
4593 * HMAC_hash(secret, A(3) + seed) + ...
4594 *
4595 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004596 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004597 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004598 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004599 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004600 * is currently extracted as `output_block` and where i is
4601 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004602 */
4603
Janos Follathea29bfb2019-06-19 12:21:20 +01004604 /* Save the hash context before using it, to preserve the hash state with
4605 * only the inner padding in it. We need this, because inner padding depends
4606 * on the key (secret in the RFC's terminology). */
4607 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4608 if( status != PSA_SUCCESS )
4609 goto cleanup;
4610
4611 /* Calculate A(i) where i = tls12_prf->block_number. */
4612 if( tls12_prf->block_number == 1 )
4613 {
4614 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4615 * the variable seed and in this instance means it in the context of the
4616 * P_hash function, where seed = label + seed.) */
4617 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4618 tls12_prf->label, tls12_prf->label_length );
4619 if( status != PSA_SUCCESS )
4620 goto cleanup;
4621 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4622 tls12_prf->seed, tls12_prf->seed_length );
4623 if( status != PSA_SUCCESS )
4624 goto cleanup;
4625 }
4626 else
4627 {
4628 /* A(i) = HMAC_hash(secret, A(i-1)) */
4629 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4630 tls12_prf->Ai, hash_length );
4631 if( status != PSA_SUCCESS )
4632 goto cleanup;
4633 }
4634
4635 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4636 tls12_prf->Ai, hash_length );
4637 if( status != PSA_SUCCESS )
4638 goto cleanup;
4639 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4640 if( status != PSA_SUCCESS )
4641 goto cleanup;
4642
4643 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4644 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4645 tls12_prf->Ai, hash_length );
4646 if( status != PSA_SUCCESS )
4647 goto cleanup;
4648 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4649 tls12_prf->label, tls12_prf->label_length );
4650 if( status != PSA_SUCCESS )
4651 goto cleanup;
4652 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4653 tls12_prf->seed, tls12_prf->seed_length );
4654 if( status != PSA_SUCCESS )
4655 goto cleanup;
4656 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4657 tls12_prf->output_block, hash_length );
4658 if( status != PSA_SUCCESS )
4659 goto cleanup;
4660 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4661 if( status != PSA_SUCCESS )
4662 goto cleanup;
4663
Janos Follath7742fee2019-06-17 12:58:10 +01004664
4665cleanup:
4666
Janos Follathea29bfb2019-06-19 12:21:20 +01004667 cleanup_status = psa_hash_abort( &backup );
4668 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4669 status = cleanup_status;
4670
4671 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004672}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004673
Janos Follath844eb0e2019-06-19 12:10:49 +01004674static psa_status_t psa_key_derivation_tls12_prf_read(
4675 psa_tls12_prf_key_derivation_t *tls12_prf,
4676 psa_algorithm_t alg,
4677 uint8_t *output,
4678 size_t output_length )
4679{
4680 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4681 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4682 psa_status_t status;
4683 uint8_t offset, length;
4684
4685 while( output_length != 0 )
4686 {
4687 /* Check if we have fully processed the current block. */
4688 if( tls12_prf->left_in_block == 0 )
4689 {
4690 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4691 alg );
4692 if( status != PSA_SUCCESS )
4693 return( status );
4694
4695 continue;
4696 }
4697
4698 if( tls12_prf->left_in_block > output_length )
4699 length = (uint8_t) output_length;
4700 else
4701 length = tls12_prf->left_in_block;
4702
4703 offset = hash_length - tls12_prf->left_in_block;
4704 memcpy( output, tls12_prf->output_block + offset, length );
4705 output += length;
4706 output_length -= length;
4707 tls12_prf->left_in_block -= length;
4708 }
4709
4710 return( PSA_SUCCESS );
4711}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004712#endif /* MBEDTLS_MD_C */
4713
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004714psa_status_t psa_key_derivation_output_bytes(
4715 psa_key_derivation_operation_t *operation,
4716 uint8_t *output,
4717 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004718{
4719 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004723 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004725 return PSA_ERROR_BAD_STATE;
4726 }
4727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004733 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004734 goto exit;
4735 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004739 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4741 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004744 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004747
Gilles Peskinebef7f142018-07-12 17:22:21 +02004748#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004750 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004753 output, output_length );
4754 }
Janos Follathadbec812019-06-14 11:05:39 +01004755 else
Janos Follathadbec812019-06-14 11:05:39 +01004756 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004758 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004760 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004761 output_length );
4762 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004763 else
4764#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004765 {
4766 return( PSA_ERROR_BAD_STATE );
4767 }
4768
4769exit:
4770 if( status != PSA_SUCCESS )
4771 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004772 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 * This allows us to differentiate between exhausted operations and
4774 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4775 * operations. */
4776 psa_algorithm_t alg = operation->alg;
4777 psa_key_derivation_abort( operation );
4778 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004779 memset( output, '!', output_length );
4780 }
4781 return( status );
4782}
4783
Gilles Peskine08542d82018-07-19 17:05:42 +02004784#if defined(MBEDTLS_DES_C)
4785static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4786{
4787 if( data_size >= 8 )
4788 mbedtls_des_key_set_parity( data );
4789 if( data_size >= 16 )
4790 mbedtls_des_key_set_parity( data + 8 );
4791 if( data_size >= 24 )
4792 mbedtls_des_key_set_parity( data + 16 );
4793}
4794#endif /* MBEDTLS_DES_C */
4795
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004796static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004797 psa_key_slot_t *slot,
4798 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004800{
4801 uint8_t *data = NULL;
4802 size_t bytes = PSA_BITS_TO_BYTES( bits );
4803 psa_status_t status;
4804
Gilles Peskine8e338702019-07-30 20:06:31 +02004805 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 return( PSA_ERROR_INVALID_ARGUMENT );
4807 if( bits % 8 != 0 )
4808 return( PSA_ERROR_INVALID_ARGUMENT );
4809 data = mbedtls_calloc( 1, bytes );
4810 if( data == NULL )
4811 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4812
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814 if( status != PSA_SUCCESS )
4815 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004816#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004817 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004818 psa_des_set_key_parity( data, bytes );
4819#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004820 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004821
4822exit:
4823 mbedtls_free( data );
4824 return( status );
4825}
4826
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004827psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004829 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004830{
4831 psa_status_t status;
4832 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004833 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004834
4835 /* Reject any attempt to create a zero-length key so that we don't
4836 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4837 if( psa_get_key_bits( attributes ) == 0 )
4838 return( PSA_ERROR_INVALID_ARGUMENT );
4839
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004840 if( ! operation->can_output_key )
4841 return( PSA_ERROR_NOT_PERMITTED );
4842
Gilles Peskinedf179142019-07-15 22:02:14 +02004843 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4844 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004845#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4846 if( driver != NULL )
4847 {
4848 /* Deriving a key in a secure element is not implemented yet. */
4849 status = PSA_ERROR_NOT_SUPPORTED;
4850 }
4851#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004852 if( status == PSA_SUCCESS )
4853 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004854 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004855 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 }
4858 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004859 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 if( status != PSA_SUCCESS )
4861 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004862 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004863 *handle = 0;
4864 }
4865 return( status );
4866}
4867
Gilles Peskineeab56e42018-07-12 17:12:33 +02004868
4869
4870/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004871/* Key derivation */
4872/****************************************************************/
4873
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004876 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877{
Janos Follath5fe19732019-06-20 15:09:30 +01004878 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4879 * initialisers for this union leave some bytes unspecified.) */
4880 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4881
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004884 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4885 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4886 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4890 if( hash_size == 0 )
4891 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004892 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4893 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004894 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 {
4896 return( PSA_ERROR_NOT_SUPPORTED );
4897 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900 }
4901#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904}
4905
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 psa_algorithm_t alg )
4908{
4909 psa_status_t status;
4910
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 return( PSA_ERROR_BAD_STATE );
4913
Gilles Peskine6843c292019-01-18 16:44:49 +01004914 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4915 return( PSA_ERROR_INVALID_ARGUMENT );
4916 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917 {
4918 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004920 }
4921 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4922 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925 else
4926 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927
4928 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931}
4932
4933#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004934static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004935 psa_algorithm_t hash_alg,
4936 psa_key_derivation_step_t step,
4937 const uint8_t *data,
4938 size_t data_length )
4939{
4940 psa_status_t status;
4941 switch( step )
4942 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004943 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004944 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004946 status = psa_hmac_setup_internal( &hkdf->hmac,
4947 data, data_length,
4948 hash_alg );
4949 if( status != PSA_SUCCESS )
4950 return( status );
4951 hkdf->state = HKDF_STATE_STARTED;
4952 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004953 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004954 /* If no salt was provided, use an empty salt. */
4955 if( hkdf->state == HKDF_STATE_INIT )
4956 {
4957 status = psa_hmac_setup_internal( &hkdf->hmac,
4958 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004959 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004960 if( status != PSA_SUCCESS )
4961 return( status );
4962 hkdf->state = HKDF_STATE_STARTED;
4963 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004964 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004966 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4967 data, data_length );
4968 if( status != PSA_SUCCESS )
4969 return( status );
4970 status = psa_hmac_finish_internal( &hkdf->hmac,
4971 hkdf->prk,
4972 sizeof( hkdf->prk ) );
4973 if( status != PSA_SUCCESS )
4974 return( status );
4975 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4976 hkdf->block_number = 0;
4977 hkdf->state = HKDF_STATE_KEYED;
4978 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004979 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980 if( hkdf->state == HKDF_STATE_OUTPUT )
4981 return( PSA_ERROR_BAD_STATE );
4982 if( hkdf->info_set )
4983 return( PSA_ERROR_BAD_STATE );
4984 hkdf->info_length = data_length;
4985 if( data_length != 0 )
4986 {
4987 hkdf->info = mbedtls_calloc( 1, data_length );
4988 if( hkdf->info == NULL )
4989 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4990 memcpy( hkdf->info, data, data_length );
4991 }
4992 hkdf->info_set = 1;
4993 return( PSA_SUCCESS );
4994 default:
4995 return( PSA_ERROR_INVALID_ARGUMENT );
4996 }
4997}
Janos Follathb03233e2019-06-11 15:30:30 +01004998
Janos Follathf08e2652019-06-13 09:05:41 +01004999static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5000 const uint8_t *data,
5001 size_t data_length )
5002{
5003 if( prf->state != TLS12_PRF_STATE_INIT )
5004 return( PSA_ERROR_BAD_STATE );
5005
Janos Follathd6dce9f2019-07-04 09:11:38 +01005006 if( data_length != 0 )
5007 {
5008 prf->seed = mbedtls_calloc( 1, data_length );
5009 if( prf->seed == NULL )
5010 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005011
Janos Follathd6dce9f2019-07-04 09:11:38 +01005012 memcpy( prf->seed, data, data_length );
5013 prf->seed_length = data_length;
5014 }
Janos Follathf08e2652019-06-13 09:05:41 +01005015
5016 prf->state = TLS12_PRF_STATE_SEED_SET;
5017
5018 return( PSA_SUCCESS );
5019}
5020
Janos Follath81550542019-06-13 14:26:34 +01005021static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5022 psa_algorithm_t hash_alg,
5023 const uint8_t *data,
5024 size_t data_length )
5025{
5026 psa_status_t status;
5027 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5028 return( PSA_ERROR_BAD_STATE );
5029
5030 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5031 if( status != PSA_SUCCESS )
5032 return( status );
5033
5034 prf->state = TLS12_PRF_STATE_KEY_SET;
5035
5036 return( PSA_SUCCESS );
5037}
5038
Janos Follath6660f0e2019-06-17 08:44:03 +01005039static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5040 psa_tls12_prf_key_derivation_t *prf,
5041 psa_algorithm_t hash_alg,
5042 const uint8_t *data,
5043 size_t data_length )
5044{
5045 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005046 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5047 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005048
5049 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5050 return( PSA_ERROR_INVALID_ARGUMENT );
5051
5052 /* Quoting RFC 4279, Section 2:
5053 *
5054 * The premaster secret is formed as follows: if the PSK is N octets
5055 * long, concatenate a uint16 with the value N, N zero octets, a second
5056 * uint16 with the value N, and the PSK itself.
5057 */
5058
Janos Follath40e13932019-06-26 13:22:29 +01005059 *cur++ = ( data_length >> 8 ) & 0xff;
5060 *cur++ = ( data_length >> 0 ) & 0xff;
5061 memset( cur, 0, data_length );
5062 cur += data_length;
5063 *cur++ = pms[0];
5064 *cur++ = pms[1];
5065 memcpy( cur, data, data_length );
5066 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005067
Janos Follath40e13932019-06-26 13:22:29 +01005068 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005069
5070 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5071 return( status );
5072}
5073
Janos Follath63028dd2019-06-13 09:15:47 +01005074static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5075 const uint8_t *data,
5076 size_t data_length )
5077{
5078 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5079 return( PSA_ERROR_BAD_STATE );
5080
Janos Follathd6dce9f2019-07-04 09:11:38 +01005081 if( data_length != 0 )
5082 {
5083 prf->label = mbedtls_calloc( 1, data_length );
5084 if( prf->label == NULL )
5085 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005086
Janos Follathd6dce9f2019-07-04 09:11:38 +01005087 memcpy( prf->label, data, data_length );
5088 prf->label_length = data_length;
5089 }
Janos Follath63028dd2019-06-13 09:15:47 +01005090
5091 prf->state = TLS12_PRF_STATE_LABEL_SET;
5092
5093 return( PSA_SUCCESS );
5094}
5095
Janos Follathb03233e2019-06-11 15:30:30 +01005096static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5097 psa_algorithm_t hash_alg,
5098 psa_key_derivation_step_t step,
5099 const uint8_t *data,
5100 size_t data_length )
5101{
Janos Follathb03233e2019-06-11 15:30:30 +01005102 switch( step )
5103 {
Janos Follathf08e2652019-06-13 09:05:41 +01005104 case PSA_KEY_DERIVATION_INPUT_SEED:
5105 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005106 case PSA_KEY_DERIVATION_INPUT_SECRET:
5107 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005108 case PSA_KEY_DERIVATION_INPUT_LABEL:
5109 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005110 default:
5111 return( PSA_ERROR_INVALID_ARGUMENT );
5112 }
5113}
Janos Follath6660f0e2019-06-17 08:44:03 +01005114
5115static psa_status_t psa_tls12_prf_psk_to_ms_input(
5116 psa_tls12_prf_key_derivation_t *prf,
5117 psa_algorithm_t hash_alg,
5118 psa_key_derivation_step_t step,
5119 const uint8_t *data,
5120 size_t data_length )
5121{
5122 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005123 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005124 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5125 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005126 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005127
5128 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5129}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130#endif /* MBEDTLS_MD_C */
5131
Gilles Peskineb8965192019-09-24 16:21:10 +02005132/** Check whether the given key type is acceptable for the given
5133 * input step of a key derivation.
5134 *
5135 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5136 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5137 * Both secret and non-secret inputs can alternatively have the type
5138 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5139 * that the input was passed as a buffer rather than via a key object.
5140 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005141static int psa_key_derivation_check_input_type(
5142 psa_key_derivation_step_t step,
5143 psa_key_type_t key_type )
5144{
5145 switch( step )
5146 {
5147 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005148 if( key_type == PSA_KEY_TYPE_DERIVE )
5149 return( PSA_SUCCESS );
5150 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005151 return( PSA_SUCCESS );
5152 break;
5153 case PSA_KEY_DERIVATION_INPUT_LABEL:
5154 case PSA_KEY_DERIVATION_INPUT_SALT:
5155 case PSA_KEY_DERIVATION_INPUT_INFO:
5156 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005157 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5158 return( PSA_SUCCESS );
5159 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005160 return( PSA_SUCCESS );
5161 break;
5162 }
5163 return( PSA_ERROR_INVALID_ARGUMENT );
5164}
5165
Janos Follathb80a94e2019-06-12 15:54:46 +01005166static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005167 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005168 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005169 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005170 const uint8_t *data,
5171 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005172{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005173 psa_status_t status;
5174 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5175
5176 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005177 if( status != PSA_SUCCESS )
5178 goto exit;
5179
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005181 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005183 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005184 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 step, data, data_length );
5186 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005187 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188 {
Janos Follathb03233e2019-06-11 15:30:30 +01005189 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5190 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5191 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005192 }
5193 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5194 {
5195 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5196 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5197 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198 }
5199 else
5200#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203 return( PSA_ERROR_BAD_STATE );
5204 }
5205
Gilles Peskine224b0d62019-09-23 18:13:17 +02005206exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 return( status );
5210}
5211
Janos Follath51f4a0f2019-06-14 11:35:55 +01005212psa_status_t psa_key_derivation_input_bytes(
5213 psa_key_derivation_operation_t *operation,
5214 psa_key_derivation_step_t step,
5215 const uint8_t *data,
5216 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005217{
Gilles Peskineb8965192019-09-24 16:21:10 +02005218 return( psa_key_derivation_input_internal( operation, step,
5219 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005220 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005221}
5222
Janos Follath51f4a0f2019-06-14 11:35:55 +01005223psa_status_t psa_key_derivation_input_key(
5224 psa_key_derivation_operation_t *operation,
5225 psa_key_derivation_step_t step,
5226 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227{
5228 psa_key_slot_t *slot;
5229 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005230
Gilles Peskine28f8f302019-07-24 13:30:31 +02005231 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005232 PSA_KEY_USAGE_DERIVE,
5233 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005235 {
5236 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005238 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005239
5240 /* Passing a key object as a SECRET input unlocks the permission
5241 * to output to a key object. */
5242 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5243 operation->can_output_key = 1;
5244
Janos Follathb80a94e2019-06-12 15:54:46 +01005245 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005246 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005247 slot->data.raw.data,
5248 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005249}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005250
5251
5252
5253/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005254/* Key agreement */
5255/****************************************************************/
5256
Gilles Peskinea05219c2018-11-16 16:02:56 +01005257#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005258static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5259 size_t peer_key_length,
5260 const mbedtls_ecp_keypair *our_key,
5261 uint8_t *shared_secret,
5262 size_t shared_secret_size,
5263 size_t *shared_secret_length )
5264{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005265 mbedtls_ecp_keypair *their_key = NULL;
5266 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005267 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005268 size_t bits = 0;
5269 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005270 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005271
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005272 status = psa_import_ec_public_key( curve,
5273 peer_key, peer_key_length,
5274 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005275 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005276 goto exit;
5277
Jaeden Amero97271b32019-01-10 19:38:51 +00005278 status = mbedtls_to_psa_error(
5279 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5280 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005281 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005282 status = mbedtls_to_psa_error(
5283 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5284 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285 goto exit;
5286
Jaeden Amero97271b32019-01-10 19:38:51 +00005287 status = mbedtls_to_psa_error(
5288 mbedtls_ecdh_calc_secret( &ecdh,
5289 shared_secret_length,
5290 shared_secret, shared_secret_size,
5291 mbedtls_ctr_drbg_random,
5292 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005293 if( status != PSA_SUCCESS )
5294 goto exit;
5295 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5296 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005297
5298exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005299 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005300 mbedtls_ecp_keypair_free( their_key );
5301 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005302 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005303}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005304#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305
Gilles Peskine01d718c2018-09-18 12:01:02 +02005306#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5307
Gilles Peskine0216fe12019-04-11 21:23:21 +02005308static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5309 psa_key_slot_t *private_key,
5310 const uint8_t *peer_key,
5311 size_t peer_key_length,
5312 uint8_t *shared_secret,
5313 size_t shared_secret_size,
5314 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005315{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005316 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005317 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005318#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005319 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005320 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005321 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005322 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5323 private_key->data.ecp,
5324 shared_secret, shared_secret_size,
5325 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005326#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005327 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005328 (void) private_key;
5329 (void) peer_key;
5330 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005331 (void) shared_secret;
5332 (void) shared_secret_size;
5333 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005334 return( PSA_ERROR_NOT_SUPPORTED );
5335 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005336}
5337
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005338/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005339 * to potentially free embedded data structures and wipe confidential data.
5340 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005342 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005343 psa_key_slot_t *private_key,
5344 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005345 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005346{
5347 psa_status_t status;
5348 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5349 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005351
5352 /* Step 1: run the secret agreement algorithm to generate the shared
5353 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005354 status = psa_key_agreement_raw_internal( ka_alg,
5355 private_key,
5356 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005357 shared_secret,
5358 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005359 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005360 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005361 goto exit;
5362
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005363 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005364 * the shared secret. A shared secret is permitted wherever a key
5365 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005366 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005367 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005368 shared_secret,
5369 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005370
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005372 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005373 return( status );
5374}
5375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005377 psa_key_derivation_step_t step,
5378 psa_key_handle_t private_key,
5379 const uint8_t *peer_key,
5380 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005382 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005383 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005385 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005386 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005387 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388 if( status != PSA_SUCCESS )
5389 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005391 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005392 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005393 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005394 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005395 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005396}
5397
Gilles Peskinebe697d82019-05-16 18:00:41 +02005398psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5399 psa_key_handle_t private_key,
5400 const uint8_t *peer_key,
5401 size_t peer_key_length,
5402 uint8_t *output,
5403 size_t output_size,
5404 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005405{
5406 psa_key_slot_t *slot;
5407 psa_status_t status;
5408
5409 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5410 {
5411 status = PSA_ERROR_INVALID_ARGUMENT;
5412 goto exit;
5413 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005414 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005415 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005416 if( status != PSA_SUCCESS )
5417 goto exit;
5418
5419 status = psa_key_agreement_raw_internal( alg, slot,
5420 peer_key, peer_key_length,
5421 output, output_size,
5422 output_length );
5423
5424exit:
5425 if( status != PSA_SUCCESS )
5426 {
5427 /* If an error happens and is not handled properly, the output
5428 * may be used as a key to protect sensitive data. Arrange for such
5429 * a key to be random, which is likely to result in decryption or
5430 * verification errors. This is better than filling the buffer with
5431 * some constant data such as zeros, which would result in the data
5432 * being protected with a reproducible, easily knowable key.
5433 */
5434 psa_generate_random( output, output_size );
5435 *output_length = output_size;
5436 }
5437 return( status );
5438}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005439
5440
5441/****************************************************************/
5442/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005443/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005444
Gilles Peskine4c317f42018-07-12 01:24:09 +02005445psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005446 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005447{
Janos Follath24eed8d2019-11-22 13:21:35 +00005448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449 GUARD_MODULE_INITIALIZED;
5450
Gilles Peskinef181eca2019-08-07 13:49:00 +02005451 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5452 {
5453 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5454 output,
5455 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5456 if( ret != 0 )
5457 return( mbedtls_to_psa_error( ret ) );
5458 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5459 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5460 }
5461
Gilles Peskinee59236f2018-01-27 23:32:46 +01005462 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5463 return( mbedtls_to_psa_error( ret ) );
5464}
5465
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005466#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5467#include "mbedtls/entropy_poll.h"
5468
Gilles Peskine7228da22019-07-15 11:06:15 +02005469psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005470 size_t seed_size )
5471{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005472 if( global_data.initialized )
5473 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005474
5475 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5476 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5477 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5478 return( PSA_ERROR_INVALID_ARGUMENT );
5479
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005480 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005481}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005482#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005483
Gilles Peskinee56e8782019-04-26 17:34:02 +02005484#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5485static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5486 size_t domain_parameters_size,
5487 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005488{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005489 size_t i;
5490 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005491
Gilles Peskinee56e8782019-04-26 17:34:02 +02005492 if( domain_parameters_size == 0 )
5493 {
5494 *exponent = 65537;
5495 return( PSA_SUCCESS );
5496 }
5497
5498 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5499 * support values that fit in a 32-bit integer, which is larger than
5500 * int on just about every platform anyway. */
5501 if( domain_parameters_size > sizeof( acc ) )
5502 return( PSA_ERROR_NOT_SUPPORTED );
5503 for( i = 0; i < domain_parameters_size; i++ )
5504 acc = ( acc << 8 ) | domain_parameters[i];
5505 if( acc > INT_MAX )
5506 return( PSA_ERROR_NOT_SUPPORTED );
5507 *exponent = acc;
5508 return( PSA_SUCCESS );
5509}
5510#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5511
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005512static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005513 psa_key_slot_t *slot, size_t bits,
5514 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005515{
Gilles Peskine8e338702019-07-30 20:06:31 +02005516 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517
Gilles Peskinee56e8782019-04-26 17:34:02 +02005518 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519 return( PSA_ERROR_INVALID_ARGUMENT );
5520
Gilles Peskinee59236f2018-01-27 23:32:46 +01005521 if( key_type_is_raw_bytes( type ) )
5522 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005523 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5525 if( status != PSA_SUCCESS )
5526 return( status );
5527 status = psa_generate_random( slot->data.raw.data,
5528 slot->data.raw.bytes );
5529 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531#if defined(MBEDTLS_DES_C)
5532 if( type == PSA_KEY_TYPE_DES )
5533 psa_des_set_key_parity( slot->data.raw.data,
5534 slot->data.raw.bytes );
5535#endif /* MBEDTLS_DES_C */
5536 }
5537 else
5538
5539#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005540 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005541 {
5542 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005544 int exponent;
5545 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005546 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5547 return( PSA_ERROR_NOT_SUPPORTED );
5548 /* Accept only byte-aligned keys, for the same reasons as
5549 * in psa_import_rsa_key(). */
5550 if( bits % 8 != 0 )
5551 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005552 status = psa_read_rsa_exponent( domain_parameters,
5553 domain_parameters_size,
5554 &exponent );
5555 if( status != PSA_SUCCESS )
5556 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005557 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5558 if( rsa == NULL )
5559 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5560 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5561 ret = mbedtls_rsa_gen_key( rsa,
5562 mbedtls_ctr_drbg_random,
5563 &global_data.ctr_drbg,
5564 (unsigned int) bits,
5565 exponent );
5566 if( ret != 0 )
5567 {
5568 mbedtls_rsa_free( rsa );
5569 mbedtls_free( rsa );
5570 return( mbedtls_to_psa_error( ret ) );
5571 }
5572 slot->data.rsa = rsa;
5573 }
5574 else
5575#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5576
5577#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005578 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005579 {
5580 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5581 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5582 const mbedtls_ecp_curve_info *curve_info =
5583 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5584 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005586 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587 return( PSA_ERROR_NOT_SUPPORTED );
5588 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5589 return( PSA_ERROR_NOT_SUPPORTED );
5590 if( curve_info->bit_size != bits )
5591 return( PSA_ERROR_INVALID_ARGUMENT );
5592 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5593 if( ecp == NULL )
5594 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5595 mbedtls_ecp_keypair_init( ecp );
5596 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5597 mbedtls_ctr_drbg_random,
5598 &global_data.ctr_drbg );
5599 if( ret != 0 )
5600 {
5601 mbedtls_ecp_keypair_free( ecp );
5602 mbedtls_free( ecp );
5603 return( mbedtls_to_psa_error( ret ) );
5604 }
5605 slot->data.ecp = ecp;
5606 }
5607 else
5608#endif /* MBEDTLS_ECP_C */
5609
5610 return( PSA_ERROR_NOT_SUPPORTED );
5611
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005612 return( PSA_SUCCESS );
5613}
Darryl Green0c6575a2018-11-07 16:05:30 +00005614
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005615psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005616 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005617{
5618 psa_status_t status;
5619 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005620 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005621
Gilles Peskine0f84d622019-09-12 19:03:13 +02005622 /* Reject any attempt to create a zero-length key so that we don't
5623 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5624 if( psa_get_key_bits( attributes ) == 0 )
5625 return( PSA_ERROR_INVALID_ARGUMENT );
5626
Gilles Peskinedf179142019-07-15 22:02:14 +02005627 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5628 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005629 if( status != PSA_SUCCESS )
5630 goto exit;
5631
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005632#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5633 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005634 {
Gilles Peskine11792082019-08-06 18:36:36 +02005635 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5636 size_t pubkey_length = 0; /* We don't support this feature yet */
5637 if( drv->key_management == NULL ||
5638 drv->key_management->p_generate == NULL )
5639 {
5640 status = PSA_ERROR_NOT_SUPPORTED;
5641 goto exit;
5642 }
5643 status = drv->key_management->p_generate(
5644 psa_get_se_driver_context( driver ),
5645 slot->data.se.slot_number, attributes,
5646 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005647 }
Gilles Peskine11792082019-08-06 18:36:36 +02005648 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005649#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005650 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005651 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005652 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005653 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005654 }
Gilles Peskine11792082019-08-06 18:36:36 +02005655
5656exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005657 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005658 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005659 if( status != PSA_SUCCESS )
5660 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005661 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 *handle = 0;
5663 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005664 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005665}
5666
5667
Gilles Peskinee59236f2018-01-27 23:32:46 +01005668
5669/****************************************************************/
5670/* Module setup */
5671/****************************************************************/
5672
Gilles Peskine5e769522018-11-20 21:59:56 +01005673psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5674 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5675 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5676{
5677 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5678 return( PSA_ERROR_BAD_STATE );
5679 global_data.entropy_init = entropy_init;
5680 global_data.entropy_free = entropy_free;
5681 return( PSA_SUCCESS );
5682}
5683
Gilles Peskinee59236f2018-01-27 23:32:46 +01005684void mbedtls_psa_crypto_free( void )
5685{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005686 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005687 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5688 {
5689 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005690 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005691 }
5692 /* Wipe all remaining data, including configuration.
5693 * In particular, this sets all state indicator to the value
5694 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005695 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005696#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005697 /* Unregister all secure element drivers, so that we restart from
5698 * a pristine state. */
5699 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005700#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005701}
5702
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005703#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5704/** Recover a transaction that was interrupted by a power failure.
5705 *
5706 * This function is called during initialization, before psa_crypto_init()
5707 * returns. If this function returns a failure status, the initialization
5708 * fails.
5709 */
5710static psa_status_t psa_crypto_recover_transaction(
5711 const psa_crypto_transaction_t *transaction )
5712{
5713 switch( transaction->unknown.type )
5714 {
5715 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5716 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005717 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005718 * is implemented.
5719 * https://github.com/ARMmbed/mbed-crypto/issues/218
5720 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005721 default:
5722 /* We found an unsupported transaction in the storage.
5723 * We don't know what state the storage is in. Give up. */
5724 return( PSA_ERROR_STORAGE_FAILURE );
5725 }
5726}
5727#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5728
Gilles Peskinee59236f2018-01-27 23:32:46 +01005729psa_status_t psa_crypto_init( void )
5730{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005731 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005732 const unsigned char drbg_seed[] = "PSA";
5733
Gilles Peskinec6b69072018-11-20 21:42:52 +01005734 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005735 if( global_data.initialized != 0 )
5736 return( PSA_SUCCESS );
5737
Gilles Peskine5e769522018-11-20 21:59:56 +01005738 /* Set default configuration if
5739 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5740 if( global_data.entropy_init == NULL )
5741 global_data.entropy_init = mbedtls_entropy_init;
5742 if( global_data.entropy_free == NULL )
5743 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744
Gilles Peskinec6b69072018-11-20 21:42:52 +01005745 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005746 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005747#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5748 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5749 /* The PSA entropy injection feature depends on using NV seed as an entropy
5750 * source. Add NV seed as an entropy source for PSA entropy injection. */
5751 mbedtls_entropy_add_source( &global_data.entropy,
5752 mbedtls_nv_seed_poll, NULL,
5753 MBEDTLS_ENTROPY_BLOCK_SIZE,
5754 MBEDTLS_ENTROPY_SOURCE_STRONG );
5755#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005756 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005757 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005758 status = mbedtls_to_psa_error(
5759 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5760 mbedtls_entropy_func,
5761 &global_data.entropy,
5762 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5763 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005764 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005765 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005766
Gilles Peskine66fb1262018-12-10 16:29:04 +01005767 status = psa_initialize_key_slots( );
5768 if( status != PSA_SUCCESS )
5769 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005770
Gilles Peskined9348f22019-10-01 15:22:29 +02005771#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5772 status = psa_init_all_se_drivers( );
5773 if( status != PSA_SUCCESS )
5774 goto exit;
5775#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5776
Gilles Peskine4b734222019-07-24 15:56:31 +02005777#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005778 status = psa_crypto_load_transaction( );
5779 if( status == PSA_SUCCESS )
5780 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005781 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5782 if( status != PSA_SUCCESS )
5783 goto exit;
5784 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005785 }
5786 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5787 {
5788 /* There's no transaction to complete. It's all good. */
5789 status = PSA_SUCCESS;
5790 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005791#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005792
Gilles Peskinec6b69072018-11-20 21:42:52 +01005793 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005794 global_data.initialized = 1;
5795
5796exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005797 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005798 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005799 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005800}
5801
5802#endif /* MBEDTLS_PSA_CRYPTO_C */