blob: f0972b6c81ff6de2e8e9db7dd8096cd38cfe5909 [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 Peskine34ef7f52018-06-18 20:47:51 +0200385 return( PSA_ECC_CURVE_SECP192R1 );
386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388 return( PSA_ECC_CURVE_SECP224R1 );
389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200391 return( PSA_ECC_CURVE_SECP256R1 );
392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200394 return( PSA_ECC_CURVE_SECP384R1 );
395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200397 return( PSA_ECC_CURVE_SECP521R1 );
398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200400 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200403 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200406 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200412 return( PSA_ECC_CURVE_SECP192K1 );
413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200415 return( PSA_ECC_CURVE_SECP224K1 );
416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200418 return( PSA_ECC_CURVE_SECP256K1 );
419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200421 return( PSA_ECC_CURVE_CURVE448 );
422 default:
423 return( 0 );
424 }
425}
426
Gilles 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 Peskine12313cd2018-06-20 00:20:32 +0200454 case PSA_ECC_CURVE_SECP192R1:
455 return( MBEDTLS_ECP_DP_SECP192R1 );
456 case PSA_ECC_CURVE_SECP224R1:
457 return( MBEDTLS_ECP_DP_SECP224R1 );
458 case PSA_ECC_CURVE_SECP256R1:
459 return( MBEDTLS_ECP_DP_SECP256R1 );
460 case PSA_ECC_CURVE_SECP384R1:
461 return( MBEDTLS_ECP_DP_SECP384R1 );
462 case PSA_ECC_CURVE_SECP521R1:
463 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100464
465 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
466 switch( byte_length )
467 {
468 case PSA_BITS_TO_BYTES( 256 ):
469 return( MBEDTLS_ECP_DP_BP256R1 );
470 case PSA_BITS_TO_BYTES( 384 ):
471 return( MBEDTLS_ECP_DP_BP384R1 );
472 case PSA_BITS_TO_BYTES( 512 ):
473 return( MBEDTLS_ECP_DP_BP512R1 );
474 default:
475 return( MBEDTLS_ECP_DP_NONE );
476 }
477 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
479 return( MBEDTLS_ECP_DP_BP256R1 );
480 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
481 return( MBEDTLS_ECP_DP_BP384R1 );
482 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
483 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100484
485 case PSA_ECC_CURVE_MONTGOMERY:
486 switch( byte_length )
487 {
488 case PSA_BITS_TO_BYTES( 255 ):
489 return( MBEDTLS_ECP_DP_CURVE25519 );
490 case PSA_BITS_TO_BYTES( 448 ):
491 return( MBEDTLS_ECP_DP_CURVE448 );
492 default:
493 return( MBEDTLS_ECP_DP_NONE );
494 }
495 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200496 case PSA_ECC_CURVE_CURVE25519:
497 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100498 case PSA_ECC_CURVE_CURVE448:
499 return( MBEDTLS_ECP_DP_CURVE448 );
500
501 case PSA_ECC_CURVE_SECP_K1:
502 switch( byte_length )
503 {
504 case PSA_BITS_TO_BYTES( 192 ):
505 return( MBEDTLS_ECP_DP_SECP192K1 );
506 case PSA_BITS_TO_BYTES( 224 ):
507 return( MBEDTLS_ECP_DP_SECP224K1 );
508 case PSA_BITS_TO_BYTES( 256 ):
509 return( MBEDTLS_ECP_DP_SECP256K1 );
510 default:
511 return( MBEDTLS_ECP_DP_NONE );
512 }
513 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200514 case PSA_ECC_CURVE_SECP192K1:
515 return( MBEDTLS_ECP_DP_SECP192K1 );
516 case PSA_ECC_CURVE_SECP224K1:
517 return( MBEDTLS_ECP_DP_SECP224K1 );
518 case PSA_ECC_CURVE_SECP256K1:
519 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100520
Gilles Peskine12313cd2018-06-20 00:20:32 +0200521 default:
522 return( MBEDTLS_ECP_DP_NONE );
523 }
524}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100525#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200526
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
528 size_t bits,
529 struct raw_data *raw )
530{
531 /* Check that the bit size is acceptable for the key type */
532 switch( type )
533 {
534 case PSA_KEY_TYPE_RAW_DATA:
535#if defined(MBEDTLS_MD_C)
536 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200537#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200538 case PSA_KEY_TYPE_DERIVE:
539 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540#if defined(MBEDTLS_AES_C)
541 case PSA_KEY_TYPE_AES:
542 if( bits != 128 && bits != 192 && bits != 256 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
544 break;
545#endif
546#if defined(MBEDTLS_CAMELLIA_C)
547 case PSA_KEY_TYPE_CAMELLIA:
548 if( bits != 128 && bits != 192 && bits != 256 )
549 return( PSA_ERROR_INVALID_ARGUMENT );
550 break;
551#endif
552#if defined(MBEDTLS_DES_C)
553 case PSA_KEY_TYPE_DES:
554 if( bits != 64 && bits != 128 && bits != 192 )
555 return( PSA_ERROR_INVALID_ARGUMENT );
556 break;
557#endif
558#if defined(MBEDTLS_ARC4_C)
559 case PSA_KEY_TYPE_ARC4:
560 if( bits < 8 || bits > 2048 )
561 return( PSA_ERROR_INVALID_ARGUMENT );
562 break;
563#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200564#if defined(MBEDTLS_CHACHA20_C)
565 case PSA_KEY_TYPE_CHACHA20:
566 if( bits != 256 )
567 return( PSA_ERROR_INVALID_ARGUMENT );
568 break;
569#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200570 default:
571 return( PSA_ERROR_NOT_SUPPORTED );
572 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200573 if( bits % 8 != 0 )
574 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200575
576 /* Allocate memory for the key */
577 raw->bytes = PSA_BITS_TO_BYTES( bits );
578 raw->data = mbedtls_calloc( 1, raw->bytes );
579 if( raw->data == NULL )
580 {
581 raw->bytes = 0;
582 return( PSA_ERROR_INSUFFICIENT_MEMORY );
583 }
584 return( PSA_SUCCESS );
585}
586
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100588/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
589 * that are not a multiple of 8) well. For example, there is only
590 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
591 * way to return the exact bit size of a key.
592 * To keep things simple, reject non-byte-aligned key sizes. */
593static psa_status_t psa_check_rsa_key_byte_aligned(
594 const mbedtls_rsa_context *rsa )
595{
596 mbedtls_mpi n;
597 psa_status_t status;
598 mbedtls_mpi_init( &n );
599 status = mbedtls_to_psa_error(
600 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
601 if( status == PSA_SUCCESS )
602 {
603 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
604 status = PSA_ERROR_NOT_SUPPORTED;
605 }
606 mbedtls_mpi_free( &n );
607 return( status );
608}
609
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000610static psa_status_t psa_import_rsa_key( psa_key_type_t type,
611 const uint8_t *data,
612 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200613 mbedtls_rsa_context **p_rsa )
614{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000615 psa_status_t status;
616 mbedtls_pk_context pk;
617 mbedtls_rsa_context *rsa;
618 size_t bits;
619
620 mbedtls_pk_init( &pk );
621
622 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200623 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000624 status = mbedtls_to_psa_error(
625 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000627 status = mbedtls_to_psa_error(
628 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
629 if( status != PSA_SUCCESS )
630 goto exit;
631
632 /* We have something that the pkparse module recognizes. If it is a
633 * valid RSA key, store it. */
634 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000636 status = PSA_ERROR_INVALID_ARGUMENT;
637 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000639
640 rsa = mbedtls_pk_rsa( pk );
641 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
642 * supports non-byte-aligned key sizes, but not well. For example,
643 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
644 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
645 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
646 {
647 status = PSA_ERROR_NOT_SUPPORTED;
648 goto exit;
649 }
650 status = psa_check_rsa_key_byte_aligned( rsa );
651
652exit:
653 /* Free the content of the pk object only on error. */
654 if( status != PSA_SUCCESS )
655 {
656 mbedtls_pk_free( &pk );
657 return( status );
658 }
659
660 /* On success, store the content of the object in the RSA context. */
661 *p_rsa = rsa;
662
663 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200664}
665#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
666
Jaeden Ameroccdce902019-01-10 11:42:27 +0000667#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100668static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100669 size_t data_length,
670 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100671 mbedtls_ecp_keypair **p_ecp )
672{
673 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
674 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
675 if( *p_ecp == NULL )
676 return( PSA_ERROR_INSUFFICIENT_MEMORY );
677 mbedtls_ecp_keypair_init( *p_ecp );
678
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100679 if( is_public )
680 {
681 /* A public key is represented as:
682 * - The byte 0x04;
683 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
684 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
685 * So its data length is 2m+1 where n is the key size in bits.
686 */
687 if( ( data_length & 1 ) == 0 )
688 return( PSA_ERROR_INVALID_ARGUMENT );
689 data_length = data_length / 2;
690 }
691
Gilles Peskine4cd32772019-12-02 20:49:42 +0100692 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100693 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
694 if( grp_id == MBEDTLS_ECP_DP_NONE )
695 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100696 return( mbedtls_to_psa_error(
697 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
698}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000699
700/* Import a public key given as the uncompressed representation defined by SEC1
701 * 2.3.3 as the content of an ECPoint. */
702static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
703 const uint8_t *data,
704 size_t data_length,
705 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200706{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000708 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000709
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100710 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000711 if( status != PSA_SUCCESS )
712 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714 /* Load the public value. */
715 status = mbedtls_to_psa_error(
716 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
717 data, data_length ) );
718 if( status != PSA_SUCCESS )
719 goto exit;
720
721 /* Check that the point is on the curve. */
722 status = mbedtls_to_psa_error(
723 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
724 if( status != PSA_SUCCESS )
725 goto exit;
726
727 *p_ecp = ecp;
728 return( PSA_SUCCESS );
729
730exit:
731 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200732 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000733 mbedtls_ecp_keypair_free( ecp );
734 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200735 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000736 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200737}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200738
Gilles Peskinef76aa772018-10-29 19:24:33 +0100739/* Import a private key given as a byte string which is the private value
740 * in big-endian order. */
741static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
742 const uint8_t *data,
743 size_t data_length,
744 mbedtls_ecp_keypair **p_ecp )
745{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100747 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100748
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100749 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100750 if( status != PSA_SUCCESS )
751 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100752
Gilles Peskinef76aa772018-10-29 19:24:33 +0100753 /* Load the secret value. */
754 status = mbedtls_to_psa_error(
755 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
756 if( status != PSA_SUCCESS )
757 goto exit;
758 /* Validate the private key. */
759 status = mbedtls_to_psa_error(
760 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
761 if( status != PSA_SUCCESS )
762 goto exit;
763 /* Calculate the public key from the private key. */
764 status = mbedtls_to_psa_error(
765 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
766 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
767 if( status != PSA_SUCCESS )
768 goto exit;
769
770 *p_ecp = ecp;
771 return( PSA_SUCCESS );
772
773exit:
774 if( ecp != NULL )
775 {
776 mbedtls_ecp_keypair_free( ecp );
777 mbedtls_free( ecp );
778 }
779 return( status );
780}
781#endif /* defined(MBEDTLS_ECP_C) */
782
Gilles Peskineb46bef22019-07-30 21:32:04 +0200783
784/** Return the size of the key in the given slot, in bits.
785 *
786 * \param[in] slot A key slot.
787 *
788 * \return The key size in bits, read from the metadata in the slot.
789 */
790static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
791{
792 return( slot->attr.bits );
793}
794
795/** Calculate the size of the key in the given slot, in bits.
796 *
797 * \param[in] slot A key slot containing a transparent key.
798 *
799 * \return The key size in bits, calculated from the key data.
800 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200801static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200802{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200803 size_t bits = 0; /* return 0 on an empty slot */
804
Gilles Peskineb46bef22019-07-30 21:32:04 +0200805 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200806 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200807#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200808 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
809 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200810#endif /* defined(MBEDTLS_RSA_C) */
811#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200812 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
813 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200814#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200815
816 /* We know that the size fits in psa_key_bits_t thanks to checks
817 * when the key was created. */
818 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200819}
820
Gilles Peskine8e338702019-07-30 20:06:31 +0200821/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100822 * previously. This function assumes that the slot does not contain
823 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100824psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
825 const uint8_t *data,
826 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200828 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Gilles Peskine8e338702019-07-30 20:06:31 +0200830 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100831 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200832 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200833 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100834 if( data_length > SIZE_MAX / 8 )
835 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200836 /* Enforce a size limit, and in particular ensure that the bit
837 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200838 if( bit_size > PSA_MAX_KEY_BITS )
839 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200840 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200841 &slot->data.raw );
842 if( status != PSA_SUCCESS )
843 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200844 if( data_length != 0 )
845 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846 }
847 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100848#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200849 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100850 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200851 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100852 data, data_length,
853 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100854 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200855 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000857 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200858 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000859 data, data_length,
860 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861 }
862 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100863#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000864#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200865 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100866 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200867 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000868 data, data_length,
869 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870 }
871 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000872#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 {
874 return( PSA_ERROR_NOT_SUPPORTED );
875 }
Darryl Green940d72c2018-07-13 13:18:51 +0100876
Gilles Peskineb46bef22019-07-30 21:32:04 +0200877 if( status == PSA_SUCCESS )
878 {
879 /* Write the actual key size to the slot.
880 * psa_start_key_creation() wrote the size declared by the
881 * caller, which may be 0 (meaning unspecified) or wrong. */
882 slot->attr.bits = psa_calculate_key_bits( slot );
883 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100884 return( status );
885}
886
Gilles Peskinef603c712019-01-19 13:40:11 +0100887/** Calculate the intersection of two algorithm usage policies.
888 *
889 * Return 0 (which allows no operation) on incompatibility.
890 */
891static psa_algorithm_t psa_key_policy_algorithm_intersection(
892 psa_algorithm_t alg1,
893 psa_algorithm_t alg2 )
894{
Gilles Peskine549ea862019-05-22 11:45:59 +0200895 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100896 if( alg1 == alg2 )
897 return( alg1 );
898 /* If the policies are from the same hash-and-sign family, check
899 * if one is a wildcard. If so the other has the specific algorithm. */
900 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
901 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
902 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
903 {
904 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
905 return( alg2 );
906 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
907 return( alg1 );
908 }
909 /* If the policies are incompatible, allow nothing. */
910 return( 0 );
911}
912
Gilles Peskined6f371b2019-05-10 19:33:38 +0200913static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
914 psa_algorithm_t requested_alg )
915{
Gilles Peskine549ea862019-05-22 11:45:59 +0200916 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200917 if( requested_alg == policy_alg )
918 return( 1 );
919 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200920 * and requested_alg is the same hash-and-sign family with any hash,
921 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200922 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
923 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
924 {
925 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
926 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
927 }
928 /* If it isn't permitted, it's forbidden. */
929 return( 0 );
930}
931
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100932/** Test whether a policy permits an algorithm.
933 *
934 * The caller must test usage flags separately.
935 */
936static int psa_key_policy_permits( const psa_key_policy_t *policy,
937 psa_algorithm_t alg )
938{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200939 return( psa_key_algorithm_permits( policy->alg, alg ) ||
940 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100941}
942
Gilles Peskinef603c712019-01-19 13:40:11 +0100943/** Restrict a key policy based on a constraint.
944 *
945 * \param[in,out] policy The policy to restrict.
946 * \param[in] constraint The policy constraint to apply.
947 *
948 * \retval #PSA_SUCCESS
949 * \c *policy contains the intersection of the original value of
950 * \c *policy and \c *constraint.
951 * \retval #PSA_ERROR_INVALID_ARGUMENT
952 * \c *policy and \c *constraint are incompatible.
953 * \c *policy is unchanged.
954 */
955static psa_status_t psa_restrict_key_policy(
956 psa_key_policy_t *policy,
957 const psa_key_policy_t *constraint )
958{
959 psa_algorithm_t intersection_alg =
960 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200961 psa_algorithm_t intersection_alg2 =
962 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100963 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
964 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200965 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
966 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100967 policy->usage &= constraint->usage;
968 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200969 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100970 return( PSA_SUCCESS );
971}
972
Darryl Green06fd18d2018-07-16 11:21:11 +0100973/** Retrieve a slot which must contain a key. The key must have allow all the
974 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
975 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100976static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100977 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100978 psa_key_usage_t usage,
979 psa_algorithm_t alg )
980{
981 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100982 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100983
984 *p_slot = NULL;
985
Gilles Peskinec5487a82018-12-03 18:08:14 +0100986 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100987 if( status != PSA_SUCCESS )
988 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100989
990 /* Enforce that usage policy for the key slot contains all the flags
991 * required by the usage parameter. There is one exception: public
992 * keys can always be exported, so we treat public key objects as
993 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200994 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100995 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200996 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100997 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100998
999 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001000 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001001 return( PSA_ERROR_NOT_PERMITTED );
1002
1003 *p_slot = slot;
1004 return( PSA_SUCCESS );
1005}
Darryl Green940d72c2018-07-13 13:18:51 +01001006
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007/** Retrieve a slot which must contain a transparent key.
1008 *
1009 * A transparent key is a key for which the key material is directly
1010 * available, as opposed to a key in a secure element.
1011 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001012 * This is a temporary function to use instead of psa_get_key_from_slot()
1013 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 */
1015#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1016static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1017 psa_key_slot_t **p_slot,
1018 psa_key_usage_t usage,
1019 psa_algorithm_t alg )
1020{
1021 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1022 if( status != PSA_SUCCESS )
1023 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001024 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025 {
1026 *p_slot = NULL;
1027 return( PSA_ERROR_NOT_SUPPORTED );
1028 }
1029 return( PSA_SUCCESS );
1030}
1031#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1032/* With no secure element support, all keys are transparent. */
1033#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1034 psa_get_key_from_slot( handle, p_slot, usage, alg )
1035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1036
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001037/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001038static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001039{
Gilles Peskine73167e12019-07-12 23:44:37 +02001040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1041 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001042 {
1043 /* No key material to clean. */
1044 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001045 else
1046#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001047 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001048 {
1049 /* No key material to clean. */
1050 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001051 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001052 {
1053 mbedtls_free( slot->data.raw.data );
1054 }
1055 else
1056#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001057 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001058 {
1059 mbedtls_rsa_free( slot->data.rsa );
1060 mbedtls_free( slot->data.rsa );
1061 }
1062 else
1063#endif /* defined(MBEDTLS_RSA_C) */
1064#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001065 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001066 {
1067 mbedtls_ecp_keypair_free( slot->data.ecp );
1068 mbedtls_free( slot->data.ecp );
1069 }
1070 else
1071#endif /* defined(MBEDTLS_ECP_C) */
1072 {
1073 /* Shouldn't happen: the key type is not any type that we
1074 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001075 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001076 }
1077
1078 return( PSA_SUCCESS );
1079}
1080
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001081/** Completely wipe a slot in memory, including its policy.
1082 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001083psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001084{
1085 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001086 /* Multipart operations may still be using the key. This is safe
1087 * because all multipart operation objects are independent from
1088 * the key slot: if they need to access the key after the setup
1089 * phase, they have a copy of the key. Note that this means that
1090 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001091 /* At this point, key material and other type-specific content has
1092 * been wiped. Clear remaining metadata. We can call memset and not
1093 * zeroize because the metadata is not particularly sensitive. */
1094 memset( slot, 0, sizeof( *slot ) );
1095 return( status );
1096}
1097
Gilles Peskinec5487a82018-12-03 18:08:14 +01001098psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001100 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001101 psa_status_t status; /* status of the last operation */
1102 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1104 psa_se_drv_table_entry_t *driver;
1105#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001106
Gilles Peskine1841cf42019-10-08 15:48:25 +02001107 if( handle == 0 )
1108 return( PSA_SUCCESS );
1109
Gilles Peskinec5487a82018-12-03 18:08:14 +01001110 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111 if( status != PSA_SUCCESS )
1112 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001113
1114#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001115 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001116 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001117 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001118 /* For a key in a secure element, we need to do three things:
1119 * remove the key file in internal storage, destroy the
1120 * key inside the secure element, and update the driver's
1121 * persistent data. Start a transaction that will encompass these
1122 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001123 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001124 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001125 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001126 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001127 status = psa_crypto_save_transaction( );
1128 if( status != PSA_SUCCESS )
1129 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001130 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001131 /* We should still try to destroy the key in the secure
1132 * element and the key metadata in storage. This is especially
1133 * important if the error is that the storage is full.
1134 * But how to do it exactly without risking an inconsistent
1135 * state after a reset?
1136 * https://github.com/ARMmbed/mbed-crypto/issues/215
1137 */
1138 overall_status = status;
1139 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 }
1141
Gilles Peskine354f7672019-07-12 23:46:38 +02001142 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001143 if( overall_status == PSA_SUCCESS )
1144 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001145 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1147
Darryl Greend49a4992018-06-18 17:27:26 +01001148#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001149 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001150 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001151 status = psa_destroy_persistent_key( slot->attr.id );
1152 if( overall_status == PSA_SUCCESS )
1153 overall_status = status;
1154
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001155 /* TODO: other slots may have a copy of the same key. We should
1156 * invalidate them.
1157 * https://github.com/ARMmbed/mbed-crypto/issues/214
1158 */
Darryl Greend49a4992018-06-18 17:27:26 +01001159 }
1160#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001161
Gilles Peskinefc762652019-07-22 19:30:34 +02001162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1163 if( driver != NULL )
1164 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001165 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001166 if( overall_status == PSA_SUCCESS )
1167 overall_status = status;
1168 status = psa_crypto_stop_transaction( );
1169 if( overall_status == PSA_SUCCESS )
1170 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001171 }
1172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1173
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1175exit:
1176#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001177 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001178 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1179 if( overall_status == PSA_SUCCESS )
1180 overall_status = status;
1181 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001182}
1183
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001185{
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001188}
1189
Gilles Peskineb699f072019-04-26 16:06:02 +02001190psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1191 psa_key_type_t type,
1192 const uint8_t *data,
1193 size_t data_length )
1194{
1195 uint8_t *copy = NULL;
1196
1197 if( data_length != 0 )
1198 {
1199 copy = mbedtls_calloc( 1, data_length );
1200 if( copy == NULL )
1201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1202 memcpy( copy, data, data_length );
1203 }
1204 /* After this point, this function is guaranteed to succeed, so it
1205 * can start modifying `*attributes`. */
1206
1207 if( attributes->domain_parameters != NULL )
1208 {
1209 mbedtls_free( attributes->domain_parameters );
1210 attributes->domain_parameters = NULL;
1211 attributes->domain_parameters_size = 0;
1212 }
1213
1214 attributes->domain_parameters = copy;
1215 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001216 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 return( PSA_SUCCESS );
1218}
1219
1220psa_status_t psa_get_key_domain_parameters(
1221 const psa_key_attributes_t *attributes,
1222 uint8_t *data, size_t data_size, size_t *data_length )
1223{
1224 if( attributes->domain_parameters_size > data_size )
1225 return( PSA_ERROR_BUFFER_TOO_SMALL );
1226 *data_length = attributes->domain_parameters_size;
1227 if( attributes->domain_parameters_size != 0 )
1228 memcpy( data, attributes->domain_parameters,
1229 attributes->domain_parameters_size );
1230 return( PSA_SUCCESS );
1231}
1232
1233#if defined(MBEDTLS_RSA_C)
1234static psa_status_t psa_get_rsa_public_exponent(
1235 const mbedtls_rsa_context *rsa,
1236 psa_key_attributes_t *attributes )
1237{
1238 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001240 uint8_t *buffer = NULL;
1241 size_t buflen;
1242 mbedtls_mpi_init( &mpi );
1243
1244 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1245 if( ret != 0 )
1246 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001247 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1248 {
1249 /* It's the default value, which is reported as an empty string,
1250 * so there's nothing to do. */
1251 goto exit;
1252 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001253
1254 buflen = mbedtls_mpi_size( &mpi );
1255 buffer = mbedtls_calloc( 1, buflen );
1256 if( buffer == NULL )
1257 {
1258 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1259 goto exit;
1260 }
1261 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1262 if( ret != 0 )
1263 goto exit;
1264 attributes->domain_parameters = buffer;
1265 attributes->domain_parameters_size = buflen;
1266
1267exit:
1268 mbedtls_mpi_free( &mpi );
1269 if( ret != 0 )
1270 mbedtls_free( buffer );
1271 return( mbedtls_to_psa_error( ret ) );
1272}
1273#endif /* MBEDTLS_RSA_C */
1274
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001275/** Retrieve all the publicly-accessible attributes of a key.
1276 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001277psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1278 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001280 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001281 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001283 psa_reset_key_attributes( attributes );
1284
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001285 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001286 if( status != PSA_SUCCESS )
1287 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001288
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001289 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001290 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1291 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1292
1293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294 if( psa_key_slot_is_external( slot ) )
1295 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001297
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001299 {
1300#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001301 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001302 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001304 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001305 * is not yet implemented.
1306 * https://github.com/ARMmbed/mbed-crypto/issues/216
1307 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001308 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001309 break;
1310#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001311 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1312 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001313#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001314 default:
1315 /* Nothing else to do. */
1316 break;
1317 }
1318
1319 if( status != PSA_SUCCESS )
1320 psa_reset_key_attributes( attributes );
1321 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001322}
1323
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1325psa_status_t psa_get_key_slot_number(
1326 const psa_key_attributes_t *attributes,
1327 psa_key_slot_number_t *slot_number )
1328{
1329 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1330 {
1331 *slot_number = attributes->slot_number;
1332 return( PSA_SUCCESS );
1333 }
1334 else
1335 return( PSA_ERROR_INVALID_ARGUMENT );
1336}
1337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1338
Jaeden Ameroccdce902019-01-10 11:42:27 +00001339#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001340static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1341 unsigned char *buf, size_t size )
1342{
Janos Follath24eed8d2019-11-22 13:21:35 +00001343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001344 unsigned char *c;
1345 size_t len = 0;
1346
1347 c = buf + size;
1348
1349 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1350
1351 return( (int) len );
1352}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001353#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001354
Gilles Peskinef603c712019-01-19 13:40:11 +01001355static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1356 uint8_t *data,
1357 size_t data_size,
1358 size_t *data_length,
1359 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360{
Gilles Peskine5d309672019-07-12 23:47:28 +02001361#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1362 const psa_drv_se_t *drv;
1363 psa_drv_se_context_t *drv_context;
1364#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1365
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001366 *data_length = 0;
1367
Gilles Peskine8e338702019-07-30 20:06:31 +02001368 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001369 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001370
Gilles Peskinef9168942019-09-12 19:20:29 +02001371 /* Reject a zero-length output buffer now, since this can never be a
1372 * valid key representation. This way we know that data must be a valid
1373 * pointer and we can do things like memset(data, ..., data_size). */
1374 if( data_size == 0 )
1375 return( PSA_ERROR_BUFFER_TOO_SMALL );
1376
Gilles Peskine5d309672019-07-12 23:47:28 +02001377#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001378 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001379 {
1380 psa_drv_se_export_key_t method;
1381 if( drv->key_management == NULL )
1382 return( PSA_ERROR_NOT_SUPPORTED );
1383 method = ( export_public_key ?
1384 drv->key_management->p_export_public :
1385 drv->key_management->p_export );
1386 if( method == NULL )
1387 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001388 return( method( drv_context,
1389 slot->data.se.slot_number,
1390 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001391 }
1392#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1393
Gilles Peskine8e338702019-07-30 20:06:31 +02001394 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395 {
1396 if( slot->data.raw.bytes > data_size )
1397 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001398 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1399 memset( data + slot->data.raw.bytes, 0,
1400 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401 *data_length = slot->data.raw.bytes;
1402 return( PSA_SUCCESS );
1403 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001404#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001405 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001406 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001407 psa_status_t status;
1408
Gilles Peskineb46bef22019-07-30 21:32:04 +02001409 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001410 if( bytes > data_size )
1411 return( PSA_ERROR_BUFFER_TOO_SMALL );
1412 status = mbedtls_to_psa_error(
1413 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1414 if( status != PSA_SUCCESS )
1415 return( status );
1416 memset( data + bytes, 0, data_size - bytes );
1417 *data_length = bytes;
1418 return( PSA_SUCCESS );
1419 }
1420#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421 else
Moran Peker17e36e12018-05-02 12:55:20 +03001422 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001423#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001424 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1425 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001426 {
Moran Pekera998bc62018-04-16 18:16:20 +03001427 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001429 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001430 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001431#if defined(MBEDTLS_RSA_C)
1432 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001433 pk.pk_info = &mbedtls_rsa_info;
1434 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001435#else
1436 return( PSA_ERROR_NOT_SUPPORTED );
1437#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001438 }
1439 else
1440 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001441#if defined(MBEDTLS_ECP_C)
1442 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001443 pk.pk_info = &mbedtls_eckey_info;
1444 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001445#else
1446 return( PSA_ERROR_NOT_SUPPORTED );
1447#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001448 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001449 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001450 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001451 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001452 }
Moran Peker17e36e12018-05-02 12:55:20 +03001453 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001454 {
Moran Peker17e36e12018-05-02 12:55:20 +03001455 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001456 }
Moran Peker60364322018-04-29 11:34:58 +03001457 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001458 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001459 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001460 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001461 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001462 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1463 * Move the data to the beginning and erase remaining data
1464 * at the original location. */
1465 if( 2 * (size_t) ret <= data_size )
1466 {
1467 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001468 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001469 }
1470 else if( (size_t) ret < data_size )
1471 {
1472 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001473 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001474 }
Moran Pekera998bc62018-04-16 18:16:20 +03001475 *data_length = ret;
1476 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001477 }
1478 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001479#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001480 {
1481 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001482 it is valid for a special-purpose implementation to omit
1483 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001484 return( PSA_ERROR_NOT_SUPPORTED );
1485 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486 }
1487}
1488
Gilles Peskinec5487a82018-12-03 18:08:14 +01001489psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001490 uint8_t *data,
1491 size_t data_size,
1492 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001493{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001494 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001495 psa_status_t status;
1496
1497 /* Set the key to empty now, so that even when there are errors, we always
1498 * set data_length to a value between 0 and data_size. On error, setting
1499 * the key to empty is a good choice because an empty key representation is
1500 * unlikely to be accepted anywhere. */
1501 *data_length = 0;
1502
1503 /* Export requires the EXPORT flag. There is an exception for public keys,
1504 * which don't require any flag, but psa_get_key_from_slot takes
1505 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001506 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001507 if( status != PSA_SUCCESS )
1508 return( status );
1509 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001510 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512
Gilles Peskinec5487a82018-12-03 18:08:14 +01001513psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001514 uint8_t *data,
1515 size_t data_size,
1516 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001517{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001518 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001519 psa_status_t status;
1520
1521 /* Set the key to empty now, so that even when there are errors, we always
1522 * set data_length to a value between 0 and data_size. On error, setting
1523 * the key to empty is a good choice because an empty key representation is
1524 * unlikely to be accepted anywhere. */
1525 *data_length = 0;
1526
1527 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001528 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001529 if( status != PSA_SUCCESS )
1530 return( status );
1531 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001532 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001533}
1534
Gilles Peskine91e8c332019-08-02 19:19:39 +02001535#if defined(static_assert)
1536static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1537 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001538static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001539 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001540static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001541 "One or more key attribute flag is listed as both internal-only and external-only" );
1542#endif
1543
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544/** Validate that a key policy is internally well-formed.
1545 *
1546 * This function only rejects invalid policies. It does not validate the
1547 * consistency of the policy with respect to other attributes of the key
1548 * such as the key type.
1549 */
1550static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001551{
1552 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001553 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 PSA_KEY_USAGE_ENCRYPT |
1555 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001556 PSA_KEY_USAGE_SIGN_HASH |
1557 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1559 return( PSA_ERROR_INVALID_ARGUMENT );
1560
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 return( PSA_SUCCESS );
1562}
1563
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564/** Validate the internal consistency of key attributes.
1565 *
1566 * This function only rejects invalid attribute values. If does not
1567 * validate the consistency of the attributes with any key data that may
1568 * be involved in the creation of the key.
1569 *
1570 * Call this function early in the key creation process.
1571 *
1572 * \param[in] attributes Key attributes for the new key.
1573 * \param[out] p_drv On any return, the driver for the key, if any.
1574 * NULL for a transparent key.
1575 *
1576 */
1577static psa_status_t psa_validate_key_attributes(
1578 const psa_key_attributes_t *attributes,
1579 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001580{
1581 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582
1583 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001584 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585 status = psa_validate_persistent_key_parameters(
1586 attributes->core.lifetime, attributes->core.id,
1587 p_drv, 1 );
1588 if( status != PSA_SUCCESS )
1589 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001590 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001591
1592 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001593 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001594 return( status );
1595
1596 /* Refuse to create overly large keys.
1597 * Note that this doesn't trigger on import if the attributes don't
1598 * explicitly specify a size (so psa_get_key_bits returns 0), so
1599 * psa_import_key() needs its own checks. */
1600 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1601 return( PSA_ERROR_NOT_SUPPORTED );
1602
Gilles Peskine91e8c332019-08-02 19:19:39 +02001603 /* Reject invalid flags. These should not be reachable through the API. */
1604 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1605 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1606 return( PSA_ERROR_INVALID_ARGUMENT );
1607
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001608 return( PSA_SUCCESS );
1609}
1610
Gilles Peskine4747d192019-04-17 15:05:45 +02001611/** Prepare a key slot to receive key material.
1612 *
1613 * This function allocates a key slot and sets its metadata.
1614 *
1615 * If this function fails, call psa_fail_key_creation().
1616 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001617 * This function is intended to be used as follows:
1618 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1619 * it with the specified attributes, and assign it a handle.
1620 * -# Populate the slot with the key material.
1621 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1622 * In case of failure at any step, stop the sequence and call
1623 * psa_fail_key_creation().
1624 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001625 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001626 * \param[in] attributes Key attributes for the new key.
1627 * \param[out] handle On success, a handle for the allocated slot.
1628 * \param[out] p_slot On success, a pointer to the prepared slot.
1629 * \param[out] p_drv On any return, the driver for the key, if any.
1630 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 *
1632 * \retval #PSA_SUCCESS
1633 * The key slot is ready to receive key material.
1634 * \return If this function fails, the key slot is an invalid state.
1635 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 */
1637static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001638 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 const psa_key_attributes_t *attributes,
1640 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001641 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001642 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001643{
1644 psa_status_t status;
1645 psa_key_slot_t *slot;
1646
Gilles Peskinedf179142019-07-15 22:02:14 +02001647 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001648 *p_drv = NULL;
1649
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001650 status = psa_validate_key_attributes( attributes, p_drv );
1651 if( status != PSA_SUCCESS )
1652 return( status );
1653
Gilles Peskineedbed562019-08-07 18:19:59 +02001654 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 if( status != PSA_SUCCESS )
1656 return( status );
1657 slot = *p_slot;
1658
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001659 /* We're storing the declared bit-size of the key. It's up to each
1660 * creation mechanism to verify that this information is correct.
1661 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001662 * an input (generate, device) but not for those where the bit-size
1663 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001664
1665 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001666
Gilles Peskine91e8c332019-08-02 19:19:39 +02001667 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001668 * external-only flags, query `attributes`. Thanks to the check
1669 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001670 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001671 * may have set. */
1672 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001673
Gilles Peskinecbaff462019-07-12 23:46:04 +02001674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001675 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001676 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001677 * create the key file in internal storage, create the
1678 * key inside the secure element, and update the driver's
1679 * persistent data. Start a transaction that will encompass these
1680 * three actions. */
1681 /* The first thing to do is to find a slot number for the new key.
1682 * We save the slot number in persistent storage as part of the
1683 * transaction data. It will be needed to recover if the power
1684 * fails during the key creation process, to clean up on the secure
1685 * element side after restarting. Obtaining a slot number from the
1686 * secure element driver updates its persistent state, but we do not yet
1687 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001688 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001689 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001690 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001691 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001692 &slot->data.se.slot_number );
1693 if( status != PSA_SUCCESS )
1694 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001695 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001696 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001697 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001698 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001699 status = psa_crypto_save_transaction( );
1700 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001701 {
1702 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001703 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001704 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001705 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001706
1707 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1708 {
1709 /* Key registration only makes sense with a secure element. */
1710 return( PSA_ERROR_INVALID_ARGUMENT );
1711 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001712#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1713
Darryl Green0c6575a2018-11-07 16:05:30 +00001714 return( status );
1715}
Gilles Peskine4747d192019-04-17 15:05:45 +02001716
1717/** Finalize the creation of a key once its key material has been set.
1718 *
1719 * This entails writing the key to persistent storage.
1720 *
1721 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001722 * See the documentation of psa_start_key_creation() for the intended use
1723 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001725 * \param[in,out] slot Pointer to the slot with key material.
1726 * \param[in] driver The secure element driver for the key,
1727 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001728 *
1729 * \retval #PSA_SUCCESS
1730 * The key was successfully created. The handle is now valid.
1731 * \return If this function fails, the key slot is an invalid state.
1732 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001734static psa_status_t psa_finish_key_creation(
1735 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001736 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001737{
1738 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001739 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001740 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001741
1742#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001743 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001745#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1746 if( driver != NULL )
1747 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001748 psa_se_key_data_storage_t data;
1749#if defined(static_assert)
1750 static_assert( sizeof( slot->data.se.slot_number ) ==
1751 sizeof( data.slot_number ),
1752 "Slot number size does not match psa_se_key_data_storage_t" );
1753 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1754 "Bit-size size does not match psa_se_key_data_storage_t" );
1755#endif
1756 memcpy( &data.slot_number, &slot->data.se.slot_number,
1757 sizeof( slot->data.se.slot_number ) );
1758 memcpy( &data.bits, &slot->attr.bits,
1759 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001760 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001761 (uint8_t*) &data,
1762 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001763 }
1764 else
1765#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1766 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001767 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001768 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001769 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001770 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1771 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001772 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001773 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1774 status = psa_internal_export_key( slot,
1775 buffer, buffer_size, &length,
1776 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001777 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001778 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001779 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001780
Gilles Peskinef9168942019-09-12 19:20:29 +02001781 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001782 mbedtls_free( buffer );
1783 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001784 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001785#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1786
Gilles Peskinecbaff462019-07-12 23:46:04 +02001787#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001788 /* Finish the transaction for a key creation. This does not
1789 * happen when registering an existing key. Detect this case
1790 * by checking whether a transaction is in progress (actual
1791 * creation of a key in a secure element requires a transaction,
1792 * but registration doesn't use one). */
1793 if( driver != NULL &&
1794 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001795 {
1796 status = psa_save_se_persistent_data( driver );
1797 if( status != PSA_SUCCESS )
1798 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001799 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001800 return( status );
1801 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001802 status = psa_crypto_stop_transaction( );
1803 if( status != PSA_SUCCESS )
1804 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001805 }
1806#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1807
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 return( status );
1809}
1810
1811/** Abort the creation of a key.
1812 *
1813 * You may call this function after calling psa_start_key_creation(),
1814 * or after psa_finish_key_creation() fails. In other circumstances, this
1815 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001816 * See the documentation of psa_start_key_creation() for the intended use
1817 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001819 * \param[in,out] slot Pointer to the slot with key material.
1820 * \param[in] driver The secure element driver for the key,
1821 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001823static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001824 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001825{
Gilles Peskine011e4282019-06-26 18:34:38 +02001826 (void) driver;
1827
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 if( slot == NULL )
1829 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001830
1831#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001832 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001833 * element, and the failure happened later (when saving metadata
1834 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001835 * element.
1836 * https://github.com/ARMmbed/mbed-crypto/issues/217
1837 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001838
Gilles Peskined7729582019-08-05 15:55:54 +02001839 /* Abort the ongoing transaction if any (there may not be one if
1840 * the creation process failed before starting one, or if the
1841 * key creation is a registration of a key in a secure element).
1842 * Earlier functions must already have done what it takes to undo any
1843 * partial creation. All that's left is to update the transaction data
1844 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001845 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001846#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1847
Gilles Peskine4747d192019-04-17 15:05:45 +02001848 psa_wipe_key_slot( slot );
1849}
1850
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001851/** Validate optional attributes during key creation.
1852 *
1853 * Some key attributes are optional during key creation. If they are
1854 * specified in the attributes structure, check that they are consistent
1855 * with the data in the slot.
1856 *
1857 * This function should be called near the end of key creation, after
1858 * the slot in memory is fully populated but before saving persistent data.
1859 */
1860static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001861 const psa_key_slot_t *slot,
1862 const psa_key_attributes_t *attributes )
1863{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001864 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001865 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001866 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001867 return( PSA_ERROR_INVALID_ARGUMENT );
1868 }
1869
1870 if( attributes->domain_parameters_size != 0 )
1871 {
1872#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001873 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001874 {
1875 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001877 mbedtls_mpi_init( &actual );
1878 mbedtls_mpi_init( &required );
1879 ret = mbedtls_rsa_export( slot->data.rsa,
1880 NULL, NULL, NULL, NULL, &actual );
1881 if( ret != 0 )
1882 goto rsa_exit;
1883 ret = mbedtls_mpi_read_binary( &required,
1884 attributes->domain_parameters,
1885 attributes->domain_parameters_size );
1886 if( ret != 0 )
1887 goto rsa_exit;
1888 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1889 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1890 rsa_exit:
1891 mbedtls_mpi_free( &actual );
1892 mbedtls_mpi_free( &required );
1893 if( ret != 0)
1894 return( mbedtls_to_psa_error( ret ) );
1895 }
1896 else
1897#endif
1898 {
1899 return( PSA_ERROR_INVALID_ARGUMENT );
1900 }
1901 }
1902
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001903 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001905 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 return( PSA_ERROR_INVALID_ARGUMENT );
1907 }
1908
1909 return( PSA_SUCCESS );
1910}
1911
Gilles Peskine4747d192019-04-17 15:05:45 +02001912psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001913 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001914 size_t data_length,
1915 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001916{
1917 psa_status_t status;
1918 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001919 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001920
Gilles Peskine0f84d622019-09-12 19:03:13 +02001921 /* Reject zero-length symmetric keys (including raw data key objects).
1922 * This also rejects any key which might be encoded as an empty string,
1923 * which is never valid. */
1924 if( data_length == 0 )
1925 return( PSA_ERROR_INVALID_ARGUMENT );
1926
Gilles Peskinedf179142019-07-15 22:02:14 +02001927 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1928 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929 if( status != PSA_SUCCESS )
1930 goto exit;
1931
Gilles Peskine5d309672019-07-12 23:47:28 +02001932#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1933 if( driver != NULL )
1934 {
1935 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001936 /* The driver should set the number of key bits, however in
1937 * case it doesn't, we initialize bits to an invalid value. */
1938 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001939 if( drv->key_management == NULL ||
1940 drv->key_management->p_import == NULL )
1941 {
1942 status = PSA_ERROR_NOT_SUPPORTED;
1943 goto exit;
1944 }
1945 status = drv->key_management->p_import(
1946 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001947 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001948 &bits );
1949 if( status != PSA_SUCCESS )
1950 goto exit;
1951 if( bits > PSA_MAX_KEY_BITS )
1952 {
1953 status = PSA_ERROR_NOT_SUPPORTED;
1954 goto exit;
1955 }
1956 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001957 }
1958 else
1959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1960 {
1961 status = psa_import_key_into_slot( slot, data, data_length );
1962 if( status != PSA_SUCCESS )
1963 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001964 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001965 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001966 if( status != PSA_SUCCESS )
1967 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001968
Gilles Peskine011e4282019-06-26 18:34:38 +02001969 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 if( status != PSA_SUCCESS )
1972 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 *handle = 0;
1975 }
1976 return( status );
1977}
1978
Gilles Peskined7729582019-08-05 15:55:54 +02001979#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1980psa_status_t mbedtls_psa_register_se_key(
1981 const psa_key_attributes_t *attributes )
1982{
1983 psa_status_t status;
1984 psa_key_slot_t *slot = NULL;
1985 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001986 psa_key_handle_t handle = 0;
1987
1988 /* Leaving attributes unspecified is not currently supported.
1989 * It could make sense to query the key type and size from the
1990 * secure element, but not all secure elements support this
1991 * and the driver HAL doesn't currently support it. */
1992 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1993 return( PSA_ERROR_NOT_SUPPORTED );
1994 if( psa_get_key_bits( attributes ) == 0 )
1995 return( PSA_ERROR_NOT_SUPPORTED );
1996
1997 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1998 &handle, &slot, &driver );
1999 if( status != PSA_SUCCESS )
2000 goto exit;
2001
Gilles Peskined7729582019-08-05 15:55:54 +02002002 status = psa_finish_key_creation( slot, driver );
2003
2004exit:
2005 if( status != PSA_SUCCESS )
2006 {
2007 psa_fail_key_creation( slot, driver );
2008 }
2009 /* Registration doesn't keep the key in RAM. */
2010 psa_close_key( handle );
2011 return( status );
2012}
2013#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2014
Gilles Peskinef603c712019-01-19 13:40:11 +01002015static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002017{
2018 psa_status_t status;
2019 uint8_t *buffer = NULL;
2020 size_t buffer_size = 0;
2021 size_t length;
2022
Gilles Peskine8e338702019-07-30 20:06:31 +02002023 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002024 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002025 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002026 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002027 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002028 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2029 if( status != PSA_SUCCESS )
2030 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002031 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002032 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002033
2034exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002035 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002036 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002037 return( status );
2038}
2039
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2041 const psa_key_attributes_t *specified_attributes,
2042 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002043{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002044 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002045 psa_key_slot_t *source_slot = NULL;
2046 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002047 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002048 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002049
Gilles Peskine28f8f302019-07-24 13:30:31 +02002050 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002051 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002052 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002053 goto exit;
2054
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002055 status = psa_validate_optional_attributes( source_slot,
2056 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057 if( status != PSA_SUCCESS )
2058 goto exit;
2059
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002060 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002061 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002062 if( status != PSA_SUCCESS )
2063 goto exit;
2064
Gilles Peskinedf179142019-07-15 22:02:14 +02002065 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2066 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002067 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002068 if( status != PSA_SUCCESS )
2069 goto exit;
2070
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2072 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002073 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002074 /* Copying to a secure element is not implemented yet. */
2075 status = PSA_ERROR_NOT_SUPPORTED;
2076 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002077 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002079
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002080 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002081 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002083
Gilles Peskine011e4282019-06-26 18:34:38 +02002084 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002085exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002086 if( status != PSA_SUCCESS )
2087 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002088 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089 *target_handle = 0;
2090 }
2091 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002092}
2093
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002094
2095
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002096/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002097/* Message digests */
2098/****************************************************************/
2099
Gilles Peskineb16841e2019-10-10 20:36:12 +02002100#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002101static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002102{
2103 switch( alg )
2104 {
2105#if defined(MBEDTLS_MD2_C)
2106 case PSA_ALG_MD2:
2107 return( &mbedtls_md2_info );
2108#endif
2109#if defined(MBEDTLS_MD4_C)
2110 case PSA_ALG_MD4:
2111 return( &mbedtls_md4_info );
2112#endif
2113#if defined(MBEDTLS_MD5_C)
2114 case PSA_ALG_MD5:
2115 return( &mbedtls_md5_info );
2116#endif
2117#if defined(MBEDTLS_RIPEMD160_C)
2118 case PSA_ALG_RIPEMD160:
2119 return( &mbedtls_ripemd160_info );
2120#endif
2121#if defined(MBEDTLS_SHA1_C)
2122 case PSA_ALG_SHA_1:
2123 return( &mbedtls_sha1_info );
2124#endif
2125#if defined(MBEDTLS_SHA256_C)
2126 case PSA_ALG_SHA_224:
2127 return( &mbedtls_sha224_info );
2128 case PSA_ALG_SHA_256:
2129 return( &mbedtls_sha256_info );
2130#endif
2131#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002132#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002133 case PSA_ALG_SHA_384:
2134 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002135#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002136 case PSA_ALG_SHA_512:
2137 return( &mbedtls_sha512_info );
2138#endif
2139 default:
2140 return( NULL );
2141 }
2142}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002143#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002144
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2146{
2147 switch( operation->alg )
2148 {
Gilles Peskine81736312018-06-26 15:04:31 +02002149 case 0:
2150 /* The object has (apparently) been initialized but it is not
2151 * in use. It's ok to call abort on such an object, and there's
2152 * nothing to do. */
2153 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154#if defined(MBEDTLS_MD2_C)
2155 case PSA_ALG_MD2:
2156 mbedtls_md2_free( &operation->ctx.md2 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_MD4_C)
2160 case PSA_ALG_MD4:
2161 mbedtls_md4_free( &operation->ctx.md4 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_MD5_C)
2165 case PSA_ALG_MD5:
2166 mbedtls_md5_free( &operation->ctx.md5 );
2167 break;
2168#endif
2169#if defined(MBEDTLS_RIPEMD160_C)
2170 case PSA_ALG_RIPEMD160:
2171 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2172 break;
2173#endif
2174#if defined(MBEDTLS_SHA1_C)
2175 case PSA_ALG_SHA_1:
2176 mbedtls_sha1_free( &operation->ctx.sha1 );
2177 break;
2178#endif
2179#if defined(MBEDTLS_SHA256_C)
2180 case PSA_ALG_SHA_224:
2181 case PSA_ALG_SHA_256:
2182 mbedtls_sha256_free( &operation->ctx.sha256 );
2183 break;
2184#endif
2185#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002186#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002188#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189 case PSA_ALG_SHA_512:
2190 mbedtls_sha512_free( &operation->ctx.sha512 );
2191 break;
2192#endif
2193 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002194 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 }
2196 operation->alg = 0;
2197 return( PSA_SUCCESS );
2198}
2199
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002200psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201 psa_algorithm_t alg )
2202{
Janos Follath24eed8d2019-11-22 13:21:35 +00002203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002204
2205 /* A context must be freshly initialized before it can be set up. */
2206 if( operation->alg != 0 )
2207 {
2208 return( PSA_ERROR_BAD_STATE );
2209 }
2210
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211 switch( alg )
2212 {
2213#if defined(MBEDTLS_MD2_C)
2214 case PSA_ALG_MD2:
2215 mbedtls_md2_init( &operation->ctx.md2 );
2216 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2217 break;
2218#endif
2219#if defined(MBEDTLS_MD4_C)
2220 case PSA_ALG_MD4:
2221 mbedtls_md4_init( &operation->ctx.md4 );
2222 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2223 break;
2224#endif
2225#if defined(MBEDTLS_MD5_C)
2226 case PSA_ALG_MD5:
2227 mbedtls_md5_init( &operation->ctx.md5 );
2228 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2229 break;
2230#endif
2231#if defined(MBEDTLS_RIPEMD160_C)
2232 case PSA_ALG_RIPEMD160:
2233 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2234 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2235 break;
2236#endif
2237#if defined(MBEDTLS_SHA1_C)
2238 case PSA_ALG_SHA_1:
2239 mbedtls_sha1_init( &operation->ctx.sha1 );
2240 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2241 break;
2242#endif
2243#if defined(MBEDTLS_SHA256_C)
2244 case PSA_ALG_SHA_224:
2245 mbedtls_sha256_init( &operation->ctx.sha256 );
2246 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2247 break;
2248 case PSA_ALG_SHA_256:
2249 mbedtls_sha256_init( &operation->ctx.sha256 );
2250 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2251 break;
2252#endif
2253#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002254#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 case PSA_ALG_SHA_384:
2256 mbedtls_sha512_init( &operation->ctx.sha512 );
2257 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2258 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002259#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 case PSA_ALG_SHA_512:
2261 mbedtls_sha512_init( &operation->ctx.sha512 );
2262 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2263 break;
2264#endif
2265 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002266 return( PSA_ALG_IS_HASH( alg ) ?
2267 PSA_ERROR_NOT_SUPPORTED :
2268 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 }
2270 if( ret == 0 )
2271 operation->alg = alg;
2272 else
2273 psa_hash_abort( operation );
2274 return( mbedtls_to_psa_error( ret ) );
2275}
2276
2277psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2278 const uint8_t *input,
2279 size_t input_length )
2280{
Janos Follath24eed8d2019-11-22 13:21:35 +00002281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002282
2283 /* Don't require hash implementations to behave correctly on a
2284 * zero-length input, which may have an invalid pointer. */
2285 if( input_length == 0 )
2286 return( PSA_SUCCESS );
2287
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288 switch( operation->alg )
2289 {
2290#if defined(MBEDTLS_MD2_C)
2291 case PSA_ALG_MD2:
2292 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2293 input, input_length );
2294 break;
2295#endif
2296#if defined(MBEDTLS_MD4_C)
2297 case PSA_ALG_MD4:
2298 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2299 input, input_length );
2300 break;
2301#endif
2302#if defined(MBEDTLS_MD5_C)
2303 case PSA_ALG_MD5:
2304 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2305 input, input_length );
2306 break;
2307#endif
2308#if defined(MBEDTLS_RIPEMD160_C)
2309 case PSA_ALG_RIPEMD160:
2310 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2311 input, input_length );
2312 break;
2313#endif
2314#if defined(MBEDTLS_SHA1_C)
2315 case PSA_ALG_SHA_1:
2316 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2317 input, input_length );
2318 break;
2319#endif
2320#if defined(MBEDTLS_SHA256_C)
2321 case PSA_ALG_SHA_224:
2322 case PSA_ALG_SHA_256:
2323 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2324 input, input_length );
2325 break;
2326#endif
2327#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002328#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002330#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_512:
2332 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2333 input, input_length );
2334 break;
2335#endif
2336 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002337 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002339
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 if( ret != 0 )
2341 psa_hash_abort( operation );
2342 return( mbedtls_to_psa_error( ret ) );
2343}
2344
2345psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2346 uint8_t *hash,
2347 size_t hash_size,
2348 size_t *hash_length )
2349{
itayzafrir40835d42018-08-02 13:14:17 +03002350 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002352 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353
2354 /* Fill the output buffer with something that isn't a valid hash
2355 * (barring an attack on the hash and deliberately-crafted input),
2356 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002357 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002358 /* If hash_size is 0 then hash may be NULL and then the
2359 * call to memset would have undefined behavior. */
2360 if( hash_size != 0 )
2361 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362
2363 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002364 {
2365 status = PSA_ERROR_BUFFER_TOO_SMALL;
2366 goto exit;
2367 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368
2369 switch( operation->alg )
2370 {
2371#if defined(MBEDTLS_MD2_C)
2372 case PSA_ALG_MD2:
2373 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2374 break;
2375#endif
2376#if defined(MBEDTLS_MD4_C)
2377 case PSA_ALG_MD4:
2378 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2379 break;
2380#endif
2381#if defined(MBEDTLS_MD5_C)
2382 case PSA_ALG_MD5:
2383 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2384 break;
2385#endif
2386#if defined(MBEDTLS_RIPEMD160_C)
2387 case PSA_ALG_RIPEMD160:
2388 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2389 break;
2390#endif
2391#if defined(MBEDTLS_SHA1_C)
2392 case PSA_ALG_SHA_1:
2393 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2394 break;
2395#endif
2396#if defined(MBEDTLS_SHA256_C)
2397 case PSA_ALG_SHA_224:
2398 case PSA_ALG_SHA_256:
2399 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2400 break;
2401#endif
2402#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002403#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002405#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406 case PSA_ALG_SHA_512:
2407 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2408 break;
2409#endif
2410 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002411 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002412 }
itayzafrir40835d42018-08-02 13:14:17 +03002413 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414
itayzafrir40835d42018-08-02 13:14:17 +03002415exit:
2416 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002418 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419 return( psa_hash_abort( operation ) );
2420 }
2421 else
2422 {
2423 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002424 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425 }
2426}
2427
Gilles Peskine2d277862018-06-18 15:41:12 +02002428psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2429 const uint8_t *hash,
2430 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431{
2432 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2433 size_t actual_hash_length;
2434 psa_status_t status = psa_hash_finish( operation,
2435 actual_hash, sizeof( actual_hash ),
2436 &actual_hash_length );
2437 if( status != PSA_SUCCESS )
2438 return( status );
2439 if( actual_hash_length != hash_length )
2440 return( PSA_ERROR_INVALID_SIGNATURE );
2441 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2442 return( PSA_ERROR_INVALID_SIGNATURE );
2443 return( PSA_SUCCESS );
2444}
2445
Gilles Peskine0a749c82019-11-28 19:33:58 +01002446psa_status_t psa_hash_compute( psa_algorithm_t alg,
2447 const uint8_t *input, size_t input_length,
2448 uint8_t *hash, size_t hash_size,
2449 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 *hash_length = hash_size;
2455 status = psa_hash_setup( &operation, alg );
2456 if( status != PSA_SUCCESS )
2457 goto exit;
2458 status = psa_hash_update( &operation, input, input_length );
2459 if( status != PSA_SUCCESS )
2460 goto exit;
2461 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2462 if( status != PSA_SUCCESS )
2463 goto exit;
2464
2465exit:
2466 if( status == PSA_SUCCESS )
2467 status = psa_hash_abort( &operation );
2468 else
2469 psa_hash_abort( &operation );
2470 return( status );
2471}
2472
2473psa_status_t psa_hash_compare( psa_algorithm_t alg,
2474 const uint8_t *input, size_t input_length,
2475 const uint8_t *hash, size_t hash_length )
2476{
2477 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2479
2480 status = psa_hash_setup( &operation, alg );
2481 if( status != PSA_SUCCESS )
2482 goto exit;
2483 status = psa_hash_update( &operation, input, input_length );
2484 if( status != PSA_SUCCESS )
2485 goto exit;
2486 status = psa_hash_verify( &operation, hash, hash_length );
2487 if( status != PSA_SUCCESS )
2488 goto exit;
2489
2490exit:
2491 if( status == PSA_SUCCESS )
2492 status = psa_hash_abort( &operation );
2493 else
2494 psa_hash_abort( &operation );
2495 return( status );
2496}
2497
Gilles Peskineeb35d782019-01-22 17:56:16 +01002498psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2499 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002500{
2501 if( target_operation->alg != 0 )
2502 return( PSA_ERROR_BAD_STATE );
2503
2504 switch( source_operation->alg )
2505 {
2506 case 0:
2507 return( PSA_ERROR_BAD_STATE );
2508#if defined(MBEDTLS_MD2_C)
2509 case PSA_ALG_MD2:
2510 mbedtls_md2_clone( &target_operation->ctx.md2,
2511 &source_operation->ctx.md2 );
2512 break;
2513#endif
2514#if defined(MBEDTLS_MD4_C)
2515 case PSA_ALG_MD4:
2516 mbedtls_md4_clone( &target_operation->ctx.md4,
2517 &source_operation->ctx.md4 );
2518 break;
2519#endif
2520#if defined(MBEDTLS_MD5_C)
2521 case PSA_ALG_MD5:
2522 mbedtls_md5_clone( &target_operation->ctx.md5,
2523 &source_operation->ctx.md5 );
2524 break;
2525#endif
2526#if defined(MBEDTLS_RIPEMD160_C)
2527 case PSA_ALG_RIPEMD160:
2528 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2529 &source_operation->ctx.ripemd160 );
2530 break;
2531#endif
2532#if defined(MBEDTLS_SHA1_C)
2533 case PSA_ALG_SHA_1:
2534 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2535 &source_operation->ctx.sha1 );
2536 break;
2537#endif
2538#if defined(MBEDTLS_SHA256_C)
2539 case PSA_ALG_SHA_224:
2540 case PSA_ALG_SHA_256:
2541 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2542 &source_operation->ctx.sha256 );
2543 break;
2544#endif
2545#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002546#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002547 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002548#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002549 case PSA_ALG_SHA_512:
2550 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2551 &source_operation->ctx.sha512 );
2552 break;
2553#endif
2554 default:
2555 return( PSA_ERROR_NOT_SUPPORTED );
2556 }
2557
2558 target_operation->alg = source_operation->alg;
2559 return( PSA_SUCCESS );
2560}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002561
2562
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002563/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564/* MAC */
2565/****************************************************************/
2566
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002567static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 psa_algorithm_t alg,
2569 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002570 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002571 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002574 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002576 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002577 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002578
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2580 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002581 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002583 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002584 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 mode = MBEDTLS_MODE_STREAM;
2586 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587 case PSA_ALG_CTR:
2588 mode = MBEDTLS_MODE_CTR;
2589 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002590 case PSA_ALG_CFB:
2591 mode = MBEDTLS_MODE_CFB;
2592 break;
2593 case PSA_ALG_OFB:
2594 mode = MBEDTLS_MODE_OFB;
2595 break;
2596 case PSA_ALG_CBC_NO_PADDING:
2597 mode = MBEDTLS_MODE_CBC;
2598 break;
2599 case PSA_ALG_CBC_PKCS7:
2600 mode = MBEDTLS_MODE_CBC;
2601 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002602 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 mode = MBEDTLS_MODE_CCM;
2604 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002605 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 mode = MBEDTLS_MODE_GCM;
2607 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002608 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2609 mode = MBEDTLS_MODE_CHACHAPOLY;
2610 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 default:
2612 return( NULL );
2613 }
2614 }
2615 else if( alg == PSA_ALG_CMAC )
2616 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 else
2618 return( NULL );
2619
2620 switch( key_type )
2621 {
2622 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002623 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 break;
2625 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002626 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2627 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002628 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002629 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002631 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002632 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2633 * but two-key Triple-DES is functionally three-key Triple-DES
2634 * with K1=K3, so that's how we present it to mbedtls. */
2635 if( key_bits == 128 )
2636 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637 break;
2638 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002639 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640 break;
2641 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002642 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002644 case PSA_KEY_TYPE_CHACHA20:
2645 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2646 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647 default:
2648 return( NULL );
2649 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002650 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002651 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652
Jaeden Amero23bbb752018-06-26 14:16:54 +01002653 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2654 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655}
2656
Gilles Peskinea05219c2018-11-16 16:02:56 +01002657#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002658static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002659{
Gilles Peskine2d277862018-06-18 15:41:12 +02002660 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002661 {
2662 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002663 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002664 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002665 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002666 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002667 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002668 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002669 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002670 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002671 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002672 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002673 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002674 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002675 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002676 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002677 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002678 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002679 return( 128 );
2680 default:
2681 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002682 }
2683}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002684#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002685
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686/* Initialize the MAC operation structure. Once this function has been
2687 * called, psa_mac_abort can run and will do the right thing. */
2688static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2689 psa_algorithm_t alg )
2690{
2691 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2692
2693 operation->alg = alg;
2694 operation->key_set = 0;
2695 operation->iv_set = 0;
2696 operation->iv_required = 0;
2697 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002698 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002699
2700#if defined(MBEDTLS_CMAC_C)
2701 if( alg == PSA_ALG_CMAC )
2702 {
2703 operation->iv_required = 0;
2704 mbedtls_cipher_init( &operation->ctx.cmac );
2705 status = PSA_SUCCESS;
2706 }
2707 else
2708#endif /* MBEDTLS_CMAC_C */
2709#if defined(MBEDTLS_MD_C)
2710 if( PSA_ALG_IS_HMAC( operation->alg ) )
2711 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002712 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2713 operation->ctx.hmac.hash_ctx.alg = 0;
2714 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002715 }
2716 else
2717#endif /* MBEDTLS_MD_C */
2718 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002719 if( ! PSA_ALG_IS_MAC( alg ) )
2720 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002721 }
2722
2723 if( status != PSA_SUCCESS )
2724 memset( operation, 0, sizeof( *operation ) );
2725 return( status );
2726}
2727
Gilles Peskine01126fa2018-07-12 17:04:55 +02002728#if defined(MBEDTLS_MD_C)
2729static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2730{
Gilles Peskine3f108122018-12-07 18:14:53 +01002731 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002732 return( psa_hash_abort( &hmac->hash_ctx ) );
2733}
2734#endif /* MBEDTLS_MD_C */
2735
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2737{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002738 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002740 /* The object has (apparently) been initialized but it is not
2741 * in use. It's ok to call abort on such an object, and there's
2742 * nothing to do. */
2743 return( PSA_SUCCESS );
2744 }
2745 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002747 if( operation->alg == PSA_ALG_CMAC )
2748 {
2749 mbedtls_cipher_free( &operation->ctx.cmac );
2750 }
2751 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002754 if( PSA_ALG_IS_HMAC( operation->alg ) )
2755 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002756 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002757 }
2758 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002760 {
2761 /* Sanity check (shouldn't happen: operation->alg should
2762 * always have been initialized to a valid value). */
2763 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764 }
Moran Peker41deec42018-04-04 15:43:05 +03002765
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766 operation->alg = 0;
2767 operation->key_set = 0;
2768 operation->iv_set = 0;
2769 operation->iv_required = 0;
2770 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002771 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002772
Gilles Peskine8c9def32018-02-08 10:02:12 +01002773 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002774
2775bad_state:
2776 /* If abort is called on an uninitialized object, we can't trust
2777 * anything. Wipe the object in case it contains confidential data.
2778 * This may result in a memory leak if a pointer gets overwritten,
2779 * but it's too late to do anything about this. */
2780 memset( operation, 0, sizeof( *operation ) );
2781 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782}
2783
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002784#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002785static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002787 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788 const mbedtls_cipher_info_t *cipher_info )
2789{
Janos Follath24eed8d2019-11-22 13:21:35 +00002790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002791
2792 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002793
2794 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2795 if( ret != 0 )
2796 return( ret );
2797
2798 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2799 slot->data.raw.data,
2800 key_bits );
2801 return( ret );
2802}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002803#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002804
Gilles Peskine248051a2018-06-20 16:09:38 +02002805#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002806static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2807 const uint8_t *key,
2808 size_t key_length,
2809 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002810{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002811 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002812 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002813 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 psa_status_t status;
2816
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002817 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2818 * overflow below. This should never trigger if the hash algorithm
2819 * is implemented correctly. */
2820 /* The size checks against the ipad and opad buffers cannot be written
2821 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2822 * because that triggers -Wlogical-op on GCC 7.3. */
2823 if( block_size > sizeof( ipad ) )
2824 return( PSA_ERROR_NOT_SUPPORTED );
2825 if( block_size > sizeof( hmac->opad ) )
2826 return( PSA_ERROR_NOT_SUPPORTED );
2827 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002828 return( PSA_ERROR_NOT_SUPPORTED );
2829
Gilles Peskined223b522018-06-11 18:12:58 +02002830 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002831 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002832 status = psa_hash_compute( hash_alg, key, key_length,
2833 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002835 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002836 }
Gilles Peskine96889972018-07-12 17:07:03 +02002837 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2838 * but it is permitted. It is common when HMAC is used in HKDF, for
2839 * example. Don't call `memcpy` in the 0-length because `key` could be
2840 * an invalid pointer which would make the behavior undefined. */
2841 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002842 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002843
Gilles Peskined223b522018-06-11 18:12:58 +02002844 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2845 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002846 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002847 ipad[i] ^= 0x36;
2848 memset( ipad + key_length, 0x36, block_size - key_length );
2849
2850 /* Copy the key material from ipad to opad, flipping the requisite bits,
2851 * and filling the rest of opad with the requisite constant. */
2852 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002853 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2854 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002855
Gilles Peskine01126fa2018-07-12 17:04:55 +02002856 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002857 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002858 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002859
Gilles Peskine01126fa2018-07-12 17:04:55 +02002860 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002861
2862cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002863 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002864
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002865 return( status );
2866}
Gilles Peskine248051a2018-06-20 16:09:38 +02002867#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002868
Gilles Peskine89167cb2018-07-08 20:12:23 +02002869static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002870 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002871 psa_algorithm_t alg,
2872 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002875 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002877 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002878 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002879 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002880 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002882 /* A context must be freshly initialized before it can be set up. */
2883 if( operation->alg != 0 )
2884 {
2885 return( PSA_ERROR_BAD_STATE );
2886 }
2887
Gilles Peskined911eb72018-08-14 15:18:45 +02002888 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002889 if( status != PSA_SUCCESS )
2890 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002891 if( is_sign )
2892 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893
Gilles Peskine28f8f302019-07-24 13:30:31 +02002894 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002895 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002896 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002897 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002898
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002900 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901 {
2902 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002903 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002904 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 if( cipher_info == NULL )
2907 {
2908 status = PSA_ERROR_NOT_SUPPORTED;
2909 goto exit;
2910 }
2911 operation->mac_size = cipher_info->block_size;
2912 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2913 status = mbedtls_to_psa_error( ret );
2914 }
2915 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002918 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002919 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002920 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921 if( hash_alg == 0 )
2922 {
2923 status = PSA_ERROR_NOT_SUPPORTED;
2924 goto exit;
2925 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002926
2927 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2928 /* Sanity check. This shouldn't fail on a valid configuration. */
2929 if( operation->mac_size == 0 ||
2930 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2931 {
2932 status = PSA_ERROR_NOT_SUPPORTED;
2933 goto exit;
2934 }
2935
Gilles Peskine8e338702019-07-30 20:06:31 +02002936 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002937 {
2938 status = PSA_ERROR_INVALID_ARGUMENT;
2939 goto exit;
2940 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002941
Gilles Peskine01126fa2018-07-12 17:04:55 +02002942 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2943 slot->data.raw.data,
2944 slot->data.raw.bytes,
2945 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002946 }
2947 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002949 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002950 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002951 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002952 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002953
Gilles Peskined911eb72018-08-14 15:18:45 +02002954 if( truncated == 0 )
2955 {
2956 /* The "normal" case: untruncated algorithm. Nothing to do. */
2957 }
2958 else if( truncated < 4 )
2959 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002960 /* A very short MAC is too short for security since it can be
2961 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2962 * so we make this our minimum, even though 32 bits is still
2963 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002964 status = PSA_ERROR_NOT_SUPPORTED;
2965 }
2966 else if( truncated > operation->mac_size )
2967 {
2968 /* It's impossible to "truncate" to a larger length. */
2969 status = PSA_ERROR_INVALID_ARGUMENT;
2970 }
2971 else
2972 operation->mac_size = truncated;
2973
Gilles Peskinefbfac682018-07-08 20:51:54 +02002974exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002977 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002979 else
2980 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002981 operation->key_set = 1;
2982 }
2983 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984}
2985
Gilles Peskine89167cb2018-07-08 20:12:23 +02002986psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002987 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002988 psa_algorithm_t alg )
2989{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002990 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002991}
2992
2993psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002994 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002995 psa_algorithm_t alg )
2996{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002997 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002998}
2999
Gilles Peskine8c9def32018-02-08 10:02:12 +01003000psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3001 const uint8_t *input,
3002 size_t input_length )
3003{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003004 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003006 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003008 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009 operation->has_input = 1;
3010
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003012 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003013 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003014 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3015 input, input_length );
3016 status = mbedtls_to_psa_error( ret );
3017 }
3018 else
3019#endif /* MBEDTLS_CMAC_C */
3020#if defined(MBEDTLS_MD_C)
3021 if( PSA_ALG_IS_HMAC( operation->alg ) )
3022 {
3023 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3024 input_length );
3025 }
3026 else
3027#endif /* MBEDTLS_MD_C */
3028 {
3029 /* This shouldn't happen if `operation` was initialized by
3030 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003031 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003032 }
3033
Gilles Peskinefbfac682018-07-08 20:51:54 +02003034 if( status != PSA_SUCCESS )
3035 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003036 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037}
3038
Gilles Peskine01126fa2018-07-12 17:04:55 +02003039#if defined(MBEDTLS_MD_C)
3040static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3041 uint8_t *mac,
3042 size_t mac_size )
3043{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003044 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003045 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3046 size_t hash_size = 0;
3047 size_t block_size = psa_get_hash_block_size( hash_alg );
3048 psa_status_t status;
3049
Gilles Peskine01126fa2018-07-12 17:04:55 +02003050 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3051 if( status != PSA_SUCCESS )
3052 return( status );
3053 /* From here on, tmp needs to be wiped. */
3054
3055 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3056 if( status != PSA_SUCCESS )
3057 goto exit;
3058
3059 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3060 if( status != PSA_SUCCESS )
3061 goto exit;
3062
3063 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3064 if( status != PSA_SUCCESS )
3065 goto exit;
3066
Gilles Peskined911eb72018-08-14 15:18:45 +02003067 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3068 if( status != PSA_SUCCESS )
3069 goto exit;
3070
3071 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003072
3073exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003074 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003075 return( status );
3076}
3077#endif /* MBEDTLS_MD_C */
3078
mohammad16036df908f2018-04-02 08:34:15 -07003079static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003080 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003081 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003083 if( ! operation->key_set )
3084 return( PSA_ERROR_BAD_STATE );
3085 if( operation->iv_required && ! operation->iv_set )
3086 return( PSA_ERROR_BAD_STATE );
3087
Gilles Peskine8c9def32018-02-08 10:02:12 +01003088 if( mac_size < operation->mac_size )
3089 return( PSA_ERROR_BUFFER_TOO_SMALL );
3090
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003092 if( operation->alg == PSA_ALG_CMAC )
3093 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003094 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3095 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3096 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003097 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003098 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003099 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003101 else
3102#endif /* MBEDTLS_CMAC_C */
3103#if defined(MBEDTLS_MD_C)
3104 if( PSA_ALG_IS_HMAC( operation->alg ) )
3105 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003106 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003107 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 }
3109 else
3110#endif /* MBEDTLS_MD_C */
3111 {
3112 /* This shouldn't happen if `operation` was initialized by
3113 * a setup function. */
3114 return( PSA_ERROR_BAD_STATE );
3115 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116}
3117
Gilles Peskineacd4be32018-07-08 19:56:25 +02003118psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3119 uint8_t *mac,
3120 size_t mac_size,
3121 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003122{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003123 psa_status_t status;
3124
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 if( operation->alg == 0 )
3126 {
3127 return( PSA_ERROR_BAD_STATE );
3128 }
3129
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003130 /* Fill the output buffer with something that isn't a valid mac
3131 * (barring an attack on the mac and deliberately-crafted input),
3132 * in case the caller doesn't check the return status properly. */
3133 *mac_length = mac_size;
3134 /* If mac_size is 0 then mac may be NULL and then the
3135 * call to memset would have undefined behavior. */
3136 if( mac_size != 0 )
3137 memset( mac, '!', mac_size );
3138
Gilles Peskine89167cb2018-07-08 20:12:23 +02003139 if( ! operation->is_sign )
3140 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003141 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003142 }
mohammad16036df908f2018-04-02 08:34:15 -07003143
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003144 status = psa_mac_finish_internal( operation, mac, mac_size );
3145
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003146 if( status == PSA_SUCCESS )
3147 {
3148 status = psa_mac_abort( operation );
3149 if( status == PSA_SUCCESS )
3150 *mac_length = operation->mac_size;
3151 else
3152 memset( mac, '!', mac_size );
3153 }
3154 else
3155 psa_mac_abort( operation );
3156 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003157}
3158
Gilles Peskineacd4be32018-07-08 19:56:25 +02003159psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3160 const uint8_t *mac,
3161 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003162{
Gilles Peskine828ed142018-06-18 23:25:51 +02003163 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003164 psa_status_t status;
3165
Jaeden Amero252ef282019-02-15 14:05:35 +00003166 if( operation->alg == 0 )
3167 {
3168 return( PSA_ERROR_BAD_STATE );
3169 }
3170
Gilles Peskine89167cb2018-07-08 20:12:23 +02003171 if( operation->is_sign )
3172 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003173 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003174 }
3175 if( operation->mac_size != mac_length )
3176 {
3177 status = PSA_ERROR_INVALID_SIGNATURE;
3178 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003179 }
mohammad16036df908f2018-04-02 08:34:15 -07003180
3181 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003182 actual_mac, sizeof( actual_mac ) );
3183
3184 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3185 status = PSA_ERROR_INVALID_SIGNATURE;
3186
3187cleanup:
3188 if( status == PSA_SUCCESS )
3189 status = psa_mac_abort( operation );
3190 else
3191 psa_mac_abort( operation );
3192
Gilles Peskine3f108122018-12-07 18:14:53 +01003193 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003194
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003195 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003196}
3197
3198
Gilles Peskine20035e32018-02-03 22:44:14 +01003199
Gilles Peskine20035e32018-02-03 22:44:14 +01003200/****************************************************************/
3201/* Asymmetric cryptography */
3202/****************************************************************/
3203
Gilles Peskine2b450e32018-06-27 15:42:46 +02003204#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003205/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003206 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003207static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3208 size_t hash_length,
3209 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003210{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003211 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003212 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003213 *md_alg = mbedtls_md_get_type( md_info );
3214
3215 /* The Mbed TLS RSA module uses an unsigned int for hash length
3216 * parameters. Validate that it fits so that we don't risk an
3217 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003218#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003219 if( hash_length > UINT_MAX )
3220 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003221#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003222
3223#if defined(MBEDTLS_PKCS1_V15)
3224 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3225 * must be correct. */
3226 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3227 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003228 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003229 if( md_info == NULL )
3230 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003231 if( mbedtls_md_get_size( md_info ) != hash_length )
3232 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003233 }
3234#endif /* MBEDTLS_PKCS1_V15 */
3235
3236#if defined(MBEDTLS_PKCS1_V21)
3237 /* PSS requires a hash internally. */
3238 if( PSA_ALG_IS_RSA_PSS( alg ) )
3239 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003240 if( md_info == NULL )
3241 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003242 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003243#endif /* MBEDTLS_PKCS1_V21 */
3244
Gilles Peskine61b91d42018-06-08 16:09:36 +02003245 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003246}
3247
Gilles Peskine2b450e32018-06-27 15:42:46 +02003248static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3249 psa_algorithm_t alg,
3250 const uint8_t *hash,
3251 size_t hash_length,
3252 uint8_t *signature,
3253 size_t signature_size,
3254 size_t *signature_length )
3255{
3256 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003258 mbedtls_md_type_t md_alg;
3259
3260 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3261 if( status != PSA_SUCCESS )
3262 return( status );
3263
Gilles Peskine630a18a2018-06-29 17:49:35 +02003264 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003265 return( PSA_ERROR_BUFFER_TOO_SMALL );
3266
3267#if defined(MBEDTLS_PKCS1_V15)
3268 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3269 {
3270 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3271 MBEDTLS_MD_NONE );
3272 ret = mbedtls_rsa_pkcs1_sign( rsa,
3273 mbedtls_ctr_drbg_random,
3274 &global_data.ctr_drbg,
3275 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003276 md_alg,
3277 (unsigned int) hash_length,
3278 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003279 signature );
3280 }
3281 else
3282#endif /* MBEDTLS_PKCS1_V15 */
3283#if defined(MBEDTLS_PKCS1_V21)
3284 if( PSA_ALG_IS_RSA_PSS( alg ) )
3285 {
3286 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3287 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3288 mbedtls_ctr_drbg_random,
3289 &global_data.ctr_drbg,
3290 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003291 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003292 (unsigned int) hash_length,
3293 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003294 signature );
3295 }
3296 else
3297#endif /* MBEDTLS_PKCS1_V21 */
3298 {
3299 return( PSA_ERROR_INVALID_ARGUMENT );
3300 }
3301
3302 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003303 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003304 return( mbedtls_to_psa_error( ret ) );
3305}
3306
3307static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3308 psa_algorithm_t alg,
3309 const uint8_t *hash,
3310 size_t hash_length,
3311 const uint8_t *signature,
3312 size_t signature_length )
3313{
3314 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003316 mbedtls_md_type_t md_alg;
3317
3318 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3319 if( status != PSA_SUCCESS )
3320 return( status );
3321
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003322 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3323 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003324
3325#if defined(MBEDTLS_PKCS1_V15)
3326 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3327 {
3328 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3329 MBEDTLS_MD_NONE );
3330 ret = mbedtls_rsa_pkcs1_verify( rsa,
3331 mbedtls_ctr_drbg_random,
3332 &global_data.ctr_drbg,
3333 MBEDTLS_RSA_PUBLIC,
3334 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003335 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003336 hash,
3337 signature );
3338 }
3339 else
3340#endif /* MBEDTLS_PKCS1_V15 */
3341#if defined(MBEDTLS_PKCS1_V21)
3342 if( PSA_ALG_IS_RSA_PSS( alg ) )
3343 {
3344 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3345 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3346 mbedtls_ctr_drbg_random,
3347 &global_data.ctr_drbg,
3348 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003349 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003350 (unsigned int) hash_length,
3351 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003352 signature );
3353 }
3354 else
3355#endif /* MBEDTLS_PKCS1_V21 */
3356 {
3357 return( PSA_ERROR_INVALID_ARGUMENT );
3358 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003359
3360 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3361 * the rest of the signature is invalid". This has little use in
3362 * practice and PSA doesn't report this distinction. */
3363 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3364 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003365 return( mbedtls_to_psa_error( ret ) );
3366}
3367#endif /* MBEDTLS_RSA_C */
3368
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003369#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003370/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3371 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3372 * (even though these functions don't modify it). */
3373static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3374 psa_algorithm_t alg,
3375 const uint8_t *hash,
3376 size_t hash_length,
3377 uint8_t *signature,
3378 size_t signature_size,
3379 size_t *signature_length )
3380{
Janos Follath24eed8d2019-11-22 13:21:35 +00003381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003382 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003383 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003384 mbedtls_mpi_init( &r );
3385 mbedtls_mpi_init( &s );
3386
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003387 if( signature_size < 2 * curve_bytes )
3388 {
3389 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3390 goto cleanup;
3391 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003392
Gilles Peskinea05219c2018-11-16 16:02:56 +01003393#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003394 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3395 {
3396 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3397 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3398 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003399 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3400 &ecp->d, hash,
3401 hash_length, md_alg,
3402 mbedtls_ctr_drbg_random,
3403 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003404 }
3405 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003406#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003407 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003408 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003409 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3410 hash, hash_length,
3411 mbedtls_ctr_drbg_random,
3412 &global_data.ctr_drbg ) );
3413 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003414
3415 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3416 signature,
3417 curve_bytes ) );
3418 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3419 signature + curve_bytes,
3420 curve_bytes ) );
3421
3422cleanup:
3423 mbedtls_mpi_free( &r );
3424 mbedtls_mpi_free( &s );
3425 if( ret == 0 )
3426 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003427 return( mbedtls_to_psa_error( ret ) );
3428}
3429
3430static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3431 const uint8_t *hash,
3432 size_t hash_length,
3433 const uint8_t *signature,
3434 size_t signature_length )
3435{
Janos Follath24eed8d2019-11-22 13:21:35 +00003436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003437 mbedtls_mpi r, s;
3438 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3439 mbedtls_mpi_init( &r );
3440 mbedtls_mpi_init( &s );
3441
3442 if( signature_length != 2 * curve_bytes )
3443 return( PSA_ERROR_INVALID_SIGNATURE );
3444
3445 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3446 signature,
3447 curve_bytes ) );
3448 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3449 signature + curve_bytes,
3450 curve_bytes ) );
3451
3452 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3453 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003454
3455cleanup:
3456 mbedtls_mpi_free( &r );
3457 mbedtls_mpi_free( &s );
3458 return( mbedtls_to_psa_error( ret ) );
3459}
3460#endif /* MBEDTLS_ECDSA_C */
3461
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003462psa_status_t psa_sign_hash( psa_key_handle_t handle,
3463 psa_algorithm_t alg,
3464 const uint8_t *hash,
3465 size_t hash_length,
3466 uint8_t *signature,
3467 size_t signature_size,
3468 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003469{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003470 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003471 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003472#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3473 const psa_drv_se_t *drv;
3474 psa_drv_se_context_t *drv_context;
3475#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003476
3477 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003478 /* Immediately reject a zero-length signature buffer. This guarantees
3479 * that signature must be a valid pointer. (On the other hand, the hash
3480 * buffer can in principle be empty since it doesn't actually have
3481 * to be a hash.) */
3482 if( signature_size == 0 )
3483 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003484
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003485 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003486 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003487 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003488 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003489 {
3490 status = PSA_ERROR_INVALID_ARGUMENT;
3491 goto exit;
3492 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003493
Gilles Peskineedc64242019-08-07 21:05:07 +02003494#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3495 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3496 {
3497 if( drv->asymmetric == NULL ||
3498 drv->asymmetric->p_sign == NULL )
3499 {
3500 status = PSA_ERROR_NOT_SUPPORTED;
3501 goto exit;
3502 }
3503 status = drv->asymmetric->p_sign( drv_context,
3504 slot->data.se.slot_number,
3505 alg,
3506 hash, hash_length,
3507 signature, signature_size,
3508 signature_length );
3509 }
3510 else
3511#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003512#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003513 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003514 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003515 status = psa_rsa_sign( slot->data.rsa,
3516 alg,
3517 hash, hash_length,
3518 signature, signature_size,
3519 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003520 }
3521 else
3522#endif /* defined(MBEDTLS_RSA_C) */
3523#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003524 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003525 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003526#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003527 if(
3528#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3529 PSA_ALG_IS_ECDSA( alg )
3530#else
3531 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3532#endif
3533 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003534 status = psa_ecdsa_sign( slot->data.ecp,
3535 alg,
3536 hash, hash_length,
3537 signature, signature_size,
3538 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003539 else
3540#endif /* defined(MBEDTLS_ECDSA_C) */
3541 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003542 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003543 }
itayzafrir5c753392018-05-08 11:18:38 +03003544 }
3545 else
3546#endif /* defined(MBEDTLS_ECP_C) */
3547 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003548 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003549 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003550
3551exit:
3552 /* Fill the unused part of the output buffer (the whole buffer on error,
3553 * the trailing part on success) with something that isn't a valid mac
3554 * (barring an attack on the mac and deliberately-crafted input),
3555 * in case the caller doesn't check the return status properly. */
3556 if( status == PSA_SUCCESS )
3557 memset( signature + *signature_length, '!',
3558 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003559 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003560 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003561 /* If signature_size is 0 then we have nothing to do. We must not call
3562 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003563 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003564}
3565
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003566psa_status_t psa_verify_hash( psa_key_handle_t handle,
3567 psa_algorithm_t alg,
3568 const uint8_t *hash,
3569 size_t hash_length,
3570 const uint8_t *signature,
3571 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003572{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003573 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003574 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003575#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3576 const psa_drv_se_t *drv;
3577 psa_drv_se_context_t *drv_context;
3578#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003579
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003580 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003581 if( status != PSA_SUCCESS )
3582 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003583
Gilles Peskineedc64242019-08-07 21:05:07 +02003584#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3585 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3586 {
3587 if( drv->asymmetric == NULL ||
3588 drv->asymmetric->p_verify == NULL )
3589 return( PSA_ERROR_NOT_SUPPORTED );
3590 return( drv->asymmetric->p_verify( drv_context,
3591 slot->data.se.slot_number,
3592 alg,
3593 hash, hash_length,
3594 signature, signature_length ) );
3595 }
3596 else
3597#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003598#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003599 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003601 return( psa_rsa_verify( slot->data.rsa,
3602 alg,
3603 hash, hash_length,
3604 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605 }
3606 else
3607#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003608#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003609 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003610 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003611#if defined(MBEDTLS_ECDSA_C)
3612 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003613 return( psa_ecdsa_verify( slot->data.ecp,
3614 hash, hash_length,
3615 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003616 else
3617#endif /* defined(MBEDTLS_ECDSA_C) */
3618 {
3619 return( PSA_ERROR_INVALID_ARGUMENT );
3620 }
itayzafrir5c753392018-05-08 11:18:38 +03003621 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003622 else
3623#endif /* defined(MBEDTLS_ECP_C) */
3624 {
3625 return( PSA_ERROR_NOT_SUPPORTED );
3626 }
3627}
3628
Gilles Peskine072ac562018-06-30 00:21:29 +02003629#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3630static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3631 mbedtls_rsa_context *rsa )
3632{
3633 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3634 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3635 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3636 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3637}
3638#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3639
Gilles Peskinec5487a82018-12-03 18:08:14 +01003640psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003641 psa_algorithm_t alg,
3642 const uint8_t *input,
3643 size_t input_length,
3644 const uint8_t *salt,
3645 size_t salt_length,
3646 uint8_t *output,
3647 size_t output_size,
3648 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003650 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003651 psa_status_t status;
3652
Darryl Green5cc689a2018-07-24 15:34:10 +01003653 (void) input;
3654 (void) input_length;
3655 (void) salt;
3656 (void) output;
3657 (void) output_size;
3658
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003659 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003661 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3662 return( PSA_ERROR_INVALID_ARGUMENT );
3663
Gilles Peskine28f8f302019-07-24 13:30:31 +02003664 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003665 if( status != PSA_SUCCESS )
3666 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003667 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3668 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670
3671#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003672 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003673 {
3674 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003676 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003677 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003679 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003681 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3682 mbedtls_ctr_drbg_random,
3683 &global_data.ctr_drbg,
3684 MBEDTLS_RSA_PUBLIC,
3685 input_length,
3686 input,
3687 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003688 }
3689 else
3690#endif /* MBEDTLS_PKCS1_V15 */
3691#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003692 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003693 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003694 psa_rsa_oaep_set_padding_mode( alg, rsa );
3695 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3696 mbedtls_ctr_drbg_random,
3697 &global_data.ctr_drbg,
3698 MBEDTLS_RSA_PUBLIC,
3699 salt, salt_length,
3700 input_length,
3701 input,
3702 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703 }
3704 else
3705#endif /* MBEDTLS_PKCS1_V21 */
3706 {
3707 return( PSA_ERROR_INVALID_ARGUMENT );
3708 }
3709 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003710 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711 return( mbedtls_to_psa_error( ret ) );
3712 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003714#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 {
3716 return( PSA_ERROR_NOT_SUPPORTED );
3717 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003718}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719
Gilles Peskinec5487a82018-12-03 18:08:14 +01003720psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003721 psa_algorithm_t alg,
3722 const uint8_t *input,
3723 size_t input_length,
3724 const uint8_t *salt,
3725 size_t salt_length,
3726 uint8_t *output,
3727 size_t output_size,
3728 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003730 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003731 psa_status_t status;
3732
Darryl Green5cc689a2018-07-24 15:34:10 +01003733 (void) input;
3734 (void) input_length;
3735 (void) salt;
3736 (void) output;
3737 (void) output_size;
3738
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003739 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003741 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3742 return( PSA_ERROR_INVALID_ARGUMENT );
3743
Gilles Peskine28f8f302019-07-24 13:30:31 +02003744 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003745 if( status != PSA_SUCCESS )
3746 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003747 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003748 return( PSA_ERROR_INVALID_ARGUMENT );
3749
3750#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003751 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003752 {
3753 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003755
Gilles Peskine630a18a2018-06-29 17:49:35 +02003756 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003757 return( PSA_ERROR_INVALID_ARGUMENT );
3758
3759#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003760 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003761 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003762 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3763 mbedtls_ctr_drbg_random,
3764 &global_data.ctr_drbg,
3765 MBEDTLS_RSA_PRIVATE,
3766 output_length,
3767 input,
3768 output,
3769 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770 }
3771 else
3772#endif /* MBEDTLS_PKCS1_V15 */
3773#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003774 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003776 psa_rsa_oaep_set_padding_mode( alg, rsa );
3777 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3778 mbedtls_ctr_drbg_random,
3779 &global_data.ctr_drbg,
3780 MBEDTLS_RSA_PRIVATE,
3781 salt, salt_length,
3782 output_length,
3783 input,
3784 output,
3785 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003786 }
3787 else
3788#endif /* MBEDTLS_PKCS1_V21 */
3789 {
3790 return( PSA_ERROR_INVALID_ARGUMENT );
3791 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003792
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003793 return( mbedtls_to_psa_error( ret ) );
3794 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003795 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003796#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003797 {
3798 return( PSA_ERROR_NOT_SUPPORTED );
3799 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003800}
Gilles Peskine20035e32018-02-03 22:44:14 +01003801
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003802
3803
mohammad1603503973b2018-03-12 15:59:30 +02003804/****************************************************************/
3805/* Symmetric cryptography */
3806/****************************************************************/
3807
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003808/* Initialize the cipher operation structure. Once this function has been
3809 * called, psa_cipher_abort can run and will do the right thing. */
3810static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3811 psa_algorithm_t alg )
3812{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003813 if( ! PSA_ALG_IS_CIPHER( alg ) )
3814 {
3815 memset( operation, 0, sizeof( *operation ) );
3816 return( PSA_ERROR_INVALID_ARGUMENT );
3817 }
3818
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003819 operation->alg = alg;
3820 operation->key_set = 0;
3821 operation->iv_set = 0;
3822 operation->iv_required = 1;
3823 operation->iv_size = 0;
3824 operation->block_size = 0;
3825 mbedtls_cipher_init( &operation->ctx.cipher );
3826 return( PSA_SUCCESS );
3827}
3828
Gilles Peskinee553c652018-06-04 16:22:46 +02003829static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003830 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003831 psa_algorithm_t alg,
3832 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003833{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003834 int ret = 0;
3835 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003836 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003837 size_t key_bits;
3838 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003839 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3840 PSA_KEY_USAGE_ENCRYPT :
3841 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003842
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003843 /* A context must be freshly initialized before it can be set up. */
3844 if( operation->alg != 0 )
3845 {
3846 return( PSA_ERROR_BAD_STATE );
3847 }
3848
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003849 status = psa_cipher_init( operation, alg );
3850 if( status != PSA_SUCCESS )
3851 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003852
Gilles Peskine28f8f302019-07-24 13:30:31 +02003853 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003854 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003855 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003856 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003857
Gilles Peskine8e338702019-07-30 20:06:31 +02003858 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003859 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003860 {
3861 status = PSA_ERROR_NOT_SUPPORTED;
3862 goto exit;
3863 }
mohammad1603503973b2018-03-12 15:59:30 +02003864
mohammad1603503973b2018-03-12 15:59:30 +02003865 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003866 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003867 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003868
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003869#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003870 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003871 {
3872 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003873 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003874 memcpy( keys, slot->data.raw.data, 16 );
3875 memcpy( keys + 16, slot->data.raw.data, 8 );
3876 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3877 keys,
3878 192, cipher_operation );
3879 }
3880 else
3881#endif
3882 {
3883 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3884 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003885 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003886 }
Moran Peker41deec42018-04-04 15:43:05 +03003887 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003888 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003889
mohammad16038481e742018-03-18 13:57:31 +02003890#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003891 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003892 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003893 case PSA_ALG_CBC_NO_PADDING:
3894 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3895 MBEDTLS_PADDING_NONE );
3896 break;
3897 case PSA_ALG_CBC_PKCS7:
3898 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3899 MBEDTLS_PADDING_PKCS7 );
3900 break;
3901 default:
3902 /* The algorithm doesn't involve padding. */
3903 ret = 0;
3904 break;
3905 }
3906 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003907 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003908#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3909
mohammad1603503973b2018-03-12 15:59:30 +02003910 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003911 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003912 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003913 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003914 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003915 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003916 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003917#if defined(MBEDTLS_CHACHA20_C)
3918 else
3919 if( alg == PSA_ALG_CHACHA20 )
3920 operation->iv_size = 12;
3921#endif
mohammad1603503973b2018-03-12 15:59:30 +02003922
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003923exit:
3924 if( status == 0 )
3925 status = mbedtls_to_psa_error( ret );
3926 if( status != 0 )
3927 psa_cipher_abort( operation );
3928 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003929}
3930
Gilles Peskinefe119512018-07-08 21:39:34 +02003931psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003932 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003933 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003934{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003935 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003936}
3937
Gilles Peskinefe119512018-07-08 21:39:34 +02003938psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003939 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003940 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003941{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003942 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003943}
3944
Gilles Peskinefe119512018-07-08 21:39:34 +02003945psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003946 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003947 size_t iv_size,
3948 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003949{
itayzafrir534bd7c2018-08-02 13:56:32 +03003950 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003952 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003953 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003954 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003955 }
Moran Peker41deec42018-04-04 15:43:05 +03003956 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003957 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003958 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003959 goto exit;
3960 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003961 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3962 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003963 if( ret != 0 )
3964 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003965 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003966 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003967 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003968
mohammad16038481e742018-03-18 13:57:31 +02003969 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003970 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003971
Moran Peker395db872018-05-31 14:07:14 +03003972exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003973 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003974 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003975 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003976}
3977
Gilles Peskinefe119512018-07-08 21:39:34 +02003978psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003979 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003980 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003981{
itayzafrir534bd7c2018-08-02 13:56:32 +03003982 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003984 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003985 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003986 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003987 }
Moran Pekera28258c2018-05-29 16:25:04 +03003988 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003989 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003990 status = PSA_ERROR_INVALID_ARGUMENT;
3991 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003992 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003993 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3994 status = mbedtls_to_psa_error( ret );
3995exit:
3996 if( status == PSA_SUCCESS )
3997 operation->iv_set = 1;
3998 else
mohammad1603503973b2018-03-12 15:59:30 +02003999 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004000 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004001}
4002
Gilles Peskinee553c652018-06-04 16:22:46 +02004003psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4004 const uint8_t *input,
4005 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004006 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004007 size_t output_size,
4008 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004009{
itayzafrir534bd7c2018-08-02 13:56:32 +03004010 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004012 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004013
4014 if( operation->alg == 0 )
4015 {
4016 return( PSA_ERROR_BAD_STATE );
4017 }
4018
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004019 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004020 {
4021 /* Take the unprocessed partial block left over from previous
4022 * update calls, if any, plus the input to this call. Remove
4023 * the last partial block, if any. You get the data that will be
4024 * output in this call. */
4025 expected_output_size =
4026 ( operation->ctx.cipher.unprocessed_len + input_length )
4027 / operation->block_size * operation->block_size;
4028 }
4029 else
4030 {
4031 expected_output_size = input_length;
4032 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004033
Gilles Peskine89d789c2018-06-04 17:17:16 +02004034 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004035 {
4036 status = PSA_ERROR_BUFFER_TOO_SMALL;
4037 goto exit;
4038 }
mohammad160382759612018-03-12 18:16:40 +02004039
mohammad1603503973b2018-03-12 15:59:30 +02004040 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004041 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004042 status = mbedtls_to_psa_error( ret );
4043exit:
4044 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004045 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004046 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004047}
4048
Gilles Peskinee553c652018-06-04 16:22:46 +02004049psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4050 uint8_t *output,
4051 size_t output_size,
4052 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004053{
David Saadab4ecc272019-02-14 13:48:10 +02004054 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004055 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004056 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004057
mohammad1603503973b2018-03-12 15:59:30 +02004058 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004059 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004060 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004061 }
4062 if( operation->iv_required && ! operation->iv_set )
4063 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004064 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004065 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004066
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004067 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004068 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4069 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004070 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004071 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004072 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004073 }
4074
Janos Follath315b51c2018-07-09 16:04:51 +01004075 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4076 temp_output_buffer,
4077 output_length );
4078 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004079 {
Janos Follath315b51c2018-07-09 16:04:51 +01004080 status = mbedtls_to_psa_error( cipher_ret );
4081 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004082 }
Janos Follath315b51c2018-07-09 16:04:51 +01004083
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004084 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004085 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004086 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004087 memcpy( output, temp_output_buffer, *output_length );
4088 else
4089 {
Janos Follath315b51c2018-07-09 16:04:51 +01004090 status = PSA_ERROR_BUFFER_TOO_SMALL;
4091 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004092 }
mohammad1603503973b2018-03-12 15:59:30 +02004093
Gilles Peskine3f108122018-12-07 18:14:53 +01004094 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004095 status = psa_cipher_abort( operation );
4096
4097 return( status );
4098
4099error:
4100
4101 *output_length = 0;
4102
Gilles Peskine3f108122018-12-07 18:14:53 +01004103 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004104 (void) psa_cipher_abort( operation );
4105
4106 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004107}
4108
Gilles Peskinee553c652018-06-04 16:22:46 +02004109psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4110{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004111 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004112 {
4113 /* The object has (apparently) been initialized but it is not
4114 * in use. It's ok to call abort on such an object, and there's
4115 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004116 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004117 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004118
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004119 /* Sanity check (shouldn't happen: operation->alg should
4120 * always have been initialized to a valid value). */
4121 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4122 return( PSA_ERROR_BAD_STATE );
4123
mohammad1603503973b2018-03-12 15:59:30 +02004124 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004125
Moran Peker41deec42018-04-04 15:43:05 +03004126 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004127 operation->key_set = 0;
4128 operation->iv_set = 0;
4129 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004130 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004131 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004132
Moran Peker395db872018-05-31 14:07:14 +03004133 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004134}
4135
Gilles Peskinea0655c32018-04-30 17:06:50 +02004136
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004137
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004138
mohammad16035955c982018-04-26 00:53:03 +03004139/****************************************************************/
4140/* AEAD */
4141/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004142
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143typedef struct
4144{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004145 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004146 const mbedtls_cipher_info_t *cipher_info;
4147 union
4148 {
4149#if defined(MBEDTLS_CCM_C)
4150 mbedtls_ccm_context ccm;
4151#endif /* MBEDTLS_CCM_C */
4152#if defined(MBEDTLS_GCM_C)
4153 mbedtls_gcm_context gcm;
4154#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004155#if defined(MBEDTLS_CHACHAPOLY_C)
4156 mbedtls_chachapoly_context chachapoly;
4157#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004158 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004159 psa_algorithm_t core_alg;
4160 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 uint8_t tag_length;
4162} aead_operation_t;
4163
Gilles Peskine30a9e412019-01-14 18:36:12 +01004164static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004165{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004166 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 {
4168#if defined(MBEDTLS_CCM_C)
4169 case PSA_ALG_CCM:
4170 mbedtls_ccm_free( &operation->ctx.ccm );
4171 break;
4172#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004173#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004174 case PSA_ALG_GCM:
4175 mbedtls_gcm_free( &operation->ctx.gcm );
4176 break;
4177#endif /* MBEDTLS_GCM_C */
4178 }
4179}
4180
4181static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004182 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004183 psa_key_usage_t usage,
4184 psa_algorithm_t alg )
4185{
4186 psa_status_t status;
4187 size_t key_bits;
4188 mbedtls_cipher_id_t cipher_id;
4189
Gilles Peskine28f8f302019-07-24 13:30:31 +02004190 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004191 if( status != PSA_SUCCESS )
4192 return( status );
4193
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004194 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004195
4196 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004197 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004198 &cipher_id );
4199 if( operation->cipher_info == NULL )
4200 return( PSA_ERROR_NOT_SUPPORTED );
4201
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004202 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004203 {
4204#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004205 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4206 operation->core_alg = PSA_ALG_CCM;
4207 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004208 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4209 * The call to mbedtls_ccm_encrypt_and_tag or
4210 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004211 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212 return( PSA_ERROR_INVALID_ARGUMENT );
4213 mbedtls_ccm_init( &operation->ctx.ccm );
4214 status = mbedtls_to_psa_error(
4215 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4216 operation->slot->data.raw.data,
4217 (unsigned int) key_bits ) );
4218 if( status != 0 )
4219 goto cleanup;
4220 break;
4221#endif /* MBEDTLS_CCM_C */
4222
4223#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004224 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4225 operation->core_alg = PSA_ALG_GCM;
4226 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004227 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4228 * The call to mbedtls_gcm_crypt_and_tag or
4229 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004230 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004231 return( PSA_ERROR_INVALID_ARGUMENT );
4232 mbedtls_gcm_init( &operation->ctx.gcm );
4233 status = mbedtls_to_psa_error(
4234 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4235 operation->slot->data.raw.data,
4236 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004237 if( status != 0 )
4238 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004239 break;
4240#endif /* MBEDTLS_GCM_C */
4241
Gilles Peskine26869f22019-05-06 15:25:00 +02004242#if defined(MBEDTLS_CHACHAPOLY_C)
4243 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4244 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4245 operation->full_tag_length = 16;
4246 /* We only support the default tag length. */
4247 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4248 return( PSA_ERROR_NOT_SUPPORTED );
4249 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4250 status = mbedtls_to_psa_error(
4251 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4252 operation->slot->data.raw.data ) );
4253 if( status != 0 )
4254 goto cleanup;
4255 break;
4256#endif /* MBEDTLS_CHACHAPOLY_C */
4257
Gilles Peskineedf9a652018-08-17 18:11:56 +02004258 default:
4259 return( PSA_ERROR_NOT_SUPPORTED );
4260 }
4261
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004262 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4263 {
4264 status = PSA_ERROR_INVALID_ARGUMENT;
4265 goto cleanup;
4266 }
4267 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004268
Gilles Peskineedf9a652018-08-17 18:11:56 +02004269 return( PSA_SUCCESS );
4270
4271cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004272 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004273 return( status );
4274}
4275
Gilles Peskinec5487a82018-12-03 18:08:14 +01004276psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004277 psa_algorithm_t alg,
4278 const uint8_t *nonce,
4279 size_t nonce_length,
4280 const uint8_t *additional_data,
4281 size_t additional_data_length,
4282 const uint8_t *plaintext,
4283 size_t plaintext_length,
4284 uint8_t *ciphertext,
4285 size_t ciphertext_size,
4286 size_t *ciphertext_length )
4287{
mohammad16035955c982018-04-26 00:53:03 +03004288 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004289 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004290 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004291
mohammad1603f08a5502018-06-03 15:05:47 +03004292 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004293
Gilles Peskinec5487a82018-12-03 18:08:14 +01004294 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004295 if( status != PSA_SUCCESS )
4296 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004297
Gilles Peskineedf9a652018-08-17 18:11:56 +02004298 /* For all currently supported modes, the tag is at the end of the
4299 * ciphertext. */
4300 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4301 {
4302 status = PSA_ERROR_BUFFER_TOO_SMALL;
4303 goto exit;
4304 }
4305 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004306
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004307#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004308 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004309 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004310 status = mbedtls_to_psa_error(
4311 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4312 MBEDTLS_GCM_ENCRYPT,
4313 plaintext_length,
4314 nonce, nonce_length,
4315 additional_data, additional_data_length,
4316 plaintext, ciphertext,
4317 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004318 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004319 else
4320#endif /* MBEDTLS_GCM_C */
4321#if defined(MBEDTLS_CCM_C)
4322 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004323 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004324 status = mbedtls_to_psa_error(
4325 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4326 plaintext_length,
4327 nonce, nonce_length,
4328 additional_data,
4329 additional_data_length,
4330 plaintext, ciphertext,
4331 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004332 }
mohammad16035c8845f2018-05-09 05:40:09 -07004333 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004334#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004335#if defined(MBEDTLS_CHACHAPOLY_C)
4336 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4337 {
4338 if( nonce_length != 12 || operation.tag_length != 16 )
4339 {
4340 status = PSA_ERROR_NOT_SUPPORTED;
4341 goto exit;
4342 }
4343 status = mbedtls_to_psa_error(
4344 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4345 plaintext_length,
4346 nonce,
4347 additional_data,
4348 additional_data_length,
4349 plaintext,
4350 ciphertext,
4351 tag ) );
4352 }
4353 else
4354#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004355 {
mohammad1603554faad2018-06-03 15:07:38 +03004356 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004357 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004358
Gilles Peskineedf9a652018-08-17 18:11:56 +02004359 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4360 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004361
Gilles Peskineedf9a652018-08-17 18:11:56 +02004362exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004363 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004364 if( status == PSA_SUCCESS )
4365 *ciphertext_length = plaintext_length + operation.tag_length;
4366 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004367}
4368
Gilles Peskineee652a32018-06-01 19:23:52 +02004369/* Locate the tag in a ciphertext buffer containing the encrypted data
4370 * followed by the tag. Return the length of the part preceding the tag in
4371 * *plaintext_length. This is the size of the plaintext in modes where
4372 * the encrypted data has the same size as the plaintext, such as
4373 * CCM and GCM. */
4374static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4375 const uint8_t *ciphertext,
4376 size_t ciphertext_length,
4377 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004378 const uint8_t **p_tag )
4379{
4380 size_t payload_length;
4381 if( tag_length > ciphertext_length )
4382 return( PSA_ERROR_INVALID_ARGUMENT );
4383 payload_length = ciphertext_length - tag_length;
4384 if( payload_length > plaintext_size )
4385 return( PSA_ERROR_BUFFER_TOO_SMALL );
4386 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004387 return( PSA_SUCCESS );
4388}
4389
Gilles Peskinec5487a82018-12-03 18:08:14 +01004390psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004391 psa_algorithm_t alg,
4392 const uint8_t *nonce,
4393 size_t nonce_length,
4394 const uint8_t *additional_data,
4395 size_t additional_data_length,
4396 const uint8_t *ciphertext,
4397 size_t ciphertext_length,
4398 uint8_t *plaintext,
4399 size_t plaintext_size,
4400 size_t *plaintext_length )
4401{
mohammad16035955c982018-04-26 00:53:03 +03004402 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004403 aead_operation_t operation;
4404 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004405
Gilles Peskineee652a32018-06-01 19:23:52 +02004406 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004407
Gilles Peskinec5487a82018-12-03 18:08:14 +01004408 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004409 if( status != PSA_SUCCESS )
4410 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004411
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004412 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4413 ciphertext, ciphertext_length,
4414 plaintext_size, &tag );
4415 if( status != PSA_SUCCESS )
4416 goto exit;
4417
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004418#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004419 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004420 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004421 status = mbedtls_to_psa_error(
4422 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4423 ciphertext_length - operation.tag_length,
4424 nonce, nonce_length,
4425 additional_data,
4426 additional_data_length,
4427 tag, operation.tag_length,
4428 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004429 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004430 else
4431#endif /* MBEDTLS_GCM_C */
4432#if defined(MBEDTLS_CCM_C)
4433 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004434 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004435 status = mbedtls_to_psa_error(
4436 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4437 ciphertext_length - operation.tag_length,
4438 nonce, nonce_length,
4439 additional_data,
4440 additional_data_length,
4441 ciphertext, plaintext,
4442 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004443 }
mohammad160339574652018-06-01 04:39:53 -07004444 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004445#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004446#if defined(MBEDTLS_CHACHAPOLY_C)
4447 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4448 {
4449 if( nonce_length != 12 || operation.tag_length != 16 )
4450 {
4451 status = PSA_ERROR_NOT_SUPPORTED;
4452 goto exit;
4453 }
4454 status = mbedtls_to_psa_error(
4455 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4456 ciphertext_length - operation.tag_length,
4457 nonce,
4458 additional_data,
4459 additional_data_length,
4460 tag,
4461 ciphertext,
4462 plaintext ) );
4463 }
4464 else
4465#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004466 {
mohammad1603554faad2018-06-03 15:07:38 +03004467 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004468 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004469
Gilles Peskineedf9a652018-08-17 18:11:56 +02004470 if( status != PSA_SUCCESS && plaintext_size != 0 )
4471 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004472
Gilles Peskineedf9a652018-08-17 18:11:56 +02004473exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004474 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004475 if( status == PSA_SUCCESS )
4476 *plaintext_length = ciphertext_length - operation.tag_length;
4477 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004478}
4479
Gilles Peskinea0655c32018-04-30 17:06:50 +02004480
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004481
Gilles Peskine20035e32018-02-03 22:44:14 +01004482/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004483/* Generators */
4484/****************************************************************/
4485
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004486#define HKDF_STATE_INIT 0 /* no input yet */
4487#define HKDF_STATE_STARTED 1 /* got salt */
4488#define HKDF_STATE_KEYED 2 /* got key */
4489#define HKDF_STATE_OUTPUT 3 /* output started */
4490
Gilles Peskinecbe66502019-05-16 16:59:18 +02004491static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004493{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4495 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004496 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004498}
4499
4500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004502{
4503 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004505 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004506 {
4507 /* The object has (apparently) been initialized but it is not
4508 * in use. It's ok to call abort on such an object, and there's
4509 * nothing to do. */
4510 }
4511 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004512#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004513 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004514 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004515 mbedtls_free( operation->ctx.hkdf.info );
4516 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004517 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004518 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004520 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004521 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004522 if( operation->ctx.tls12_prf.seed != NULL )
4523 {
4524 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4525 operation->ctx.tls12_prf.seed_length );
4526 mbedtls_free( operation->ctx.tls12_prf.seed );
4527 }
4528
4529 if( operation->ctx.tls12_prf.label != NULL )
4530 {
4531 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4532 operation->ctx.tls12_prf.label_length );
4533 mbedtls_free( operation->ctx.tls12_prf.label );
4534 }
4535
4536 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4537
4538 /* We leave the fields Ai and output_block to be erased safely by the
4539 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004540 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004541 else
4542#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004543 {
4544 status = PSA_ERROR_BAD_STATE;
4545 }
Janos Follath083036a2019-06-11 10:22:26 +01004546 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004547 return( status );
4548}
4549
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004551 size_t *capacity)
4552{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004554 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004556 return PSA_ERROR_BAD_STATE;
4557 }
4558
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004560 return( PSA_SUCCESS );
4561}
4562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004564 size_t capacity )
4565{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004567 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004568 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004569 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004571 return( PSA_SUCCESS );
4572}
4573
Gilles Peskinebef7f142018-07-12 17:22:21 +02004574#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004576 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004577static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004578 psa_algorithm_t hash_alg,
4579 uint8_t *output,
4580 size_t output_length )
4581{
4582 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4583 psa_status_t status;
4584
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004585 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4586 return( PSA_ERROR_BAD_STATE );
4587 hkdf->state = HKDF_STATE_OUTPUT;
4588
Gilles Peskinebef7f142018-07-12 17:22:21 +02004589 while( output_length != 0 )
4590 {
4591 /* Copy what remains of the current block */
4592 uint8_t n = hash_length - hkdf->offset_in_block;
4593 if( n > output_length )
4594 n = (uint8_t) output_length;
4595 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4596 output += n;
4597 output_length -= n;
4598 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004599 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004600 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004601 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004602 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004604 * object was corrupted or if this function is called directly
4605 * inside the library. */
4606 if( hkdf->block_number == 0xff )
4607 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004608
4609 /* We need a new block */
4610 ++hkdf->block_number;
4611 hkdf->offset_in_block = 0;
4612 status = psa_hmac_setup_internal( &hkdf->hmac,
4613 hkdf->prk, hash_length,
4614 hash_alg );
4615 if( status != PSA_SUCCESS )
4616 return( status );
4617 if( hkdf->block_number != 1 )
4618 {
4619 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4620 hkdf->output_block,
4621 hash_length );
4622 if( status != PSA_SUCCESS )
4623 return( status );
4624 }
4625 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4626 hkdf->info,
4627 hkdf->info_length );
4628 if( status != PSA_SUCCESS )
4629 return( status );
4630 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4631 &hkdf->block_number, 1 );
4632 if( status != PSA_SUCCESS )
4633 return( status );
4634 status = psa_hmac_finish_internal( &hkdf->hmac,
4635 hkdf->output_block,
4636 sizeof( hkdf->output_block ) );
4637 if( status != PSA_SUCCESS )
4638 return( status );
4639 }
4640
4641 return( PSA_SUCCESS );
4642}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004643
Janos Follath7742fee2019-06-17 12:58:10 +01004644static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4645 psa_tls12_prf_key_derivation_t *tls12_prf,
4646 psa_algorithm_t alg )
4647{
4648 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4649 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004650 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4651 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004652
Janos Follath7742fee2019-06-17 12:58:10 +01004653 /* We can't be wanting more output after block 0xff, otherwise
4654 * the capacity check in psa_key_derivation_output_bytes() would have
4655 * prevented this call. It could happen only if the operation
4656 * object was corrupted or if this function is called directly
4657 * inside the library. */
4658 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004659 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004660
4661 /* We need a new block */
4662 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004663 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004664
4665 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4666 *
4667 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4668 *
4669 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4670 * HMAC_hash(secret, A(2) + seed) +
4671 * HMAC_hash(secret, A(3) + seed) + ...
4672 *
4673 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004674 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004675 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004676 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004677 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004678 * is currently extracted as `output_block` and where i is
4679 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004680 */
4681
Janos Follathea29bfb2019-06-19 12:21:20 +01004682 /* Save the hash context before using it, to preserve the hash state with
4683 * only the inner padding in it. We need this, because inner padding depends
4684 * on the key (secret in the RFC's terminology). */
4685 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4686 if( status != PSA_SUCCESS )
4687 goto cleanup;
4688
4689 /* Calculate A(i) where i = tls12_prf->block_number. */
4690 if( tls12_prf->block_number == 1 )
4691 {
4692 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4693 * the variable seed and in this instance means it in the context of the
4694 * P_hash function, where seed = label + seed.) */
4695 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4696 tls12_prf->label, tls12_prf->label_length );
4697 if( status != PSA_SUCCESS )
4698 goto cleanup;
4699 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4700 tls12_prf->seed, tls12_prf->seed_length );
4701 if( status != PSA_SUCCESS )
4702 goto cleanup;
4703 }
4704 else
4705 {
4706 /* A(i) = HMAC_hash(secret, A(i-1)) */
4707 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4708 tls12_prf->Ai, hash_length );
4709 if( status != PSA_SUCCESS )
4710 goto cleanup;
4711 }
4712
4713 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4714 tls12_prf->Ai, hash_length );
4715 if( status != PSA_SUCCESS )
4716 goto cleanup;
4717 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4718 if( status != PSA_SUCCESS )
4719 goto cleanup;
4720
4721 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4722 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4723 tls12_prf->Ai, hash_length );
4724 if( status != PSA_SUCCESS )
4725 goto cleanup;
4726 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4727 tls12_prf->label, tls12_prf->label_length );
4728 if( status != PSA_SUCCESS )
4729 goto cleanup;
4730 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4731 tls12_prf->seed, tls12_prf->seed_length );
4732 if( status != PSA_SUCCESS )
4733 goto cleanup;
4734 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4735 tls12_prf->output_block, hash_length );
4736 if( status != PSA_SUCCESS )
4737 goto cleanup;
4738 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4739 if( status != PSA_SUCCESS )
4740 goto cleanup;
4741
Janos Follath7742fee2019-06-17 12:58:10 +01004742
4743cleanup:
4744
Janos Follathea29bfb2019-06-19 12:21:20 +01004745 cleanup_status = psa_hash_abort( &backup );
4746 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4747 status = cleanup_status;
4748
4749 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004750}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004751
Janos Follath844eb0e2019-06-19 12:10:49 +01004752static psa_status_t psa_key_derivation_tls12_prf_read(
4753 psa_tls12_prf_key_derivation_t *tls12_prf,
4754 psa_algorithm_t alg,
4755 uint8_t *output,
4756 size_t output_length )
4757{
4758 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4759 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4760 psa_status_t status;
4761 uint8_t offset, length;
4762
4763 while( output_length != 0 )
4764 {
4765 /* Check if we have fully processed the current block. */
4766 if( tls12_prf->left_in_block == 0 )
4767 {
4768 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4769 alg );
4770 if( status != PSA_SUCCESS )
4771 return( status );
4772
4773 continue;
4774 }
4775
4776 if( tls12_prf->left_in_block > output_length )
4777 length = (uint8_t) output_length;
4778 else
4779 length = tls12_prf->left_in_block;
4780
4781 offset = hash_length - tls12_prf->left_in_block;
4782 memcpy( output, tls12_prf->output_block + offset, length );
4783 output += length;
4784 output_length -= length;
4785 tls12_prf->left_in_block -= length;
4786 }
4787
4788 return( PSA_SUCCESS );
4789}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004790#endif /* MBEDTLS_MD_C */
4791
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004792psa_status_t psa_key_derivation_output_bytes(
4793 psa_key_derivation_operation_t *operation,
4794 uint8_t *output,
4795 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004796{
4797 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004799
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004801 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004803 return PSA_ERROR_BAD_STATE;
4804 }
4805
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004807 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004811 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 goto exit;
4813 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004817 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004818 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4819 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004821 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004822 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004823 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004825
Gilles Peskinebef7f142018-07-12 17:22:21 +02004826#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004827 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004828 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004831 output, output_length );
4832 }
Janos Follathadbec812019-06-14 11:05:39 +01004833 else
Janos Follathadbec812019-06-14 11:05:39 +01004834 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004835 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004836 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004838 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004839 output_length );
4840 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004841 else
4842#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004843 {
4844 return( PSA_ERROR_BAD_STATE );
4845 }
4846
4847exit:
4848 if( status != PSA_SUCCESS )
4849 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004850 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 * This allows us to differentiate between exhausted operations and
4852 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4853 * operations. */
4854 psa_algorithm_t alg = operation->alg;
4855 psa_key_derivation_abort( operation );
4856 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004857 memset( output, '!', output_length );
4858 }
4859 return( status );
4860}
4861
Gilles Peskine08542d82018-07-19 17:05:42 +02004862#if defined(MBEDTLS_DES_C)
4863static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4864{
4865 if( data_size >= 8 )
4866 mbedtls_des_key_set_parity( data );
4867 if( data_size >= 16 )
4868 mbedtls_des_key_set_parity( data + 8 );
4869 if( data_size >= 24 )
4870 mbedtls_des_key_set_parity( data + 16 );
4871}
4872#endif /* MBEDTLS_DES_C */
4873
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004874static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 psa_key_slot_t *slot,
4876 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004878{
4879 uint8_t *data = NULL;
4880 size_t bytes = PSA_BITS_TO_BYTES( bits );
4881 psa_status_t status;
4882
Gilles Peskine8e338702019-07-30 20:06:31 +02004883 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004884 return( PSA_ERROR_INVALID_ARGUMENT );
4885 if( bits % 8 != 0 )
4886 return( PSA_ERROR_INVALID_ARGUMENT );
4887 data = mbedtls_calloc( 1, bytes );
4888 if( data == NULL )
4889 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4890
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004891 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004892 if( status != PSA_SUCCESS )
4893 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004894#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004895 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004896 psa_des_set_key_parity( data, bytes );
4897#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004898 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004899
4900exit:
4901 mbedtls_free( data );
4902 return( status );
4903}
4904
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004905psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004907 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004908{
4909 psa_status_t status;
4910 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004911 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004912
4913 /* Reject any attempt to create a zero-length key so that we don't
4914 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4915 if( psa_get_key_bits( attributes ) == 0 )
4916 return( PSA_ERROR_INVALID_ARGUMENT );
4917
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004918 if( ! operation->can_output_key )
4919 return( PSA_ERROR_NOT_PERMITTED );
4920
Gilles Peskinedf179142019-07-15 22:02:14 +02004921 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4922 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004923#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4924 if( driver != NULL )
4925 {
4926 /* Deriving a key in a secure element is not implemented yet. */
4927 status = PSA_ERROR_NOT_SUPPORTED;
4928 }
4929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004930 if( status == PSA_SUCCESS )
4931 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004932 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004933 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004935 }
4936 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004937 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004938 if( status != PSA_SUCCESS )
4939 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004940 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004941 *handle = 0;
4942 }
4943 return( status );
4944}
4945
Gilles Peskineeab56e42018-07-12 17:12:33 +02004946
4947
4948/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004949/* Key derivation */
4950/****************************************************************/
4951
Gilles Peskine969c5d62019-01-16 15:53:06 +01004952static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955{
Janos Follath5fe19732019-06-20 15:09:30 +01004956 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4957 * initialisers for this union leave some bytes unspecified.) */
4958 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4959
Gilles Peskine969c5d62019-01-16 15:53:06 +01004960 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004961#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4963 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4964 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004966 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4968 if( hash_size == 0 )
4969 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004970 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4971 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004972 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973 {
4974 return( PSA_ERROR_NOT_SUPPORTED );
4975 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004977 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004978 }
4979#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004980 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004981 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004982}
4983
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004985 psa_algorithm_t alg )
4986{
4987 psa_status_t status;
4988
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004990 return( PSA_ERROR_BAD_STATE );
4991
Gilles Peskine6843c292019-01-18 16:44:49 +01004992 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4993 return( PSA_ERROR_INVALID_ARGUMENT );
4994 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995 {
4996 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004998 }
4999 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5000 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005002 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005003 else
5004 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005005
5006 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005008 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005009}
5010
5011#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005012static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005013 psa_algorithm_t hash_alg,
5014 psa_key_derivation_step_t step,
5015 const uint8_t *data,
5016 size_t data_length )
5017{
5018 psa_status_t status;
5019 switch( step )
5020 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005021 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005022 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005024 status = psa_hmac_setup_internal( &hkdf->hmac,
5025 data, data_length,
5026 hash_alg );
5027 if( status != PSA_SUCCESS )
5028 return( status );
5029 hkdf->state = HKDF_STATE_STARTED;
5030 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005031 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 /* If no salt was provided, use an empty salt. */
5033 if( hkdf->state == HKDF_STATE_INIT )
5034 {
5035 status = psa_hmac_setup_internal( &hkdf->hmac,
5036 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005037 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038 if( status != PSA_SUCCESS )
5039 return( status );
5040 hkdf->state = HKDF_STATE_STARTED;
5041 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005042 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005043 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005044 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5045 data, data_length );
5046 if( status != PSA_SUCCESS )
5047 return( status );
5048 status = psa_hmac_finish_internal( &hkdf->hmac,
5049 hkdf->prk,
5050 sizeof( hkdf->prk ) );
5051 if( status != PSA_SUCCESS )
5052 return( status );
5053 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5054 hkdf->block_number = 0;
5055 hkdf->state = HKDF_STATE_KEYED;
5056 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005057 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005058 if( hkdf->state == HKDF_STATE_OUTPUT )
5059 return( PSA_ERROR_BAD_STATE );
5060 if( hkdf->info_set )
5061 return( PSA_ERROR_BAD_STATE );
5062 hkdf->info_length = data_length;
5063 if( data_length != 0 )
5064 {
5065 hkdf->info = mbedtls_calloc( 1, data_length );
5066 if( hkdf->info == NULL )
5067 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5068 memcpy( hkdf->info, data, data_length );
5069 }
5070 hkdf->info_set = 1;
5071 return( PSA_SUCCESS );
5072 default:
5073 return( PSA_ERROR_INVALID_ARGUMENT );
5074 }
5075}
Janos Follathb03233e2019-06-11 15:30:30 +01005076
Janos Follathf08e2652019-06-13 09:05:41 +01005077static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5078 const uint8_t *data,
5079 size_t data_length )
5080{
5081 if( prf->state != TLS12_PRF_STATE_INIT )
5082 return( PSA_ERROR_BAD_STATE );
5083
Janos Follathd6dce9f2019-07-04 09:11:38 +01005084 if( data_length != 0 )
5085 {
5086 prf->seed = mbedtls_calloc( 1, data_length );
5087 if( prf->seed == NULL )
5088 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005089
Janos Follathd6dce9f2019-07-04 09:11:38 +01005090 memcpy( prf->seed, data, data_length );
5091 prf->seed_length = data_length;
5092 }
Janos Follathf08e2652019-06-13 09:05:41 +01005093
5094 prf->state = TLS12_PRF_STATE_SEED_SET;
5095
5096 return( PSA_SUCCESS );
5097}
5098
Janos Follath81550542019-06-13 14:26:34 +01005099static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5100 psa_algorithm_t hash_alg,
5101 const uint8_t *data,
5102 size_t data_length )
5103{
5104 psa_status_t status;
5105 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5106 return( PSA_ERROR_BAD_STATE );
5107
5108 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5109 if( status != PSA_SUCCESS )
5110 return( status );
5111
5112 prf->state = TLS12_PRF_STATE_KEY_SET;
5113
5114 return( PSA_SUCCESS );
5115}
5116
Janos Follath6660f0e2019-06-17 08:44:03 +01005117static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5118 psa_tls12_prf_key_derivation_t *prf,
5119 psa_algorithm_t hash_alg,
5120 const uint8_t *data,
5121 size_t data_length )
5122{
5123 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005124 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5125 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005126
5127 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5128 return( PSA_ERROR_INVALID_ARGUMENT );
5129
5130 /* Quoting RFC 4279, Section 2:
5131 *
5132 * The premaster secret is formed as follows: if the PSK is N octets
5133 * long, concatenate a uint16 with the value N, N zero octets, a second
5134 * uint16 with the value N, and the PSK itself.
5135 */
5136
Janos Follath40e13932019-06-26 13:22:29 +01005137 *cur++ = ( data_length >> 8 ) & 0xff;
5138 *cur++ = ( data_length >> 0 ) & 0xff;
5139 memset( cur, 0, data_length );
5140 cur += data_length;
5141 *cur++ = pms[0];
5142 *cur++ = pms[1];
5143 memcpy( cur, data, data_length );
5144 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005145
Janos Follath40e13932019-06-26 13:22:29 +01005146 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005147
5148 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5149 return( status );
5150}
5151
Janos Follath63028dd2019-06-13 09:15:47 +01005152static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5153 const uint8_t *data,
5154 size_t data_length )
5155{
5156 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5157 return( PSA_ERROR_BAD_STATE );
5158
Janos Follathd6dce9f2019-07-04 09:11:38 +01005159 if( data_length != 0 )
5160 {
5161 prf->label = mbedtls_calloc( 1, data_length );
5162 if( prf->label == NULL )
5163 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005164
Janos Follathd6dce9f2019-07-04 09:11:38 +01005165 memcpy( prf->label, data, data_length );
5166 prf->label_length = data_length;
5167 }
Janos Follath63028dd2019-06-13 09:15:47 +01005168
5169 prf->state = TLS12_PRF_STATE_LABEL_SET;
5170
5171 return( PSA_SUCCESS );
5172}
5173
Janos Follathb03233e2019-06-11 15:30:30 +01005174static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5175 psa_algorithm_t hash_alg,
5176 psa_key_derivation_step_t step,
5177 const uint8_t *data,
5178 size_t data_length )
5179{
Janos Follathb03233e2019-06-11 15:30:30 +01005180 switch( step )
5181 {
Janos Follathf08e2652019-06-13 09:05:41 +01005182 case PSA_KEY_DERIVATION_INPUT_SEED:
5183 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005184 case PSA_KEY_DERIVATION_INPUT_SECRET:
5185 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005186 case PSA_KEY_DERIVATION_INPUT_LABEL:
5187 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005188 default:
5189 return( PSA_ERROR_INVALID_ARGUMENT );
5190 }
5191}
Janos Follath6660f0e2019-06-17 08:44:03 +01005192
5193static psa_status_t psa_tls12_prf_psk_to_ms_input(
5194 psa_tls12_prf_key_derivation_t *prf,
5195 psa_algorithm_t hash_alg,
5196 psa_key_derivation_step_t step,
5197 const uint8_t *data,
5198 size_t data_length )
5199{
5200 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005201 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005202 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5203 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005204 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005205
5206 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5207}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208#endif /* MBEDTLS_MD_C */
5209
Gilles Peskineb8965192019-09-24 16:21:10 +02005210/** Check whether the given key type is acceptable for the given
5211 * input step of a key derivation.
5212 *
5213 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5214 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5215 * Both secret and non-secret inputs can alternatively have the type
5216 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5217 * that the input was passed as a buffer rather than via a key object.
5218 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005219static int psa_key_derivation_check_input_type(
5220 psa_key_derivation_step_t step,
5221 psa_key_type_t key_type )
5222{
5223 switch( step )
5224 {
5225 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005226 if( key_type == PSA_KEY_TYPE_DERIVE )
5227 return( PSA_SUCCESS );
5228 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005229 return( PSA_SUCCESS );
5230 break;
5231 case PSA_KEY_DERIVATION_INPUT_LABEL:
5232 case PSA_KEY_DERIVATION_INPUT_SALT:
5233 case PSA_KEY_DERIVATION_INPUT_INFO:
5234 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005235 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5236 return( PSA_SUCCESS );
5237 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005238 return( PSA_SUCCESS );
5239 break;
5240 }
5241 return( PSA_ERROR_INVALID_ARGUMENT );
5242}
5243
Janos Follathb80a94e2019-06-12 15:54:46 +01005244static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005246 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005247 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005248 const uint8_t *data,
5249 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005251 psa_status_t status;
5252 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5253
5254 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005255 if( status != PSA_SUCCESS )
5256 goto exit;
5257
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005258#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005259 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005260 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005262 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005263 step, data, data_length );
5264 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005265 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005266 {
Janos Follathb03233e2019-06-11 15:30:30 +01005267 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5268 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5269 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005270 }
5271 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5272 {
5273 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5274 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5275 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276 }
5277 else
5278#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 return( PSA_ERROR_BAD_STATE );
5282 }
5283
Gilles Peskine224b0d62019-09-23 18:13:17 +02005284exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005285 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 return( status );
5288}
5289
Janos Follath51f4a0f2019-06-14 11:35:55 +01005290psa_status_t psa_key_derivation_input_bytes(
5291 psa_key_derivation_operation_t *operation,
5292 psa_key_derivation_step_t step,
5293 const uint8_t *data,
5294 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005295{
Gilles Peskineb8965192019-09-24 16:21:10 +02005296 return( psa_key_derivation_input_internal( operation, step,
5297 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005298 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005299}
5300
Janos Follath51f4a0f2019-06-14 11:35:55 +01005301psa_status_t psa_key_derivation_input_key(
5302 psa_key_derivation_operation_t *operation,
5303 psa_key_derivation_step_t step,
5304 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305{
5306 psa_key_slot_t *slot;
5307 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005308
Gilles Peskine28f8f302019-07-24 13:30:31 +02005309 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005310 PSA_KEY_USAGE_DERIVE,
5311 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005312 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005313 {
5314 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005315 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005316 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005317
5318 /* Passing a key object as a SECRET input unlocks the permission
5319 * to output to a key object. */
5320 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5321 operation->can_output_key = 1;
5322
Janos Follathb80a94e2019-06-12 15:54:46 +01005323 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005324 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005325 slot->data.raw.data,
5326 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005328
5329
5330
5331/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005332/* Key agreement */
5333/****************************************************************/
5334
Gilles Peskinea05219c2018-11-16 16:02:56 +01005335#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005336static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5337 size_t peer_key_length,
5338 const mbedtls_ecp_keypair *our_key,
5339 uint8_t *shared_secret,
5340 size_t shared_secret_size,
5341 size_t *shared_secret_length )
5342{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005343 mbedtls_ecp_keypair *their_key = NULL;
5344 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005345 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005346 size_t bits = 0;
5347 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005348 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005349
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005350 status = psa_import_ec_public_key( curve,
5351 peer_key, peer_key_length,
5352 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005353 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005354 goto exit;
5355
Jaeden Amero97271b32019-01-10 19:38:51 +00005356 status = mbedtls_to_psa_error(
5357 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5358 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005359 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005360 status = mbedtls_to_psa_error(
5361 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5362 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005363 goto exit;
5364
Jaeden Amero97271b32019-01-10 19:38:51 +00005365 status = mbedtls_to_psa_error(
5366 mbedtls_ecdh_calc_secret( &ecdh,
5367 shared_secret_length,
5368 shared_secret, shared_secret_size,
5369 mbedtls_ctr_drbg_random,
5370 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005371 if( status != PSA_SUCCESS )
5372 goto exit;
5373 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5374 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005375
5376exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005377 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005378 mbedtls_ecp_keypair_free( their_key );
5379 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005380 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005381}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005382#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005383
Gilles Peskine01d718c2018-09-18 12:01:02 +02005384#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5385
Gilles Peskine0216fe12019-04-11 21:23:21 +02005386static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5387 psa_key_slot_t *private_key,
5388 const uint8_t *peer_key,
5389 size_t peer_key_length,
5390 uint8_t *shared_secret,
5391 size_t shared_secret_size,
5392 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005393{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005394 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005396#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005397 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005398 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005399 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005400 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5401 private_key->data.ecp,
5402 shared_secret, shared_secret_size,
5403 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005404#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005405 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005406 (void) private_key;
5407 (void) peer_key;
5408 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005409 (void) shared_secret;
5410 (void) shared_secret_size;
5411 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005412 return( PSA_ERROR_NOT_SUPPORTED );
5413 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005414}
5415
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005416/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005417 * to potentially free embedded data structures and wipe confidential data.
5418 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005420 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005421 psa_key_slot_t *private_key,
5422 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005424{
5425 psa_status_t status;
5426 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5427 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005429
5430 /* Step 1: run the secret agreement algorithm to generate the shared
5431 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005432 status = psa_key_agreement_raw_internal( ka_alg,
5433 private_key,
5434 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005435 shared_secret,
5436 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005437 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005438 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005439 goto exit;
5440
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005441 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005442 * the shared secret. A shared secret is permitted wherever a key
5443 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005444 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005445 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005446 shared_secret,
5447 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005448
Gilles Peskine12313cd2018-06-20 00:20:32 +02005449exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005450 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005451 return( status );
5452}
5453
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005455 psa_key_derivation_step_t step,
5456 psa_key_handle_t private_key,
5457 const uint8_t *peer_key,
5458 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005459{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005460 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005461 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005463 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005464 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005465 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005466 if( status != PSA_SUCCESS )
5467 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005469 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005470 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005471 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005473 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005474}
5475
Gilles Peskinebe697d82019-05-16 18:00:41 +02005476psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5477 psa_key_handle_t private_key,
5478 const uint8_t *peer_key,
5479 size_t peer_key_length,
5480 uint8_t *output,
5481 size_t output_size,
5482 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005483{
5484 psa_key_slot_t *slot;
5485 psa_status_t status;
5486
5487 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5488 {
5489 status = PSA_ERROR_INVALID_ARGUMENT;
5490 goto exit;
5491 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005492 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005493 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005494 if( status != PSA_SUCCESS )
5495 goto exit;
5496
5497 status = psa_key_agreement_raw_internal( alg, slot,
5498 peer_key, peer_key_length,
5499 output, output_size,
5500 output_length );
5501
5502exit:
5503 if( status != PSA_SUCCESS )
5504 {
5505 /* If an error happens and is not handled properly, the output
5506 * may be used as a key to protect sensitive data. Arrange for such
5507 * a key to be random, which is likely to result in decryption or
5508 * verification errors. This is better than filling the buffer with
5509 * some constant data such as zeros, which would result in the data
5510 * being protected with a reproducible, easily knowable key.
5511 */
5512 psa_generate_random( output, output_size );
5513 *output_length = output_size;
5514 }
5515 return( status );
5516}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005517
5518
5519/****************************************************************/
5520/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005521/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005522
Gilles Peskine4c317f42018-07-12 01:24:09 +02005523psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005524 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005525{
Janos Follath24eed8d2019-11-22 13:21:35 +00005526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005527 GUARD_MODULE_INITIALIZED;
5528
Gilles Peskinef181eca2019-08-07 13:49:00 +02005529 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5530 {
5531 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5532 output,
5533 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5534 if( ret != 0 )
5535 return( mbedtls_to_psa_error( ret ) );
5536 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5537 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5538 }
5539
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5541 return( mbedtls_to_psa_error( ret ) );
5542}
5543
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005544#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5545#include "mbedtls/entropy_poll.h"
5546
Gilles Peskine7228da22019-07-15 11:06:15 +02005547psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005548 size_t seed_size )
5549{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005550 if( global_data.initialized )
5551 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005552
5553 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5554 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5555 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5556 return( PSA_ERROR_INVALID_ARGUMENT );
5557
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005558 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005559}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005560#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005561
Gilles Peskinee56e8782019-04-26 17:34:02 +02005562#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5563static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5564 size_t domain_parameters_size,
5565 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005566{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005567 size_t i;
5568 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005569
Gilles Peskinee56e8782019-04-26 17:34:02 +02005570 if( domain_parameters_size == 0 )
5571 {
5572 *exponent = 65537;
5573 return( PSA_SUCCESS );
5574 }
5575
5576 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5577 * support values that fit in a 32-bit integer, which is larger than
5578 * int on just about every platform anyway. */
5579 if( domain_parameters_size > sizeof( acc ) )
5580 return( PSA_ERROR_NOT_SUPPORTED );
5581 for( i = 0; i < domain_parameters_size; i++ )
5582 acc = ( acc << 8 ) | domain_parameters[i];
5583 if( acc > INT_MAX )
5584 return( PSA_ERROR_NOT_SUPPORTED );
5585 *exponent = acc;
5586 return( PSA_SUCCESS );
5587}
5588#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5589
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005590static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005591 psa_key_slot_t *slot, size_t bits,
5592 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593{
Gilles Peskine8e338702019-07-30 20:06:31 +02005594 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595
Gilles Peskinee56e8782019-04-26 17:34:02 +02005596 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005597 return( PSA_ERROR_INVALID_ARGUMENT );
5598
Gilles Peskinee59236f2018-01-27 23:32:46 +01005599 if( key_type_is_raw_bytes( type ) )
5600 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005601 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005602 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5603 if( status != PSA_SUCCESS )
5604 return( status );
5605 status = psa_generate_random( slot->data.raw.data,
5606 slot->data.raw.bytes );
5607 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005608 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005609#if defined(MBEDTLS_DES_C)
5610 if( type == PSA_KEY_TYPE_DES )
5611 psa_des_set_key_parity( slot->data.raw.data,
5612 slot->data.raw.bytes );
5613#endif /* MBEDTLS_DES_C */
5614 }
5615 else
5616
5617#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005618 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005619 {
5620 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005622 int exponent;
5623 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005624 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5625 return( PSA_ERROR_NOT_SUPPORTED );
5626 /* Accept only byte-aligned keys, for the same reasons as
5627 * in psa_import_rsa_key(). */
5628 if( bits % 8 != 0 )
5629 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005630 status = psa_read_rsa_exponent( domain_parameters,
5631 domain_parameters_size,
5632 &exponent );
5633 if( status != PSA_SUCCESS )
5634 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005635 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5636 if( rsa == NULL )
5637 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5638 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5639 ret = mbedtls_rsa_gen_key( rsa,
5640 mbedtls_ctr_drbg_random,
5641 &global_data.ctr_drbg,
5642 (unsigned int) bits,
5643 exponent );
5644 if( ret != 0 )
5645 {
5646 mbedtls_rsa_free( rsa );
5647 mbedtls_free( rsa );
5648 return( mbedtls_to_psa_error( ret ) );
5649 }
5650 slot->data.rsa = rsa;
5651 }
5652 else
5653#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5654
5655#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005656 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005657 {
5658 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005659 mbedtls_ecp_group_id grp_id =
5660 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005661 const mbedtls_ecp_curve_info *curve_info =
5662 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5663 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005665 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005666 return( PSA_ERROR_NOT_SUPPORTED );
5667 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5668 return( PSA_ERROR_NOT_SUPPORTED );
5669 if( curve_info->bit_size != bits )
5670 return( PSA_ERROR_INVALID_ARGUMENT );
5671 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5672 if( ecp == NULL )
5673 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5674 mbedtls_ecp_keypair_init( ecp );
5675 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5676 mbedtls_ctr_drbg_random,
5677 &global_data.ctr_drbg );
5678 if( ret != 0 )
5679 {
5680 mbedtls_ecp_keypair_free( ecp );
5681 mbedtls_free( ecp );
5682 return( mbedtls_to_psa_error( ret ) );
5683 }
5684 slot->data.ecp = ecp;
5685 }
5686 else
5687#endif /* MBEDTLS_ECP_C */
5688
5689 return( PSA_ERROR_NOT_SUPPORTED );
5690
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005691 return( PSA_SUCCESS );
5692}
Darryl Green0c6575a2018-11-07 16:05:30 +00005693
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005694psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005695 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005696{
5697 psa_status_t status;
5698 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005699 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005700
Gilles Peskine0f84d622019-09-12 19:03:13 +02005701 /* Reject any attempt to create a zero-length key so that we don't
5702 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5703 if( psa_get_key_bits( attributes ) == 0 )
5704 return( PSA_ERROR_INVALID_ARGUMENT );
5705
Gilles Peskinedf179142019-07-15 22:02:14 +02005706 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5707 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005708 if( status != PSA_SUCCESS )
5709 goto exit;
5710
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005711#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5712 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005713 {
Gilles Peskine11792082019-08-06 18:36:36 +02005714 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5715 size_t pubkey_length = 0; /* We don't support this feature yet */
5716 if( drv->key_management == NULL ||
5717 drv->key_management->p_generate == NULL )
5718 {
5719 status = PSA_ERROR_NOT_SUPPORTED;
5720 goto exit;
5721 }
5722 status = drv->key_management->p_generate(
5723 psa_get_se_driver_context( driver ),
5724 slot->data.se.slot_number, attributes,
5725 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005726 }
Gilles Peskine11792082019-08-06 18:36:36 +02005727 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005728#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005729 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005730 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005731 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005732 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005733 }
Gilles Peskine11792082019-08-06 18:36:36 +02005734
5735exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005736 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005737 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005738 if( status != PSA_SUCCESS )
5739 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005740 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005741 *handle = 0;
5742 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005743 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744}
5745
5746
Gilles Peskinee59236f2018-01-27 23:32:46 +01005747
5748/****************************************************************/
5749/* Module setup */
5750/****************************************************************/
5751
Gilles Peskine5e769522018-11-20 21:59:56 +01005752psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5753 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5754 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5755{
5756 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5757 return( PSA_ERROR_BAD_STATE );
5758 global_data.entropy_init = entropy_init;
5759 global_data.entropy_free = entropy_free;
5760 return( PSA_SUCCESS );
5761}
5762
Gilles Peskinee59236f2018-01-27 23:32:46 +01005763void mbedtls_psa_crypto_free( void )
5764{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005765 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005766 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5767 {
5768 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005769 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005770 }
5771 /* Wipe all remaining data, including configuration.
5772 * In particular, this sets all state indicator to the value
5773 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005774 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005775#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005776 /* Unregister all secure element drivers, so that we restart from
5777 * a pristine state. */
5778 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005779#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005780}
5781
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005782#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5783/** Recover a transaction that was interrupted by a power failure.
5784 *
5785 * This function is called during initialization, before psa_crypto_init()
5786 * returns. If this function returns a failure status, the initialization
5787 * fails.
5788 */
5789static psa_status_t psa_crypto_recover_transaction(
5790 const psa_crypto_transaction_t *transaction )
5791{
5792 switch( transaction->unknown.type )
5793 {
5794 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5795 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005796 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005797 * is implemented.
5798 * https://github.com/ARMmbed/mbed-crypto/issues/218
5799 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005800 default:
5801 /* We found an unsupported transaction in the storage.
5802 * We don't know what state the storage is in. Give up. */
5803 return( PSA_ERROR_STORAGE_FAILURE );
5804 }
5805}
5806#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5807
Gilles Peskinee59236f2018-01-27 23:32:46 +01005808psa_status_t psa_crypto_init( void )
5809{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005810 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811 const unsigned char drbg_seed[] = "PSA";
5812
Gilles Peskinec6b69072018-11-20 21:42:52 +01005813 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005814 if( global_data.initialized != 0 )
5815 return( PSA_SUCCESS );
5816
Gilles Peskine5e769522018-11-20 21:59:56 +01005817 /* Set default configuration if
5818 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5819 if( global_data.entropy_init == NULL )
5820 global_data.entropy_init = mbedtls_entropy_init;
5821 if( global_data.entropy_free == NULL )
5822 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005823
Gilles Peskinec6b69072018-11-20 21:42:52 +01005824 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005825 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005826#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5827 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5828 /* The PSA entropy injection feature depends on using NV seed as an entropy
5829 * source. Add NV seed as an entropy source for PSA entropy injection. */
5830 mbedtls_entropy_add_source( &global_data.entropy,
5831 mbedtls_nv_seed_poll, NULL,
5832 MBEDTLS_ENTROPY_BLOCK_SIZE,
5833 MBEDTLS_ENTROPY_SOURCE_STRONG );
5834#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005835 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005836 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005837 status = mbedtls_to_psa_error(
5838 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5839 mbedtls_entropy_func,
5840 &global_data.entropy,
5841 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5842 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005843 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005844 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005845
Gilles Peskine66fb1262018-12-10 16:29:04 +01005846 status = psa_initialize_key_slots( );
5847 if( status != PSA_SUCCESS )
5848 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005849
Gilles Peskined9348f22019-10-01 15:22:29 +02005850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5851 status = psa_init_all_se_drivers( );
5852 if( status != PSA_SUCCESS )
5853 goto exit;
5854#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5855
Gilles Peskine4b734222019-07-24 15:56:31 +02005856#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005857 status = psa_crypto_load_transaction( );
5858 if( status == PSA_SUCCESS )
5859 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005860 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5861 if( status != PSA_SUCCESS )
5862 goto exit;
5863 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005864 }
5865 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5866 {
5867 /* There's no transaction to complete. It's all good. */
5868 status = PSA_SUCCESS;
5869 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005870#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005871
Gilles Peskinec6b69072018-11-20 21:42:52 +01005872 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873 global_data.initialized = 1;
5874
5875exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005876 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005877 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005878 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005879}
5880
5881#endif /* MBEDTLS_PSA_CRYPTO_C */