blob: 63fbaed0223e742081dcbcb65f2e0abd1cf9dec7 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132static psa_status_t mbedtls_to_psa_error( int ret )
133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
206 return( PSA_ERROR_BAD_STATE );
207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100378psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
379 size_t *bits )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200380{
381 switch( grpid )
382 {
383 case MBEDTLS_ECP_DP_SECP192R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100384 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100385 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100388 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100391 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100394 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100397 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100400 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100403 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100406 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100409 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100412 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100415 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100418 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100421 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200422 default:
423 return( 0 );
424 }
425}
426
Gilles Peskine5055b232019-12-12 17:49:31 +0100427mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
428 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429{
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 if( ( curve & 0xffff ) != 0 )
431 {
432 if( PSA_BITS_TO_BYTES( curve & 0xffff ) != byte_length )
433 return( MBEDTLS_ECP_DP_NONE );
434 }
Gilles Peskine12313cd2018-06-20 00:20:32 +0200435 switch( curve )
436 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 case PSA_ECC_CURVE_SECP_R1:
438 switch( byte_length )
439 {
440 case PSA_BITS_TO_BYTES( 192 ):
441 return( MBEDTLS_ECP_DP_SECP192R1 );
442 case PSA_BITS_TO_BYTES( 224 ):
443 return( MBEDTLS_ECP_DP_SECP224R1 );
444 case PSA_BITS_TO_BYTES( 256 ):
445 return( MBEDTLS_ECP_DP_SECP256R1 );
446 case PSA_BITS_TO_BYTES( 384 ):
447 return( MBEDTLS_ECP_DP_SECP384R1 );
448 case PSA_BITS_TO_BYTES( 521 ):
449 return( MBEDTLS_ECP_DP_SECP521R1 );
450 default:
451 return( MBEDTLS_ECP_DP_NONE );
452 }
453 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100454
455 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
456 switch( byte_length )
457 {
458 case PSA_BITS_TO_BYTES( 256 ):
459 return( MBEDTLS_ECP_DP_BP256R1 );
460 case PSA_BITS_TO_BYTES( 384 ):
461 return( MBEDTLS_ECP_DP_BP384R1 );
462 case PSA_BITS_TO_BYTES( 512 ):
463 return( MBEDTLS_ECP_DP_BP512R1 );
464 default:
465 return( MBEDTLS_ECP_DP_NONE );
466 }
467 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100468
469 case PSA_ECC_CURVE_MONTGOMERY:
470 switch( byte_length )
471 {
472 case PSA_BITS_TO_BYTES( 255 ):
473 return( MBEDTLS_ECP_DP_CURVE25519 );
474 case PSA_BITS_TO_BYTES( 448 ):
475 return( MBEDTLS_ECP_DP_CURVE448 );
476 default:
477 return( MBEDTLS_ECP_DP_NONE );
478 }
479 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100480
481 case PSA_ECC_CURVE_SECP_K1:
482 switch( byte_length )
483 {
484 case PSA_BITS_TO_BYTES( 192 ):
485 return( MBEDTLS_ECP_DP_SECP192K1 );
486 case PSA_BITS_TO_BYTES( 224 ):
487 return( MBEDTLS_ECP_DP_SECP224K1 );
488 case PSA_BITS_TO_BYTES( 256 ):
489 return( MBEDTLS_ECP_DP_SECP256K1 );
490 default:
491 return( MBEDTLS_ECP_DP_NONE );
492 }
493 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100494
Gilles Peskine12313cd2018-06-20 00:20:32 +0200495 default:
496 return( MBEDTLS_ECP_DP_NONE );
497 }
498}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100499#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200500
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
502 size_t bits,
503 struct raw_data *raw )
504{
505 /* Check that the bit size is acceptable for the key type */
506 switch( type )
507 {
508 case PSA_KEY_TYPE_RAW_DATA:
509#if defined(MBEDTLS_MD_C)
510 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200511#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200512 case PSA_KEY_TYPE_DERIVE:
513 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514#if defined(MBEDTLS_AES_C)
515 case PSA_KEY_TYPE_AES:
516 if( bits != 128 && bits != 192 && bits != 256 )
517 return( PSA_ERROR_INVALID_ARGUMENT );
518 break;
519#endif
520#if defined(MBEDTLS_CAMELLIA_C)
521 case PSA_KEY_TYPE_CAMELLIA:
522 if( bits != 128 && bits != 192 && bits != 256 )
523 return( PSA_ERROR_INVALID_ARGUMENT );
524 break;
525#endif
526#if defined(MBEDTLS_DES_C)
527 case PSA_KEY_TYPE_DES:
528 if( bits != 64 && bits != 128 && bits != 192 )
529 return( PSA_ERROR_INVALID_ARGUMENT );
530 break;
531#endif
532#if defined(MBEDTLS_ARC4_C)
533 case PSA_KEY_TYPE_ARC4:
534 if( bits < 8 || bits > 2048 )
535 return( PSA_ERROR_INVALID_ARGUMENT );
536 break;
537#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200538#if defined(MBEDTLS_CHACHA20_C)
539 case PSA_KEY_TYPE_CHACHA20:
540 if( bits != 256 )
541 return( PSA_ERROR_INVALID_ARGUMENT );
542 break;
543#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544 default:
545 return( PSA_ERROR_NOT_SUPPORTED );
546 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200547 if( bits % 8 != 0 )
548 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200549
550 /* Allocate memory for the key */
551 raw->bytes = PSA_BITS_TO_BYTES( bits );
552 raw->data = mbedtls_calloc( 1, raw->bytes );
553 if( raw->data == NULL )
554 {
555 raw->bytes = 0;
556 return( PSA_ERROR_INSUFFICIENT_MEMORY );
557 }
558 return( PSA_SUCCESS );
559}
560
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100562/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
563 * that are not a multiple of 8) well. For example, there is only
564 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
565 * way to return the exact bit size of a key.
566 * To keep things simple, reject non-byte-aligned key sizes. */
567static psa_status_t psa_check_rsa_key_byte_aligned(
568 const mbedtls_rsa_context *rsa )
569{
570 mbedtls_mpi n;
571 psa_status_t status;
572 mbedtls_mpi_init( &n );
573 status = mbedtls_to_psa_error(
574 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
575 if( status == PSA_SUCCESS )
576 {
577 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
578 status = PSA_ERROR_NOT_SUPPORTED;
579 }
580 mbedtls_mpi_free( &n );
581 return( status );
582}
583
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000584static psa_status_t psa_import_rsa_key( psa_key_type_t type,
585 const uint8_t *data,
586 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587 mbedtls_rsa_context **p_rsa )
588{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000589 psa_status_t status;
590 mbedtls_pk_context pk;
591 mbedtls_rsa_context *rsa;
592 size_t bits;
593
594 mbedtls_pk_init( &pk );
595
596 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200597 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000598 status = mbedtls_to_psa_error(
599 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000601 status = mbedtls_to_psa_error(
602 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
603 if( status != PSA_SUCCESS )
604 goto exit;
605
606 /* We have something that the pkparse module recognizes. If it is a
607 * valid RSA key, store it. */
608 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000610 status = PSA_ERROR_INVALID_ARGUMENT;
611 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200612 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000613
614 rsa = mbedtls_pk_rsa( pk );
615 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
616 * supports non-byte-aligned key sizes, but not well. For example,
617 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
618 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
619 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
620 {
621 status = PSA_ERROR_NOT_SUPPORTED;
622 goto exit;
623 }
624 status = psa_check_rsa_key_byte_aligned( rsa );
625
626exit:
627 /* Free the content of the pk object only on error. */
628 if( status != PSA_SUCCESS )
629 {
630 mbedtls_pk_free( &pk );
631 return( status );
632 }
633
634 /* On success, store the content of the object in the RSA context. */
635 *p_rsa = rsa;
636
637 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638}
639#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
640
Jaeden Ameroccdce902019-01-10 11:42:27 +0000641#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100642static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100643 size_t data_length,
644 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100645 mbedtls_ecp_keypair **p_ecp )
646{
647 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
648 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
649 if( *p_ecp == NULL )
650 return( PSA_ERROR_INSUFFICIENT_MEMORY );
651 mbedtls_ecp_keypair_init( *p_ecp );
652
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100653 if( is_public )
654 {
655 /* A public key is represented as:
656 * - The byte 0x04;
657 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
658 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
659 * So its data length is 2m+1 where n is the key size in bits.
660 */
661 if( ( data_length & 1 ) == 0 )
662 return( PSA_ERROR_INVALID_ARGUMENT );
663 data_length = data_length / 2;
664 }
665
Gilles Peskine4cd32772019-12-02 20:49:42 +0100666 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100667 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
668 if( grp_id == MBEDTLS_ECP_DP_NONE )
669 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100670 return( mbedtls_to_psa_error(
671 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
672}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000673
674/* Import a public key given as the uncompressed representation defined by SEC1
675 * 2.3.3 as the content of an ECPoint. */
676static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
677 const uint8_t *data,
678 size_t data_length,
679 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200680{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200681 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000682 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000683
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100684 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000685 if( status != PSA_SUCCESS )
686 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100687
Jaeden Ameroccdce902019-01-10 11:42:27 +0000688 /* Load the public value. */
689 status = mbedtls_to_psa_error(
690 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
691 data, data_length ) );
692 if( status != PSA_SUCCESS )
693 goto exit;
694
695 /* Check that the point is on the curve. */
696 status = mbedtls_to_psa_error(
697 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
698 if( status != PSA_SUCCESS )
699 goto exit;
700
701 *p_ecp = ecp;
702 return( PSA_SUCCESS );
703
704exit:
705 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200706 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000707 mbedtls_ecp_keypair_free( ecp );
708 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200709 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000710 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200712
Gilles Peskinef76aa772018-10-29 19:24:33 +0100713/* Import a private key given as a byte string which is the private value
714 * in big-endian order. */
715static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
716 const uint8_t *data,
717 size_t data_length,
718 mbedtls_ecp_keypair **p_ecp )
719{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100722
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100723 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100724 if( status != PSA_SUCCESS )
725 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100726
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 /* Load the secret value. */
728 status = mbedtls_to_psa_error(
729 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
730 if( status != PSA_SUCCESS )
731 goto exit;
732 /* Validate the private key. */
733 status = mbedtls_to_psa_error(
734 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
735 if( status != PSA_SUCCESS )
736 goto exit;
737 /* Calculate the public key from the private key. */
738 status = mbedtls_to_psa_error(
739 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
740 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
741 if( status != PSA_SUCCESS )
742 goto exit;
743
744 *p_ecp = ecp;
745 return( PSA_SUCCESS );
746
747exit:
748 if( ecp != NULL )
749 {
750 mbedtls_ecp_keypair_free( ecp );
751 mbedtls_free( ecp );
752 }
753 return( status );
754}
755#endif /* defined(MBEDTLS_ECP_C) */
756
Gilles Peskineb46bef22019-07-30 21:32:04 +0200757
758/** Return the size of the key in the given slot, in bits.
759 *
760 * \param[in] slot A key slot.
761 *
762 * \return The key size in bits, read from the metadata in the slot.
763 */
764static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
765{
766 return( slot->attr.bits );
767}
768
769/** Calculate the size of the key in the given slot, in bits.
770 *
771 * \param[in] slot A key slot containing a transparent key.
772 *
773 * \return The key size in bits, calculated from the key data.
774 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200775static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200776{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200777 size_t bits = 0; /* return 0 on an empty slot */
778
Gilles Peskineb46bef22019-07-30 21:32:04 +0200779 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200780 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200781#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200782 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
783 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200784#endif /* defined(MBEDTLS_RSA_C) */
785#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200786 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
787 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200788#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200789
790 /* We know that the size fits in psa_key_bits_t thanks to checks
791 * when the key was created. */
792 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200793}
794
Gilles Peskine8e338702019-07-30 20:06:31 +0200795/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100796 * previously. This function assumes that the slot does not contain
797 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100798psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
799 const uint8_t *data,
800 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200802 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803
Gilles Peskine8e338702019-07-30 20:06:31 +0200804 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200806 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200807 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 if( data_length > SIZE_MAX / 8 )
809 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200810 /* Enforce a size limit, and in particular ensure that the bit
811 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200812 if( bit_size > PSA_MAX_KEY_BITS )
813 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200814 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200815 &slot->data.raw );
816 if( status != PSA_SUCCESS )
817 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200818 if( data_length != 0 )
819 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 }
821 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100822#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200823 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100824 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200825 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100826 data, data_length,
827 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100828 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200829 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100830 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000831 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200832 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000833 data, data_length,
834 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 }
836 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100837#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000838#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200839 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200841 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000842 data, data_length,
843 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100844 }
845 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000846#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100847 {
848 return( PSA_ERROR_NOT_SUPPORTED );
849 }
Darryl Green940d72c2018-07-13 13:18:51 +0100850
Gilles Peskineb46bef22019-07-30 21:32:04 +0200851 if( status == PSA_SUCCESS )
852 {
853 /* Write the actual key size to the slot.
854 * psa_start_key_creation() wrote the size declared by the
855 * caller, which may be 0 (meaning unspecified) or wrong. */
856 slot->attr.bits = psa_calculate_key_bits( slot );
857 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100858 return( status );
859}
860
Gilles Peskinef603c712019-01-19 13:40:11 +0100861/** Calculate the intersection of two algorithm usage policies.
862 *
863 * Return 0 (which allows no operation) on incompatibility.
864 */
865static psa_algorithm_t psa_key_policy_algorithm_intersection(
866 psa_algorithm_t alg1,
867 psa_algorithm_t alg2 )
868{
Gilles Peskine549ea862019-05-22 11:45:59 +0200869 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100870 if( alg1 == alg2 )
871 return( alg1 );
872 /* If the policies are from the same hash-and-sign family, check
873 * if one is a wildcard. If so the other has the specific algorithm. */
874 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
875 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
876 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
877 {
878 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
879 return( alg2 );
880 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
881 return( alg1 );
882 }
883 /* If the policies are incompatible, allow nothing. */
884 return( 0 );
885}
886
Gilles Peskined6f371b2019-05-10 19:33:38 +0200887static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
888 psa_algorithm_t requested_alg )
889{
Gilles Peskine549ea862019-05-22 11:45:59 +0200890 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891 if( requested_alg == policy_alg )
892 return( 1 );
893 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200894 * and requested_alg is the same hash-and-sign family with any hash,
895 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200896 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
897 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
898 {
899 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
900 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
901 }
902 /* If it isn't permitted, it's forbidden. */
903 return( 0 );
904}
905
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100906/** Test whether a policy permits an algorithm.
907 *
908 * The caller must test usage flags separately.
909 */
910static int psa_key_policy_permits( const psa_key_policy_t *policy,
911 psa_algorithm_t alg )
912{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200913 return( psa_key_algorithm_permits( policy->alg, alg ) ||
914 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100915}
916
Gilles Peskinef603c712019-01-19 13:40:11 +0100917/** Restrict a key policy based on a constraint.
918 *
919 * \param[in,out] policy The policy to restrict.
920 * \param[in] constraint The policy constraint to apply.
921 *
922 * \retval #PSA_SUCCESS
923 * \c *policy contains the intersection of the original value of
924 * \c *policy and \c *constraint.
925 * \retval #PSA_ERROR_INVALID_ARGUMENT
926 * \c *policy and \c *constraint are incompatible.
927 * \c *policy is unchanged.
928 */
929static psa_status_t psa_restrict_key_policy(
930 psa_key_policy_t *policy,
931 const psa_key_policy_t *constraint )
932{
933 psa_algorithm_t intersection_alg =
934 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200935 psa_algorithm_t intersection_alg2 =
936 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100937 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
938 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200939 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
940 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100941 policy->usage &= constraint->usage;
942 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200943 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100944 return( PSA_SUCCESS );
945}
946
Darryl Green06fd18d2018-07-16 11:21:11 +0100947/** Retrieve a slot which must contain a key. The key must have allow all the
948 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
949 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100950static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100952 psa_key_usage_t usage,
953 psa_algorithm_t alg )
954{
955 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100956 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100957
958 *p_slot = NULL;
959
Gilles Peskinec5487a82018-12-03 18:08:14 +0100960 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100961 if( status != PSA_SUCCESS )
962 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100963
964 /* Enforce that usage policy for the key slot contains all the flags
965 * required by the usage parameter. There is one exception: public
966 * keys can always be exported, so we treat public key objects as
967 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200968 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100969 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200970 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100971 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100972
973 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100975 return( PSA_ERROR_NOT_PERMITTED );
976
977 *p_slot = slot;
978 return( PSA_SUCCESS );
979}
Darryl Green940d72c2018-07-13 13:18:51 +0100980
Gilles Peskine28f8f302019-07-24 13:30:31 +0200981/** Retrieve a slot which must contain a transparent key.
982 *
983 * A transparent key is a key for which the key material is directly
984 * available, as opposed to a key in a secure element.
985 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200986 * This is a temporary function to use instead of psa_get_key_from_slot()
987 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200988 */
989#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
990static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
991 psa_key_slot_t **p_slot,
992 psa_key_usage_t usage,
993 psa_algorithm_t alg )
994{
995 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
996 if( status != PSA_SUCCESS )
997 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200998 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200999 {
1000 *p_slot = NULL;
1001 return( PSA_ERROR_NOT_SUPPORTED );
1002 }
1003 return( PSA_SUCCESS );
1004}
1005#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1006/* With no secure element support, all keys are transparent. */
1007#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1008 psa_get_key_from_slot( handle, p_slot, usage, alg )
1009#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1010
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001011/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001012static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001013{
Gilles Peskine73167e12019-07-12 23:44:37 +02001014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1015 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001016 {
1017 /* No key material to clean. */
1018 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001019 else
1020#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001022 {
1023 /* No key material to clean. */
1024 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001025 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001026 {
1027 mbedtls_free( slot->data.raw.data );
1028 }
1029 else
1030#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001031 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001032 {
1033 mbedtls_rsa_free( slot->data.rsa );
1034 mbedtls_free( slot->data.rsa );
1035 }
1036 else
1037#endif /* defined(MBEDTLS_RSA_C) */
1038#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001040 {
1041 mbedtls_ecp_keypair_free( slot->data.ecp );
1042 mbedtls_free( slot->data.ecp );
1043 }
1044 else
1045#endif /* defined(MBEDTLS_ECP_C) */
1046 {
1047 /* Shouldn't happen: the key type is not any type that we
1048 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001049 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001050 }
1051
1052 return( PSA_SUCCESS );
1053}
1054
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001055/** Completely wipe a slot in memory, including its policy.
1056 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001057psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001058{
1059 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001060 /* Multipart operations may still be using the key. This is safe
1061 * because all multipart operation objects are independent from
1062 * the key slot: if they need to access the key after the setup
1063 * phase, they have a copy of the key. Note that this means that
1064 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001065 /* At this point, key material and other type-specific content has
1066 * been wiped. Clear remaining metadata. We can call memset and not
1067 * zeroize because the metadata is not particularly sensitive. */
1068 memset( slot, 0, sizeof( *slot ) );
1069 return( status );
1070}
1071
Gilles Peskinec5487a82018-12-03 18:08:14 +01001072psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001074 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 psa_status_t status; /* status of the last operation */
1076 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001077#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1078 psa_se_drv_table_entry_t *driver;
1079#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080
Gilles Peskine1841cf42019-10-08 15:48:25 +02001081 if( handle == 0 )
1082 return( PSA_SUCCESS );
1083
Gilles Peskinec5487a82018-12-03 18:08:14 +01001084 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085 if( status != PSA_SUCCESS )
1086 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001087
1088#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001089 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001090 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001092 /* For a key in a secure element, we need to do three things:
1093 * remove the key file in internal storage, destroy the
1094 * key inside the secure element, and update the driver's
1095 * persistent data. Start a transaction that will encompass these
1096 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001097 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001098 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001099 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001100 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001101 status = psa_crypto_save_transaction( );
1102 if( status != PSA_SUCCESS )
1103 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001104 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001105 /* We should still try to destroy the key in the secure
1106 * element and the key metadata in storage. This is especially
1107 * important if the error is that the storage is full.
1108 * But how to do it exactly without risking an inconsistent
1109 * state after a reset?
1110 * https://github.com/ARMmbed/mbed-crypto/issues/215
1111 */
1112 overall_status = status;
1113 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001114 }
1115
Gilles Peskine354f7672019-07-12 23:46:38 +02001116 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001117 if( overall_status == PSA_SUCCESS )
1118 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001119 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001120#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1121
Darryl Greend49a4992018-06-18 17:27:26 +01001122#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001123 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001124 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001125 status = psa_destroy_persistent_key( slot->attr.id );
1126 if( overall_status == PSA_SUCCESS )
1127 overall_status = status;
1128
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001129 /* TODO: other slots may have a copy of the same key. We should
1130 * invalidate them.
1131 * https://github.com/ARMmbed/mbed-crypto/issues/214
1132 */
Darryl Greend49a4992018-06-18 17:27:26 +01001133 }
1134#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001135
Gilles Peskinefc762652019-07-22 19:30:34 +02001136#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1137 if( driver != NULL )
1138 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001139 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001140 if( overall_status == PSA_SUCCESS )
1141 overall_status = status;
1142 status = psa_crypto_stop_transaction( );
1143 if( overall_status == PSA_SUCCESS )
1144 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001145 }
1146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1147
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1149exit:
1150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001151 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001152 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1153 if( overall_status == PSA_SUCCESS )
1154 overall_status = status;
1155 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156}
1157
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001158void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001159{
Gilles Peskineb699f072019-04-26 16:06:02 +02001160 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001161 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001162}
1163
Gilles Peskineb699f072019-04-26 16:06:02 +02001164psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1165 psa_key_type_t type,
1166 const uint8_t *data,
1167 size_t data_length )
1168{
1169 uint8_t *copy = NULL;
1170
1171 if( data_length != 0 )
1172 {
1173 copy = mbedtls_calloc( 1, data_length );
1174 if( copy == NULL )
1175 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1176 memcpy( copy, data, data_length );
1177 }
1178 /* After this point, this function is guaranteed to succeed, so it
1179 * can start modifying `*attributes`. */
1180
1181 if( attributes->domain_parameters != NULL )
1182 {
1183 mbedtls_free( attributes->domain_parameters );
1184 attributes->domain_parameters = NULL;
1185 attributes->domain_parameters_size = 0;
1186 }
1187
1188 attributes->domain_parameters = copy;
1189 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001190 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001191 return( PSA_SUCCESS );
1192}
1193
1194psa_status_t psa_get_key_domain_parameters(
1195 const psa_key_attributes_t *attributes,
1196 uint8_t *data, size_t data_size, size_t *data_length )
1197{
1198 if( attributes->domain_parameters_size > data_size )
1199 return( PSA_ERROR_BUFFER_TOO_SMALL );
1200 *data_length = attributes->domain_parameters_size;
1201 if( attributes->domain_parameters_size != 0 )
1202 memcpy( data, attributes->domain_parameters,
1203 attributes->domain_parameters_size );
1204 return( PSA_SUCCESS );
1205}
1206
1207#if defined(MBEDTLS_RSA_C)
1208static psa_status_t psa_get_rsa_public_exponent(
1209 const mbedtls_rsa_context *rsa,
1210 psa_key_attributes_t *attributes )
1211{
1212 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 uint8_t *buffer = NULL;
1215 size_t buflen;
1216 mbedtls_mpi_init( &mpi );
1217
1218 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1219 if( ret != 0 )
1220 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001221 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1222 {
1223 /* It's the default value, which is reported as an empty string,
1224 * so there's nothing to do. */
1225 goto exit;
1226 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001227
1228 buflen = mbedtls_mpi_size( &mpi );
1229 buffer = mbedtls_calloc( 1, buflen );
1230 if( buffer == NULL )
1231 {
1232 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1233 goto exit;
1234 }
1235 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1236 if( ret != 0 )
1237 goto exit;
1238 attributes->domain_parameters = buffer;
1239 attributes->domain_parameters_size = buflen;
1240
1241exit:
1242 mbedtls_mpi_free( &mpi );
1243 if( ret != 0 )
1244 mbedtls_free( buffer );
1245 return( mbedtls_to_psa_error( ret ) );
1246}
1247#endif /* MBEDTLS_RSA_C */
1248
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001249/** Retrieve all the publicly-accessible attributes of a key.
1250 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001251psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1252 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001254 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001255 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001257 psa_reset_key_attributes( attributes );
1258
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001259 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001260 if( status != PSA_SUCCESS )
1261 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001262
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001263 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001264 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1265 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1266
1267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1268 if( psa_key_slot_is_external( slot ) )
1269 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001271
Gilles Peskine8e338702019-07-30 20:06:31 +02001272 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001273 {
1274#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001275 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001276 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001278 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001279 * is not yet implemented.
1280 * https://github.com/ARMmbed/mbed-crypto/issues/216
1281 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001282 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001283 break;
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001285 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1286 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001287#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001288 default:
1289 /* Nothing else to do. */
1290 break;
1291 }
1292
1293 if( status != PSA_SUCCESS )
1294 psa_reset_key_attributes( attributes );
1295 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296}
1297
Gilles Peskinec8000c02019-08-02 20:15:51 +02001298#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1299psa_status_t psa_get_key_slot_number(
1300 const psa_key_attributes_t *attributes,
1301 psa_key_slot_number_t *slot_number )
1302{
1303 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1304 {
1305 *slot_number = attributes->slot_number;
1306 return( PSA_SUCCESS );
1307 }
1308 else
1309 return( PSA_ERROR_INVALID_ARGUMENT );
1310}
1311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1312
Jaeden Ameroccdce902019-01-10 11:42:27 +00001313#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001314static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1315 unsigned char *buf, size_t size )
1316{
Janos Follath24eed8d2019-11-22 13:21:35 +00001317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001318 unsigned char *c;
1319 size_t len = 0;
1320
1321 c = buf + size;
1322
1323 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1324
1325 return( (int) len );
1326}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001327#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001328
Gilles Peskinef603c712019-01-19 13:40:11 +01001329static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1330 uint8_t *data,
1331 size_t data_size,
1332 size_t *data_length,
1333 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334{
Gilles Peskine5d309672019-07-12 23:47:28 +02001335#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1336 const psa_drv_se_t *drv;
1337 psa_drv_se_context_t *drv_context;
1338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1339
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001340 *data_length = 0;
1341
Gilles Peskine8e338702019-07-30 20:06:31 +02001342 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001343 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001344
Gilles Peskinef9168942019-09-12 19:20:29 +02001345 /* Reject a zero-length output buffer now, since this can never be a
1346 * valid key representation. This way we know that data must be a valid
1347 * pointer and we can do things like memset(data, ..., data_size). */
1348 if( data_size == 0 )
1349 return( PSA_ERROR_BUFFER_TOO_SMALL );
1350
Gilles Peskine5d309672019-07-12 23:47:28 +02001351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001352 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001353 {
1354 psa_drv_se_export_key_t method;
1355 if( drv->key_management == NULL )
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357 method = ( export_public_key ?
1358 drv->key_management->p_export_public :
1359 drv->key_management->p_export );
1360 if( method == NULL )
1361 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001362 return( method( drv_context,
1363 slot->data.se.slot_number,
1364 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001365 }
1366#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1367
Gilles Peskine8e338702019-07-30 20:06:31 +02001368 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369 {
1370 if( slot->data.raw.bytes > data_size )
1371 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001372 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1373 memset( data + slot->data.raw.bytes, 0,
1374 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001375 *data_length = slot->data.raw.bytes;
1376 return( PSA_SUCCESS );
1377 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001378#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001379 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001380 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001381 psa_status_t status;
1382
Gilles Peskineb46bef22019-07-30 21:32:04 +02001383 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001384 if( bytes > data_size )
1385 return( PSA_ERROR_BUFFER_TOO_SMALL );
1386 status = mbedtls_to_psa_error(
1387 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1388 if( status != PSA_SUCCESS )
1389 return( status );
1390 memset( data + bytes, 0, data_size - bytes );
1391 *data_length = bytes;
1392 return( PSA_SUCCESS );
1393 }
1394#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395 else
Moran Peker17e36e12018-05-02 12:55:20 +03001396 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001397#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001398 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1399 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001400 {
Moran Pekera998bc62018-04-16 18:16:20 +03001401 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001403 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001404 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001405#if defined(MBEDTLS_RSA_C)
1406 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001407 pk.pk_info = &mbedtls_rsa_info;
1408 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001409#else
1410 return( PSA_ERROR_NOT_SUPPORTED );
1411#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001412 }
1413 else
1414 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001415#if defined(MBEDTLS_ECP_C)
1416 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001417 pk.pk_info = &mbedtls_eckey_info;
1418 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001419#else
1420 return( PSA_ERROR_NOT_SUPPORTED );
1421#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001422 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001423 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001424 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001425 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001426 }
Moran Peker17e36e12018-05-02 12:55:20 +03001427 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001428 {
Moran Peker17e36e12018-05-02 12:55:20 +03001429 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001430 }
Moran Peker60364322018-04-29 11:34:58 +03001431 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001432 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001433 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001434 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001435 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001436 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1437 * Move the data to the beginning and erase remaining data
1438 * at the original location. */
1439 if( 2 * (size_t) ret <= data_size )
1440 {
1441 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001442 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001443 }
1444 else if( (size_t) ret < data_size )
1445 {
1446 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001447 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001448 }
Moran Pekera998bc62018-04-16 18:16:20 +03001449 *data_length = ret;
1450 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001451 }
1452 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001453#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001454 {
1455 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001456 it is valid for a special-purpose implementation to omit
1457 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001458 return( PSA_ERROR_NOT_SUPPORTED );
1459 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001460 }
1461}
1462
Gilles Peskinec5487a82018-12-03 18:08:14 +01001463psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001464 uint8_t *data,
1465 size_t data_size,
1466 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001467{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001468 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001469 psa_status_t status;
1470
1471 /* Set the key to empty now, so that even when there are errors, we always
1472 * set data_length to a value between 0 and data_size. On error, setting
1473 * the key to empty is a good choice because an empty key representation is
1474 * unlikely to be accepted anywhere. */
1475 *data_length = 0;
1476
1477 /* Export requires the EXPORT flag. There is an exception for public keys,
1478 * which don't require any flag, but psa_get_key_from_slot takes
1479 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001480 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001481 if( status != PSA_SUCCESS )
1482 return( status );
1483 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001484 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486
Gilles Peskinec5487a82018-12-03 18:08:14 +01001487psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001488 uint8_t *data,
1489 size_t data_size,
1490 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001491{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001492 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001493 psa_status_t status;
1494
1495 /* Set the key to empty now, so that even when there are errors, we always
1496 * set data_length to a value between 0 and data_size. On error, setting
1497 * the key to empty is a good choice because an empty key representation is
1498 * unlikely to be accepted anywhere. */
1499 *data_length = 0;
1500
1501 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001502 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001503 if( status != PSA_SUCCESS )
1504 return( status );
1505 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001506 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001507}
1508
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509#if defined(static_assert)
1510static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1511 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001512static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001513 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001514static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001515 "One or more key attribute flag is listed as both internal-only and external-only" );
1516#endif
1517
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001518/** Validate that a key policy is internally well-formed.
1519 *
1520 * This function only rejects invalid policies. It does not validate the
1521 * consistency of the policy with respect to other attributes of the key
1522 * such as the key type.
1523 */
1524static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001525{
1526 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001527 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001528 PSA_KEY_USAGE_ENCRYPT |
1529 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001530 PSA_KEY_USAGE_SIGN_HASH |
1531 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001532 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1533 return( PSA_ERROR_INVALID_ARGUMENT );
1534
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 return( PSA_SUCCESS );
1536}
1537
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001538/** Validate the internal consistency of key attributes.
1539 *
1540 * This function only rejects invalid attribute values. If does not
1541 * validate the consistency of the attributes with any key data that may
1542 * be involved in the creation of the key.
1543 *
1544 * Call this function early in the key creation process.
1545 *
1546 * \param[in] attributes Key attributes for the new key.
1547 * \param[out] p_drv On any return, the driver for the key, if any.
1548 * NULL for a transparent key.
1549 *
1550 */
1551static psa_status_t psa_validate_key_attributes(
1552 const psa_key_attributes_t *attributes,
1553 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001554{
1555 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001556
1557 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001558 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559 status = psa_validate_persistent_key_parameters(
1560 attributes->core.lifetime, attributes->core.id,
1561 p_drv, 1 );
1562 if( status != PSA_SUCCESS )
1563 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001564 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565
1566 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001567 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568 return( status );
1569
1570 /* Refuse to create overly large keys.
1571 * Note that this doesn't trigger on import if the attributes don't
1572 * explicitly specify a size (so psa_get_key_bits returns 0), so
1573 * psa_import_key() needs its own checks. */
1574 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1575 return( PSA_ERROR_NOT_SUPPORTED );
1576
Gilles Peskine91e8c332019-08-02 19:19:39 +02001577 /* Reject invalid flags. These should not be reachable through the API. */
1578 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1579 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1580 return( PSA_ERROR_INVALID_ARGUMENT );
1581
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582 return( PSA_SUCCESS );
1583}
1584
Gilles Peskine4747d192019-04-17 15:05:45 +02001585/** Prepare a key slot to receive key material.
1586 *
1587 * This function allocates a key slot and sets its metadata.
1588 *
1589 * If this function fails, call psa_fail_key_creation().
1590 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001591 * This function is intended to be used as follows:
1592 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1593 * it with the specified attributes, and assign it a handle.
1594 * -# Populate the slot with the key material.
1595 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1596 * In case of failure at any step, stop the sequence and call
1597 * psa_fail_key_creation().
1598 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001599 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001600 * \param[in] attributes Key attributes for the new key.
1601 * \param[out] handle On success, a handle for the allocated slot.
1602 * \param[out] p_slot On success, a pointer to the prepared slot.
1603 * \param[out] p_drv On any return, the driver for the key, if any.
1604 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001605 *
1606 * \retval #PSA_SUCCESS
1607 * The key slot is ready to receive key material.
1608 * \return If this function fails, the key slot is an invalid state.
1609 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001610 */
1611static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001612 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001613 const psa_key_attributes_t *attributes,
1614 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001616 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001617{
1618 psa_status_t status;
1619 psa_key_slot_t *slot;
1620
Gilles Peskinedf179142019-07-15 22:02:14 +02001621 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001622 *p_drv = NULL;
1623
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001624 status = psa_validate_key_attributes( attributes, p_drv );
1625 if( status != PSA_SUCCESS )
1626 return( status );
1627
Gilles Peskineedbed562019-08-07 18:19:59 +02001628 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 if( status != PSA_SUCCESS )
1630 return( status );
1631 slot = *p_slot;
1632
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001633 /* We're storing the declared bit-size of the key. It's up to each
1634 * creation mechanism to verify that this information is correct.
1635 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001636 * an input (generate, device) but not for those where the bit-size
1637 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001638
1639 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001640
Gilles Peskine91e8c332019-08-02 19:19:39 +02001641 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001642 * external-only flags, query `attributes`. Thanks to the check
1643 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001644 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001645 * may have set. */
1646 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001647
Gilles Peskinecbaff462019-07-12 23:46:04 +02001648#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001649 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001650 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001651 * create the key file in internal storage, create the
1652 * key inside the secure element, and update the driver's
1653 * persistent data. Start a transaction that will encompass these
1654 * three actions. */
1655 /* The first thing to do is to find a slot number for the new key.
1656 * We save the slot number in persistent storage as part of the
1657 * transaction data. It will be needed to recover if the power
1658 * fails during the key creation process, to clean up on the secure
1659 * element side after restarting. Obtaining a slot number from the
1660 * secure element driver updates its persistent state, but we do not yet
1661 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001662 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001663 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001664 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001665 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001666 &slot->data.se.slot_number );
1667 if( status != PSA_SUCCESS )
1668 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001669 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001670 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001671 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001672 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001673 status = psa_crypto_save_transaction( );
1674 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001675 {
1676 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001677 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001678 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001679 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001680
1681 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1682 {
1683 /* Key registration only makes sense with a secure element. */
1684 return( PSA_ERROR_INVALID_ARGUMENT );
1685 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001686#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1687
Darryl Green0c6575a2018-11-07 16:05:30 +00001688 return( status );
1689}
Gilles Peskine4747d192019-04-17 15:05:45 +02001690
1691/** Finalize the creation of a key once its key material has been set.
1692 *
1693 * This entails writing the key to persistent storage.
1694 *
1695 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001696 * See the documentation of psa_start_key_creation() for the intended use
1697 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001698 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001699 * \param[in,out] slot Pointer to the slot with key material.
1700 * \param[in] driver The secure element driver for the key,
1701 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001702 *
1703 * \retval #PSA_SUCCESS
1704 * The key was successfully created. The handle is now valid.
1705 * \return If this function fails, the key slot is an invalid state.
1706 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001707 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001708static psa_status_t psa_finish_key_creation(
1709 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001710 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001711{
1712 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001713 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001714 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001715
1716#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001717 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001719#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1720 if( driver != NULL )
1721 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001722 psa_se_key_data_storage_t data;
1723#if defined(static_assert)
1724 static_assert( sizeof( slot->data.se.slot_number ) ==
1725 sizeof( data.slot_number ),
1726 "Slot number size does not match psa_se_key_data_storage_t" );
1727 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1728 "Bit-size size does not match psa_se_key_data_storage_t" );
1729#endif
1730 memcpy( &data.slot_number, &slot->data.se.slot_number,
1731 sizeof( slot->data.se.slot_number ) );
1732 memcpy( &data.bits, &slot->attr.bits,
1733 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001734 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001735 (uint8_t*) &data,
1736 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001737 }
1738 else
1739#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1740 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001741 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001742 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001743 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001744 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1745 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001746 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001747 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1748 status = psa_internal_export_key( slot,
1749 buffer, buffer_size, &length,
1750 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001751 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001752 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001753 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001754
Gilles Peskinef9168942019-09-12 19:20:29 +02001755 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001756 mbedtls_free( buffer );
1757 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001759#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1760
Gilles Peskinecbaff462019-07-12 23:46:04 +02001761#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001762 /* Finish the transaction for a key creation. This does not
1763 * happen when registering an existing key. Detect this case
1764 * by checking whether a transaction is in progress (actual
1765 * creation of a key in a secure element requires a transaction,
1766 * but registration doesn't use one). */
1767 if( driver != NULL &&
1768 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001769 {
1770 status = psa_save_se_persistent_data( driver );
1771 if( status != PSA_SUCCESS )
1772 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001773 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001774 return( status );
1775 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001776 status = psa_crypto_stop_transaction( );
1777 if( status != PSA_SUCCESS )
1778 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001779 }
1780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1781
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 return( status );
1783}
1784
1785/** Abort the creation of a key.
1786 *
1787 * You may call this function after calling psa_start_key_creation(),
1788 * or after psa_finish_key_creation() fails. In other circumstances, this
1789 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001790 * See the documentation of psa_start_key_creation() for the intended use
1791 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001793 * \param[in,out] slot Pointer to the slot with key material.
1794 * \param[in] driver The secure element driver for the key,
1795 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001797static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001798 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001799{
Gilles Peskine011e4282019-06-26 18:34:38 +02001800 (void) driver;
1801
Gilles Peskine4747d192019-04-17 15:05:45 +02001802 if( slot == NULL )
1803 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001804
1805#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001806 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001807 * element, and the failure happened later (when saving metadata
1808 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001809 * element.
1810 * https://github.com/ARMmbed/mbed-crypto/issues/217
1811 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001812
Gilles Peskined7729582019-08-05 15:55:54 +02001813 /* Abort the ongoing transaction if any (there may not be one if
1814 * the creation process failed before starting one, or if the
1815 * key creation is a registration of a key in a secure element).
1816 * Earlier functions must already have done what it takes to undo any
1817 * partial creation. All that's left is to update the transaction data
1818 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001819 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001820#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1821
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 psa_wipe_key_slot( slot );
1823}
1824
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001825/** Validate optional attributes during key creation.
1826 *
1827 * Some key attributes are optional during key creation. If they are
1828 * specified in the attributes structure, check that they are consistent
1829 * with the data in the slot.
1830 *
1831 * This function should be called near the end of key creation, after
1832 * the slot in memory is fully populated but before saving persistent data.
1833 */
1834static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 const psa_key_slot_t *slot,
1836 const psa_key_attributes_t *attributes )
1837{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001838 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001839 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001840 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001841 return( PSA_ERROR_INVALID_ARGUMENT );
1842 }
1843
1844 if( attributes->domain_parameters_size != 0 )
1845 {
1846#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001847 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001848 {
1849 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 mbedtls_mpi_init( &actual );
1852 mbedtls_mpi_init( &required );
1853 ret = mbedtls_rsa_export( slot->data.rsa,
1854 NULL, NULL, NULL, NULL, &actual );
1855 if( ret != 0 )
1856 goto rsa_exit;
1857 ret = mbedtls_mpi_read_binary( &required,
1858 attributes->domain_parameters,
1859 attributes->domain_parameters_size );
1860 if( ret != 0 )
1861 goto rsa_exit;
1862 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1863 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1864 rsa_exit:
1865 mbedtls_mpi_free( &actual );
1866 mbedtls_mpi_free( &required );
1867 if( ret != 0)
1868 return( mbedtls_to_psa_error( ret ) );
1869 }
1870 else
1871#endif
1872 {
1873 return( PSA_ERROR_INVALID_ARGUMENT );
1874 }
1875 }
1876
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001877 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001878 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001879 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880 return( PSA_ERROR_INVALID_ARGUMENT );
1881 }
1882
1883 return( PSA_SUCCESS );
1884}
1885
Gilles Peskine4747d192019-04-17 15:05:45 +02001886psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001888 size_t data_length,
1889 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001890{
1891 psa_status_t status;
1892 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894
Gilles Peskine0f84d622019-09-12 19:03:13 +02001895 /* Reject zero-length symmetric keys (including raw data key objects).
1896 * This also rejects any key which might be encoded as an empty string,
1897 * which is never valid. */
1898 if( data_length == 0 )
1899 return( PSA_ERROR_INVALID_ARGUMENT );
1900
Gilles Peskinedf179142019-07-15 22:02:14 +02001901 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1902 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903 if( status != PSA_SUCCESS )
1904 goto exit;
1905
Gilles Peskine5d309672019-07-12 23:47:28 +02001906#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1907 if( driver != NULL )
1908 {
1909 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001910 /* The driver should set the number of key bits, however in
1911 * case it doesn't, we initialize bits to an invalid value. */
1912 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001913 if( drv->key_management == NULL ||
1914 drv->key_management->p_import == NULL )
1915 {
1916 status = PSA_ERROR_NOT_SUPPORTED;
1917 goto exit;
1918 }
1919 status = drv->key_management->p_import(
1920 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001921 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001922 &bits );
1923 if( status != PSA_SUCCESS )
1924 goto exit;
1925 if( bits > PSA_MAX_KEY_BITS )
1926 {
1927 status = PSA_ERROR_NOT_SUPPORTED;
1928 goto exit;
1929 }
1930 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001931 }
1932 else
1933#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1934 {
1935 status = psa_import_key_into_slot( slot, data, data_length );
1936 if( status != PSA_SUCCESS )
1937 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001938 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001939 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001940 if( status != PSA_SUCCESS )
1941 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942
Gilles Peskine011e4282019-06-26 18:34:38 +02001943 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001945 if( status != PSA_SUCCESS )
1946 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001947 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001948 *handle = 0;
1949 }
1950 return( status );
1951}
1952
Gilles Peskined7729582019-08-05 15:55:54 +02001953#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1954psa_status_t mbedtls_psa_register_se_key(
1955 const psa_key_attributes_t *attributes )
1956{
1957 psa_status_t status;
1958 psa_key_slot_t *slot = NULL;
1959 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001960 psa_key_handle_t handle = 0;
1961
1962 /* Leaving attributes unspecified is not currently supported.
1963 * It could make sense to query the key type and size from the
1964 * secure element, but not all secure elements support this
1965 * and the driver HAL doesn't currently support it. */
1966 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1967 return( PSA_ERROR_NOT_SUPPORTED );
1968 if( psa_get_key_bits( attributes ) == 0 )
1969 return( PSA_ERROR_NOT_SUPPORTED );
1970
1971 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1972 &handle, &slot, &driver );
1973 if( status != PSA_SUCCESS )
1974 goto exit;
1975
Gilles Peskined7729582019-08-05 15:55:54 +02001976 status = psa_finish_key_creation( slot, driver );
1977
1978exit:
1979 if( status != PSA_SUCCESS )
1980 {
1981 psa_fail_key_creation( slot, driver );
1982 }
1983 /* Registration doesn't keep the key in RAM. */
1984 psa_close_key( handle );
1985 return( status );
1986}
1987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1988
Gilles Peskinef603c712019-01-19 13:40:11 +01001989static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001991{
1992 psa_status_t status;
1993 uint8_t *buffer = NULL;
1994 size_t buffer_size = 0;
1995 size_t length;
1996
Gilles Peskine8e338702019-07-30 20:06:31 +02001997 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001998 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001999 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002000 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002002 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2003 if( status != PSA_SUCCESS )
2004 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002005 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002007
2008exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002009 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002010 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002011 return( status );
2012}
2013
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2015 const psa_key_attributes_t *specified_attributes,
2016 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002017{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002019 psa_key_slot_t *source_slot = NULL;
2020 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002022 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002023
Gilles Peskine28f8f302019-07-24 13:30:31 +02002024 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002025 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002026 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002027 goto exit;
2028
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002029 status = psa_validate_optional_attributes( source_slot,
2030 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 if( status != PSA_SUCCESS )
2032 goto exit;
2033
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002034 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002035 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002036 if( status != PSA_SUCCESS )
2037 goto exit;
2038
Gilles Peskinedf179142019-07-15 22:02:14 +02002039 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2040 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002041 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002042 if( status != PSA_SUCCESS )
2043 goto exit;
2044
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002045#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2046 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002047 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002048 /* Copying to a secure element is not implemented yet. */
2049 status = PSA_ERROR_NOT_SUPPORTED;
2050 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002051 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002053
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002054 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002055 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002057
Gilles Peskine011e4282019-06-26 18:34:38 +02002058 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002059exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002060 if( status != PSA_SUCCESS )
2061 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002062 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002063 *target_handle = 0;
2064 }
2065 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002066}
2067
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002068
2069
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002070/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002071/* Message digests */
2072/****************************************************************/
2073
Gilles Peskineb16841e2019-10-10 20:36:12 +02002074#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002075static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002076{
2077 switch( alg )
2078 {
2079#if defined(MBEDTLS_MD2_C)
2080 case PSA_ALG_MD2:
2081 return( &mbedtls_md2_info );
2082#endif
2083#if defined(MBEDTLS_MD4_C)
2084 case PSA_ALG_MD4:
2085 return( &mbedtls_md4_info );
2086#endif
2087#if defined(MBEDTLS_MD5_C)
2088 case PSA_ALG_MD5:
2089 return( &mbedtls_md5_info );
2090#endif
2091#if defined(MBEDTLS_RIPEMD160_C)
2092 case PSA_ALG_RIPEMD160:
2093 return( &mbedtls_ripemd160_info );
2094#endif
2095#if defined(MBEDTLS_SHA1_C)
2096 case PSA_ALG_SHA_1:
2097 return( &mbedtls_sha1_info );
2098#endif
2099#if defined(MBEDTLS_SHA256_C)
2100 case PSA_ALG_SHA_224:
2101 return( &mbedtls_sha224_info );
2102 case PSA_ALG_SHA_256:
2103 return( &mbedtls_sha256_info );
2104#endif
2105#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002106#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002107 case PSA_ALG_SHA_384:
2108 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002109#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002110 case PSA_ALG_SHA_512:
2111 return( &mbedtls_sha512_info );
2112#endif
2113 default:
2114 return( NULL );
2115 }
2116}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002117#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002118
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2120{
2121 switch( operation->alg )
2122 {
Gilles Peskine81736312018-06-26 15:04:31 +02002123 case 0:
2124 /* The object has (apparently) been initialized but it is not
2125 * in use. It's ok to call abort on such an object, and there's
2126 * nothing to do. */
2127 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128#if defined(MBEDTLS_MD2_C)
2129 case PSA_ALG_MD2:
2130 mbedtls_md2_free( &operation->ctx.md2 );
2131 break;
2132#endif
2133#if defined(MBEDTLS_MD4_C)
2134 case PSA_ALG_MD4:
2135 mbedtls_md4_free( &operation->ctx.md4 );
2136 break;
2137#endif
2138#if defined(MBEDTLS_MD5_C)
2139 case PSA_ALG_MD5:
2140 mbedtls_md5_free( &operation->ctx.md5 );
2141 break;
2142#endif
2143#if defined(MBEDTLS_RIPEMD160_C)
2144 case PSA_ALG_RIPEMD160:
2145 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_SHA1_C)
2149 case PSA_ALG_SHA_1:
2150 mbedtls_sha1_free( &operation->ctx.sha1 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_SHA256_C)
2154 case PSA_ALG_SHA_224:
2155 case PSA_ALG_SHA_256:
2156 mbedtls_sha256_free( &operation->ctx.sha256 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002160#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002161 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002162#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163 case PSA_ALG_SHA_512:
2164 mbedtls_sha512_free( &operation->ctx.sha512 );
2165 break;
2166#endif
2167 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002168 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002169 }
2170 operation->alg = 0;
2171 return( PSA_SUCCESS );
2172}
2173
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002174psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175 psa_algorithm_t alg )
2176{
Janos Follath24eed8d2019-11-22 13:21:35 +00002177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002178
2179 /* A context must be freshly initialized before it can be set up. */
2180 if( operation->alg != 0 )
2181 {
2182 return( PSA_ERROR_BAD_STATE );
2183 }
2184
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185 switch( alg )
2186 {
2187#if defined(MBEDTLS_MD2_C)
2188 case PSA_ALG_MD2:
2189 mbedtls_md2_init( &operation->ctx.md2 );
2190 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2191 break;
2192#endif
2193#if defined(MBEDTLS_MD4_C)
2194 case PSA_ALG_MD4:
2195 mbedtls_md4_init( &operation->ctx.md4 );
2196 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2197 break;
2198#endif
2199#if defined(MBEDTLS_MD5_C)
2200 case PSA_ALG_MD5:
2201 mbedtls_md5_init( &operation->ctx.md5 );
2202 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2203 break;
2204#endif
2205#if defined(MBEDTLS_RIPEMD160_C)
2206 case PSA_ALG_RIPEMD160:
2207 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2208 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2209 break;
2210#endif
2211#if defined(MBEDTLS_SHA1_C)
2212 case PSA_ALG_SHA_1:
2213 mbedtls_sha1_init( &operation->ctx.sha1 );
2214 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2215 break;
2216#endif
2217#if defined(MBEDTLS_SHA256_C)
2218 case PSA_ALG_SHA_224:
2219 mbedtls_sha256_init( &operation->ctx.sha256 );
2220 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2221 break;
2222 case PSA_ALG_SHA_256:
2223 mbedtls_sha256_init( &operation->ctx.sha256 );
2224 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2225 break;
2226#endif
2227#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002228#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 case PSA_ALG_SHA_384:
2230 mbedtls_sha512_init( &operation->ctx.sha512 );
2231 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2232 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002233#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234 case PSA_ALG_SHA_512:
2235 mbedtls_sha512_init( &operation->ctx.sha512 );
2236 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2237 break;
2238#endif
2239 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002240 return( PSA_ALG_IS_HASH( alg ) ?
2241 PSA_ERROR_NOT_SUPPORTED :
2242 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243 }
2244 if( ret == 0 )
2245 operation->alg = alg;
2246 else
2247 psa_hash_abort( operation );
2248 return( mbedtls_to_psa_error( ret ) );
2249}
2250
2251psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2252 const uint8_t *input,
2253 size_t input_length )
2254{
Janos Follath24eed8d2019-11-22 13:21:35 +00002255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002256
2257 /* Don't require hash implementations to behave correctly on a
2258 * zero-length input, which may have an invalid pointer. */
2259 if( input_length == 0 )
2260 return( PSA_SUCCESS );
2261
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 switch( operation->alg )
2263 {
2264#if defined(MBEDTLS_MD2_C)
2265 case PSA_ALG_MD2:
2266 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2267 input, input_length );
2268 break;
2269#endif
2270#if defined(MBEDTLS_MD4_C)
2271 case PSA_ALG_MD4:
2272 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2273 input, input_length );
2274 break;
2275#endif
2276#if defined(MBEDTLS_MD5_C)
2277 case PSA_ALG_MD5:
2278 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2279 input, input_length );
2280 break;
2281#endif
2282#if defined(MBEDTLS_RIPEMD160_C)
2283 case PSA_ALG_RIPEMD160:
2284 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2285 input, input_length );
2286 break;
2287#endif
2288#if defined(MBEDTLS_SHA1_C)
2289 case PSA_ALG_SHA_1:
2290 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2291 input, input_length );
2292 break;
2293#endif
2294#if defined(MBEDTLS_SHA256_C)
2295 case PSA_ALG_SHA_224:
2296 case PSA_ALG_SHA_256:
2297 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2298 input, input_length );
2299 break;
2300#endif
2301#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002302#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002304#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305 case PSA_ALG_SHA_512:
2306 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2307 input, input_length );
2308 break;
2309#endif
2310 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002311 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002312 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002313
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 if( ret != 0 )
2315 psa_hash_abort( operation );
2316 return( mbedtls_to_psa_error( ret ) );
2317}
2318
2319psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2320 uint8_t *hash,
2321 size_t hash_size,
2322 size_t *hash_length )
2323{
itayzafrir40835d42018-08-02 13:14:17 +03002324 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002326 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327
2328 /* Fill the output buffer with something that isn't a valid hash
2329 * (barring an attack on the hash and deliberately-crafted input),
2330 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002331 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002332 /* If hash_size is 0 then hash may be NULL and then the
2333 * call to memset would have undefined behavior. */
2334 if( hash_size != 0 )
2335 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336
2337 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002338 {
2339 status = PSA_ERROR_BUFFER_TOO_SMALL;
2340 goto exit;
2341 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342
2343 switch( operation->alg )
2344 {
2345#if defined(MBEDTLS_MD2_C)
2346 case PSA_ALG_MD2:
2347 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2348 break;
2349#endif
2350#if defined(MBEDTLS_MD4_C)
2351 case PSA_ALG_MD4:
2352 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2353 break;
2354#endif
2355#if defined(MBEDTLS_MD5_C)
2356 case PSA_ALG_MD5:
2357 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2358 break;
2359#endif
2360#if defined(MBEDTLS_RIPEMD160_C)
2361 case PSA_ALG_RIPEMD160:
2362 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2363 break;
2364#endif
2365#if defined(MBEDTLS_SHA1_C)
2366 case PSA_ALG_SHA_1:
2367 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2368 break;
2369#endif
2370#if defined(MBEDTLS_SHA256_C)
2371 case PSA_ALG_SHA_224:
2372 case PSA_ALG_SHA_256:
2373 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2374 break;
2375#endif
2376#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002377#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002378 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002379#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380 case PSA_ALG_SHA_512:
2381 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2382 break;
2383#endif
2384 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002385 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386 }
itayzafrir40835d42018-08-02 13:14:17 +03002387 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388
itayzafrir40835d42018-08-02 13:14:17 +03002389exit:
2390 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002392 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393 return( psa_hash_abort( operation ) );
2394 }
2395 else
2396 {
2397 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002398 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399 }
2400}
2401
Gilles Peskine2d277862018-06-18 15:41:12 +02002402psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2403 const uint8_t *hash,
2404 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405{
2406 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2407 size_t actual_hash_length;
2408 psa_status_t status = psa_hash_finish( operation,
2409 actual_hash, sizeof( actual_hash ),
2410 &actual_hash_length );
2411 if( status != PSA_SUCCESS )
2412 return( status );
2413 if( actual_hash_length != hash_length )
2414 return( PSA_ERROR_INVALID_SIGNATURE );
2415 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2416 return( PSA_ERROR_INVALID_SIGNATURE );
2417 return( PSA_SUCCESS );
2418}
2419
Gilles Peskine0a749c82019-11-28 19:33:58 +01002420psa_status_t psa_hash_compute( psa_algorithm_t alg,
2421 const uint8_t *input, size_t input_length,
2422 uint8_t *hash, size_t hash_size,
2423 size_t *hash_length )
2424{
2425 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2427
2428 *hash_length = hash_size;
2429 status = psa_hash_setup( &operation, alg );
2430 if( status != PSA_SUCCESS )
2431 goto exit;
2432 status = psa_hash_update( &operation, input, input_length );
2433 if( status != PSA_SUCCESS )
2434 goto exit;
2435 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2436 if( status != PSA_SUCCESS )
2437 goto exit;
2438
2439exit:
2440 if( status == PSA_SUCCESS )
2441 status = psa_hash_abort( &operation );
2442 else
2443 psa_hash_abort( &operation );
2444 return( status );
2445}
2446
2447psa_status_t psa_hash_compare( psa_algorithm_t alg,
2448 const uint8_t *input, size_t input_length,
2449 const uint8_t *hash, size_t hash_length )
2450{
2451 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2453
2454 status = psa_hash_setup( &operation, alg );
2455 if( status != PSA_SUCCESS )
2456 goto exit;
2457 status = psa_hash_update( &operation, input, input_length );
2458 if( status != PSA_SUCCESS )
2459 goto exit;
2460 status = psa_hash_verify( &operation, hash, hash_length );
2461 if( status != PSA_SUCCESS )
2462 goto exit;
2463
2464exit:
2465 if( status == PSA_SUCCESS )
2466 status = psa_hash_abort( &operation );
2467 else
2468 psa_hash_abort( &operation );
2469 return( status );
2470}
2471
Gilles Peskineeb35d782019-01-22 17:56:16 +01002472psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2473 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474{
2475 if( target_operation->alg != 0 )
2476 return( PSA_ERROR_BAD_STATE );
2477
2478 switch( source_operation->alg )
2479 {
2480 case 0:
2481 return( PSA_ERROR_BAD_STATE );
2482#if defined(MBEDTLS_MD2_C)
2483 case PSA_ALG_MD2:
2484 mbedtls_md2_clone( &target_operation->ctx.md2,
2485 &source_operation->ctx.md2 );
2486 break;
2487#endif
2488#if defined(MBEDTLS_MD4_C)
2489 case PSA_ALG_MD4:
2490 mbedtls_md4_clone( &target_operation->ctx.md4,
2491 &source_operation->ctx.md4 );
2492 break;
2493#endif
2494#if defined(MBEDTLS_MD5_C)
2495 case PSA_ALG_MD5:
2496 mbedtls_md5_clone( &target_operation->ctx.md5,
2497 &source_operation->ctx.md5 );
2498 break;
2499#endif
2500#if defined(MBEDTLS_RIPEMD160_C)
2501 case PSA_ALG_RIPEMD160:
2502 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2503 &source_operation->ctx.ripemd160 );
2504 break;
2505#endif
2506#if defined(MBEDTLS_SHA1_C)
2507 case PSA_ALG_SHA_1:
2508 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2509 &source_operation->ctx.sha1 );
2510 break;
2511#endif
2512#if defined(MBEDTLS_SHA256_C)
2513 case PSA_ALG_SHA_224:
2514 case PSA_ALG_SHA_256:
2515 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2516 &source_operation->ctx.sha256 );
2517 break;
2518#endif
2519#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002520#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002521 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002522#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002523 case PSA_ALG_SHA_512:
2524 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2525 &source_operation->ctx.sha512 );
2526 break;
2527#endif
2528 default:
2529 return( PSA_ERROR_NOT_SUPPORTED );
2530 }
2531
2532 target_operation->alg = source_operation->alg;
2533 return( PSA_SUCCESS );
2534}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535
2536
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002537/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538/* MAC */
2539/****************************************************************/
2540
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002541static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 psa_algorithm_t alg,
2543 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002544 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002545 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002548 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002550 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002551 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002552
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2554 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002555 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002557 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002558 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 mode = MBEDTLS_MODE_STREAM;
2560 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 case PSA_ALG_CTR:
2562 mode = MBEDTLS_MODE_CTR;
2563 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002564 case PSA_ALG_CFB:
2565 mode = MBEDTLS_MODE_CFB;
2566 break;
2567 case PSA_ALG_OFB:
2568 mode = MBEDTLS_MODE_OFB;
2569 break;
2570 case PSA_ALG_CBC_NO_PADDING:
2571 mode = MBEDTLS_MODE_CBC;
2572 break;
2573 case PSA_ALG_CBC_PKCS7:
2574 mode = MBEDTLS_MODE_CBC;
2575 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002576 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577 mode = MBEDTLS_MODE_CCM;
2578 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002579 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580 mode = MBEDTLS_MODE_GCM;
2581 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002582 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2583 mode = MBEDTLS_MODE_CHACHAPOLY;
2584 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 default:
2586 return( NULL );
2587 }
2588 }
2589 else if( alg == PSA_ALG_CMAC )
2590 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002591 else
2592 return( NULL );
2593
2594 switch( key_type )
2595 {
2596 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002597 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 break;
2599 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002600 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2601 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002603 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002605 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002606 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2607 * but two-key Triple-DES is functionally three-key Triple-DES
2608 * with K1=K3, so that's how we present it to mbedtls. */
2609 if( key_bits == 128 )
2610 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 break;
2612 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002613 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 break;
2615 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002616 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002618 case PSA_KEY_TYPE_CHACHA20:
2619 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2620 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621 default:
2622 return( NULL );
2623 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002624 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002625 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002626
Jaeden Amero23bbb752018-06-26 14:16:54 +01002627 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2628 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629}
2630
Gilles Peskinea05219c2018-11-16 16:02:56 +01002631#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002632static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002633{
Gilles Peskine2d277862018-06-18 15:41:12 +02002634 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002635 {
2636 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002637 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002638 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002639 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002640 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002641 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002642 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002643 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002644 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002645 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002646 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002647 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002648 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002649 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002650 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002651 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002652 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002653 return( 128 );
2654 default:
2655 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002656 }
2657}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002658#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002659
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660/* Initialize the MAC operation structure. Once this function has been
2661 * called, psa_mac_abort can run and will do the right thing. */
2662static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2663 psa_algorithm_t alg )
2664{
2665 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2666
2667 operation->alg = alg;
2668 operation->key_set = 0;
2669 operation->iv_set = 0;
2670 operation->iv_required = 0;
2671 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002672 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002673
2674#if defined(MBEDTLS_CMAC_C)
2675 if( alg == PSA_ALG_CMAC )
2676 {
2677 operation->iv_required = 0;
2678 mbedtls_cipher_init( &operation->ctx.cmac );
2679 status = PSA_SUCCESS;
2680 }
2681 else
2682#endif /* MBEDTLS_CMAC_C */
2683#if defined(MBEDTLS_MD_C)
2684 if( PSA_ALG_IS_HMAC( operation->alg ) )
2685 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002686 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2687 operation->ctx.hmac.hash_ctx.alg = 0;
2688 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689 }
2690 else
2691#endif /* MBEDTLS_MD_C */
2692 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002693 if( ! PSA_ALG_IS_MAC( alg ) )
2694 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002695 }
2696
2697 if( status != PSA_SUCCESS )
2698 memset( operation, 0, sizeof( *operation ) );
2699 return( status );
2700}
2701
Gilles Peskine01126fa2018-07-12 17:04:55 +02002702#if defined(MBEDTLS_MD_C)
2703static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2704{
Gilles Peskine3f108122018-12-07 18:14:53 +01002705 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002706 return( psa_hash_abort( &hmac->hash_ctx ) );
2707}
2708#endif /* MBEDTLS_MD_C */
2709
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2711{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002712 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002714 /* The object has (apparently) been initialized but it is not
2715 * in use. It's ok to call abort on such an object, and there's
2716 * nothing to do. */
2717 return( PSA_SUCCESS );
2718 }
2719 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002721 if( operation->alg == PSA_ALG_CMAC )
2722 {
2723 mbedtls_cipher_free( &operation->ctx.cmac );
2724 }
2725 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002728 if( PSA_ALG_IS_HMAC( operation->alg ) )
2729 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002730 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002731 }
2732 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002734 {
2735 /* Sanity check (shouldn't happen: operation->alg should
2736 * always have been initialized to a valid value). */
2737 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738 }
Moran Peker41deec42018-04-04 15:43:05 +03002739
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 operation->alg = 0;
2741 operation->key_set = 0;
2742 operation->iv_set = 0;
2743 operation->iv_required = 0;
2744 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002745 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002746
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002748
2749bad_state:
2750 /* If abort is called on an uninitialized object, we can't trust
2751 * anything. Wipe the object in case it contains confidential data.
2752 * This may result in a memory leak if a pointer gets overwritten,
2753 * but it's too late to do anything about this. */
2754 memset( operation, 0, sizeof( *operation ) );
2755 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756}
2757
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002758#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002759static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002761 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762 const mbedtls_cipher_info_t *cipher_info )
2763{
Janos Follath24eed8d2019-11-22 13:21:35 +00002764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765
2766 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002767
2768 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2769 if( ret != 0 )
2770 return( ret );
2771
2772 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2773 slot->data.raw.data,
2774 key_bits );
2775 return( ret );
2776}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002777#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002778
Gilles Peskine248051a2018-06-20 16:09:38 +02002779#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002780static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2781 const uint8_t *key,
2782 size_t key_length,
2783 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002784{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002785 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002787 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002788 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789 psa_status_t status;
2790
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002791 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2792 * overflow below. This should never trigger if the hash algorithm
2793 * is implemented correctly. */
2794 /* The size checks against the ipad and opad buffers cannot be written
2795 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2796 * because that triggers -Wlogical-op on GCC 7.3. */
2797 if( block_size > sizeof( ipad ) )
2798 return( PSA_ERROR_NOT_SUPPORTED );
2799 if( block_size > sizeof( hmac->opad ) )
2800 return( PSA_ERROR_NOT_SUPPORTED );
2801 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002802 return( PSA_ERROR_NOT_SUPPORTED );
2803
Gilles Peskined223b522018-06-11 18:12:58 +02002804 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002805 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002806 status = psa_hash_compute( hash_alg, key, key_length,
2807 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002808 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002809 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002810 }
Gilles Peskine96889972018-07-12 17:07:03 +02002811 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2812 * but it is permitted. It is common when HMAC is used in HKDF, for
2813 * example. Don't call `memcpy` in the 0-length because `key` could be
2814 * an invalid pointer which would make the behavior undefined. */
2815 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002816 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002817
Gilles Peskined223b522018-06-11 18:12:58 +02002818 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2819 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002820 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002821 ipad[i] ^= 0x36;
2822 memset( ipad + key_length, 0x36, block_size - key_length );
2823
2824 /* Copy the key material from ipad to opad, flipping the requisite bits,
2825 * and filling the rest of opad with the requisite constant. */
2826 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002827 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2828 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002829
Gilles Peskine01126fa2018-07-12 17:04:55 +02002830 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002831 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002832 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002833
Gilles Peskine01126fa2018-07-12 17:04:55 +02002834 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002835
2836cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002837 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002838
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002839 return( status );
2840}
Gilles Peskine248051a2018-06-20 16:09:38 +02002841#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002842
Gilles Peskine89167cb2018-07-08 20:12:23 +02002843static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002844 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002845 psa_algorithm_t alg,
2846 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002849 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002851 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002852 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002853 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002854 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002856 /* A context must be freshly initialized before it can be set up. */
2857 if( operation->alg != 0 )
2858 {
2859 return( PSA_ERROR_BAD_STATE );
2860 }
2861
Gilles Peskined911eb72018-08-14 15:18:45 +02002862 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002863 if( status != PSA_SUCCESS )
2864 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002865 if( is_sign )
2866 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002867
Gilles Peskine28f8f302019-07-24 13:30:31 +02002868 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002869 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002870 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002871 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002872
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002874 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 {
2876 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002877 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002878 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002880 if( cipher_info == NULL )
2881 {
2882 status = PSA_ERROR_NOT_SUPPORTED;
2883 goto exit;
2884 }
2885 operation->mac_size = cipher_info->block_size;
2886 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2887 status = mbedtls_to_psa_error( ret );
2888 }
2889 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002892 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002893 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002894 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002895 if( hash_alg == 0 )
2896 {
2897 status = PSA_ERROR_NOT_SUPPORTED;
2898 goto exit;
2899 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002900
2901 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2902 /* Sanity check. This shouldn't fail on a valid configuration. */
2903 if( operation->mac_size == 0 ||
2904 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2905 {
2906 status = PSA_ERROR_NOT_SUPPORTED;
2907 goto exit;
2908 }
2909
Gilles Peskine8e338702019-07-30 20:06:31 +02002910 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002911 {
2912 status = PSA_ERROR_INVALID_ARGUMENT;
2913 goto exit;
2914 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002915
Gilles Peskine01126fa2018-07-12 17:04:55 +02002916 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2917 slot->data.raw.data,
2918 slot->data.raw.bytes,
2919 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002920 }
2921 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002923 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002924 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002925 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002927
Gilles Peskined911eb72018-08-14 15:18:45 +02002928 if( truncated == 0 )
2929 {
2930 /* The "normal" case: untruncated algorithm. Nothing to do. */
2931 }
2932 else if( truncated < 4 )
2933 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002934 /* A very short MAC is too short for security since it can be
2935 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2936 * so we make this our minimum, even though 32 bits is still
2937 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002938 status = PSA_ERROR_NOT_SUPPORTED;
2939 }
2940 else if( truncated > operation->mac_size )
2941 {
2942 /* It's impossible to "truncate" to a larger length. */
2943 status = PSA_ERROR_INVALID_ARGUMENT;
2944 }
2945 else
2946 operation->mac_size = truncated;
2947
Gilles Peskinefbfac682018-07-08 20:51:54 +02002948exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002951 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002952 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002953 else
2954 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002955 operation->key_set = 1;
2956 }
2957 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958}
2959
Gilles Peskine89167cb2018-07-08 20:12:23 +02002960psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002961 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002962 psa_algorithm_t alg )
2963{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002964 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002965}
2966
2967psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002968 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002969 psa_algorithm_t alg )
2970{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002971 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002972}
2973
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2975 const uint8_t *input,
2976 size_t input_length )
2977{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002978 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002979 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002980 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002982 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002983 operation->has_input = 1;
2984
Gilles Peskine8c9def32018-02-08 10:02:12 +01002985#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002986 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002987 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002988 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2989 input, input_length );
2990 status = mbedtls_to_psa_error( ret );
2991 }
2992 else
2993#endif /* MBEDTLS_CMAC_C */
2994#if defined(MBEDTLS_MD_C)
2995 if( PSA_ALG_IS_HMAC( operation->alg ) )
2996 {
2997 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2998 input_length );
2999 }
3000 else
3001#endif /* MBEDTLS_MD_C */
3002 {
3003 /* This shouldn't happen if `operation` was initialized by
3004 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003005 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003006 }
3007
Gilles Peskinefbfac682018-07-08 20:51:54 +02003008 if( status != PSA_SUCCESS )
3009 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003010 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011}
3012
Gilles Peskine01126fa2018-07-12 17:04:55 +02003013#if defined(MBEDTLS_MD_C)
3014static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3015 uint8_t *mac,
3016 size_t mac_size )
3017{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003018 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003019 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3020 size_t hash_size = 0;
3021 size_t block_size = psa_get_hash_block_size( hash_alg );
3022 psa_status_t status;
3023
Gilles Peskine01126fa2018-07-12 17:04:55 +02003024 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3025 if( status != PSA_SUCCESS )
3026 return( status );
3027 /* From here on, tmp needs to be wiped. */
3028
3029 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3030 if( status != PSA_SUCCESS )
3031 goto exit;
3032
3033 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3034 if( status != PSA_SUCCESS )
3035 goto exit;
3036
3037 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3038 if( status != PSA_SUCCESS )
3039 goto exit;
3040
Gilles Peskined911eb72018-08-14 15:18:45 +02003041 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3042 if( status != PSA_SUCCESS )
3043 goto exit;
3044
3045 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003046
3047exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003048 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003049 return( status );
3050}
3051#endif /* MBEDTLS_MD_C */
3052
mohammad16036df908f2018-04-02 08:34:15 -07003053static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003054 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003055 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003057 if( ! operation->key_set )
3058 return( PSA_ERROR_BAD_STATE );
3059 if( operation->iv_required && ! operation->iv_set )
3060 return( PSA_ERROR_BAD_STATE );
3061
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062 if( mac_size < operation->mac_size )
3063 return( PSA_ERROR_BUFFER_TOO_SMALL );
3064
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003066 if( operation->alg == PSA_ALG_CMAC )
3067 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003068 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3069 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3070 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003071 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003072 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003073 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003075 else
3076#endif /* MBEDTLS_CMAC_C */
3077#if defined(MBEDTLS_MD_C)
3078 if( PSA_ALG_IS_HMAC( operation->alg ) )
3079 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003080 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003081 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003082 }
3083 else
3084#endif /* MBEDTLS_MD_C */
3085 {
3086 /* This shouldn't happen if `operation` was initialized by
3087 * a setup function. */
3088 return( PSA_ERROR_BAD_STATE );
3089 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003090}
3091
Gilles Peskineacd4be32018-07-08 19:56:25 +02003092psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3093 uint8_t *mac,
3094 size_t mac_size,
3095 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003096{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003097 psa_status_t status;
3098
Jaeden Amero252ef282019-02-15 14:05:35 +00003099 if( operation->alg == 0 )
3100 {
3101 return( PSA_ERROR_BAD_STATE );
3102 }
3103
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003104 /* Fill the output buffer with something that isn't a valid mac
3105 * (barring an attack on the mac and deliberately-crafted input),
3106 * in case the caller doesn't check the return status properly. */
3107 *mac_length = mac_size;
3108 /* If mac_size is 0 then mac may be NULL and then the
3109 * call to memset would have undefined behavior. */
3110 if( mac_size != 0 )
3111 memset( mac, '!', mac_size );
3112
Gilles Peskine89167cb2018-07-08 20:12:23 +02003113 if( ! operation->is_sign )
3114 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003115 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003116 }
mohammad16036df908f2018-04-02 08:34:15 -07003117
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003118 status = psa_mac_finish_internal( operation, mac, mac_size );
3119
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003120 if( status == PSA_SUCCESS )
3121 {
3122 status = psa_mac_abort( operation );
3123 if( status == PSA_SUCCESS )
3124 *mac_length = operation->mac_size;
3125 else
3126 memset( mac, '!', mac_size );
3127 }
3128 else
3129 psa_mac_abort( operation );
3130 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003131}
3132
Gilles Peskineacd4be32018-07-08 19:56:25 +02003133psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3134 const uint8_t *mac,
3135 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003136{
Gilles Peskine828ed142018-06-18 23:25:51 +02003137 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003138 psa_status_t status;
3139
Jaeden Amero252ef282019-02-15 14:05:35 +00003140 if( operation->alg == 0 )
3141 {
3142 return( PSA_ERROR_BAD_STATE );
3143 }
3144
Gilles Peskine89167cb2018-07-08 20:12:23 +02003145 if( operation->is_sign )
3146 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003147 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003148 }
3149 if( operation->mac_size != mac_length )
3150 {
3151 status = PSA_ERROR_INVALID_SIGNATURE;
3152 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003153 }
mohammad16036df908f2018-04-02 08:34:15 -07003154
3155 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003156 actual_mac, sizeof( actual_mac ) );
3157
3158 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3159 status = PSA_ERROR_INVALID_SIGNATURE;
3160
3161cleanup:
3162 if( status == PSA_SUCCESS )
3163 status = psa_mac_abort( operation );
3164 else
3165 psa_mac_abort( operation );
3166
Gilles Peskine3f108122018-12-07 18:14:53 +01003167 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003168
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003169 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170}
3171
3172
Gilles Peskine20035e32018-02-03 22:44:14 +01003173
Gilles Peskine20035e32018-02-03 22:44:14 +01003174/****************************************************************/
3175/* Asymmetric cryptography */
3176/****************************************************************/
3177
Gilles Peskine2b450e32018-06-27 15:42:46 +02003178#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003179/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003180 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003181static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3182 size_t hash_length,
3183 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003184{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003185 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003186 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003187 *md_alg = mbedtls_md_get_type( md_info );
3188
3189 /* The Mbed TLS RSA module uses an unsigned int for hash length
3190 * parameters. Validate that it fits so that we don't risk an
3191 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003192#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003193 if( hash_length > UINT_MAX )
3194 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003195#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003196
3197#if defined(MBEDTLS_PKCS1_V15)
3198 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3199 * must be correct. */
3200 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3201 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003202 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003203 if( md_info == NULL )
3204 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003205 if( mbedtls_md_get_size( md_info ) != hash_length )
3206 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003207 }
3208#endif /* MBEDTLS_PKCS1_V15 */
3209
3210#if defined(MBEDTLS_PKCS1_V21)
3211 /* PSS requires a hash internally. */
3212 if( PSA_ALG_IS_RSA_PSS( alg ) )
3213 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003214 if( md_info == NULL )
3215 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003216 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003217#endif /* MBEDTLS_PKCS1_V21 */
3218
Gilles Peskine61b91d42018-06-08 16:09:36 +02003219 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003220}
3221
Gilles Peskine2b450e32018-06-27 15:42:46 +02003222static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3223 psa_algorithm_t alg,
3224 const uint8_t *hash,
3225 size_t hash_length,
3226 uint8_t *signature,
3227 size_t signature_size,
3228 size_t *signature_length )
3229{
3230 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003232 mbedtls_md_type_t md_alg;
3233
3234 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3235 if( status != PSA_SUCCESS )
3236 return( status );
3237
Gilles Peskine630a18a2018-06-29 17:49:35 +02003238 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003239 return( PSA_ERROR_BUFFER_TOO_SMALL );
3240
3241#if defined(MBEDTLS_PKCS1_V15)
3242 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3243 {
3244 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3245 MBEDTLS_MD_NONE );
3246 ret = mbedtls_rsa_pkcs1_sign( rsa,
3247 mbedtls_ctr_drbg_random,
3248 &global_data.ctr_drbg,
3249 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003250 md_alg,
3251 (unsigned int) hash_length,
3252 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003253 signature );
3254 }
3255 else
3256#endif /* MBEDTLS_PKCS1_V15 */
3257#if defined(MBEDTLS_PKCS1_V21)
3258 if( PSA_ALG_IS_RSA_PSS( alg ) )
3259 {
3260 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3261 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3262 mbedtls_ctr_drbg_random,
3263 &global_data.ctr_drbg,
3264 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003265 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003266 (unsigned int) hash_length,
3267 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003268 signature );
3269 }
3270 else
3271#endif /* MBEDTLS_PKCS1_V21 */
3272 {
3273 return( PSA_ERROR_INVALID_ARGUMENT );
3274 }
3275
3276 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003277 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003278 return( mbedtls_to_psa_error( ret ) );
3279}
3280
3281static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3282 psa_algorithm_t alg,
3283 const uint8_t *hash,
3284 size_t hash_length,
3285 const uint8_t *signature,
3286 size_t signature_length )
3287{
3288 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003290 mbedtls_md_type_t md_alg;
3291
3292 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3293 if( status != PSA_SUCCESS )
3294 return( status );
3295
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003296 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003298
3299#if defined(MBEDTLS_PKCS1_V15)
3300 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3301 {
3302 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3303 MBEDTLS_MD_NONE );
3304 ret = mbedtls_rsa_pkcs1_verify( rsa,
3305 mbedtls_ctr_drbg_random,
3306 &global_data.ctr_drbg,
3307 MBEDTLS_RSA_PUBLIC,
3308 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003309 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003310 hash,
3311 signature );
3312 }
3313 else
3314#endif /* MBEDTLS_PKCS1_V15 */
3315#if defined(MBEDTLS_PKCS1_V21)
3316 if( PSA_ALG_IS_RSA_PSS( alg ) )
3317 {
3318 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3319 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3320 mbedtls_ctr_drbg_random,
3321 &global_data.ctr_drbg,
3322 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003323 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003324 (unsigned int) hash_length,
3325 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003326 signature );
3327 }
3328 else
3329#endif /* MBEDTLS_PKCS1_V21 */
3330 {
3331 return( PSA_ERROR_INVALID_ARGUMENT );
3332 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003333
3334 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3335 * the rest of the signature is invalid". This has little use in
3336 * practice and PSA doesn't report this distinction. */
3337 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3338 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003339 return( mbedtls_to_psa_error( ret ) );
3340}
3341#endif /* MBEDTLS_RSA_C */
3342
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003343#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003344/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3345 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3346 * (even though these functions don't modify it). */
3347static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3348 psa_algorithm_t alg,
3349 const uint8_t *hash,
3350 size_t hash_length,
3351 uint8_t *signature,
3352 size_t signature_size,
3353 size_t *signature_length )
3354{
Janos Follath24eed8d2019-11-22 13:21:35 +00003355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003356 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003357 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003358 mbedtls_mpi_init( &r );
3359 mbedtls_mpi_init( &s );
3360
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003361 if( signature_size < 2 * curve_bytes )
3362 {
3363 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3364 goto cleanup;
3365 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003366
Gilles Peskinea05219c2018-11-16 16:02:56 +01003367#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003368 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3369 {
3370 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3371 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3372 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003373 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3374 &ecp->d, hash,
3375 hash_length, md_alg,
3376 mbedtls_ctr_drbg_random,
3377 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378 }
3379 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003380#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003381 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003382 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003383 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3384 hash, hash_length,
3385 mbedtls_ctr_drbg_random,
3386 &global_data.ctr_drbg ) );
3387 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003388
3389 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3390 signature,
3391 curve_bytes ) );
3392 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3393 signature + curve_bytes,
3394 curve_bytes ) );
3395
3396cleanup:
3397 mbedtls_mpi_free( &r );
3398 mbedtls_mpi_free( &s );
3399 if( ret == 0 )
3400 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003401 return( mbedtls_to_psa_error( ret ) );
3402}
3403
3404static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3405 const uint8_t *hash,
3406 size_t hash_length,
3407 const uint8_t *signature,
3408 size_t signature_length )
3409{
Janos Follath24eed8d2019-11-22 13:21:35 +00003410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003411 mbedtls_mpi r, s;
3412 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3413 mbedtls_mpi_init( &r );
3414 mbedtls_mpi_init( &s );
3415
3416 if( signature_length != 2 * curve_bytes )
3417 return( PSA_ERROR_INVALID_SIGNATURE );
3418
3419 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3420 signature,
3421 curve_bytes ) );
3422 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3423 signature + curve_bytes,
3424 curve_bytes ) );
3425
3426 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3427 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003428
3429cleanup:
3430 mbedtls_mpi_free( &r );
3431 mbedtls_mpi_free( &s );
3432 return( mbedtls_to_psa_error( ret ) );
3433}
3434#endif /* MBEDTLS_ECDSA_C */
3435
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003436psa_status_t psa_sign_hash( psa_key_handle_t handle,
3437 psa_algorithm_t alg,
3438 const uint8_t *hash,
3439 size_t hash_length,
3440 uint8_t *signature,
3441 size_t signature_size,
3442 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003443{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003444 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003445 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003446#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3447 const psa_drv_se_t *drv;
3448 psa_drv_se_context_t *drv_context;
3449#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003450
3451 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003452 /* Immediately reject a zero-length signature buffer. This guarantees
3453 * that signature must be a valid pointer. (On the other hand, the hash
3454 * buffer can in principle be empty since it doesn't actually have
3455 * to be a hash.) */
3456 if( signature_size == 0 )
3457 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003458
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003459 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003460 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003461 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003462 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003463 {
3464 status = PSA_ERROR_INVALID_ARGUMENT;
3465 goto exit;
3466 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003467
Gilles Peskineedc64242019-08-07 21:05:07 +02003468#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3469 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3470 {
3471 if( drv->asymmetric == NULL ||
3472 drv->asymmetric->p_sign == NULL )
3473 {
3474 status = PSA_ERROR_NOT_SUPPORTED;
3475 goto exit;
3476 }
3477 status = drv->asymmetric->p_sign( drv_context,
3478 slot->data.se.slot_number,
3479 alg,
3480 hash, hash_length,
3481 signature, signature_size,
3482 signature_length );
3483 }
3484 else
3485#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003486#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003487 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003488 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003489 status = psa_rsa_sign( slot->data.rsa,
3490 alg,
3491 hash, hash_length,
3492 signature, signature_size,
3493 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003494 }
3495 else
3496#endif /* defined(MBEDTLS_RSA_C) */
3497#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003498 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003499 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003500#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003501 if(
3502#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3503 PSA_ALG_IS_ECDSA( alg )
3504#else
3505 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3506#endif
3507 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003508 status = psa_ecdsa_sign( slot->data.ecp,
3509 alg,
3510 hash, hash_length,
3511 signature, signature_size,
3512 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003513 else
3514#endif /* defined(MBEDTLS_ECDSA_C) */
3515 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003516 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003517 }
itayzafrir5c753392018-05-08 11:18:38 +03003518 }
3519 else
3520#endif /* defined(MBEDTLS_ECP_C) */
3521 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003522 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003523 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003524
3525exit:
3526 /* Fill the unused part of the output buffer (the whole buffer on error,
3527 * the trailing part on success) with something that isn't a valid mac
3528 * (barring an attack on the mac and deliberately-crafted input),
3529 * in case the caller doesn't check the return status properly. */
3530 if( status == PSA_SUCCESS )
3531 memset( signature + *signature_length, '!',
3532 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003533 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003534 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003535 /* If signature_size is 0 then we have nothing to do. We must not call
3536 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003537 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003538}
3539
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003540psa_status_t psa_verify_hash( psa_key_handle_t handle,
3541 psa_algorithm_t alg,
3542 const uint8_t *hash,
3543 size_t hash_length,
3544 const uint8_t *signature,
3545 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003546{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003547 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003548 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003549#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3550 const psa_drv_se_t *drv;
3551 psa_drv_se_context_t *drv_context;
3552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003553
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003554 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003555 if( status != PSA_SUCCESS )
3556 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003557
Gilles Peskineedc64242019-08-07 21:05:07 +02003558#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3559 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3560 {
3561 if( drv->asymmetric == NULL ||
3562 drv->asymmetric->p_verify == NULL )
3563 return( PSA_ERROR_NOT_SUPPORTED );
3564 return( drv->asymmetric->p_verify( drv_context,
3565 slot->data.se.slot_number,
3566 alg,
3567 hash, hash_length,
3568 signature, signature_length ) );
3569 }
3570 else
3571#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003572#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003573 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003574 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003575 return( psa_rsa_verify( slot->data.rsa,
3576 alg,
3577 hash, hash_length,
3578 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579 }
3580 else
3581#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003582#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003583 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003584 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003585#if defined(MBEDTLS_ECDSA_C)
3586 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003587 return( psa_ecdsa_verify( slot->data.ecp,
3588 hash, hash_length,
3589 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003590 else
3591#endif /* defined(MBEDTLS_ECDSA_C) */
3592 {
3593 return( PSA_ERROR_INVALID_ARGUMENT );
3594 }
itayzafrir5c753392018-05-08 11:18:38 +03003595 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003596 else
3597#endif /* defined(MBEDTLS_ECP_C) */
3598 {
3599 return( PSA_ERROR_NOT_SUPPORTED );
3600 }
3601}
3602
Gilles Peskine072ac562018-06-30 00:21:29 +02003603#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3604static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3605 mbedtls_rsa_context *rsa )
3606{
3607 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3608 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3609 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3610 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3611}
3612#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3613
Gilles Peskinec5487a82018-12-03 18:08:14 +01003614psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003615 psa_algorithm_t alg,
3616 const uint8_t *input,
3617 size_t input_length,
3618 const uint8_t *salt,
3619 size_t salt_length,
3620 uint8_t *output,
3621 size_t output_size,
3622 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003624 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003625 psa_status_t status;
3626
Darryl Green5cc689a2018-07-24 15:34:10 +01003627 (void) input;
3628 (void) input_length;
3629 (void) salt;
3630 (void) output;
3631 (void) output_size;
3632
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003633 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003635 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3636 return( PSA_ERROR_INVALID_ARGUMENT );
3637
Gilles Peskine28f8f302019-07-24 13:30:31 +02003638 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003639 if( status != PSA_SUCCESS )
3640 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003641 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3642 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644
3645#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003646 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647 {
3648 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003650 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003651 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003653 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003655 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3656 mbedtls_ctr_drbg_random,
3657 &global_data.ctr_drbg,
3658 MBEDTLS_RSA_PUBLIC,
3659 input_length,
3660 input,
3661 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662 }
3663 else
3664#endif /* MBEDTLS_PKCS1_V15 */
3665#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003666 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003668 psa_rsa_oaep_set_padding_mode( alg, rsa );
3669 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3670 mbedtls_ctr_drbg_random,
3671 &global_data.ctr_drbg,
3672 MBEDTLS_RSA_PUBLIC,
3673 salt, salt_length,
3674 input_length,
3675 input,
3676 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677 }
3678 else
3679#endif /* MBEDTLS_PKCS1_V21 */
3680 {
3681 return( PSA_ERROR_INVALID_ARGUMENT );
3682 }
3683 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003684 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685 return( mbedtls_to_psa_error( ret ) );
3686 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003688#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689 {
3690 return( PSA_ERROR_NOT_SUPPORTED );
3691 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003692}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003693
Gilles Peskinec5487a82018-12-03 18:08:14 +01003694psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003695 psa_algorithm_t alg,
3696 const uint8_t *input,
3697 size_t input_length,
3698 const uint8_t *salt,
3699 size_t salt_length,
3700 uint8_t *output,
3701 size_t output_size,
3702 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003704 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003705 psa_status_t status;
3706
Darryl Green5cc689a2018-07-24 15:34:10 +01003707 (void) input;
3708 (void) input_length;
3709 (void) salt;
3710 (void) output;
3711 (void) output_size;
3712
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003713 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003714
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003715 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3716 return( PSA_ERROR_INVALID_ARGUMENT );
3717
Gilles Peskine28f8f302019-07-24 13:30:31 +02003718 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003719 if( status != PSA_SUCCESS )
3720 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003721 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722 return( PSA_ERROR_INVALID_ARGUMENT );
3723
3724#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003725 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726 {
3727 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729
Gilles Peskine630a18a2018-06-29 17:49:35 +02003730 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003731 return( PSA_ERROR_INVALID_ARGUMENT );
3732
3733#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003734 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003735 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003736 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3737 mbedtls_ctr_drbg_random,
3738 &global_data.ctr_drbg,
3739 MBEDTLS_RSA_PRIVATE,
3740 output_length,
3741 input,
3742 output,
3743 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744 }
3745 else
3746#endif /* MBEDTLS_PKCS1_V15 */
3747#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003748 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003749 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003750 psa_rsa_oaep_set_padding_mode( alg, rsa );
3751 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3752 mbedtls_ctr_drbg_random,
3753 &global_data.ctr_drbg,
3754 MBEDTLS_RSA_PRIVATE,
3755 salt, salt_length,
3756 output_length,
3757 input,
3758 output,
3759 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003760 }
3761 else
3762#endif /* MBEDTLS_PKCS1_V21 */
3763 {
3764 return( PSA_ERROR_INVALID_ARGUMENT );
3765 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003766
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003767 return( mbedtls_to_psa_error( ret ) );
3768 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003769 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003770#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003771 {
3772 return( PSA_ERROR_NOT_SUPPORTED );
3773 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003774}
Gilles Peskine20035e32018-02-03 22:44:14 +01003775
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003776
3777
mohammad1603503973b2018-03-12 15:59:30 +02003778/****************************************************************/
3779/* Symmetric cryptography */
3780/****************************************************************/
3781
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003782/* Initialize the cipher operation structure. Once this function has been
3783 * called, psa_cipher_abort can run and will do the right thing. */
3784static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3785 psa_algorithm_t alg )
3786{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003787 if( ! PSA_ALG_IS_CIPHER( alg ) )
3788 {
3789 memset( operation, 0, sizeof( *operation ) );
3790 return( PSA_ERROR_INVALID_ARGUMENT );
3791 }
3792
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003793 operation->alg = alg;
3794 operation->key_set = 0;
3795 operation->iv_set = 0;
3796 operation->iv_required = 1;
3797 operation->iv_size = 0;
3798 operation->block_size = 0;
3799 mbedtls_cipher_init( &operation->ctx.cipher );
3800 return( PSA_SUCCESS );
3801}
3802
Gilles Peskinee553c652018-06-04 16:22:46 +02003803static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003804 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003805 psa_algorithm_t alg,
3806 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003807{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003808 int ret = 0;
3809 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003810 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003811 size_t key_bits;
3812 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003813 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3814 PSA_KEY_USAGE_ENCRYPT :
3815 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003816
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003817 /* A context must be freshly initialized before it can be set up. */
3818 if( operation->alg != 0 )
3819 {
3820 return( PSA_ERROR_BAD_STATE );
3821 }
3822
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003823 status = psa_cipher_init( operation, alg );
3824 if( status != PSA_SUCCESS )
3825 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003826
Gilles Peskine28f8f302019-07-24 13:30:31 +02003827 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003828 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003829 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003830 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003831
Gilles Peskine8e338702019-07-30 20:06:31 +02003832 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003833 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003834 {
3835 status = PSA_ERROR_NOT_SUPPORTED;
3836 goto exit;
3837 }
mohammad1603503973b2018-03-12 15:59:30 +02003838
mohammad1603503973b2018-03-12 15:59:30 +02003839 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003840 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003841 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003842
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003843#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003844 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003845 {
3846 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003847 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003848 memcpy( keys, slot->data.raw.data, 16 );
3849 memcpy( keys + 16, slot->data.raw.data, 8 );
3850 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3851 keys,
3852 192, cipher_operation );
3853 }
3854 else
3855#endif
3856 {
3857 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3858 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003859 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003860 }
Moran Peker41deec42018-04-04 15:43:05 +03003861 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003862 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003863
mohammad16038481e742018-03-18 13:57:31 +02003864#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003865 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003866 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003867 case PSA_ALG_CBC_NO_PADDING:
3868 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3869 MBEDTLS_PADDING_NONE );
3870 break;
3871 case PSA_ALG_CBC_PKCS7:
3872 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3873 MBEDTLS_PADDING_PKCS7 );
3874 break;
3875 default:
3876 /* The algorithm doesn't involve padding. */
3877 ret = 0;
3878 break;
3879 }
3880 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003881 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003882#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3883
mohammad1603503973b2018-03-12 15:59:30 +02003884 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003885 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003886 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003887 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003888 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003889 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003890 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003891#if defined(MBEDTLS_CHACHA20_C)
3892 else
3893 if( alg == PSA_ALG_CHACHA20 )
3894 operation->iv_size = 12;
3895#endif
mohammad1603503973b2018-03-12 15:59:30 +02003896
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003897exit:
3898 if( status == 0 )
3899 status = mbedtls_to_psa_error( ret );
3900 if( status != 0 )
3901 psa_cipher_abort( operation );
3902 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003903}
3904
Gilles Peskinefe119512018-07-08 21:39:34 +02003905psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003906 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003907 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003908{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003909 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003910}
3911
Gilles Peskinefe119512018-07-08 21:39:34 +02003912psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003913 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003914 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003915{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003916 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003917}
3918
Gilles Peskinefe119512018-07-08 21:39:34 +02003919psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003920 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003921 size_t iv_size,
3922 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003923{
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003926 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003928 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003929 }
Moran Peker41deec42018-04-04 15:43:05 +03003930 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003931 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003932 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003933 goto exit;
3934 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003935 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3936 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003937 if( ret != 0 )
3938 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003939 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003940 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003941 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003942
mohammad16038481e742018-03-18 13:57:31 +02003943 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003944 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003945
Moran Peker395db872018-05-31 14:07:14 +03003946exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003947 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003948 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003949 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003950}
3951
Gilles Peskinefe119512018-07-08 21:39:34 +02003952psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003953 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003954 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003955{
itayzafrir534bd7c2018-08-02 13:56:32 +03003956 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003958 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003959 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003960 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003961 }
Moran Pekera28258c2018-05-29 16:25:04 +03003962 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003963 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003964 status = PSA_ERROR_INVALID_ARGUMENT;
3965 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003966 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003967 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3968 status = mbedtls_to_psa_error( ret );
3969exit:
3970 if( status == PSA_SUCCESS )
3971 operation->iv_set = 1;
3972 else
mohammad1603503973b2018-03-12 15:59:30 +02003973 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003974 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003975}
3976
Gilles Peskinee553c652018-06-04 16:22:46 +02003977psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3978 const uint8_t *input,
3979 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003980 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003981 size_t output_size,
3982 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003983{
itayzafrir534bd7c2018-08-02 13:56:32 +03003984 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003985 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003986 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003987
3988 if( operation->alg == 0 )
3989 {
3990 return( PSA_ERROR_BAD_STATE );
3991 }
3992
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003993 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003994 {
3995 /* Take the unprocessed partial block left over from previous
3996 * update calls, if any, plus the input to this call. Remove
3997 * the last partial block, if any. You get the data that will be
3998 * output in this call. */
3999 expected_output_size =
4000 ( operation->ctx.cipher.unprocessed_len + input_length )
4001 / operation->block_size * operation->block_size;
4002 }
4003 else
4004 {
4005 expected_output_size = input_length;
4006 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004007
Gilles Peskine89d789c2018-06-04 17:17:16 +02004008 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004009 {
4010 status = PSA_ERROR_BUFFER_TOO_SMALL;
4011 goto exit;
4012 }
mohammad160382759612018-03-12 18:16:40 +02004013
mohammad1603503973b2018-03-12 15:59:30 +02004014 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004015 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004016 status = mbedtls_to_psa_error( ret );
4017exit:
4018 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004019 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004020 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004021}
4022
Gilles Peskinee553c652018-06-04 16:22:46 +02004023psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4024 uint8_t *output,
4025 size_t output_size,
4026 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004027{
David Saadab4ecc272019-02-14 13:48:10 +02004028 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004029 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004030 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004031
mohammad1603503973b2018-03-12 15:59:30 +02004032 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004033 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004034 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004035 }
4036 if( operation->iv_required && ! operation->iv_set )
4037 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004038 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004039 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004040
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004041 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004042 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4043 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004044 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004045 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004046 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004047 }
4048
Janos Follath315b51c2018-07-09 16:04:51 +01004049 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4050 temp_output_buffer,
4051 output_length );
4052 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004053 {
Janos Follath315b51c2018-07-09 16:04:51 +01004054 status = mbedtls_to_psa_error( cipher_ret );
4055 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004056 }
Janos Follath315b51c2018-07-09 16:04:51 +01004057
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004058 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004059 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004060 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004061 memcpy( output, temp_output_buffer, *output_length );
4062 else
4063 {
Janos Follath315b51c2018-07-09 16:04:51 +01004064 status = PSA_ERROR_BUFFER_TOO_SMALL;
4065 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004066 }
mohammad1603503973b2018-03-12 15:59:30 +02004067
Gilles Peskine3f108122018-12-07 18:14:53 +01004068 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004069 status = psa_cipher_abort( operation );
4070
4071 return( status );
4072
4073error:
4074
4075 *output_length = 0;
4076
Gilles Peskine3f108122018-12-07 18:14:53 +01004077 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004078 (void) psa_cipher_abort( operation );
4079
4080 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004081}
4082
Gilles Peskinee553c652018-06-04 16:22:46 +02004083psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4084{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004085 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004086 {
4087 /* The object has (apparently) been initialized but it is not
4088 * in use. It's ok to call abort on such an object, and there's
4089 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004090 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004091 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004092
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004093 /* Sanity check (shouldn't happen: operation->alg should
4094 * always have been initialized to a valid value). */
4095 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4096 return( PSA_ERROR_BAD_STATE );
4097
mohammad1603503973b2018-03-12 15:59:30 +02004098 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004099
Moran Peker41deec42018-04-04 15:43:05 +03004100 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004101 operation->key_set = 0;
4102 operation->iv_set = 0;
4103 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004104 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004105 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004106
Moran Peker395db872018-05-31 14:07:14 +03004107 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004108}
4109
Gilles Peskinea0655c32018-04-30 17:06:50 +02004110
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004111
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004112
mohammad16035955c982018-04-26 00:53:03 +03004113/****************************************************************/
4114/* AEAD */
4115/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004116
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117typedef struct
4118{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004119 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004120 const mbedtls_cipher_info_t *cipher_info;
4121 union
4122 {
4123#if defined(MBEDTLS_CCM_C)
4124 mbedtls_ccm_context ccm;
4125#endif /* MBEDTLS_CCM_C */
4126#if defined(MBEDTLS_GCM_C)
4127 mbedtls_gcm_context gcm;
4128#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004129#if defined(MBEDTLS_CHACHAPOLY_C)
4130 mbedtls_chachapoly_context chachapoly;
4131#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004132 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004133 psa_algorithm_t core_alg;
4134 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004135 uint8_t tag_length;
4136} aead_operation_t;
4137
Gilles Peskine30a9e412019-01-14 18:36:12 +01004138static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004139{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004140 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004141 {
4142#if defined(MBEDTLS_CCM_C)
4143 case PSA_ALG_CCM:
4144 mbedtls_ccm_free( &operation->ctx.ccm );
4145 break;
4146#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004147#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004148 case PSA_ALG_GCM:
4149 mbedtls_gcm_free( &operation->ctx.gcm );
4150 break;
4151#endif /* MBEDTLS_GCM_C */
4152 }
4153}
4154
4155static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004156 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004157 psa_key_usage_t usage,
4158 psa_algorithm_t alg )
4159{
4160 psa_status_t status;
4161 size_t key_bits;
4162 mbedtls_cipher_id_t cipher_id;
4163
Gilles Peskine28f8f302019-07-24 13:30:31 +02004164 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004165 if( status != PSA_SUCCESS )
4166 return( status );
4167
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004168 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004169
4170 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004171 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004172 &cipher_id );
4173 if( operation->cipher_info == NULL )
4174 return( PSA_ERROR_NOT_SUPPORTED );
4175
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004176 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004177 {
4178#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004179 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4180 operation->core_alg = PSA_ALG_CCM;
4181 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004182 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4183 * The call to mbedtls_ccm_encrypt_and_tag or
4184 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004185 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004186 return( PSA_ERROR_INVALID_ARGUMENT );
4187 mbedtls_ccm_init( &operation->ctx.ccm );
4188 status = mbedtls_to_psa_error(
4189 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4190 operation->slot->data.raw.data,
4191 (unsigned int) key_bits ) );
4192 if( status != 0 )
4193 goto cleanup;
4194 break;
4195#endif /* MBEDTLS_CCM_C */
4196
4197#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004198 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4199 operation->core_alg = PSA_ALG_GCM;
4200 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004201 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4202 * The call to mbedtls_gcm_crypt_and_tag or
4203 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004204 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004205 return( PSA_ERROR_INVALID_ARGUMENT );
4206 mbedtls_gcm_init( &operation->ctx.gcm );
4207 status = mbedtls_to_psa_error(
4208 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4209 operation->slot->data.raw.data,
4210 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004211 if( status != 0 )
4212 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213 break;
4214#endif /* MBEDTLS_GCM_C */
4215
Gilles Peskine26869f22019-05-06 15:25:00 +02004216#if defined(MBEDTLS_CHACHAPOLY_C)
4217 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4218 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4219 operation->full_tag_length = 16;
4220 /* We only support the default tag length. */
4221 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4222 return( PSA_ERROR_NOT_SUPPORTED );
4223 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4224 status = mbedtls_to_psa_error(
4225 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4226 operation->slot->data.raw.data ) );
4227 if( status != 0 )
4228 goto cleanup;
4229 break;
4230#endif /* MBEDTLS_CHACHAPOLY_C */
4231
Gilles Peskineedf9a652018-08-17 18:11:56 +02004232 default:
4233 return( PSA_ERROR_NOT_SUPPORTED );
4234 }
4235
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004236 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4237 {
4238 status = PSA_ERROR_INVALID_ARGUMENT;
4239 goto cleanup;
4240 }
4241 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004242
Gilles Peskineedf9a652018-08-17 18:11:56 +02004243 return( PSA_SUCCESS );
4244
4245cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004246 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004247 return( status );
4248}
4249
Gilles Peskinec5487a82018-12-03 18:08:14 +01004250psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004251 psa_algorithm_t alg,
4252 const uint8_t *nonce,
4253 size_t nonce_length,
4254 const uint8_t *additional_data,
4255 size_t additional_data_length,
4256 const uint8_t *plaintext,
4257 size_t plaintext_length,
4258 uint8_t *ciphertext,
4259 size_t ciphertext_size,
4260 size_t *ciphertext_length )
4261{
mohammad16035955c982018-04-26 00:53:03 +03004262 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004263 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004264 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004265
mohammad1603f08a5502018-06-03 15:05:47 +03004266 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004267
Gilles Peskinec5487a82018-12-03 18:08:14 +01004268 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004269 if( status != PSA_SUCCESS )
4270 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004271
Gilles Peskineedf9a652018-08-17 18:11:56 +02004272 /* For all currently supported modes, the tag is at the end of the
4273 * ciphertext. */
4274 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4275 {
4276 status = PSA_ERROR_BUFFER_TOO_SMALL;
4277 goto exit;
4278 }
4279 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004280
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004281#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004282 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004283 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004284 status = mbedtls_to_psa_error(
4285 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4286 MBEDTLS_GCM_ENCRYPT,
4287 plaintext_length,
4288 nonce, nonce_length,
4289 additional_data, additional_data_length,
4290 plaintext, ciphertext,
4291 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004292 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004293 else
4294#endif /* MBEDTLS_GCM_C */
4295#if defined(MBEDTLS_CCM_C)
4296 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004297 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004298 status = mbedtls_to_psa_error(
4299 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4300 plaintext_length,
4301 nonce, nonce_length,
4302 additional_data,
4303 additional_data_length,
4304 plaintext, ciphertext,
4305 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004306 }
mohammad16035c8845f2018-05-09 05:40:09 -07004307 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004308#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004309#if defined(MBEDTLS_CHACHAPOLY_C)
4310 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4311 {
4312 if( nonce_length != 12 || operation.tag_length != 16 )
4313 {
4314 status = PSA_ERROR_NOT_SUPPORTED;
4315 goto exit;
4316 }
4317 status = mbedtls_to_psa_error(
4318 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4319 plaintext_length,
4320 nonce,
4321 additional_data,
4322 additional_data_length,
4323 plaintext,
4324 ciphertext,
4325 tag ) );
4326 }
4327 else
4328#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004329 {
mohammad1603554faad2018-06-03 15:07:38 +03004330 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004331 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004332
Gilles Peskineedf9a652018-08-17 18:11:56 +02004333 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4334 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004335
Gilles Peskineedf9a652018-08-17 18:11:56 +02004336exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004337 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004338 if( status == PSA_SUCCESS )
4339 *ciphertext_length = plaintext_length + operation.tag_length;
4340 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004341}
4342
Gilles Peskineee652a32018-06-01 19:23:52 +02004343/* Locate the tag in a ciphertext buffer containing the encrypted data
4344 * followed by the tag. Return the length of the part preceding the tag in
4345 * *plaintext_length. This is the size of the plaintext in modes where
4346 * the encrypted data has the same size as the plaintext, such as
4347 * CCM and GCM. */
4348static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4349 const uint8_t *ciphertext,
4350 size_t ciphertext_length,
4351 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004352 const uint8_t **p_tag )
4353{
4354 size_t payload_length;
4355 if( tag_length > ciphertext_length )
4356 return( PSA_ERROR_INVALID_ARGUMENT );
4357 payload_length = ciphertext_length - tag_length;
4358 if( payload_length > plaintext_size )
4359 return( PSA_ERROR_BUFFER_TOO_SMALL );
4360 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004361 return( PSA_SUCCESS );
4362}
4363
Gilles Peskinec5487a82018-12-03 18:08:14 +01004364psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004365 psa_algorithm_t alg,
4366 const uint8_t *nonce,
4367 size_t nonce_length,
4368 const uint8_t *additional_data,
4369 size_t additional_data_length,
4370 const uint8_t *ciphertext,
4371 size_t ciphertext_length,
4372 uint8_t *plaintext,
4373 size_t plaintext_size,
4374 size_t *plaintext_length )
4375{
mohammad16035955c982018-04-26 00:53:03 +03004376 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004377 aead_operation_t operation;
4378 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004379
Gilles Peskineee652a32018-06-01 19:23:52 +02004380 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004381
Gilles Peskinec5487a82018-12-03 18:08:14 +01004382 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004383 if( status != PSA_SUCCESS )
4384 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004385
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004386 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4387 ciphertext, ciphertext_length,
4388 plaintext_size, &tag );
4389 if( status != PSA_SUCCESS )
4390 goto exit;
4391
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004392#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004393 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004394 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004395 status = mbedtls_to_psa_error(
4396 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4397 ciphertext_length - operation.tag_length,
4398 nonce, nonce_length,
4399 additional_data,
4400 additional_data_length,
4401 tag, operation.tag_length,
4402 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004403 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004404 else
4405#endif /* MBEDTLS_GCM_C */
4406#if defined(MBEDTLS_CCM_C)
4407 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004408 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004409 status = mbedtls_to_psa_error(
4410 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4411 ciphertext_length - operation.tag_length,
4412 nonce, nonce_length,
4413 additional_data,
4414 additional_data_length,
4415 ciphertext, plaintext,
4416 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004417 }
mohammad160339574652018-06-01 04:39:53 -07004418 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004419#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004420#if defined(MBEDTLS_CHACHAPOLY_C)
4421 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4422 {
4423 if( nonce_length != 12 || operation.tag_length != 16 )
4424 {
4425 status = PSA_ERROR_NOT_SUPPORTED;
4426 goto exit;
4427 }
4428 status = mbedtls_to_psa_error(
4429 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4430 ciphertext_length - operation.tag_length,
4431 nonce,
4432 additional_data,
4433 additional_data_length,
4434 tag,
4435 ciphertext,
4436 plaintext ) );
4437 }
4438 else
4439#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004440 {
mohammad1603554faad2018-06-03 15:07:38 +03004441 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004442 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004443
Gilles Peskineedf9a652018-08-17 18:11:56 +02004444 if( status != PSA_SUCCESS && plaintext_size != 0 )
4445 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004446
Gilles Peskineedf9a652018-08-17 18:11:56 +02004447exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004448 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004449 if( status == PSA_SUCCESS )
4450 *plaintext_length = ciphertext_length - operation.tag_length;
4451 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004452}
4453
Gilles Peskinea0655c32018-04-30 17:06:50 +02004454
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004455
Gilles Peskine20035e32018-02-03 22:44:14 +01004456/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004457/* Generators */
4458/****************************************************************/
4459
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004460#define HKDF_STATE_INIT 0 /* no input yet */
4461#define HKDF_STATE_STARTED 1 /* got salt */
4462#define HKDF_STATE_KEYED 2 /* got key */
4463#define HKDF_STATE_OUTPUT 3 /* output started */
4464
Gilles Peskinecbe66502019-05-16 16:59:18 +02004465static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004466 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004467{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004468 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4469 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004470 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004472}
4473
4474
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004476{
4477 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004478 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004479 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004480 {
4481 /* The object has (apparently) been initialized but it is not
4482 * in use. It's ok to call abort on such an object, and there's
4483 * nothing to do. */
4484 }
4485 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004486#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004487 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004488 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 mbedtls_free( operation->ctx.hkdf.info );
4490 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004491 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004492 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004494 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004495 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004496 if( operation->ctx.tls12_prf.seed != NULL )
4497 {
4498 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4499 operation->ctx.tls12_prf.seed_length );
4500 mbedtls_free( operation->ctx.tls12_prf.seed );
4501 }
4502
4503 if( operation->ctx.tls12_prf.label != NULL )
4504 {
4505 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4506 operation->ctx.tls12_prf.label_length );
4507 mbedtls_free( operation->ctx.tls12_prf.label );
4508 }
4509
4510 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4511
4512 /* We leave the fields Ai and output_block to be erased safely by the
4513 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004514 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004515 else
4516#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004517 {
4518 status = PSA_ERROR_BAD_STATE;
4519 }
Janos Follath083036a2019-06-11 10:22:26 +01004520 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004521 return( status );
4522}
4523
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004525 size_t *capacity)
4526{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004528 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004529 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004530 return PSA_ERROR_BAD_STATE;
4531 }
4532
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004534 return( PSA_SUCCESS );
4535}
4536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004538 size_t capacity )
4539{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004541 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004545 return( PSA_SUCCESS );
4546}
4547
Gilles Peskinebef7f142018-07-12 17:22:21 +02004548#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004549/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004550 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004551static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004552 psa_algorithm_t hash_alg,
4553 uint8_t *output,
4554 size_t output_length )
4555{
4556 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4557 psa_status_t status;
4558
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004559 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4560 return( PSA_ERROR_BAD_STATE );
4561 hkdf->state = HKDF_STATE_OUTPUT;
4562
Gilles Peskinebef7f142018-07-12 17:22:21 +02004563 while( output_length != 0 )
4564 {
4565 /* Copy what remains of the current block */
4566 uint8_t n = hash_length - hkdf->offset_in_block;
4567 if( n > output_length )
4568 n = (uint8_t) output_length;
4569 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4570 output += n;
4571 output_length -= n;
4572 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004573 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004574 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004575 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004576 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004578 * object was corrupted or if this function is called directly
4579 * inside the library. */
4580 if( hkdf->block_number == 0xff )
4581 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004582
4583 /* We need a new block */
4584 ++hkdf->block_number;
4585 hkdf->offset_in_block = 0;
4586 status = psa_hmac_setup_internal( &hkdf->hmac,
4587 hkdf->prk, hash_length,
4588 hash_alg );
4589 if( status != PSA_SUCCESS )
4590 return( status );
4591 if( hkdf->block_number != 1 )
4592 {
4593 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4594 hkdf->output_block,
4595 hash_length );
4596 if( status != PSA_SUCCESS )
4597 return( status );
4598 }
4599 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4600 hkdf->info,
4601 hkdf->info_length );
4602 if( status != PSA_SUCCESS )
4603 return( status );
4604 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4605 &hkdf->block_number, 1 );
4606 if( status != PSA_SUCCESS )
4607 return( status );
4608 status = psa_hmac_finish_internal( &hkdf->hmac,
4609 hkdf->output_block,
4610 sizeof( hkdf->output_block ) );
4611 if( status != PSA_SUCCESS )
4612 return( status );
4613 }
4614
4615 return( PSA_SUCCESS );
4616}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004617
Janos Follath7742fee2019-06-17 12:58:10 +01004618static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4619 psa_tls12_prf_key_derivation_t *tls12_prf,
4620 psa_algorithm_t alg )
4621{
4622 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4623 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004624 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4625 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004626
Janos Follath7742fee2019-06-17 12:58:10 +01004627 /* We can't be wanting more output after block 0xff, otherwise
4628 * the capacity check in psa_key_derivation_output_bytes() would have
4629 * prevented this call. It could happen only if the operation
4630 * object was corrupted or if this function is called directly
4631 * inside the library. */
4632 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004633 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004634
4635 /* We need a new block */
4636 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004637 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004638
4639 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4640 *
4641 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4642 *
4643 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4644 * HMAC_hash(secret, A(2) + seed) +
4645 * HMAC_hash(secret, A(3) + seed) + ...
4646 *
4647 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004648 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004649 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004650 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004651 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004652 * is currently extracted as `output_block` and where i is
4653 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004654 */
4655
Janos Follathea29bfb2019-06-19 12:21:20 +01004656 /* Save the hash context before using it, to preserve the hash state with
4657 * only the inner padding in it. We need this, because inner padding depends
4658 * on the key (secret in the RFC's terminology). */
4659 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4660 if( status != PSA_SUCCESS )
4661 goto cleanup;
4662
4663 /* Calculate A(i) where i = tls12_prf->block_number. */
4664 if( tls12_prf->block_number == 1 )
4665 {
4666 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4667 * the variable seed and in this instance means it in the context of the
4668 * P_hash function, where seed = label + seed.) */
4669 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4670 tls12_prf->label, tls12_prf->label_length );
4671 if( status != PSA_SUCCESS )
4672 goto cleanup;
4673 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4674 tls12_prf->seed, tls12_prf->seed_length );
4675 if( status != PSA_SUCCESS )
4676 goto cleanup;
4677 }
4678 else
4679 {
4680 /* A(i) = HMAC_hash(secret, A(i-1)) */
4681 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4682 tls12_prf->Ai, hash_length );
4683 if( status != PSA_SUCCESS )
4684 goto cleanup;
4685 }
4686
4687 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4688 tls12_prf->Ai, hash_length );
4689 if( status != PSA_SUCCESS )
4690 goto cleanup;
4691 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4692 if( status != PSA_SUCCESS )
4693 goto cleanup;
4694
4695 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4696 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4697 tls12_prf->Ai, hash_length );
4698 if( status != PSA_SUCCESS )
4699 goto cleanup;
4700 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4701 tls12_prf->label, tls12_prf->label_length );
4702 if( status != PSA_SUCCESS )
4703 goto cleanup;
4704 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4705 tls12_prf->seed, tls12_prf->seed_length );
4706 if( status != PSA_SUCCESS )
4707 goto cleanup;
4708 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4709 tls12_prf->output_block, hash_length );
4710 if( status != PSA_SUCCESS )
4711 goto cleanup;
4712 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4713 if( status != PSA_SUCCESS )
4714 goto cleanup;
4715
Janos Follath7742fee2019-06-17 12:58:10 +01004716
4717cleanup:
4718
Janos Follathea29bfb2019-06-19 12:21:20 +01004719 cleanup_status = psa_hash_abort( &backup );
4720 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4721 status = cleanup_status;
4722
4723 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004724}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004725
Janos Follath844eb0e2019-06-19 12:10:49 +01004726static psa_status_t psa_key_derivation_tls12_prf_read(
4727 psa_tls12_prf_key_derivation_t *tls12_prf,
4728 psa_algorithm_t alg,
4729 uint8_t *output,
4730 size_t output_length )
4731{
4732 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4733 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4734 psa_status_t status;
4735 uint8_t offset, length;
4736
4737 while( output_length != 0 )
4738 {
4739 /* Check if we have fully processed the current block. */
4740 if( tls12_prf->left_in_block == 0 )
4741 {
4742 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4743 alg );
4744 if( status != PSA_SUCCESS )
4745 return( status );
4746
4747 continue;
4748 }
4749
4750 if( tls12_prf->left_in_block > output_length )
4751 length = (uint8_t) output_length;
4752 else
4753 length = tls12_prf->left_in_block;
4754
4755 offset = hash_length - tls12_prf->left_in_block;
4756 memcpy( output, tls12_prf->output_block + offset, length );
4757 output += length;
4758 output_length -= length;
4759 tls12_prf->left_in_block -= length;
4760 }
4761
4762 return( PSA_SUCCESS );
4763}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764#endif /* MBEDTLS_MD_C */
4765
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004766psa_status_t psa_key_derivation_output_bytes(
4767 psa_key_derivation_operation_t *operation,
4768 uint8_t *output,
4769 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004770{
4771 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004773
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004775 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004777 return PSA_ERROR_BAD_STATE;
4778 }
4779
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004781 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004783 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004784 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004785 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004786 goto exit;
4787 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004789 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004791 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4793 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004795 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004796 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004797 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004799
Gilles Peskinebef7f142018-07-12 17:22:21 +02004800#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004802 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004803 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004805 output, output_length );
4806 }
Janos Follathadbec812019-06-14 11:05:39 +01004807 else
Janos Follathadbec812019-06-14 11:05:39 +01004808 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004810 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004812 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004813 output_length );
4814 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004815 else
4816#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004817 {
4818 return( PSA_ERROR_BAD_STATE );
4819 }
4820
4821exit:
4822 if( status != PSA_SUCCESS )
4823 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004824 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 * This allows us to differentiate between exhausted operations and
4826 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4827 * operations. */
4828 psa_algorithm_t alg = operation->alg;
4829 psa_key_derivation_abort( operation );
4830 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004831 memset( output, '!', output_length );
4832 }
4833 return( status );
4834}
4835
Gilles Peskine08542d82018-07-19 17:05:42 +02004836#if defined(MBEDTLS_DES_C)
4837static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4838{
4839 if( data_size >= 8 )
4840 mbedtls_des_key_set_parity( data );
4841 if( data_size >= 16 )
4842 mbedtls_des_key_set_parity( data + 8 );
4843 if( data_size >= 24 )
4844 mbedtls_des_key_set_parity( data + 16 );
4845}
4846#endif /* MBEDTLS_DES_C */
4847
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004848static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004849 psa_key_slot_t *slot,
4850 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004852{
4853 uint8_t *data = NULL;
4854 size_t bytes = PSA_BITS_TO_BYTES( bits );
4855 psa_status_t status;
4856
Gilles Peskine8e338702019-07-30 20:06:31 +02004857 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004858 return( PSA_ERROR_INVALID_ARGUMENT );
4859 if( bits % 8 != 0 )
4860 return( PSA_ERROR_INVALID_ARGUMENT );
4861 data = mbedtls_calloc( 1, bytes );
4862 if( data == NULL )
4863 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4864
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004866 if( status != PSA_SUCCESS )
4867 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004868#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004869 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004870 psa_des_set_key_parity( data, bytes );
4871#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004873
4874exit:
4875 mbedtls_free( data );
4876 return( status );
4877}
4878
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004879psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004881 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004882{
4883 psa_status_t status;
4884 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004885 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004886
4887 /* Reject any attempt to create a zero-length key so that we don't
4888 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4889 if( psa_get_key_bits( attributes ) == 0 )
4890 return( PSA_ERROR_INVALID_ARGUMENT );
4891
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004892 if( ! operation->can_output_key )
4893 return( PSA_ERROR_NOT_PERMITTED );
4894
Gilles Peskinedf179142019-07-15 22:02:14 +02004895 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4896 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004897#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4898 if( driver != NULL )
4899 {
4900 /* Deriving a key in a secure element is not implemented yet. */
4901 status = PSA_ERROR_NOT_SUPPORTED;
4902 }
4903#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004904 if( status == PSA_SUCCESS )
4905 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004906 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004907 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004909 }
4910 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004911 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004912 if( status != PSA_SUCCESS )
4913 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004914 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004915 *handle = 0;
4916 }
4917 return( status );
4918}
4919
Gilles Peskineeab56e42018-07-12 17:12:33 +02004920
4921
4922/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004923/* Key derivation */
4924/****************************************************************/
4925
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004929{
Janos Follath5fe19732019-06-20 15:09:30 +01004930 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4931 * initialisers for this union leave some bytes unspecified.) */
4932 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4933
Gilles Peskine969c5d62019-01-16 15:53:06 +01004934 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004935#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4937 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4938 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004939 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004940 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004941 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4942 if( hash_size == 0 )
4943 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004944 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4945 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004946 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004947 {
4948 return( PSA_ERROR_NOT_SUPPORTED );
4949 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004951 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004952 }
4953#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004956}
4957
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 psa_algorithm_t alg )
4960{
4961 psa_status_t status;
4962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004964 return( PSA_ERROR_BAD_STATE );
4965
Gilles Peskine6843c292019-01-18 16:44:49 +01004966 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4967 return( PSA_ERROR_INVALID_ARGUMENT );
4968 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004969 {
4970 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004972 }
4973 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4974 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004976 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004977 else
4978 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004979
4980 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004982 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004983}
4984
4985#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004986static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004987 psa_algorithm_t hash_alg,
4988 psa_key_derivation_step_t step,
4989 const uint8_t *data,
4990 size_t data_length )
4991{
4992 psa_status_t status;
4993 switch( step )
4994 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004995 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004996 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004997 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004998 status = psa_hmac_setup_internal( &hkdf->hmac,
4999 data, data_length,
5000 hash_alg );
5001 if( status != PSA_SUCCESS )
5002 return( status );
5003 hkdf->state = HKDF_STATE_STARTED;
5004 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005005 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005006 /* If no salt was provided, use an empty salt. */
5007 if( hkdf->state == HKDF_STATE_INIT )
5008 {
5009 status = psa_hmac_setup_internal( &hkdf->hmac,
5010 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005011 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012 if( status != PSA_SUCCESS )
5013 return( status );
5014 hkdf->state = HKDF_STATE_STARTED;
5015 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005016 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005018 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5019 data, data_length );
5020 if( status != PSA_SUCCESS )
5021 return( status );
5022 status = psa_hmac_finish_internal( &hkdf->hmac,
5023 hkdf->prk,
5024 sizeof( hkdf->prk ) );
5025 if( status != PSA_SUCCESS )
5026 return( status );
5027 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5028 hkdf->block_number = 0;
5029 hkdf->state = HKDF_STATE_KEYED;
5030 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005031 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 if( hkdf->state == HKDF_STATE_OUTPUT )
5033 return( PSA_ERROR_BAD_STATE );
5034 if( hkdf->info_set )
5035 return( PSA_ERROR_BAD_STATE );
5036 hkdf->info_length = data_length;
5037 if( data_length != 0 )
5038 {
5039 hkdf->info = mbedtls_calloc( 1, data_length );
5040 if( hkdf->info == NULL )
5041 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5042 memcpy( hkdf->info, data, data_length );
5043 }
5044 hkdf->info_set = 1;
5045 return( PSA_SUCCESS );
5046 default:
5047 return( PSA_ERROR_INVALID_ARGUMENT );
5048 }
5049}
Janos Follathb03233e2019-06-11 15:30:30 +01005050
Janos Follathf08e2652019-06-13 09:05:41 +01005051static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5052 const uint8_t *data,
5053 size_t data_length )
5054{
5055 if( prf->state != TLS12_PRF_STATE_INIT )
5056 return( PSA_ERROR_BAD_STATE );
5057
Janos Follathd6dce9f2019-07-04 09:11:38 +01005058 if( data_length != 0 )
5059 {
5060 prf->seed = mbedtls_calloc( 1, data_length );
5061 if( prf->seed == NULL )
5062 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005063
Janos Follathd6dce9f2019-07-04 09:11:38 +01005064 memcpy( prf->seed, data, data_length );
5065 prf->seed_length = data_length;
5066 }
Janos Follathf08e2652019-06-13 09:05:41 +01005067
5068 prf->state = TLS12_PRF_STATE_SEED_SET;
5069
5070 return( PSA_SUCCESS );
5071}
5072
Janos Follath81550542019-06-13 14:26:34 +01005073static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5074 psa_algorithm_t hash_alg,
5075 const uint8_t *data,
5076 size_t data_length )
5077{
5078 psa_status_t status;
5079 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5080 return( PSA_ERROR_BAD_STATE );
5081
5082 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5083 if( status != PSA_SUCCESS )
5084 return( status );
5085
5086 prf->state = TLS12_PRF_STATE_KEY_SET;
5087
5088 return( PSA_SUCCESS );
5089}
5090
Janos Follath6660f0e2019-06-17 08:44:03 +01005091static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5092 psa_tls12_prf_key_derivation_t *prf,
5093 psa_algorithm_t hash_alg,
5094 const uint8_t *data,
5095 size_t data_length )
5096{
5097 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005098 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5099 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005100
5101 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5102 return( PSA_ERROR_INVALID_ARGUMENT );
5103
5104 /* Quoting RFC 4279, Section 2:
5105 *
5106 * The premaster secret is formed as follows: if the PSK is N octets
5107 * long, concatenate a uint16 with the value N, N zero octets, a second
5108 * uint16 with the value N, and the PSK itself.
5109 */
5110
Janos Follath40e13932019-06-26 13:22:29 +01005111 *cur++ = ( data_length >> 8 ) & 0xff;
5112 *cur++ = ( data_length >> 0 ) & 0xff;
5113 memset( cur, 0, data_length );
5114 cur += data_length;
5115 *cur++ = pms[0];
5116 *cur++ = pms[1];
5117 memcpy( cur, data, data_length );
5118 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005119
Janos Follath40e13932019-06-26 13:22:29 +01005120 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005121
5122 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5123 return( status );
5124}
5125
Janos Follath63028dd2019-06-13 09:15:47 +01005126static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5127 const uint8_t *data,
5128 size_t data_length )
5129{
5130 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5131 return( PSA_ERROR_BAD_STATE );
5132
Janos Follathd6dce9f2019-07-04 09:11:38 +01005133 if( data_length != 0 )
5134 {
5135 prf->label = mbedtls_calloc( 1, data_length );
5136 if( prf->label == NULL )
5137 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005138
Janos Follathd6dce9f2019-07-04 09:11:38 +01005139 memcpy( prf->label, data, data_length );
5140 prf->label_length = data_length;
5141 }
Janos Follath63028dd2019-06-13 09:15:47 +01005142
5143 prf->state = TLS12_PRF_STATE_LABEL_SET;
5144
5145 return( PSA_SUCCESS );
5146}
5147
Janos Follathb03233e2019-06-11 15:30:30 +01005148static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5149 psa_algorithm_t hash_alg,
5150 psa_key_derivation_step_t step,
5151 const uint8_t *data,
5152 size_t data_length )
5153{
Janos Follathb03233e2019-06-11 15:30:30 +01005154 switch( step )
5155 {
Janos Follathf08e2652019-06-13 09:05:41 +01005156 case PSA_KEY_DERIVATION_INPUT_SEED:
5157 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005158 case PSA_KEY_DERIVATION_INPUT_SECRET:
5159 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005160 case PSA_KEY_DERIVATION_INPUT_LABEL:
5161 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005162 default:
5163 return( PSA_ERROR_INVALID_ARGUMENT );
5164 }
5165}
Janos Follath6660f0e2019-06-17 08:44:03 +01005166
5167static psa_status_t psa_tls12_prf_psk_to_ms_input(
5168 psa_tls12_prf_key_derivation_t *prf,
5169 psa_algorithm_t hash_alg,
5170 psa_key_derivation_step_t step,
5171 const uint8_t *data,
5172 size_t data_length )
5173{
5174 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005175 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005176 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5177 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005178 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005179
5180 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5181}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182#endif /* MBEDTLS_MD_C */
5183
Gilles Peskineb8965192019-09-24 16:21:10 +02005184/** Check whether the given key type is acceptable for the given
5185 * input step of a key derivation.
5186 *
5187 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5188 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5189 * Both secret and non-secret inputs can alternatively have the type
5190 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5191 * that the input was passed as a buffer rather than via a key object.
5192 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005193static int psa_key_derivation_check_input_type(
5194 psa_key_derivation_step_t step,
5195 psa_key_type_t key_type )
5196{
5197 switch( step )
5198 {
5199 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005200 if( key_type == PSA_KEY_TYPE_DERIVE )
5201 return( PSA_SUCCESS );
5202 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005203 return( PSA_SUCCESS );
5204 break;
5205 case PSA_KEY_DERIVATION_INPUT_LABEL:
5206 case PSA_KEY_DERIVATION_INPUT_SALT:
5207 case PSA_KEY_DERIVATION_INPUT_INFO:
5208 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005209 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5210 return( PSA_SUCCESS );
5211 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005212 return( PSA_SUCCESS );
5213 break;
5214 }
5215 return( PSA_ERROR_INVALID_ARGUMENT );
5216}
5217
Janos Follathb80a94e2019-06-12 15:54:46 +01005218static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005220 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005221 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005222 const uint8_t *data,
5223 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005224{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005225 psa_status_t status;
5226 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5227
5228 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005229 if( status != PSA_SUCCESS )
5230 goto exit;
5231
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005232#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005233 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005236 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 step, data, data_length );
5238 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005239 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240 {
Janos Follathb03233e2019-06-11 15:30:30 +01005241 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5242 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5243 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005244 }
5245 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5246 {
5247 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5248 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5249 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250 }
5251 else
5252#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005255 return( PSA_ERROR_BAD_STATE );
5256 }
5257
Gilles Peskine224b0d62019-09-23 18:13:17 +02005258exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005259 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261 return( status );
5262}
5263
Janos Follath51f4a0f2019-06-14 11:35:55 +01005264psa_status_t psa_key_derivation_input_bytes(
5265 psa_key_derivation_operation_t *operation,
5266 psa_key_derivation_step_t step,
5267 const uint8_t *data,
5268 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005269{
Gilles Peskineb8965192019-09-24 16:21:10 +02005270 return( psa_key_derivation_input_internal( operation, step,
5271 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005272 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005273}
5274
Janos Follath51f4a0f2019-06-14 11:35:55 +01005275psa_status_t psa_key_derivation_input_key(
5276 psa_key_derivation_operation_t *operation,
5277 psa_key_derivation_step_t step,
5278 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279{
5280 psa_key_slot_t *slot;
5281 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005282
Gilles Peskine28f8f302019-07-24 13:30:31 +02005283 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005284 PSA_KEY_USAGE_DERIVE,
5285 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005287 {
5288 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005289 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005290 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005291
5292 /* Passing a key object as a SECRET input unlocks the permission
5293 * to output to a key object. */
5294 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5295 operation->can_output_key = 1;
5296
Janos Follathb80a94e2019-06-12 15:54:46 +01005297 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005298 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005299 slot->data.raw.data,
5300 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005302
5303
5304
5305/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005306/* Key agreement */
5307/****************************************************************/
5308
Gilles Peskinea05219c2018-11-16 16:02:56 +01005309#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005310static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5311 size_t peer_key_length,
5312 const mbedtls_ecp_keypair *our_key,
5313 uint8_t *shared_secret,
5314 size_t shared_secret_size,
5315 size_t *shared_secret_length )
5316{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005317 mbedtls_ecp_keypair *their_key = NULL;
5318 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005319 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005320 size_t bits = 0;
5321 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005322 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005323
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005324 status = psa_import_ec_public_key( curve,
5325 peer_key, peer_key_length,
5326 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005327 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005328 goto exit;
5329
Jaeden Amero97271b32019-01-10 19:38:51 +00005330 status = mbedtls_to_psa_error(
5331 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5332 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005333 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005334 status = mbedtls_to_psa_error(
5335 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5336 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005337 goto exit;
5338
Jaeden Amero97271b32019-01-10 19:38:51 +00005339 status = mbedtls_to_psa_error(
5340 mbedtls_ecdh_calc_secret( &ecdh,
5341 shared_secret_length,
5342 shared_secret, shared_secret_size,
5343 mbedtls_ctr_drbg_random,
5344 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005345 if( status != PSA_SUCCESS )
5346 goto exit;
5347 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5348 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005349
5350exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005351 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005352 mbedtls_ecp_keypair_free( their_key );
5353 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005354 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005355}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005356#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005357
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5359
Gilles Peskine0216fe12019-04-11 21:23:21 +02005360static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5361 psa_key_slot_t *private_key,
5362 const uint8_t *peer_key,
5363 size_t peer_key_length,
5364 uint8_t *shared_secret,
5365 size_t shared_secret_size,
5366 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005367{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005368 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005369 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005370#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005371 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005372 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005373 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005374 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5375 private_key->data.ecp,
5376 shared_secret, shared_secret_size,
5377 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005378#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005379 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005380 (void) private_key;
5381 (void) peer_key;
5382 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005383 (void) shared_secret;
5384 (void) shared_secret_size;
5385 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005386 return( PSA_ERROR_NOT_SUPPORTED );
5387 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005388}
5389
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005390/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005391 * to potentially free embedded data structures and wipe confidential data.
5392 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005394 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395 psa_key_slot_t *private_key,
5396 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005397 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005398{
5399 psa_status_t status;
5400 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5401 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005402 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005403
5404 /* Step 1: run the secret agreement algorithm to generate the shared
5405 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005406 status = psa_key_agreement_raw_internal( ka_alg,
5407 private_key,
5408 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005409 shared_secret,
5410 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005411 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005412 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005413 goto exit;
5414
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005415 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005416 * the shared secret. A shared secret is permitted wherever a key
5417 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005418 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005419 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005420 shared_secret,
5421 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005422
Gilles Peskine12313cd2018-06-20 00:20:32 +02005423exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005424 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005425 return( status );
5426}
5427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005429 psa_key_derivation_step_t step,
5430 psa_key_handle_t private_key,
5431 const uint8_t *peer_key,
5432 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005433{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005434 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005435 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005437 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005438 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005439 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005440 if( status != PSA_SUCCESS )
5441 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005443 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005445 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005447 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005448}
5449
Gilles Peskinebe697d82019-05-16 18:00:41 +02005450psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5451 psa_key_handle_t private_key,
5452 const uint8_t *peer_key,
5453 size_t peer_key_length,
5454 uint8_t *output,
5455 size_t output_size,
5456 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005457{
5458 psa_key_slot_t *slot;
5459 psa_status_t status;
5460
5461 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5462 {
5463 status = PSA_ERROR_INVALID_ARGUMENT;
5464 goto exit;
5465 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005466 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005467 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005468 if( status != PSA_SUCCESS )
5469 goto exit;
5470
5471 status = psa_key_agreement_raw_internal( alg, slot,
5472 peer_key, peer_key_length,
5473 output, output_size,
5474 output_length );
5475
5476exit:
5477 if( status != PSA_SUCCESS )
5478 {
5479 /* If an error happens and is not handled properly, the output
5480 * may be used as a key to protect sensitive data. Arrange for such
5481 * a key to be random, which is likely to result in decryption or
5482 * verification errors. This is better than filling the buffer with
5483 * some constant data such as zeros, which would result in the data
5484 * being protected with a reproducible, easily knowable key.
5485 */
5486 psa_generate_random( output, output_size );
5487 *output_length = output_size;
5488 }
5489 return( status );
5490}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005491
5492
5493/****************************************************************/
5494/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005495/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005496
Gilles Peskine4c317f42018-07-12 01:24:09 +02005497psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005498 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005499{
Janos Follath24eed8d2019-11-22 13:21:35 +00005500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005501 GUARD_MODULE_INITIALIZED;
5502
Gilles Peskinef181eca2019-08-07 13:49:00 +02005503 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5504 {
5505 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5506 output,
5507 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5508 if( ret != 0 )
5509 return( mbedtls_to_psa_error( ret ) );
5510 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5511 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5512 }
5513
Gilles Peskinee59236f2018-01-27 23:32:46 +01005514 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5515 return( mbedtls_to_psa_error( ret ) );
5516}
5517
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005518#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5519#include "mbedtls/entropy_poll.h"
5520
Gilles Peskine7228da22019-07-15 11:06:15 +02005521psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005522 size_t seed_size )
5523{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005524 if( global_data.initialized )
5525 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005526
5527 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5528 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5529 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5530 return( PSA_ERROR_INVALID_ARGUMENT );
5531
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005532 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005533}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005534#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005535
Gilles Peskinee56e8782019-04-26 17:34:02 +02005536#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5537static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5538 size_t domain_parameters_size,
5539 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005541 size_t i;
5542 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005543
Gilles Peskinee56e8782019-04-26 17:34:02 +02005544 if( domain_parameters_size == 0 )
5545 {
5546 *exponent = 65537;
5547 return( PSA_SUCCESS );
5548 }
5549
5550 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5551 * support values that fit in a 32-bit integer, which is larger than
5552 * int on just about every platform anyway. */
5553 if( domain_parameters_size > sizeof( acc ) )
5554 return( PSA_ERROR_NOT_SUPPORTED );
5555 for( i = 0; i < domain_parameters_size; i++ )
5556 acc = ( acc << 8 ) | domain_parameters[i];
5557 if( acc > INT_MAX )
5558 return( PSA_ERROR_NOT_SUPPORTED );
5559 *exponent = acc;
5560 return( PSA_SUCCESS );
5561}
5562#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5563
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005564static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005565 psa_key_slot_t *slot, size_t bits,
5566 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005567{
Gilles Peskine8e338702019-07-30 20:06:31 +02005568 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005569
Gilles Peskinee56e8782019-04-26 17:34:02 +02005570 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005571 return( PSA_ERROR_INVALID_ARGUMENT );
5572
Gilles Peskinee59236f2018-01-27 23:32:46 +01005573 if( key_type_is_raw_bytes( type ) )
5574 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005575 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005576 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5577 if( status != PSA_SUCCESS )
5578 return( status );
5579 status = psa_generate_random( slot->data.raw.data,
5580 slot->data.raw.bytes );
5581 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005582 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005583#if defined(MBEDTLS_DES_C)
5584 if( type == PSA_KEY_TYPE_DES )
5585 psa_des_set_key_parity( slot->data.raw.data,
5586 slot->data.raw.bytes );
5587#endif /* MBEDTLS_DES_C */
5588 }
5589 else
5590
5591#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005592 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593 {
5594 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005596 int exponent;
5597 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005598 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5599 return( PSA_ERROR_NOT_SUPPORTED );
5600 /* Accept only byte-aligned keys, for the same reasons as
5601 * in psa_import_rsa_key(). */
5602 if( bits % 8 != 0 )
5603 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005604 status = psa_read_rsa_exponent( domain_parameters,
5605 domain_parameters_size,
5606 &exponent );
5607 if( status != PSA_SUCCESS )
5608 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005609 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5610 if( rsa == NULL )
5611 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5612 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5613 ret = mbedtls_rsa_gen_key( rsa,
5614 mbedtls_ctr_drbg_random,
5615 &global_data.ctr_drbg,
5616 (unsigned int) bits,
5617 exponent );
5618 if( ret != 0 )
5619 {
5620 mbedtls_rsa_free( rsa );
5621 mbedtls_free( rsa );
5622 return( mbedtls_to_psa_error( ret ) );
5623 }
5624 slot->data.rsa = rsa;
5625 }
5626 else
5627#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5628
5629#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005630 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005631 {
5632 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005633 mbedtls_ecp_group_id grp_id =
5634 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005635 const mbedtls_ecp_curve_info *curve_info =
5636 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5637 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005639 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005640 return( PSA_ERROR_NOT_SUPPORTED );
5641 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5642 return( PSA_ERROR_NOT_SUPPORTED );
5643 if( curve_info->bit_size != bits )
5644 return( PSA_ERROR_INVALID_ARGUMENT );
5645 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5646 if( ecp == NULL )
5647 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5648 mbedtls_ecp_keypair_init( ecp );
5649 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5650 mbedtls_ctr_drbg_random,
5651 &global_data.ctr_drbg );
5652 if( ret != 0 )
5653 {
5654 mbedtls_ecp_keypair_free( ecp );
5655 mbedtls_free( ecp );
5656 return( mbedtls_to_psa_error( ret ) );
5657 }
5658 slot->data.ecp = ecp;
5659 }
5660 else
5661#endif /* MBEDTLS_ECP_C */
5662
5663 return( PSA_ERROR_NOT_SUPPORTED );
5664
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 return( PSA_SUCCESS );
5666}
Darryl Green0c6575a2018-11-07 16:05:30 +00005667
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005668psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005669 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670{
5671 psa_status_t status;
5672 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005673 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005674
Gilles Peskine0f84d622019-09-12 19:03:13 +02005675 /* Reject any attempt to create a zero-length key so that we don't
5676 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5677 if( psa_get_key_bits( attributes ) == 0 )
5678 return( PSA_ERROR_INVALID_ARGUMENT );
5679
Gilles Peskinedf179142019-07-15 22:02:14 +02005680 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5681 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005682 if( status != PSA_SUCCESS )
5683 goto exit;
5684
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005685#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5686 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005687 {
Gilles Peskine11792082019-08-06 18:36:36 +02005688 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5689 size_t pubkey_length = 0; /* We don't support this feature yet */
5690 if( drv->key_management == NULL ||
5691 drv->key_management->p_generate == NULL )
5692 {
5693 status = PSA_ERROR_NOT_SUPPORTED;
5694 goto exit;
5695 }
5696 status = drv->key_management->p_generate(
5697 psa_get_se_driver_context( driver ),
5698 slot->data.se.slot_number, attributes,
5699 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005700 }
Gilles Peskine11792082019-08-06 18:36:36 +02005701 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005702#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005703 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005704 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005705 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005706 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 }
Gilles Peskine11792082019-08-06 18:36:36 +02005708
5709exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005710 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005711 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005712 if( status != PSA_SUCCESS )
5713 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005714 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005715 *handle = 0;
5716 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005717 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005718}
5719
5720
Gilles Peskinee59236f2018-01-27 23:32:46 +01005721
5722/****************************************************************/
5723/* Module setup */
5724/****************************************************************/
5725
Gilles Peskine5e769522018-11-20 21:59:56 +01005726psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5727 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5728 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5729{
5730 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5731 return( PSA_ERROR_BAD_STATE );
5732 global_data.entropy_init = entropy_init;
5733 global_data.entropy_free = entropy_free;
5734 return( PSA_SUCCESS );
5735}
5736
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737void mbedtls_psa_crypto_free( void )
5738{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005739 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005740 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5741 {
5742 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005743 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005744 }
5745 /* Wipe all remaining data, including configuration.
5746 * In particular, this sets all state indicator to the value
5747 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005748 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005750 /* Unregister all secure element drivers, so that we restart from
5751 * a pristine state. */
5752 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005753#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005754}
5755
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005756#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5757/** Recover a transaction that was interrupted by a power failure.
5758 *
5759 * This function is called during initialization, before psa_crypto_init()
5760 * returns. If this function returns a failure status, the initialization
5761 * fails.
5762 */
5763static psa_status_t psa_crypto_recover_transaction(
5764 const psa_crypto_transaction_t *transaction )
5765{
5766 switch( transaction->unknown.type )
5767 {
5768 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5769 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005770 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005771 * is implemented.
5772 * https://github.com/ARMmbed/mbed-crypto/issues/218
5773 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005774 default:
5775 /* We found an unsupported transaction in the storage.
5776 * We don't know what state the storage is in. Give up. */
5777 return( PSA_ERROR_STORAGE_FAILURE );
5778 }
5779}
5780#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5781
Gilles Peskinee59236f2018-01-27 23:32:46 +01005782psa_status_t psa_crypto_init( void )
5783{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005784 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 const unsigned char drbg_seed[] = "PSA";
5786
Gilles Peskinec6b69072018-11-20 21:42:52 +01005787 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005788 if( global_data.initialized != 0 )
5789 return( PSA_SUCCESS );
5790
Gilles Peskine5e769522018-11-20 21:59:56 +01005791 /* Set default configuration if
5792 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5793 if( global_data.entropy_init == NULL )
5794 global_data.entropy_init = mbedtls_entropy_init;
5795 if( global_data.entropy_free == NULL )
5796 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797
Gilles Peskinec6b69072018-11-20 21:42:52 +01005798 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005799 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005800#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5801 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5802 /* The PSA entropy injection feature depends on using NV seed as an entropy
5803 * source. Add NV seed as an entropy source for PSA entropy injection. */
5804 mbedtls_entropy_add_source( &global_data.entropy,
5805 mbedtls_nv_seed_poll, NULL,
5806 MBEDTLS_ENTROPY_BLOCK_SIZE,
5807 MBEDTLS_ENTROPY_SOURCE_STRONG );
5808#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005809 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005810 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005811 status = mbedtls_to_psa_error(
5812 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5813 mbedtls_entropy_func,
5814 &global_data.entropy,
5815 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5816 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005817 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005818 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005819
Gilles Peskine66fb1262018-12-10 16:29:04 +01005820 status = psa_initialize_key_slots( );
5821 if( status != PSA_SUCCESS )
5822 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005823
Gilles Peskined9348f22019-10-01 15:22:29 +02005824#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5825 status = psa_init_all_se_drivers( );
5826 if( status != PSA_SUCCESS )
5827 goto exit;
5828#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5829
Gilles Peskine4b734222019-07-24 15:56:31 +02005830#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005831 status = psa_crypto_load_transaction( );
5832 if( status == PSA_SUCCESS )
5833 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005834 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5835 if( status != PSA_SUCCESS )
5836 goto exit;
5837 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005838 }
5839 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5840 {
5841 /* There's no transaction to complete. It's all good. */
5842 status = PSA_SUCCESS;
5843 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005844#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005845
Gilles Peskinec6b69072018-11-20 21:42:52 +01005846 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847 global_data.initialized = 1;
5848
5849exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005850 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005851 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005852 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005853}
5854
5855#endif /* MBEDTLS_PSA_CRYPTO_C */