blob: 1120c83f9270f56c1ed4fd6011fb01c01b18256d [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 Peskine025fccd2019-12-02 19:12:00 +0100430 if( PSA_BITS_TO_BYTES( curve & 0xffff ) != byte_length )
431 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200432 switch( curve )
433 {
434 case PSA_ECC_CURVE_SECP192R1:
435 return( MBEDTLS_ECP_DP_SECP192R1 );
436 case PSA_ECC_CURVE_SECP224R1:
437 return( MBEDTLS_ECP_DP_SECP224R1 );
438 case PSA_ECC_CURVE_SECP256R1:
439 return( MBEDTLS_ECP_DP_SECP256R1 );
440 case PSA_ECC_CURVE_SECP384R1:
441 return( MBEDTLS_ECP_DP_SECP384R1 );
442 case PSA_ECC_CURVE_SECP521R1:
443 return( MBEDTLS_ECP_DP_SECP521R1 );
444 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
445 return( MBEDTLS_ECP_DP_BP256R1 );
446 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
447 return( MBEDTLS_ECP_DP_BP384R1 );
448 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
449 return( MBEDTLS_ECP_DP_BP512R1 );
450 case PSA_ECC_CURVE_CURVE25519:
451 return( MBEDTLS_ECP_DP_CURVE25519 );
452 case PSA_ECC_CURVE_SECP192K1:
453 return( MBEDTLS_ECP_DP_SECP192K1 );
454 case PSA_ECC_CURVE_SECP224K1:
455 return( MBEDTLS_ECP_DP_SECP224K1 );
456 case PSA_ECC_CURVE_SECP256K1:
457 return( MBEDTLS_ECP_DP_SECP256K1 );
458 case PSA_ECC_CURVE_CURVE448:
459 return( MBEDTLS_ECP_DP_CURVE448 );
460 default:
461 return( MBEDTLS_ECP_DP_NONE );
462 }
463}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100464#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200465
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
467 size_t bits,
468 struct raw_data *raw )
469{
470 /* Check that the bit size is acceptable for the key type */
471 switch( type )
472 {
473 case PSA_KEY_TYPE_RAW_DATA:
474#if defined(MBEDTLS_MD_C)
475 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200476#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200477 case PSA_KEY_TYPE_DERIVE:
478 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479#if defined(MBEDTLS_AES_C)
480 case PSA_KEY_TYPE_AES:
481 if( bits != 128 && bits != 192 && bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_CAMELLIA_C)
486 case PSA_KEY_TYPE_CAMELLIA:
487 if( bits != 128 && bits != 192 && bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491#if defined(MBEDTLS_DES_C)
492 case PSA_KEY_TYPE_DES:
493 if( bits != 64 && bits != 128 && bits != 192 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
495 break;
496#endif
497#if defined(MBEDTLS_ARC4_C)
498 case PSA_KEY_TYPE_ARC4:
499 if( bits < 8 || bits > 2048 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200503#if defined(MBEDTLS_CHACHA20_C)
504 case PSA_KEY_TYPE_CHACHA20:
505 if( bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 default:
510 return( PSA_ERROR_NOT_SUPPORTED );
511 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200512 if( bits % 8 != 0 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514
515 /* Allocate memory for the key */
516 raw->bytes = PSA_BITS_TO_BYTES( bits );
517 raw->data = mbedtls_calloc( 1, raw->bytes );
518 if( raw->data == NULL )
519 {
520 raw->bytes = 0;
521 return( PSA_ERROR_INSUFFICIENT_MEMORY );
522 }
523 return( PSA_SUCCESS );
524}
525
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200526#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100527/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
528 * that are not a multiple of 8) well. For example, there is only
529 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
530 * way to return the exact bit size of a key.
531 * To keep things simple, reject non-byte-aligned key sizes. */
532static psa_status_t psa_check_rsa_key_byte_aligned(
533 const mbedtls_rsa_context *rsa )
534{
535 mbedtls_mpi n;
536 psa_status_t status;
537 mbedtls_mpi_init( &n );
538 status = mbedtls_to_psa_error(
539 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
540 if( status == PSA_SUCCESS )
541 {
542 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
543 status = PSA_ERROR_NOT_SUPPORTED;
544 }
545 mbedtls_mpi_free( &n );
546 return( status );
547}
548
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549static psa_status_t psa_import_rsa_key( psa_key_type_t type,
550 const uint8_t *data,
551 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 mbedtls_rsa_context **p_rsa )
553{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000554 psa_status_t status;
555 mbedtls_pk_context pk;
556 mbedtls_rsa_context *rsa;
557 size_t bits;
558
559 mbedtls_pk_init( &pk );
560
561 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200562 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 status = mbedtls_to_psa_error(
564 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200565 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000566 status = mbedtls_to_psa_error(
567 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
568 if( status != PSA_SUCCESS )
569 goto exit;
570
571 /* We have something that the pkparse module recognizes. If it is a
572 * valid RSA key, store it. */
573 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200574 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575 status = PSA_ERROR_INVALID_ARGUMENT;
576 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200577 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578
579 rsa = mbedtls_pk_rsa( pk );
580 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
581 * supports non-byte-aligned key sizes, but not well. For example,
582 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
583 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
584 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
585 {
586 status = PSA_ERROR_NOT_SUPPORTED;
587 goto exit;
588 }
589 status = psa_check_rsa_key_byte_aligned( rsa );
590
591exit:
592 /* Free the content of the pk object only on error. */
593 if( status != PSA_SUCCESS )
594 {
595 mbedtls_pk_free( &pk );
596 return( status );
597 }
598
599 /* On success, store the content of the object in the RSA context. */
600 *p_rsa = rsa;
601
602 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200603}
604#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
605
Jaeden Ameroccdce902019-01-10 11:42:27 +0000606#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100607static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100608 size_t data_length,
609 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100610 mbedtls_ecp_keypair **p_ecp )
611{
612 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
613 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
614 if( *p_ecp == NULL )
615 return( PSA_ERROR_INSUFFICIENT_MEMORY );
616 mbedtls_ecp_keypair_init( *p_ecp );
617
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100618 if( is_public )
619 {
620 /* A public key is represented as:
621 * - The byte 0x04;
622 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
623 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
624 * So its data length is 2m+1 where n is the key size in bits.
625 */
626 if( ( data_length & 1 ) == 0 )
627 return( PSA_ERROR_INVALID_ARGUMENT );
628 data_length = data_length / 2;
629 }
630
Gilles Peskine4cd32772019-12-02 20:49:42 +0100631 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100632 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
633 if( grp_id == MBEDTLS_ECP_DP_NONE )
634 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100635 return( mbedtls_to_psa_error(
636 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
637}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000638
639/* Import a public key given as the uncompressed representation defined by SEC1
640 * 2.3.3 as the content of an ECPoint. */
641static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
642 const uint8_t *data,
643 size_t data_length,
644 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200645{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000647 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000648
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100649 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000650 if( status != PSA_SUCCESS )
651 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100652
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 /* Load the public value. */
654 status = mbedtls_to_psa_error(
655 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
656 data, data_length ) );
657 if( status != PSA_SUCCESS )
658 goto exit;
659
660 /* Check that the point is on the curve. */
661 status = mbedtls_to_psa_error(
662 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
663 if( status != PSA_SUCCESS )
664 goto exit;
665
666 *p_ecp = ecp;
667 return( PSA_SUCCESS );
668
669exit:
670 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200671 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000672 mbedtls_ecp_keypair_free( ecp );
673 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200674 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000675 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200676}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200677
Gilles Peskinef76aa772018-10-29 19:24:33 +0100678/* Import a private key given as a byte string which is the private value
679 * in big-endian order. */
680static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
681 const uint8_t *data,
682 size_t data_length,
683 mbedtls_ecp_keypair **p_ecp )
684{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200685 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100686 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100687
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200688 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
689 return( PSA_ERROR_INVALID_ARGUMENT );
690
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100691 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 if( status != PSA_SUCCESS )
693 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100694
Gilles Peskinef76aa772018-10-29 19:24:33 +0100695 /* Load the secret value. */
696 status = mbedtls_to_psa_error(
697 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
698 if( status != PSA_SUCCESS )
699 goto exit;
700 /* Validate the private key. */
701 status = mbedtls_to_psa_error(
702 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
703 if( status != PSA_SUCCESS )
704 goto exit;
705 /* Calculate the public key from the private key. */
706 status = mbedtls_to_psa_error(
707 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
708 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
709 if( status != PSA_SUCCESS )
710 goto exit;
711
712 *p_ecp = ecp;
713 return( PSA_SUCCESS );
714
715exit:
716 if( ecp != NULL )
717 {
718 mbedtls_ecp_keypair_free( ecp );
719 mbedtls_free( ecp );
720 }
721 return( status );
722}
723#endif /* defined(MBEDTLS_ECP_C) */
724
Gilles Peskineb46bef22019-07-30 21:32:04 +0200725
726/** Return the size of the key in the given slot, in bits.
727 *
728 * \param[in] slot A key slot.
729 *
730 * \return The key size in bits, read from the metadata in the slot.
731 */
732static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
733{
734 return( slot->attr.bits );
735}
736
737/** Calculate the size of the key in the given slot, in bits.
738 *
739 * \param[in] slot A key slot containing a transparent key.
740 *
741 * \return The key size in bits, calculated from the key data.
742 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200743static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200744{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200745 size_t bits = 0; /* return 0 on an empty slot */
746
Gilles Peskineb46bef22019-07-30 21:32:04 +0200747 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200748 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200749#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200750 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
751 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200752#endif /* defined(MBEDTLS_RSA_C) */
753#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200754 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
755 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200756#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200757
758 /* We know that the size fits in psa_key_bits_t thanks to checks
759 * when the key was created. */
760 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200761}
762
Gilles Peskine8e338702019-07-30 20:06:31 +0200763/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100764 * previously. This function assumes that the slot does not contain
765 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100766psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
767 const uint8_t *data,
768 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200770 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100771
Gilles Peskine8e338702019-07-30 20:06:31 +0200772 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200774 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200775 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 if( data_length > SIZE_MAX / 8 )
777 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200778 /* Enforce a size limit, and in particular ensure that the bit
779 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200780 if( bit_size > PSA_MAX_KEY_BITS )
781 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200782 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200783 &slot->data.raw );
784 if( status != PSA_SUCCESS )
785 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200786 if( data_length != 0 )
787 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 }
789 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100790#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200791 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100792 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200793 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100794 data, data_length,
795 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100796 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200797 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000799 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200800 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000801 data, data_length,
802 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 }
804 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100805#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000806#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200807 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200809 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000810 data, data_length,
811 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 }
813 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 {
816 return( PSA_ERROR_NOT_SUPPORTED );
817 }
Darryl Green940d72c2018-07-13 13:18:51 +0100818
Gilles Peskineb46bef22019-07-30 21:32:04 +0200819 if( status == PSA_SUCCESS )
820 {
821 /* Write the actual key size to the slot.
822 * psa_start_key_creation() wrote the size declared by the
823 * caller, which may be 0 (meaning unspecified) or wrong. */
824 slot->attr.bits = psa_calculate_key_bits( slot );
825 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100826 return( status );
827}
828
Gilles Peskinef603c712019-01-19 13:40:11 +0100829/** Calculate the intersection of two algorithm usage policies.
830 *
831 * Return 0 (which allows no operation) on incompatibility.
832 */
833static psa_algorithm_t psa_key_policy_algorithm_intersection(
834 psa_algorithm_t alg1,
835 psa_algorithm_t alg2 )
836{
Gilles Peskine549ea862019-05-22 11:45:59 +0200837 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100838 if( alg1 == alg2 )
839 return( alg1 );
840 /* If the policies are from the same hash-and-sign family, check
841 * if one is a wildcard. If so the other has the specific algorithm. */
842 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
843 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
844 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
845 {
846 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
847 return( alg2 );
848 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
849 return( alg1 );
850 }
851 /* If the policies are incompatible, allow nothing. */
852 return( 0 );
853}
854
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
856 psa_algorithm_t requested_alg )
857{
Gilles Peskine549ea862019-05-22 11:45:59 +0200858 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200859 if( requested_alg == policy_alg )
860 return( 1 );
861 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200862 * and requested_alg is the same hash-and-sign family with any hash,
863 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200864 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
865 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
866 {
867 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
868 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
869 }
870 /* If it isn't permitted, it's forbidden. */
871 return( 0 );
872}
873
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100874/** Test whether a policy permits an algorithm.
875 *
876 * The caller must test usage flags separately.
877 */
878static int psa_key_policy_permits( const psa_key_policy_t *policy,
879 psa_algorithm_t alg )
880{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 return( psa_key_algorithm_permits( policy->alg, alg ) ||
882 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100883}
884
Gilles Peskinef603c712019-01-19 13:40:11 +0100885/** Restrict a key policy based on a constraint.
886 *
887 * \param[in,out] policy The policy to restrict.
888 * \param[in] constraint The policy constraint to apply.
889 *
890 * \retval #PSA_SUCCESS
891 * \c *policy contains the intersection of the original value of
892 * \c *policy and \c *constraint.
893 * \retval #PSA_ERROR_INVALID_ARGUMENT
894 * \c *policy and \c *constraint are incompatible.
895 * \c *policy is unchanged.
896 */
897static psa_status_t psa_restrict_key_policy(
898 psa_key_policy_t *policy,
899 const psa_key_policy_t *constraint )
900{
901 psa_algorithm_t intersection_alg =
902 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200903 psa_algorithm_t intersection_alg2 =
904 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100905 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
906 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200907 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
908 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 policy->usage &= constraint->usage;
910 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200911 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 return( PSA_SUCCESS );
913}
914
Darryl Green06fd18d2018-07-16 11:21:11 +0100915/** Retrieve a slot which must contain a key. The key must have allow all the
916 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
917 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100918static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100919 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100920 psa_key_usage_t usage,
921 psa_algorithm_t alg )
922{
923 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100924 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100925
926 *p_slot = NULL;
927
Gilles Peskinec5487a82018-12-03 18:08:14 +0100928 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100929 if( status != PSA_SUCCESS )
930 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100931
932 /* Enforce that usage policy for the key slot contains all the flags
933 * required by the usage parameter. There is one exception: public
934 * keys can always be exported, so we treat public key objects as
935 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200936 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100937 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200938 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100939 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100940
941 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200942 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100943 return( PSA_ERROR_NOT_PERMITTED );
944
945 *p_slot = slot;
946 return( PSA_SUCCESS );
947}
Darryl Green940d72c2018-07-13 13:18:51 +0100948
Gilles Peskine28f8f302019-07-24 13:30:31 +0200949/** Retrieve a slot which must contain a transparent key.
950 *
951 * A transparent key is a key for which the key material is directly
952 * available, as opposed to a key in a secure element.
953 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200954 * This is a temporary function to use instead of psa_get_key_from_slot()
955 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200956 */
957#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
958static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
959 psa_key_slot_t **p_slot,
960 psa_key_usage_t usage,
961 psa_algorithm_t alg )
962{
963 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
964 if( status != PSA_SUCCESS )
965 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200966 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200967 {
968 *p_slot = NULL;
969 return( PSA_ERROR_NOT_SUPPORTED );
970 }
971 return( PSA_SUCCESS );
972}
973#else /* MBEDTLS_PSA_CRYPTO_SE_C */
974/* With no secure element support, all keys are transparent. */
975#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
976 psa_get_key_from_slot( handle, p_slot, usage, alg )
977#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
978
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100979/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100980static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000981{
Gilles Peskine73167e12019-07-12 23:44:37 +0200982#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
983 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000984 {
985 /* No key material to clean. */
986 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200987 else
988#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200989 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000990 {
991 /* No key material to clean. */
992 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200993 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000994 {
995 mbedtls_free( slot->data.raw.data );
996 }
997 else
998#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200999 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001000 {
1001 mbedtls_rsa_free( slot->data.rsa );
1002 mbedtls_free( slot->data.rsa );
1003 }
1004 else
1005#endif /* defined(MBEDTLS_RSA_C) */
1006#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001007 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001008 {
1009 mbedtls_ecp_keypair_free( slot->data.ecp );
1010 mbedtls_free( slot->data.ecp );
1011 }
1012 else
1013#endif /* defined(MBEDTLS_ECP_C) */
1014 {
1015 /* Shouldn't happen: the key type is not any type that we
1016 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001017 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001018 }
1019
1020 return( PSA_SUCCESS );
1021}
1022
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001023/** Completely wipe a slot in memory, including its policy.
1024 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001025psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001026{
1027 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001028 /* Multipart operations may still be using the key. This is safe
1029 * because all multipart operation objects are independent from
1030 * the key slot: if they need to access the key after the setup
1031 * phase, they have a copy of the key. Note that this means that
1032 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001033 /* At this point, key material and other type-specific content has
1034 * been wiped. Clear remaining metadata. We can call memset and not
1035 * zeroize because the metadata is not particularly sensitive. */
1036 memset( slot, 0, sizeof( *slot ) );
1037 return( status );
1038}
1039
Gilles Peskinec5487a82018-12-03 18:08:14 +01001040psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001042 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001043 psa_status_t status; /* status of the last operation */
1044 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001045#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1046 psa_se_drv_table_entry_t *driver;
1047#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001048
Gilles Peskine1841cf42019-10-08 15:48:25 +02001049 if( handle == 0 )
1050 return( PSA_SUCCESS );
1051
Gilles Peskinec5487a82018-12-03 18:08:14 +01001052 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 if( status != PSA_SUCCESS )
1054 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001055
1056#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001057 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001058 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001059 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001060 /* For a key in a secure element, we need to do three things:
1061 * remove the key file in internal storage, destroy the
1062 * key inside the secure element, and update the driver's
1063 * persistent data. Start a transaction that will encompass these
1064 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001066 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001067 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001068 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001069 status = psa_crypto_save_transaction( );
1070 if( status != PSA_SUCCESS )
1071 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001072 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001073 /* We should still try to destroy the key in the secure
1074 * element and the key metadata in storage. This is especially
1075 * important if the error is that the storage is full.
1076 * But how to do it exactly without risking an inconsistent
1077 * state after a reset?
1078 * https://github.com/ARMmbed/mbed-crypto/issues/215
1079 */
1080 overall_status = status;
1081 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001082 }
1083
Gilles Peskine354f7672019-07-12 23:46:38 +02001084 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001087 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1089
Darryl Greend49a4992018-06-18 17:27:26 +01001090#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001091 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001092 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001093 status = psa_destroy_persistent_key( slot->attr.id );
1094 if( overall_status == PSA_SUCCESS )
1095 overall_status = status;
1096
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001097 /* TODO: other slots may have a copy of the same key. We should
1098 * invalidate them.
1099 * https://github.com/ARMmbed/mbed-crypto/issues/214
1100 */
Darryl Greend49a4992018-06-18 17:27:26 +01001101 }
1102#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001103
Gilles Peskinefc762652019-07-22 19:30:34 +02001104#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1105 if( driver != NULL )
1106 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001107 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001108 if( overall_status == PSA_SUCCESS )
1109 overall_status = status;
1110 status = psa_crypto_stop_transaction( );
1111 if( overall_status == PSA_SUCCESS )
1112 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001113 }
1114#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1115
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001116#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1117exit:
1118#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001119 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001120 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1121 if( overall_status == PSA_SUCCESS )
1122 overall_status = status;
1123 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001124}
1125
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001126void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001127{
Gilles Peskineb699f072019-04-26 16:06:02 +02001128 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001129 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001130}
1131
Gilles Peskineb699f072019-04-26 16:06:02 +02001132psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1133 psa_key_type_t type,
1134 const uint8_t *data,
1135 size_t data_length )
1136{
1137 uint8_t *copy = NULL;
1138
1139 if( data_length != 0 )
1140 {
1141 copy = mbedtls_calloc( 1, data_length );
1142 if( copy == NULL )
1143 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1144 memcpy( copy, data, data_length );
1145 }
1146 /* After this point, this function is guaranteed to succeed, so it
1147 * can start modifying `*attributes`. */
1148
1149 if( attributes->domain_parameters != NULL )
1150 {
1151 mbedtls_free( attributes->domain_parameters );
1152 attributes->domain_parameters = NULL;
1153 attributes->domain_parameters_size = 0;
1154 }
1155
1156 attributes->domain_parameters = copy;
1157 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001158 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001159 return( PSA_SUCCESS );
1160}
1161
1162psa_status_t psa_get_key_domain_parameters(
1163 const psa_key_attributes_t *attributes,
1164 uint8_t *data, size_t data_size, size_t *data_length )
1165{
1166 if( attributes->domain_parameters_size > data_size )
1167 return( PSA_ERROR_BUFFER_TOO_SMALL );
1168 *data_length = attributes->domain_parameters_size;
1169 if( attributes->domain_parameters_size != 0 )
1170 memcpy( data, attributes->domain_parameters,
1171 attributes->domain_parameters_size );
1172 return( PSA_SUCCESS );
1173}
1174
1175#if defined(MBEDTLS_RSA_C)
1176static psa_status_t psa_get_rsa_public_exponent(
1177 const mbedtls_rsa_context *rsa,
1178 psa_key_attributes_t *attributes )
1179{
1180 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001182 uint8_t *buffer = NULL;
1183 size_t buflen;
1184 mbedtls_mpi_init( &mpi );
1185
1186 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1187 if( ret != 0 )
1188 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001189 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1190 {
1191 /* It's the default value, which is reported as an empty string,
1192 * so there's nothing to do. */
1193 goto exit;
1194 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001195
1196 buflen = mbedtls_mpi_size( &mpi );
1197 buffer = mbedtls_calloc( 1, buflen );
1198 if( buffer == NULL )
1199 {
1200 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1201 goto exit;
1202 }
1203 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1204 if( ret != 0 )
1205 goto exit;
1206 attributes->domain_parameters = buffer;
1207 attributes->domain_parameters_size = buflen;
1208
1209exit:
1210 mbedtls_mpi_free( &mpi );
1211 if( ret != 0 )
1212 mbedtls_free( buffer );
1213 return( mbedtls_to_psa_error( ret ) );
1214}
1215#endif /* MBEDTLS_RSA_C */
1216
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001217/** Retrieve all the publicly-accessible attributes of a key.
1218 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001219psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1220 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001222 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001223 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001225 psa_reset_key_attributes( attributes );
1226
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001227 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001228 if( status != PSA_SUCCESS )
1229 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001230
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001231 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1233 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1234
1235#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1236 if( psa_key_slot_is_external( slot ) )
1237 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001239
Gilles Peskine8e338702019-07-30 20:06:31 +02001240 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001241 {
1242#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001243 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001244 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001246 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001247 * is not yet implemented.
1248 * https://github.com/ARMmbed/mbed-crypto/issues/216
1249 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001250 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001251 break;
1252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001253 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1254 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001255#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001256 default:
1257 /* Nothing else to do. */
1258 break;
1259 }
1260
1261 if( status != PSA_SUCCESS )
1262 psa_reset_key_attributes( attributes );
1263 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264}
1265
Gilles Peskinec8000c02019-08-02 20:15:51 +02001266#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1267psa_status_t psa_get_key_slot_number(
1268 const psa_key_attributes_t *attributes,
1269 psa_key_slot_number_t *slot_number )
1270{
1271 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1272 {
1273 *slot_number = attributes->slot_number;
1274 return( PSA_SUCCESS );
1275 }
1276 else
1277 return( PSA_ERROR_INVALID_ARGUMENT );
1278}
1279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1280
Jaeden Ameroccdce902019-01-10 11:42:27 +00001281#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001282static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1283 unsigned char *buf, size_t size )
1284{
Janos Follath24eed8d2019-11-22 13:21:35 +00001285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001286 unsigned char *c;
1287 size_t len = 0;
1288
1289 c = buf + size;
1290
1291 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1292
1293 return( (int) len );
1294}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001295#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001296
Gilles Peskinef603c712019-01-19 13:40:11 +01001297static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1298 uint8_t *data,
1299 size_t data_size,
1300 size_t *data_length,
1301 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302{
Gilles Peskine5d309672019-07-12 23:47:28 +02001303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1304 const psa_drv_se_t *drv;
1305 psa_drv_se_context_t *drv_context;
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001308 *data_length = 0;
1309
Gilles Peskine8e338702019-07-30 20:06:31 +02001310 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001311 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001312
Gilles Peskinef9168942019-09-12 19:20:29 +02001313 /* Reject a zero-length output buffer now, since this can never be a
1314 * valid key representation. This way we know that data must be a valid
1315 * pointer and we can do things like memset(data, ..., data_size). */
1316 if( data_size == 0 )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
1318
Gilles Peskine5d309672019-07-12 23:47:28 +02001319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001320 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001321 {
1322 psa_drv_se_export_key_t method;
1323 if( drv->key_management == NULL )
1324 return( PSA_ERROR_NOT_SUPPORTED );
1325 method = ( export_public_key ?
1326 drv->key_management->p_export_public :
1327 drv->key_management->p_export );
1328 if( method == NULL )
1329 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001330 return( method( drv_context,
1331 slot->data.se.slot_number,
1332 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001333 }
1334#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1335
Gilles Peskine8e338702019-07-30 20:06:31 +02001336 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337 {
1338 if( slot->data.raw.bytes > data_size )
1339 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001340 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1341 memset( data + slot->data.raw.bytes, 0,
1342 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001343 *data_length = slot->data.raw.bytes;
1344 return( PSA_SUCCESS );
1345 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001346#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001347 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001348 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001349 psa_status_t status;
1350
Gilles Peskineb46bef22019-07-30 21:32:04 +02001351 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001352 if( bytes > data_size )
1353 return( PSA_ERROR_BUFFER_TOO_SMALL );
1354 status = mbedtls_to_psa_error(
1355 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1356 if( status != PSA_SUCCESS )
1357 return( status );
1358 memset( data + bytes, 0, data_size - bytes );
1359 *data_length = bytes;
1360 return( PSA_SUCCESS );
1361 }
1362#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001363 else
Moran Peker17e36e12018-05-02 12:55:20 +03001364 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001365#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001366 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1367 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001368 {
Moran Pekera998bc62018-04-16 18:16:20 +03001369 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001371 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001372 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001373#if defined(MBEDTLS_RSA_C)
1374 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001375 pk.pk_info = &mbedtls_rsa_info;
1376 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001377#else
1378 return( PSA_ERROR_NOT_SUPPORTED );
1379#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001380 }
1381 else
1382 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001383#if defined(MBEDTLS_ECP_C)
1384 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001385 pk.pk_info = &mbedtls_eckey_info;
1386 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001387#else
1388 return( PSA_ERROR_NOT_SUPPORTED );
1389#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001390 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001391 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001392 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001393 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001394 }
Moran Peker17e36e12018-05-02 12:55:20 +03001395 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001396 {
Moran Peker17e36e12018-05-02 12:55:20 +03001397 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001398 }
Moran Peker60364322018-04-29 11:34:58 +03001399 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001400 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001401 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001402 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001403 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001404 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1405 * Move the data to the beginning and erase remaining data
1406 * at the original location. */
1407 if( 2 * (size_t) ret <= data_size )
1408 {
1409 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001410 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001411 }
1412 else if( (size_t) ret < data_size )
1413 {
1414 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001415 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001416 }
Moran Pekera998bc62018-04-16 18:16:20 +03001417 *data_length = ret;
1418 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001419 }
1420 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001421#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001422 {
1423 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001424 it is valid for a special-purpose implementation to omit
1425 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001426 return( PSA_ERROR_NOT_SUPPORTED );
1427 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428 }
1429}
1430
Gilles Peskinec5487a82018-12-03 18:08:14 +01001431psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001432 uint8_t *data,
1433 size_t data_size,
1434 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001435{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001436 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001437 psa_status_t status;
1438
1439 /* Set the key to empty now, so that even when there are errors, we always
1440 * set data_length to a value between 0 and data_size. On error, setting
1441 * the key to empty is a good choice because an empty key representation is
1442 * unlikely to be accepted anywhere. */
1443 *data_length = 0;
1444
1445 /* Export requires the EXPORT flag. There is an exception for public keys,
1446 * which don't require any flag, but psa_get_key_from_slot takes
1447 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001448 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001449 if( status != PSA_SUCCESS )
1450 return( status );
1451 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001452 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001454
Gilles Peskinec5487a82018-12-03 18:08:14 +01001455psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001456 uint8_t *data,
1457 size_t data_size,
1458 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001459{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001460 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001461 psa_status_t status;
1462
1463 /* Set the key to empty now, so that even when there are errors, we always
1464 * set data_length to a value between 0 and data_size. On error, setting
1465 * the key to empty is a good choice because an empty key representation is
1466 * unlikely to be accepted anywhere. */
1467 *data_length = 0;
1468
1469 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001470 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001471 if( status != PSA_SUCCESS )
1472 return( status );
1473 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001474 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001475}
1476
Gilles Peskine91e8c332019-08-02 19:19:39 +02001477#if defined(static_assert)
1478static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1479 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001480static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001481 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001482static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001483 "One or more key attribute flag is listed as both internal-only and external-only" );
1484#endif
1485
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001486/** Validate that a key policy is internally well-formed.
1487 *
1488 * This function only rejects invalid policies. It does not validate the
1489 * consistency of the policy with respect to other attributes of the key
1490 * such as the key type.
1491 */
1492static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001493{
1494 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001495 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001496 PSA_KEY_USAGE_ENCRYPT |
1497 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001498 PSA_KEY_USAGE_SIGN_HASH |
1499 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001500 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1501 return( PSA_ERROR_INVALID_ARGUMENT );
1502
Gilles Peskine4747d192019-04-17 15:05:45 +02001503 return( PSA_SUCCESS );
1504}
1505
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506/** Validate the internal consistency of key attributes.
1507 *
1508 * This function only rejects invalid attribute values. If does not
1509 * validate the consistency of the attributes with any key data that may
1510 * be involved in the creation of the key.
1511 *
1512 * Call this function early in the key creation process.
1513 *
1514 * \param[in] attributes Key attributes for the new key.
1515 * \param[out] p_drv On any return, the driver for the key, if any.
1516 * NULL for a transparent key.
1517 *
1518 */
1519static psa_status_t psa_validate_key_attributes(
1520 const psa_key_attributes_t *attributes,
1521 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001522{
1523 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524
1525 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001526 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527 status = psa_validate_persistent_key_parameters(
1528 attributes->core.lifetime, attributes->core.id,
1529 p_drv, 1 );
1530 if( status != PSA_SUCCESS )
1531 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001532 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001533
1534 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001535 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001536 return( status );
1537
1538 /* Refuse to create overly large keys.
1539 * Note that this doesn't trigger on import if the attributes don't
1540 * explicitly specify a size (so psa_get_key_bits returns 0), so
1541 * psa_import_key() needs its own checks. */
1542 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1543 return( PSA_ERROR_NOT_SUPPORTED );
1544
Gilles Peskine91e8c332019-08-02 19:19:39 +02001545 /* Reject invalid flags. These should not be reachable through the API. */
1546 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1547 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1548 return( PSA_ERROR_INVALID_ARGUMENT );
1549
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001550 return( PSA_SUCCESS );
1551}
1552
Gilles Peskine4747d192019-04-17 15:05:45 +02001553/** Prepare a key slot to receive key material.
1554 *
1555 * This function allocates a key slot and sets its metadata.
1556 *
1557 * If this function fails, call psa_fail_key_creation().
1558 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001559 * This function is intended to be used as follows:
1560 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1561 * it with the specified attributes, and assign it a handle.
1562 * -# Populate the slot with the key material.
1563 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1564 * In case of failure at any step, stop the sequence and call
1565 * psa_fail_key_creation().
1566 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001567 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001568 * \param[in] attributes Key attributes for the new key.
1569 * \param[out] handle On success, a handle for the allocated slot.
1570 * \param[out] p_slot On success, a pointer to the prepared slot.
1571 * \param[out] p_drv On any return, the driver for the key, if any.
1572 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001573 *
1574 * \retval #PSA_SUCCESS
1575 * The key slot is ready to receive key material.
1576 * \return If this function fails, the key slot is an invalid state.
1577 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 */
1579static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001580 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 const psa_key_attributes_t *attributes,
1582 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001583 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001584 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001585{
1586 psa_status_t status;
1587 psa_key_slot_t *slot;
1588
Gilles Peskinedf179142019-07-15 22:02:14 +02001589 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001590 *p_drv = NULL;
1591
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592 status = psa_validate_key_attributes( attributes, p_drv );
1593 if( status != PSA_SUCCESS )
1594 return( status );
1595
Gilles Peskineedbed562019-08-07 18:19:59 +02001596 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001597 if( status != PSA_SUCCESS )
1598 return( status );
1599 slot = *p_slot;
1600
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001601 /* We're storing the declared bit-size of the key. It's up to each
1602 * creation mechanism to verify that this information is correct.
1603 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001604 * an input (generate, device) but not for those where the bit-size
1605 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001606
1607 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001608
Gilles Peskine91e8c332019-08-02 19:19:39 +02001609 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001610 * external-only flags, query `attributes`. Thanks to the check
1611 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001612 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001613 * may have set. */
1614 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001615
Gilles Peskinecbaff462019-07-12 23:46:04 +02001616#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001617 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001618 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001619 * create the key file in internal storage, create the
1620 * key inside the secure element, and update the driver's
1621 * persistent data. Start a transaction that will encompass these
1622 * three actions. */
1623 /* The first thing to do is to find a slot number for the new key.
1624 * We save the slot number in persistent storage as part of the
1625 * transaction data. It will be needed to recover if the power
1626 * fails during the key creation process, to clean up on the secure
1627 * element side after restarting. Obtaining a slot number from the
1628 * secure element driver updates its persistent state, but we do not yet
1629 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001630 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001631 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001632 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001633 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001634 &slot->data.se.slot_number );
1635 if( status != PSA_SUCCESS )
1636 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001637 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001638 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001639 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001640 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001641 status = psa_crypto_save_transaction( );
1642 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001643 {
1644 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001645 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001646 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001647 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001648
1649 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1650 {
1651 /* Key registration only makes sense with a secure element. */
1652 return( PSA_ERROR_INVALID_ARGUMENT );
1653 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001654#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1655
Darryl Green0c6575a2018-11-07 16:05:30 +00001656 return( status );
1657}
Gilles Peskine4747d192019-04-17 15:05:45 +02001658
1659/** Finalize the creation of a key once its key material has been set.
1660 *
1661 * This entails writing the key to persistent storage.
1662 *
1663 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001664 * See the documentation of psa_start_key_creation() for the intended use
1665 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001667 * \param[in,out] slot Pointer to the slot with key material.
1668 * \param[in] driver The secure element driver for the key,
1669 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001670 *
1671 * \retval #PSA_SUCCESS
1672 * The key was successfully created. The handle is now valid.
1673 * \return If this function fails, the key slot is an invalid state.
1674 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001676static psa_status_t psa_finish_key_creation(
1677 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001678 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001679{
1680 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001681 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001682 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001683
1684#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001685 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1688 if( driver != NULL )
1689 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001690 psa_se_key_data_storage_t data;
1691#if defined(static_assert)
1692 static_assert( sizeof( slot->data.se.slot_number ) ==
1693 sizeof( data.slot_number ),
1694 "Slot number size does not match psa_se_key_data_storage_t" );
1695 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1696 "Bit-size size does not match psa_se_key_data_storage_t" );
1697#endif
1698 memcpy( &data.slot_number, &slot->data.se.slot_number,
1699 sizeof( slot->data.se.slot_number ) );
1700 memcpy( &data.bits, &slot->attr.bits,
1701 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001702 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 (uint8_t*) &data,
1704 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001705 }
1706 else
1707#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1708 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001709 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001710 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001711 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001712 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1713 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001714 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001715 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1716 status = psa_internal_export_key( slot,
1717 buffer, buffer_size, &length,
1718 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001719 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001720 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001721 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001722
Gilles Peskinef9168942019-09-12 19:20:29 +02001723 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001724 mbedtls_free( buffer );
1725 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001727#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1728
Gilles Peskinecbaff462019-07-12 23:46:04 +02001729#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001730 /* Finish the transaction for a key creation. This does not
1731 * happen when registering an existing key. Detect this case
1732 * by checking whether a transaction is in progress (actual
1733 * creation of a key in a secure element requires a transaction,
1734 * but registration doesn't use one). */
1735 if( driver != NULL &&
1736 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001737 {
1738 status = psa_save_se_persistent_data( driver );
1739 if( status != PSA_SUCCESS )
1740 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001741 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742 return( status );
1743 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001744 status = psa_crypto_stop_transaction( );
1745 if( status != PSA_SUCCESS )
1746 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001747 }
1748#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1749
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 return( status );
1751}
1752
1753/** Abort the creation of a key.
1754 *
1755 * You may call this function after calling psa_start_key_creation(),
1756 * or after psa_finish_key_creation() fails. In other circumstances, this
1757 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001758 * See the documentation of psa_start_key_creation() for the intended use
1759 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * \param[in,out] slot Pointer to the slot with key material.
1762 * \param[in] driver The secure element driver for the key,
1763 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001765static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001766 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001767{
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 (void) driver;
1769
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 if( slot == NULL )
1771 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001772
1773#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001774 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 * element, and the failure happened later (when saving metadata
1776 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001777 * element.
1778 * https://github.com/ARMmbed/mbed-crypto/issues/217
1779 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001780
Gilles Peskined7729582019-08-05 15:55:54 +02001781 /* Abort the ongoing transaction if any (there may not be one if
1782 * the creation process failed before starting one, or if the
1783 * key creation is a registration of a key in a secure element).
1784 * Earlier functions must already have done what it takes to undo any
1785 * partial creation. All that's left is to update the transaction data
1786 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001787 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1789
Gilles Peskine4747d192019-04-17 15:05:45 +02001790 psa_wipe_key_slot( slot );
1791}
1792
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001793/** Validate optional attributes during key creation.
1794 *
1795 * Some key attributes are optional during key creation. If they are
1796 * specified in the attributes structure, check that they are consistent
1797 * with the data in the slot.
1798 *
1799 * This function should be called near the end of key creation, after
1800 * the slot in memory is fully populated but before saving persistent data.
1801 */
1802static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001803 const psa_key_slot_t *slot,
1804 const psa_key_attributes_t *attributes )
1805{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001806 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001808 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809 return( PSA_ERROR_INVALID_ARGUMENT );
1810 }
1811
1812 if( attributes->domain_parameters_size != 0 )
1813 {
1814#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001815 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001816 {
1817 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001819 mbedtls_mpi_init( &actual );
1820 mbedtls_mpi_init( &required );
1821 ret = mbedtls_rsa_export( slot->data.rsa,
1822 NULL, NULL, NULL, NULL, &actual );
1823 if( ret != 0 )
1824 goto rsa_exit;
1825 ret = mbedtls_mpi_read_binary( &required,
1826 attributes->domain_parameters,
1827 attributes->domain_parameters_size );
1828 if( ret != 0 )
1829 goto rsa_exit;
1830 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1831 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1832 rsa_exit:
1833 mbedtls_mpi_free( &actual );
1834 mbedtls_mpi_free( &required );
1835 if( ret != 0)
1836 return( mbedtls_to_psa_error( ret ) );
1837 }
1838 else
1839#endif
1840 {
1841 return( PSA_ERROR_INVALID_ARGUMENT );
1842 }
1843 }
1844
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001845 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001847 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001848 return( PSA_ERROR_INVALID_ARGUMENT );
1849 }
1850
1851 return( PSA_SUCCESS );
1852}
1853
Gilles Peskine4747d192019-04-17 15:05:45 +02001854psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001856 size_t data_length,
1857 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001858{
1859 psa_status_t status;
1860 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001861 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862
Gilles Peskine0f84d622019-09-12 19:03:13 +02001863 /* Reject zero-length symmetric keys (including raw data key objects).
1864 * This also rejects any key which might be encoded as an empty string,
1865 * which is never valid. */
1866 if( data_length == 0 )
1867 return( PSA_ERROR_INVALID_ARGUMENT );
1868
Gilles Peskinedf179142019-07-15 22:02:14 +02001869 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1870 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001871 if( status != PSA_SUCCESS )
1872 goto exit;
1873
Gilles Peskine5d309672019-07-12 23:47:28 +02001874#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1875 if( driver != NULL )
1876 {
1877 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001878 /* The driver should set the number of key bits, however in
1879 * case it doesn't, we initialize bits to an invalid value. */
1880 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001881 if( drv->key_management == NULL ||
1882 drv->key_management->p_import == NULL )
1883 {
1884 status = PSA_ERROR_NOT_SUPPORTED;
1885 goto exit;
1886 }
1887 status = drv->key_management->p_import(
1888 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001889 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001890 &bits );
1891 if( status != PSA_SUCCESS )
1892 goto exit;
1893 if( bits > PSA_MAX_KEY_BITS )
1894 {
1895 status = PSA_ERROR_NOT_SUPPORTED;
1896 goto exit;
1897 }
1898 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001899 }
1900 else
1901#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1902 {
1903 status = psa_import_key_into_slot( slot, data, data_length );
1904 if( status != PSA_SUCCESS )
1905 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001906 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001907 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001908 if( status != PSA_SUCCESS )
1909 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910
Gilles Peskine011e4282019-06-26 18:34:38 +02001911 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001913 if( status != PSA_SUCCESS )
1914 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001915 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001916 *handle = 0;
1917 }
1918 return( status );
1919}
1920
Gilles Peskined7729582019-08-05 15:55:54 +02001921#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1922psa_status_t mbedtls_psa_register_se_key(
1923 const psa_key_attributes_t *attributes )
1924{
1925 psa_status_t status;
1926 psa_key_slot_t *slot = NULL;
1927 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001928 psa_key_handle_t handle = 0;
1929
1930 /* Leaving attributes unspecified is not currently supported.
1931 * It could make sense to query the key type and size from the
1932 * secure element, but not all secure elements support this
1933 * and the driver HAL doesn't currently support it. */
1934 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1935 return( PSA_ERROR_NOT_SUPPORTED );
1936 if( psa_get_key_bits( attributes ) == 0 )
1937 return( PSA_ERROR_NOT_SUPPORTED );
1938
1939 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1940 &handle, &slot, &driver );
1941 if( status != PSA_SUCCESS )
1942 goto exit;
1943
Gilles Peskined7729582019-08-05 15:55:54 +02001944 status = psa_finish_key_creation( slot, driver );
1945
1946exit:
1947 if( status != PSA_SUCCESS )
1948 {
1949 psa_fail_key_creation( slot, driver );
1950 }
1951 /* Registration doesn't keep the key in RAM. */
1952 psa_close_key( handle );
1953 return( status );
1954}
1955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1956
Gilles Peskinef603c712019-01-19 13:40:11 +01001957static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001958 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001959{
1960 psa_status_t status;
1961 uint8_t *buffer = NULL;
1962 size_t buffer_size = 0;
1963 size_t length;
1964
Gilles Peskine8e338702019-07-30 20:06:31 +02001965 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001966 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001967 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001968 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001969 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1971 if( status != PSA_SUCCESS )
1972 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001973 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001975
1976exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001977 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001978 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001979 return( status );
1980}
1981
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1983 const psa_key_attributes_t *specified_attributes,
1984 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001985{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001987 psa_key_slot_t *source_slot = NULL;
1988 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001989 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001990 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001991
Gilles Peskine28f8f302019-07-24 13:30:31 +02001992 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001993 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001994 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 goto exit;
1996
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001997 status = psa_validate_optional_attributes( source_slot,
1998 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 if( status != PSA_SUCCESS )
2000 goto exit;
2001
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002002 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002003 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004 if( status != PSA_SUCCESS )
2005 goto exit;
2006
Gilles Peskinedf179142019-07-15 22:02:14 +02002007 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2008 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002009 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010 if( status != PSA_SUCCESS )
2011 goto exit;
2012
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2014 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002015 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002016 /* Copying to a secure element is not implemented yet. */
2017 status = PSA_ERROR_NOT_SUPPORTED;
2018 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002019 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002020#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002021
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002022 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002023 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002025
Gilles Peskine011e4282019-06-26 18:34:38 +02002026 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002027exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002028 if( status != PSA_SUCCESS )
2029 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002030 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002031 *target_handle = 0;
2032 }
2033 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002034}
2035
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002036
2037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002038/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002039/* Message digests */
2040/****************************************************************/
2041
Gilles Peskineb16841e2019-10-10 20:36:12 +02002042#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002043static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002044{
2045 switch( alg )
2046 {
2047#if defined(MBEDTLS_MD2_C)
2048 case PSA_ALG_MD2:
2049 return( &mbedtls_md2_info );
2050#endif
2051#if defined(MBEDTLS_MD4_C)
2052 case PSA_ALG_MD4:
2053 return( &mbedtls_md4_info );
2054#endif
2055#if defined(MBEDTLS_MD5_C)
2056 case PSA_ALG_MD5:
2057 return( &mbedtls_md5_info );
2058#endif
2059#if defined(MBEDTLS_RIPEMD160_C)
2060 case PSA_ALG_RIPEMD160:
2061 return( &mbedtls_ripemd160_info );
2062#endif
2063#if defined(MBEDTLS_SHA1_C)
2064 case PSA_ALG_SHA_1:
2065 return( &mbedtls_sha1_info );
2066#endif
2067#if defined(MBEDTLS_SHA256_C)
2068 case PSA_ALG_SHA_224:
2069 return( &mbedtls_sha224_info );
2070 case PSA_ALG_SHA_256:
2071 return( &mbedtls_sha256_info );
2072#endif
2073#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002074#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002075 case PSA_ALG_SHA_384:
2076 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002077#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002078 case PSA_ALG_SHA_512:
2079 return( &mbedtls_sha512_info );
2080#endif
2081 default:
2082 return( NULL );
2083 }
2084}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002085#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002086
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002087psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2088{
2089 switch( operation->alg )
2090 {
Gilles Peskine81736312018-06-26 15:04:31 +02002091 case 0:
2092 /* The object has (apparently) been initialized but it is not
2093 * in use. It's ok to call abort on such an object, and there's
2094 * nothing to do. */
2095 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002096#if defined(MBEDTLS_MD2_C)
2097 case PSA_ALG_MD2:
2098 mbedtls_md2_free( &operation->ctx.md2 );
2099 break;
2100#endif
2101#if defined(MBEDTLS_MD4_C)
2102 case PSA_ALG_MD4:
2103 mbedtls_md4_free( &operation->ctx.md4 );
2104 break;
2105#endif
2106#if defined(MBEDTLS_MD5_C)
2107 case PSA_ALG_MD5:
2108 mbedtls_md5_free( &operation->ctx.md5 );
2109 break;
2110#endif
2111#if defined(MBEDTLS_RIPEMD160_C)
2112 case PSA_ALG_RIPEMD160:
2113 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2114 break;
2115#endif
2116#if defined(MBEDTLS_SHA1_C)
2117 case PSA_ALG_SHA_1:
2118 mbedtls_sha1_free( &operation->ctx.sha1 );
2119 break;
2120#endif
2121#if defined(MBEDTLS_SHA256_C)
2122 case PSA_ALG_SHA_224:
2123 case PSA_ALG_SHA_256:
2124 mbedtls_sha256_free( &operation->ctx.sha256 );
2125 break;
2126#endif
2127#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002128#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002130#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002131 case PSA_ALG_SHA_512:
2132 mbedtls_sha512_free( &operation->ctx.sha512 );
2133 break;
2134#endif
2135 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002136 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002137 }
2138 operation->alg = 0;
2139 return( PSA_SUCCESS );
2140}
2141
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002142psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002143 psa_algorithm_t alg )
2144{
Janos Follath24eed8d2019-11-22 13:21:35 +00002145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002146
2147 /* A context must be freshly initialized before it can be set up. */
2148 if( operation->alg != 0 )
2149 {
2150 return( PSA_ERROR_BAD_STATE );
2151 }
2152
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002153 switch( alg )
2154 {
2155#if defined(MBEDTLS_MD2_C)
2156 case PSA_ALG_MD2:
2157 mbedtls_md2_init( &operation->ctx.md2 );
2158 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2159 break;
2160#endif
2161#if defined(MBEDTLS_MD4_C)
2162 case PSA_ALG_MD4:
2163 mbedtls_md4_init( &operation->ctx.md4 );
2164 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2165 break;
2166#endif
2167#if defined(MBEDTLS_MD5_C)
2168 case PSA_ALG_MD5:
2169 mbedtls_md5_init( &operation->ctx.md5 );
2170 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2171 break;
2172#endif
2173#if defined(MBEDTLS_RIPEMD160_C)
2174 case PSA_ALG_RIPEMD160:
2175 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2176 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2177 break;
2178#endif
2179#if defined(MBEDTLS_SHA1_C)
2180 case PSA_ALG_SHA_1:
2181 mbedtls_sha1_init( &operation->ctx.sha1 );
2182 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2183 break;
2184#endif
2185#if defined(MBEDTLS_SHA256_C)
2186 case PSA_ALG_SHA_224:
2187 mbedtls_sha256_init( &operation->ctx.sha256 );
2188 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2189 break;
2190 case PSA_ALG_SHA_256:
2191 mbedtls_sha256_init( &operation->ctx.sha256 );
2192 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2193 break;
2194#endif
2195#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002196#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 case PSA_ALG_SHA_384:
2198 mbedtls_sha512_init( &operation->ctx.sha512 );
2199 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2200 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002201#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 case PSA_ALG_SHA_512:
2203 mbedtls_sha512_init( &operation->ctx.sha512 );
2204 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2205 break;
2206#endif
2207 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002208 return( PSA_ALG_IS_HASH( alg ) ?
2209 PSA_ERROR_NOT_SUPPORTED :
2210 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211 }
2212 if( ret == 0 )
2213 operation->alg = alg;
2214 else
2215 psa_hash_abort( operation );
2216 return( mbedtls_to_psa_error( ret ) );
2217}
2218
2219psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2220 const uint8_t *input,
2221 size_t input_length )
2222{
Janos Follath24eed8d2019-11-22 13:21:35 +00002223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002224
2225 /* Don't require hash implementations to behave correctly on a
2226 * zero-length input, which may have an invalid pointer. */
2227 if( input_length == 0 )
2228 return( PSA_SUCCESS );
2229
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230 switch( operation->alg )
2231 {
2232#if defined(MBEDTLS_MD2_C)
2233 case PSA_ALG_MD2:
2234 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2235 input, input_length );
2236 break;
2237#endif
2238#if defined(MBEDTLS_MD4_C)
2239 case PSA_ALG_MD4:
2240 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2241 input, input_length );
2242 break;
2243#endif
2244#if defined(MBEDTLS_MD5_C)
2245 case PSA_ALG_MD5:
2246 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2247 input, input_length );
2248 break;
2249#endif
2250#if defined(MBEDTLS_RIPEMD160_C)
2251 case PSA_ALG_RIPEMD160:
2252 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2253 input, input_length );
2254 break;
2255#endif
2256#if defined(MBEDTLS_SHA1_C)
2257 case PSA_ALG_SHA_1:
2258 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2259 input, input_length );
2260 break;
2261#endif
2262#if defined(MBEDTLS_SHA256_C)
2263 case PSA_ALG_SHA_224:
2264 case PSA_ALG_SHA_256:
2265 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2266 input, input_length );
2267 break;
2268#endif
2269#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002270#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002272#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 case PSA_ALG_SHA_512:
2274 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2275 input, input_length );
2276 break;
2277#endif
2278 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002281
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282 if( ret != 0 )
2283 psa_hash_abort( operation );
2284 return( mbedtls_to_psa_error( ret ) );
2285}
2286
2287psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2288 uint8_t *hash,
2289 size_t hash_size,
2290 size_t *hash_length )
2291{
itayzafrir40835d42018-08-02 13:14:17 +03002292 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002294 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295
2296 /* Fill the output buffer with something that isn't a valid hash
2297 * (barring an attack on the hash and deliberately-crafted input),
2298 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002299 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002300 /* If hash_size is 0 then hash may be NULL and then the
2301 * call to memset would have undefined behavior. */
2302 if( hash_size != 0 )
2303 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304
2305 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002306 {
2307 status = PSA_ERROR_BUFFER_TOO_SMALL;
2308 goto exit;
2309 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310
2311 switch( operation->alg )
2312 {
2313#if defined(MBEDTLS_MD2_C)
2314 case PSA_ALG_MD2:
2315 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2316 break;
2317#endif
2318#if defined(MBEDTLS_MD4_C)
2319 case PSA_ALG_MD4:
2320 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2321 break;
2322#endif
2323#if defined(MBEDTLS_MD5_C)
2324 case PSA_ALG_MD5:
2325 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2326 break;
2327#endif
2328#if defined(MBEDTLS_RIPEMD160_C)
2329 case PSA_ALG_RIPEMD160:
2330 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2331 break;
2332#endif
2333#if defined(MBEDTLS_SHA1_C)
2334 case PSA_ALG_SHA_1:
2335 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2336 break;
2337#endif
2338#if defined(MBEDTLS_SHA256_C)
2339 case PSA_ALG_SHA_224:
2340 case PSA_ALG_SHA_256:
2341 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2342 break;
2343#endif
2344#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002345#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002347#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 case PSA_ALG_SHA_512:
2349 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2350 break;
2351#endif
2352 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002353 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354 }
itayzafrir40835d42018-08-02 13:14:17 +03002355 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356
itayzafrir40835d42018-08-02 13:14:17 +03002357exit:
2358 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002360 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361 return( psa_hash_abort( operation ) );
2362 }
2363 else
2364 {
2365 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002366 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367 }
2368}
2369
Gilles Peskine2d277862018-06-18 15:41:12 +02002370psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2371 const uint8_t *hash,
2372 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373{
2374 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2375 size_t actual_hash_length;
2376 psa_status_t status = psa_hash_finish( operation,
2377 actual_hash, sizeof( actual_hash ),
2378 &actual_hash_length );
2379 if( status != PSA_SUCCESS )
2380 return( status );
2381 if( actual_hash_length != hash_length )
2382 return( PSA_ERROR_INVALID_SIGNATURE );
2383 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2384 return( PSA_ERROR_INVALID_SIGNATURE );
2385 return( PSA_SUCCESS );
2386}
2387
Gilles Peskine0a749c82019-11-28 19:33:58 +01002388psa_status_t psa_hash_compute( psa_algorithm_t alg,
2389 const uint8_t *input, size_t input_length,
2390 uint8_t *hash, size_t hash_size,
2391 size_t *hash_length )
2392{
2393 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2395
2396 *hash_length = hash_size;
2397 status = psa_hash_setup( &operation, alg );
2398 if( status != PSA_SUCCESS )
2399 goto exit;
2400 status = psa_hash_update( &operation, input, input_length );
2401 if( status != PSA_SUCCESS )
2402 goto exit;
2403 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2404 if( status != PSA_SUCCESS )
2405 goto exit;
2406
2407exit:
2408 if( status == PSA_SUCCESS )
2409 status = psa_hash_abort( &operation );
2410 else
2411 psa_hash_abort( &operation );
2412 return( status );
2413}
2414
2415psa_status_t psa_hash_compare( psa_algorithm_t alg,
2416 const uint8_t *input, size_t input_length,
2417 const uint8_t *hash, size_t hash_length )
2418{
2419 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2421
2422 status = psa_hash_setup( &operation, alg );
2423 if( status != PSA_SUCCESS )
2424 goto exit;
2425 status = psa_hash_update( &operation, input, input_length );
2426 if( status != PSA_SUCCESS )
2427 goto exit;
2428 status = psa_hash_verify( &operation, hash, hash_length );
2429 if( status != PSA_SUCCESS )
2430 goto exit;
2431
2432exit:
2433 if( status == PSA_SUCCESS )
2434 status = psa_hash_abort( &operation );
2435 else
2436 psa_hash_abort( &operation );
2437 return( status );
2438}
2439
Gilles Peskineeb35d782019-01-22 17:56:16 +01002440psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2441 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002442{
2443 if( target_operation->alg != 0 )
2444 return( PSA_ERROR_BAD_STATE );
2445
2446 switch( source_operation->alg )
2447 {
2448 case 0:
2449 return( PSA_ERROR_BAD_STATE );
2450#if defined(MBEDTLS_MD2_C)
2451 case PSA_ALG_MD2:
2452 mbedtls_md2_clone( &target_operation->ctx.md2,
2453 &source_operation->ctx.md2 );
2454 break;
2455#endif
2456#if defined(MBEDTLS_MD4_C)
2457 case PSA_ALG_MD4:
2458 mbedtls_md4_clone( &target_operation->ctx.md4,
2459 &source_operation->ctx.md4 );
2460 break;
2461#endif
2462#if defined(MBEDTLS_MD5_C)
2463 case PSA_ALG_MD5:
2464 mbedtls_md5_clone( &target_operation->ctx.md5,
2465 &source_operation->ctx.md5 );
2466 break;
2467#endif
2468#if defined(MBEDTLS_RIPEMD160_C)
2469 case PSA_ALG_RIPEMD160:
2470 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2471 &source_operation->ctx.ripemd160 );
2472 break;
2473#endif
2474#if defined(MBEDTLS_SHA1_C)
2475 case PSA_ALG_SHA_1:
2476 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2477 &source_operation->ctx.sha1 );
2478 break;
2479#endif
2480#if defined(MBEDTLS_SHA256_C)
2481 case PSA_ALG_SHA_224:
2482 case PSA_ALG_SHA_256:
2483 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2484 &source_operation->ctx.sha256 );
2485 break;
2486#endif
2487#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002488#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002489 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002490#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002491 case PSA_ALG_SHA_512:
2492 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2493 &source_operation->ctx.sha512 );
2494 break;
2495#endif
2496 default:
2497 return( PSA_ERROR_NOT_SUPPORTED );
2498 }
2499
2500 target_operation->alg = source_operation->alg;
2501 return( PSA_SUCCESS );
2502}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503
2504
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506/* MAC */
2507/****************************************************************/
2508
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002509static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 psa_algorithm_t alg,
2511 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002512 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002513 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002516 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002518 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002519 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002520
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2522 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002523 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002525 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002526 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527 mode = MBEDTLS_MODE_STREAM;
2528 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529 case PSA_ALG_CTR:
2530 mode = MBEDTLS_MODE_CTR;
2531 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002532 case PSA_ALG_CFB:
2533 mode = MBEDTLS_MODE_CFB;
2534 break;
2535 case PSA_ALG_OFB:
2536 mode = MBEDTLS_MODE_OFB;
2537 break;
2538 case PSA_ALG_CBC_NO_PADDING:
2539 mode = MBEDTLS_MODE_CBC;
2540 break;
2541 case PSA_ALG_CBC_PKCS7:
2542 mode = MBEDTLS_MODE_CBC;
2543 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002544 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 mode = MBEDTLS_MODE_CCM;
2546 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002547 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 mode = MBEDTLS_MODE_GCM;
2549 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002550 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2551 mode = MBEDTLS_MODE_CHACHAPOLY;
2552 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553 default:
2554 return( NULL );
2555 }
2556 }
2557 else if( alg == PSA_ALG_CMAC )
2558 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 else
2560 return( NULL );
2561
2562 switch( key_type )
2563 {
2564 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002565 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 break;
2567 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002568 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2569 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002571 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002573 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002574 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2575 * but two-key Triple-DES is functionally three-key Triple-DES
2576 * with K1=K3, so that's how we present it to mbedtls. */
2577 if( key_bits == 128 )
2578 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579 break;
2580 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002581 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 break;
2583 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002584 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002586 case PSA_KEY_TYPE_CHACHA20:
2587 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2588 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589 default:
2590 return( NULL );
2591 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002592 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002593 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594
Jaeden Amero23bbb752018-06-26 14:16:54 +01002595 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2596 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597}
2598
Gilles Peskinea05219c2018-11-16 16:02:56 +01002599#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002600static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002601{
Gilles Peskine2d277862018-06-18 15:41:12 +02002602 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002603 {
2604 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002605 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002606 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002607 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002608 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002609 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002610 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002611 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002612 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002613 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002614 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002615 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002616 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002617 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002618 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002619 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002620 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002621 return( 128 );
2622 default:
2623 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002624 }
2625}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002626#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002627
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002628/* Initialize the MAC operation structure. Once this function has been
2629 * called, psa_mac_abort can run and will do the right thing. */
2630static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2631 psa_algorithm_t alg )
2632{
2633 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2634
2635 operation->alg = alg;
2636 operation->key_set = 0;
2637 operation->iv_set = 0;
2638 operation->iv_required = 0;
2639 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002640 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641
2642#if defined(MBEDTLS_CMAC_C)
2643 if( alg == PSA_ALG_CMAC )
2644 {
2645 operation->iv_required = 0;
2646 mbedtls_cipher_init( &operation->ctx.cmac );
2647 status = PSA_SUCCESS;
2648 }
2649 else
2650#endif /* MBEDTLS_CMAC_C */
2651#if defined(MBEDTLS_MD_C)
2652 if( PSA_ALG_IS_HMAC( operation->alg ) )
2653 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002654 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2655 operation->ctx.hmac.hash_ctx.alg = 0;
2656 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657 }
2658 else
2659#endif /* MBEDTLS_MD_C */
2660 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002661 if( ! PSA_ALG_IS_MAC( alg ) )
2662 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002663 }
2664
2665 if( status != PSA_SUCCESS )
2666 memset( operation, 0, sizeof( *operation ) );
2667 return( status );
2668}
2669
Gilles Peskine01126fa2018-07-12 17:04:55 +02002670#if defined(MBEDTLS_MD_C)
2671static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2672{
Gilles Peskine3f108122018-12-07 18:14:53 +01002673 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002674 return( psa_hash_abort( &hmac->hash_ctx ) );
2675}
2676#endif /* MBEDTLS_MD_C */
2677
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2679{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002680 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 /* The object has (apparently) been initialized but it is not
2683 * in use. It's ok to call abort on such an object, and there's
2684 * nothing to do. */
2685 return( PSA_SUCCESS );
2686 }
2687 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002689 if( operation->alg == PSA_ALG_CMAC )
2690 {
2691 mbedtls_cipher_free( &operation->ctx.cmac );
2692 }
2693 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002696 if( PSA_ALG_IS_HMAC( operation->alg ) )
2697 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002698 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002699 }
2700 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002702 {
2703 /* Sanity check (shouldn't happen: operation->alg should
2704 * always have been initialized to a valid value). */
2705 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706 }
Moran Peker41deec42018-04-04 15:43:05 +03002707
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 operation->alg = 0;
2709 operation->key_set = 0;
2710 operation->iv_set = 0;
2711 operation->iv_required = 0;
2712 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002713 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002714
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002716
2717bad_state:
2718 /* If abort is called on an uninitialized object, we can't trust
2719 * anything. Wipe the object in case it contains confidential data.
2720 * This may result in a memory leak if a pointer gets overwritten,
2721 * but it's too late to do anything about this. */
2722 memset( operation, 0, sizeof( *operation ) );
2723 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724}
2725
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002726#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002727static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002728 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002729 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730 const mbedtls_cipher_info_t *cipher_info )
2731{
Janos Follath24eed8d2019-11-22 13:21:35 +00002732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002733
2734 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002735
2736 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2737 if( ret != 0 )
2738 return( ret );
2739
2740 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2741 slot->data.raw.data,
2742 key_bits );
2743 return( ret );
2744}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002745#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746
Gilles Peskine248051a2018-06-20 16:09:38 +02002747#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002748static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2749 const uint8_t *key,
2750 size_t key_length,
2751 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002752{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002753 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002754 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002755 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002756 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002757 psa_status_t status;
2758
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002759 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2760 * overflow below. This should never trigger if the hash algorithm
2761 * is implemented correctly. */
2762 /* The size checks against the ipad and opad buffers cannot be written
2763 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2764 * because that triggers -Wlogical-op on GCC 7.3. */
2765 if( block_size > sizeof( ipad ) )
2766 return( PSA_ERROR_NOT_SUPPORTED );
2767 if( block_size > sizeof( hmac->opad ) )
2768 return( PSA_ERROR_NOT_SUPPORTED );
2769 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770 return( PSA_ERROR_NOT_SUPPORTED );
2771
Gilles Peskined223b522018-06-11 18:12:58 +02002772 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002774 status = psa_hash_compute( hash_alg, key, key_length,
2775 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002776 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002777 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002778 }
Gilles Peskine96889972018-07-12 17:07:03 +02002779 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2780 * but it is permitted. It is common when HMAC is used in HKDF, for
2781 * example. Don't call `memcpy` in the 0-length because `key` could be
2782 * an invalid pointer which would make the behavior undefined. */
2783 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002784 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785
Gilles Peskined223b522018-06-11 18:12:58 +02002786 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2787 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002789 ipad[i] ^= 0x36;
2790 memset( ipad + key_length, 0x36, block_size - key_length );
2791
2792 /* Copy the key material from ipad to opad, flipping the requisite bits,
2793 * and filling the rest of opad with the requisite constant. */
2794 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002795 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2796 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002797
Gilles Peskine01126fa2018-07-12 17:04:55 +02002798 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002799 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002800 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801
Gilles Peskine01126fa2018-07-12 17:04:55 +02002802 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002803
2804cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002805 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002806
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002807 return( status );
2808}
Gilles Peskine248051a2018-06-20 16:09:38 +02002809#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002810
Gilles Peskine89167cb2018-07-08 20:12:23 +02002811static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002812 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002813 psa_algorithm_t alg,
2814 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002817 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002819 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002820 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002821 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002822 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002824 /* A context must be freshly initialized before it can be set up. */
2825 if( operation->alg != 0 )
2826 {
2827 return( PSA_ERROR_BAD_STATE );
2828 }
2829
Gilles Peskined911eb72018-08-14 15:18:45 +02002830 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002831 if( status != PSA_SUCCESS )
2832 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002833 if( is_sign )
2834 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835
Gilles Peskine28f8f302019-07-24 13:30:31 +02002836 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002837 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002838 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002839 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002840
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002842 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002843 {
2844 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002845 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002846 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002848 if( cipher_info == NULL )
2849 {
2850 status = PSA_ERROR_NOT_SUPPORTED;
2851 goto exit;
2852 }
2853 operation->mac_size = cipher_info->block_size;
2854 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2855 status = mbedtls_to_psa_error( ret );
2856 }
2857 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002860 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002861 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002862 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002863 if( hash_alg == 0 )
2864 {
2865 status = PSA_ERROR_NOT_SUPPORTED;
2866 goto exit;
2867 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002868
2869 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2870 /* Sanity check. This shouldn't fail on a valid configuration. */
2871 if( operation->mac_size == 0 ||
2872 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2873 {
2874 status = PSA_ERROR_NOT_SUPPORTED;
2875 goto exit;
2876 }
2877
Gilles Peskine8e338702019-07-30 20:06:31 +02002878 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002879 {
2880 status = PSA_ERROR_INVALID_ARGUMENT;
2881 goto exit;
2882 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002883
Gilles Peskine01126fa2018-07-12 17:04:55 +02002884 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2885 slot->data.raw.data,
2886 slot->data.raw.bytes,
2887 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002888 }
2889 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002891 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002892 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002893 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002895
Gilles Peskined911eb72018-08-14 15:18:45 +02002896 if( truncated == 0 )
2897 {
2898 /* The "normal" case: untruncated algorithm. Nothing to do. */
2899 }
2900 else if( truncated < 4 )
2901 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002902 /* A very short MAC is too short for security since it can be
2903 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2904 * so we make this our minimum, even though 32 bits is still
2905 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002906 status = PSA_ERROR_NOT_SUPPORTED;
2907 }
2908 else if( truncated > operation->mac_size )
2909 {
2910 /* It's impossible to "truncate" to a larger length. */
2911 status = PSA_ERROR_INVALID_ARGUMENT;
2912 }
2913 else
2914 operation->mac_size = truncated;
2915
Gilles Peskinefbfac682018-07-08 20:51:54 +02002916exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002917 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002919 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002920 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002921 else
2922 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002923 operation->key_set = 1;
2924 }
2925 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926}
2927
Gilles Peskine89167cb2018-07-08 20:12:23 +02002928psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002929 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002930 psa_algorithm_t alg )
2931{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002932 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002933}
2934
2935psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002936 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002937 psa_algorithm_t alg )
2938{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002939 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002940}
2941
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2943 const uint8_t *input,
2944 size_t input_length )
2945{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002946 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002948 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951 operation->has_input = 1;
2952
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002955 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2957 input, input_length );
2958 status = mbedtls_to_psa_error( ret );
2959 }
2960 else
2961#endif /* MBEDTLS_CMAC_C */
2962#if defined(MBEDTLS_MD_C)
2963 if( PSA_ALG_IS_HMAC( operation->alg ) )
2964 {
2965 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2966 input_length );
2967 }
2968 else
2969#endif /* MBEDTLS_MD_C */
2970 {
2971 /* This shouldn't happen if `operation` was initialized by
2972 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002973 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002974 }
2975
Gilles Peskinefbfac682018-07-08 20:51:54 +02002976 if( status != PSA_SUCCESS )
2977 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002978 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002979}
2980
Gilles Peskine01126fa2018-07-12 17:04:55 +02002981#if defined(MBEDTLS_MD_C)
2982static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2983 uint8_t *mac,
2984 size_t mac_size )
2985{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002986 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002987 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2988 size_t hash_size = 0;
2989 size_t block_size = psa_get_hash_block_size( hash_alg );
2990 psa_status_t status;
2991
Gilles Peskine01126fa2018-07-12 17:04:55 +02002992 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2993 if( status != PSA_SUCCESS )
2994 return( status );
2995 /* From here on, tmp needs to be wiped. */
2996
2997 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2998 if( status != PSA_SUCCESS )
2999 goto exit;
3000
3001 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3002 if( status != PSA_SUCCESS )
3003 goto exit;
3004
3005 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3006 if( status != PSA_SUCCESS )
3007 goto exit;
3008
Gilles Peskined911eb72018-08-14 15:18:45 +02003009 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3010 if( status != PSA_SUCCESS )
3011 goto exit;
3012
3013 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003014
3015exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003016 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003017 return( status );
3018}
3019#endif /* MBEDTLS_MD_C */
3020
mohammad16036df908f2018-04-02 08:34:15 -07003021static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003022 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003023 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003025 if( ! operation->key_set )
3026 return( PSA_ERROR_BAD_STATE );
3027 if( operation->iv_required && ! operation->iv_set )
3028 return( PSA_ERROR_BAD_STATE );
3029
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030 if( mac_size < operation->mac_size )
3031 return( PSA_ERROR_BUFFER_TOO_SMALL );
3032
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003034 if( operation->alg == PSA_ALG_CMAC )
3035 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003036 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3037 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3038 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003039 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003040 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003041 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003043 else
3044#endif /* MBEDTLS_CMAC_C */
3045#if defined(MBEDTLS_MD_C)
3046 if( PSA_ALG_IS_HMAC( operation->alg ) )
3047 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003048 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003049 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003050 }
3051 else
3052#endif /* MBEDTLS_MD_C */
3053 {
3054 /* This shouldn't happen if `operation` was initialized by
3055 * a setup function. */
3056 return( PSA_ERROR_BAD_STATE );
3057 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058}
3059
Gilles Peskineacd4be32018-07-08 19:56:25 +02003060psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3061 uint8_t *mac,
3062 size_t mac_size,
3063 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003064{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003065 psa_status_t status;
3066
Jaeden Amero252ef282019-02-15 14:05:35 +00003067 if( operation->alg == 0 )
3068 {
3069 return( PSA_ERROR_BAD_STATE );
3070 }
3071
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003072 /* Fill the output buffer with something that isn't a valid mac
3073 * (barring an attack on the mac and deliberately-crafted input),
3074 * in case the caller doesn't check the return status properly. */
3075 *mac_length = mac_size;
3076 /* If mac_size is 0 then mac may be NULL and then the
3077 * call to memset would have undefined behavior. */
3078 if( mac_size != 0 )
3079 memset( mac, '!', mac_size );
3080
Gilles Peskine89167cb2018-07-08 20:12:23 +02003081 if( ! operation->is_sign )
3082 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003083 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003084 }
mohammad16036df908f2018-04-02 08:34:15 -07003085
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003086 status = psa_mac_finish_internal( operation, mac, mac_size );
3087
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003088 if( status == PSA_SUCCESS )
3089 {
3090 status = psa_mac_abort( operation );
3091 if( status == PSA_SUCCESS )
3092 *mac_length = operation->mac_size;
3093 else
3094 memset( mac, '!', mac_size );
3095 }
3096 else
3097 psa_mac_abort( operation );
3098 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003099}
3100
Gilles Peskineacd4be32018-07-08 19:56:25 +02003101psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3102 const uint8_t *mac,
3103 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104{
Gilles Peskine828ed142018-06-18 23:25:51 +02003105 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003106 psa_status_t status;
3107
Jaeden Amero252ef282019-02-15 14:05:35 +00003108 if( operation->alg == 0 )
3109 {
3110 return( PSA_ERROR_BAD_STATE );
3111 }
3112
Gilles Peskine89167cb2018-07-08 20:12:23 +02003113 if( operation->is_sign )
3114 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003115 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003116 }
3117 if( operation->mac_size != mac_length )
3118 {
3119 status = PSA_ERROR_INVALID_SIGNATURE;
3120 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003121 }
mohammad16036df908f2018-04-02 08:34:15 -07003122
3123 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003124 actual_mac, sizeof( actual_mac ) );
3125
3126 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3127 status = PSA_ERROR_INVALID_SIGNATURE;
3128
3129cleanup:
3130 if( status == PSA_SUCCESS )
3131 status = psa_mac_abort( operation );
3132 else
3133 psa_mac_abort( operation );
3134
Gilles Peskine3f108122018-12-07 18:14:53 +01003135 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003136
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003137 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003138}
3139
3140
Gilles Peskine20035e32018-02-03 22:44:14 +01003141
Gilles Peskine20035e32018-02-03 22:44:14 +01003142/****************************************************************/
3143/* Asymmetric cryptography */
3144/****************************************************************/
3145
Gilles Peskine2b450e32018-06-27 15:42:46 +02003146#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003147/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003148 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003149static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3150 size_t hash_length,
3151 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003152{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003153 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003154 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003155 *md_alg = mbedtls_md_get_type( md_info );
3156
3157 /* The Mbed TLS RSA module uses an unsigned int for hash length
3158 * parameters. Validate that it fits so that we don't risk an
3159 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003160#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003161 if( hash_length > UINT_MAX )
3162 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003163#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003164
3165#if defined(MBEDTLS_PKCS1_V15)
3166 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3167 * must be correct. */
3168 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3169 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003170 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003171 if( md_info == NULL )
3172 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003173 if( mbedtls_md_get_size( md_info ) != hash_length )
3174 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003175 }
3176#endif /* MBEDTLS_PKCS1_V15 */
3177
3178#if defined(MBEDTLS_PKCS1_V21)
3179 /* PSS requires a hash internally. */
3180 if( PSA_ALG_IS_RSA_PSS( alg ) )
3181 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003182 if( md_info == NULL )
3183 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003184 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003185#endif /* MBEDTLS_PKCS1_V21 */
3186
Gilles Peskine61b91d42018-06-08 16:09:36 +02003187 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003188}
3189
Gilles Peskine2b450e32018-06-27 15:42:46 +02003190static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3191 psa_algorithm_t alg,
3192 const uint8_t *hash,
3193 size_t hash_length,
3194 uint8_t *signature,
3195 size_t signature_size,
3196 size_t *signature_length )
3197{
3198 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003200 mbedtls_md_type_t md_alg;
3201
3202 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3203 if( status != PSA_SUCCESS )
3204 return( status );
3205
Gilles Peskine630a18a2018-06-29 17:49:35 +02003206 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003207 return( PSA_ERROR_BUFFER_TOO_SMALL );
3208
3209#if defined(MBEDTLS_PKCS1_V15)
3210 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3211 {
3212 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3213 MBEDTLS_MD_NONE );
3214 ret = mbedtls_rsa_pkcs1_sign( rsa,
3215 mbedtls_ctr_drbg_random,
3216 &global_data.ctr_drbg,
3217 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003218 md_alg,
3219 (unsigned int) hash_length,
3220 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003221 signature );
3222 }
3223 else
3224#endif /* MBEDTLS_PKCS1_V15 */
3225#if defined(MBEDTLS_PKCS1_V21)
3226 if( PSA_ALG_IS_RSA_PSS( alg ) )
3227 {
3228 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3229 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3230 mbedtls_ctr_drbg_random,
3231 &global_data.ctr_drbg,
3232 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003233 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003234 (unsigned int) hash_length,
3235 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003236 signature );
3237 }
3238 else
3239#endif /* MBEDTLS_PKCS1_V21 */
3240 {
3241 return( PSA_ERROR_INVALID_ARGUMENT );
3242 }
3243
3244 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003245 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003246 return( mbedtls_to_psa_error( ret ) );
3247}
3248
3249static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3250 psa_algorithm_t alg,
3251 const uint8_t *hash,
3252 size_t hash_length,
3253 const uint8_t *signature,
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 Peskine89cc74f2019-09-12 22:08:23 +02003264 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3265 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266
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_verify( rsa,
3273 mbedtls_ctr_drbg_random,
3274 &global_data.ctr_drbg,
3275 MBEDTLS_RSA_PUBLIC,
3276 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003277 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003278 hash,
3279 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_verify( rsa,
3288 mbedtls_ctr_drbg_random,
3289 &global_data.ctr_drbg,
3290 MBEDTLS_RSA_PUBLIC,
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 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003301
3302 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3303 * the rest of the signature is invalid". This has little use in
3304 * practice and PSA doesn't report this distinction. */
3305 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3306 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003307 return( mbedtls_to_psa_error( ret ) );
3308}
3309#endif /* MBEDTLS_RSA_C */
3310
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003311#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003312/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3313 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3314 * (even though these functions don't modify it). */
3315static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3316 psa_algorithm_t alg,
3317 const uint8_t *hash,
3318 size_t hash_length,
3319 uint8_t *signature,
3320 size_t signature_size,
3321 size_t *signature_length )
3322{
Janos Follath24eed8d2019-11-22 13:21:35 +00003323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003324 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003325 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003326 mbedtls_mpi_init( &r );
3327 mbedtls_mpi_init( &s );
3328
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003329 if( signature_size < 2 * curve_bytes )
3330 {
3331 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3332 goto cleanup;
3333 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003334
Gilles Peskinea05219c2018-11-16 16:02:56 +01003335#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003336 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3337 {
3338 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3339 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3340 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003341 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3342 &ecp->d, hash,
3343 hash_length, md_alg,
3344 mbedtls_ctr_drbg_random,
3345 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003346 }
3347 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003348#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003349 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003350 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003351 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3352 hash, hash_length,
3353 mbedtls_ctr_drbg_random,
3354 &global_data.ctr_drbg ) );
3355 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003356
3357 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3358 signature,
3359 curve_bytes ) );
3360 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3361 signature + curve_bytes,
3362 curve_bytes ) );
3363
3364cleanup:
3365 mbedtls_mpi_free( &r );
3366 mbedtls_mpi_free( &s );
3367 if( ret == 0 )
3368 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003369 return( mbedtls_to_psa_error( ret ) );
3370}
3371
3372static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3373 const uint8_t *hash,
3374 size_t hash_length,
3375 const uint8_t *signature,
3376 size_t signature_length )
3377{
Janos Follath24eed8d2019-11-22 13:21:35 +00003378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003379 mbedtls_mpi r, s;
3380 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3381 mbedtls_mpi_init( &r );
3382 mbedtls_mpi_init( &s );
3383
3384 if( signature_length != 2 * curve_bytes )
3385 return( PSA_ERROR_INVALID_SIGNATURE );
3386
3387 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3388 signature,
3389 curve_bytes ) );
3390 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3391 signature + curve_bytes,
3392 curve_bytes ) );
3393
3394 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3395 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003396
3397cleanup:
3398 mbedtls_mpi_free( &r );
3399 mbedtls_mpi_free( &s );
3400 return( mbedtls_to_psa_error( ret ) );
3401}
3402#endif /* MBEDTLS_ECDSA_C */
3403
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003404psa_status_t psa_sign_hash( psa_key_handle_t handle,
3405 psa_algorithm_t alg,
3406 const uint8_t *hash,
3407 size_t hash_length,
3408 uint8_t *signature,
3409 size_t signature_size,
3410 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003411{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003412 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003413 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003414#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3415 const psa_drv_se_t *drv;
3416 psa_drv_se_context_t *drv_context;
3417#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003418
3419 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003420 /* Immediately reject a zero-length signature buffer. This guarantees
3421 * that signature must be a valid pointer. (On the other hand, the hash
3422 * buffer can in principle be empty since it doesn't actually have
3423 * to be a hash.) */
3424 if( signature_size == 0 )
3425 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003426
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003427 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003429 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003430 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003431 {
3432 status = PSA_ERROR_INVALID_ARGUMENT;
3433 goto exit;
3434 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003435
Gilles Peskineedc64242019-08-07 21:05:07 +02003436#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3437 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3438 {
3439 if( drv->asymmetric == NULL ||
3440 drv->asymmetric->p_sign == NULL )
3441 {
3442 status = PSA_ERROR_NOT_SUPPORTED;
3443 goto exit;
3444 }
3445 status = drv->asymmetric->p_sign( drv_context,
3446 slot->data.se.slot_number,
3447 alg,
3448 hash, hash_length,
3449 signature, signature_size,
3450 signature_length );
3451 }
3452 else
3453#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003454#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003455 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003456 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003457 status = psa_rsa_sign( slot->data.rsa,
3458 alg,
3459 hash, hash_length,
3460 signature, signature_size,
3461 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003462 }
3463 else
3464#endif /* defined(MBEDTLS_RSA_C) */
3465#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003466 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003467 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003468#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003469 if(
3470#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3471 PSA_ALG_IS_ECDSA( alg )
3472#else
3473 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3474#endif
3475 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003476 status = psa_ecdsa_sign( slot->data.ecp,
3477 alg,
3478 hash, hash_length,
3479 signature, signature_size,
3480 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003481 else
3482#endif /* defined(MBEDTLS_ECDSA_C) */
3483 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003484 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003485 }
itayzafrir5c753392018-05-08 11:18:38 +03003486 }
3487 else
3488#endif /* defined(MBEDTLS_ECP_C) */
3489 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003490 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003491 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003492
3493exit:
3494 /* Fill the unused part of the output buffer (the whole buffer on error,
3495 * the trailing part on success) with something that isn't a valid mac
3496 * (barring an attack on the mac and deliberately-crafted input),
3497 * in case the caller doesn't check the return status properly. */
3498 if( status == PSA_SUCCESS )
3499 memset( signature + *signature_length, '!',
3500 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003501 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003502 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003503 /* If signature_size is 0 then we have nothing to do. We must not call
3504 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003505 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003506}
3507
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003508psa_status_t psa_verify_hash( psa_key_handle_t handle,
3509 psa_algorithm_t alg,
3510 const uint8_t *hash,
3511 size_t hash_length,
3512 const uint8_t *signature,
3513 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003514{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003515 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003516 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003517#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3518 const psa_drv_se_t *drv;
3519 psa_drv_se_context_t *drv_context;
3520#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003521
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003522 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003523 if( status != PSA_SUCCESS )
3524 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003525
Gilles Peskineedc64242019-08-07 21:05:07 +02003526#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3527 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3528 {
3529 if( drv->asymmetric == NULL ||
3530 drv->asymmetric->p_verify == NULL )
3531 return( PSA_ERROR_NOT_SUPPORTED );
3532 return( drv->asymmetric->p_verify( drv_context,
3533 slot->data.se.slot_number,
3534 alg,
3535 hash, hash_length,
3536 signature, signature_length ) );
3537 }
3538 else
3539#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003540#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003541 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003543 return( psa_rsa_verify( slot->data.rsa,
3544 alg,
3545 hash, hash_length,
3546 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547 }
3548 else
3549#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003550#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003551 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003552 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553#if defined(MBEDTLS_ECDSA_C)
3554 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003555 return( psa_ecdsa_verify( slot->data.ecp,
3556 hash, hash_length,
3557 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003558 else
3559#endif /* defined(MBEDTLS_ECDSA_C) */
3560 {
3561 return( PSA_ERROR_INVALID_ARGUMENT );
3562 }
itayzafrir5c753392018-05-08 11:18:38 +03003563 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003564 else
3565#endif /* defined(MBEDTLS_ECP_C) */
3566 {
3567 return( PSA_ERROR_NOT_SUPPORTED );
3568 }
3569}
3570
Gilles Peskine072ac562018-06-30 00:21:29 +02003571#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3572static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3573 mbedtls_rsa_context *rsa )
3574{
3575 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3576 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3577 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3578 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3579}
3580#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3581
Gilles Peskinec5487a82018-12-03 18:08:14 +01003582psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003583 psa_algorithm_t alg,
3584 const uint8_t *input,
3585 size_t input_length,
3586 const uint8_t *salt,
3587 size_t salt_length,
3588 uint8_t *output,
3589 size_t output_size,
3590 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003592 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003593 psa_status_t status;
3594
Darryl Green5cc689a2018-07-24 15:34:10 +01003595 (void) input;
3596 (void) input_length;
3597 (void) salt;
3598 (void) output;
3599 (void) output_size;
3600
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003601 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003603 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3604 return( PSA_ERROR_INVALID_ARGUMENT );
3605
Gilles Peskine28f8f302019-07-24 13:30:31 +02003606 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003607 if( status != PSA_SUCCESS )
3608 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003609 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3610 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612
3613#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003614 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 {
3616 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003618 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003619 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003621 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003622 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003623 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3624 mbedtls_ctr_drbg_random,
3625 &global_data.ctr_drbg,
3626 MBEDTLS_RSA_PUBLIC,
3627 input_length,
3628 input,
3629 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630 }
3631 else
3632#endif /* MBEDTLS_PKCS1_V15 */
3633#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003634 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003636 psa_rsa_oaep_set_padding_mode( alg, rsa );
3637 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3638 mbedtls_ctr_drbg_random,
3639 &global_data.ctr_drbg,
3640 MBEDTLS_RSA_PUBLIC,
3641 salt, salt_length,
3642 input_length,
3643 input,
3644 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645 }
3646 else
3647#endif /* MBEDTLS_PKCS1_V21 */
3648 {
3649 return( PSA_ERROR_INVALID_ARGUMENT );
3650 }
3651 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003652 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003653 return( mbedtls_to_psa_error( ret ) );
3654 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003655 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003656#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003657 {
3658 return( PSA_ERROR_NOT_SUPPORTED );
3659 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003660}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003661
Gilles Peskinec5487a82018-12-03 18:08:14 +01003662psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003663 psa_algorithm_t alg,
3664 const uint8_t *input,
3665 size_t input_length,
3666 const uint8_t *salt,
3667 size_t salt_length,
3668 uint8_t *output,
3669 size_t output_size,
3670 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003672 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003673 psa_status_t status;
3674
Darryl Green5cc689a2018-07-24 15:34:10 +01003675 (void) input;
3676 (void) input_length;
3677 (void) salt;
3678 (void) output;
3679 (void) output_size;
3680
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003681 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003683 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3684 return( PSA_ERROR_INVALID_ARGUMENT );
3685
Gilles Peskine28f8f302019-07-24 13:30:31 +02003686 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003687 if( status != PSA_SUCCESS )
3688 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003689 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690 return( PSA_ERROR_INVALID_ARGUMENT );
3691
3692#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003693 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003694 {
3695 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697
Gilles Peskine630a18a2018-06-29 17:49:35 +02003698 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699 return( PSA_ERROR_INVALID_ARGUMENT );
3700
3701#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003702 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003704 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3705 mbedtls_ctr_drbg_random,
3706 &global_data.ctr_drbg,
3707 MBEDTLS_RSA_PRIVATE,
3708 output_length,
3709 input,
3710 output,
3711 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712 }
3713 else
3714#endif /* MBEDTLS_PKCS1_V15 */
3715#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003716 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003718 psa_rsa_oaep_set_padding_mode( alg, rsa );
3719 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3720 mbedtls_ctr_drbg_random,
3721 &global_data.ctr_drbg,
3722 MBEDTLS_RSA_PRIVATE,
3723 salt, salt_length,
3724 output_length,
3725 input,
3726 output,
3727 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 }
3729 else
3730#endif /* MBEDTLS_PKCS1_V21 */
3731 {
3732 return( PSA_ERROR_INVALID_ARGUMENT );
3733 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003734
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003735 return( mbedtls_to_psa_error( ret ) );
3736 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003737 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003738#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003739 {
3740 return( PSA_ERROR_NOT_SUPPORTED );
3741 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003742}
Gilles Peskine20035e32018-02-03 22:44:14 +01003743
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003744
3745
mohammad1603503973b2018-03-12 15:59:30 +02003746/****************************************************************/
3747/* Symmetric cryptography */
3748/****************************************************************/
3749
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003750/* Initialize the cipher operation structure. Once this function has been
3751 * called, psa_cipher_abort can run and will do the right thing. */
3752static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3753 psa_algorithm_t alg )
3754{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003755 if( ! PSA_ALG_IS_CIPHER( alg ) )
3756 {
3757 memset( operation, 0, sizeof( *operation ) );
3758 return( PSA_ERROR_INVALID_ARGUMENT );
3759 }
3760
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003761 operation->alg = alg;
3762 operation->key_set = 0;
3763 operation->iv_set = 0;
3764 operation->iv_required = 1;
3765 operation->iv_size = 0;
3766 operation->block_size = 0;
3767 mbedtls_cipher_init( &operation->ctx.cipher );
3768 return( PSA_SUCCESS );
3769}
3770
Gilles Peskinee553c652018-06-04 16:22:46 +02003771static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003772 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003773 psa_algorithm_t alg,
3774 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003775{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003776 int ret = 0;
3777 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003778 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003779 size_t key_bits;
3780 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003781 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3782 PSA_KEY_USAGE_ENCRYPT :
3783 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003784
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003785 /* A context must be freshly initialized before it can be set up. */
3786 if( operation->alg != 0 )
3787 {
3788 return( PSA_ERROR_BAD_STATE );
3789 }
3790
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003791 status = psa_cipher_init( operation, alg );
3792 if( status != PSA_SUCCESS )
3793 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003794
Gilles Peskine28f8f302019-07-24 13:30:31 +02003795 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003796 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003797 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003798 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003799
Gilles Peskine8e338702019-07-30 20:06:31 +02003800 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003801 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003802 {
3803 status = PSA_ERROR_NOT_SUPPORTED;
3804 goto exit;
3805 }
mohammad1603503973b2018-03-12 15:59:30 +02003806
mohammad1603503973b2018-03-12 15:59:30 +02003807 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003808 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003809 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003810
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003811#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003812 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003813 {
3814 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003815 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003816 memcpy( keys, slot->data.raw.data, 16 );
3817 memcpy( keys + 16, slot->data.raw.data, 8 );
3818 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3819 keys,
3820 192, cipher_operation );
3821 }
3822 else
3823#endif
3824 {
3825 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3826 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003827 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003828 }
Moran Peker41deec42018-04-04 15:43:05 +03003829 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003830 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003831
mohammad16038481e742018-03-18 13:57:31 +02003832#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003833 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003834 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003835 case PSA_ALG_CBC_NO_PADDING:
3836 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3837 MBEDTLS_PADDING_NONE );
3838 break;
3839 case PSA_ALG_CBC_PKCS7:
3840 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3841 MBEDTLS_PADDING_PKCS7 );
3842 break;
3843 default:
3844 /* The algorithm doesn't involve padding. */
3845 ret = 0;
3846 break;
3847 }
3848 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003849 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003850#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3851
mohammad1603503973b2018-03-12 15:59:30 +02003852 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003853 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003854 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003855 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003856 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003857 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003858 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003859#if defined(MBEDTLS_CHACHA20_C)
3860 else
3861 if( alg == PSA_ALG_CHACHA20 )
3862 operation->iv_size = 12;
3863#endif
mohammad1603503973b2018-03-12 15:59:30 +02003864
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003865exit:
3866 if( status == 0 )
3867 status = mbedtls_to_psa_error( ret );
3868 if( status != 0 )
3869 psa_cipher_abort( operation );
3870 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003871}
3872
Gilles Peskinefe119512018-07-08 21:39:34 +02003873psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003874 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003875 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003876{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003877 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003878}
3879
Gilles Peskinefe119512018-07-08 21:39:34 +02003880psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003881 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003882 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003883{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003884 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003885}
3886
Gilles Peskinefe119512018-07-08 21:39:34 +02003887psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003888 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003889 size_t iv_size,
3890 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003891{
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003894 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003896 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 }
Moran Peker41deec42018-04-04 15:43:05 +03003898 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003899 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003900 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003901 goto exit;
3902 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003903 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3904 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003905 if( ret != 0 )
3906 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003908 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003909 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003910
mohammad16038481e742018-03-18 13:57:31 +02003911 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003913
Moran Peker395db872018-05-31 14:07:14 +03003914exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003916 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003917 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003918}
3919
Gilles Peskinefe119512018-07-08 21:39:34 +02003920psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003921 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003922 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003923{
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003926 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003928 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003929 }
Moran Pekera28258c2018-05-29 16:25:04 +03003930 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003931 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003932 status = PSA_ERROR_INVALID_ARGUMENT;
3933 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003934 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003935 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3936 status = mbedtls_to_psa_error( ret );
3937exit:
3938 if( status == PSA_SUCCESS )
3939 operation->iv_set = 1;
3940 else
mohammad1603503973b2018-03-12 15:59:30 +02003941 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003942 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003943}
3944
Gilles Peskinee553c652018-06-04 16:22:46 +02003945psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3946 const uint8_t *input,
3947 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003948 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003949 size_t output_size,
3950 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003951{
itayzafrir534bd7c2018-08-02 13:56:32 +03003952 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003953 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003954 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003955
3956 if( operation->alg == 0 )
3957 {
3958 return( PSA_ERROR_BAD_STATE );
3959 }
3960
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003961 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003962 {
3963 /* Take the unprocessed partial block left over from previous
3964 * update calls, if any, plus the input to this call. Remove
3965 * the last partial block, if any. You get the data that will be
3966 * output in this call. */
3967 expected_output_size =
3968 ( operation->ctx.cipher.unprocessed_len + input_length )
3969 / operation->block_size * operation->block_size;
3970 }
3971 else
3972 {
3973 expected_output_size = input_length;
3974 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003975
Gilles Peskine89d789c2018-06-04 17:17:16 +02003976 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003977 {
3978 status = PSA_ERROR_BUFFER_TOO_SMALL;
3979 goto exit;
3980 }
mohammad160382759612018-03-12 18:16:40 +02003981
mohammad1603503973b2018-03-12 15:59:30 +02003982 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003983 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003984 status = mbedtls_to_psa_error( ret );
3985exit:
3986 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003987 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003988 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003989}
3990
Gilles Peskinee553c652018-06-04 16:22:46 +02003991psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3992 uint8_t *output,
3993 size_t output_size,
3994 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003995{
David Saadab4ecc272019-02-14 13:48:10 +02003996 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003997 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003998 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003999
mohammad1603503973b2018-03-12 15:59:30 +02004000 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004001 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004002 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004003 }
4004 if( operation->iv_required && ! operation->iv_set )
4005 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004006 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004007 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004008
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004009 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004010 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4011 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004012 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004013 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004014 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004015 }
4016
Janos Follath315b51c2018-07-09 16:04:51 +01004017 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4018 temp_output_buffer,
4019 output_length );
4020 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004021 {
Janos Follath315b51c2018-07-09 16:04:51 +01004022 status = mbedtls_to_psa_error( cipher_ret );
4023 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004024 }
Janos Follath315b51c2018-07-09 16:04:51 +01004025
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004026 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004027 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004028 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004029 memcpy( output, temp_output_buffer, *output_length );
4030 else
4031 {
Janos Follath315b51c2018-07-09 16:04:51 +01004032 status = PSA_ERROR_BUFFER_TOO_SMALL;
4033 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004034 }
mohammad1603503973b2018-03-12 15:59:30 +02004035
Gilles Peskine3f108122018-12-07 18:14:53 +01004036 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004037 status = psa_cipher_abort( operation );
4038
4039 return( status );
4040
4041error:
4042
4043 *output_length = 0;
4044
Gilles Peskine3f108122018-12-07 18:14:53 +01004045 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004046 (void) psa_cipher_abort( operation );
4047
4048 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004049}
4050
Gilles Peskinee553c652018-06-04 16:22:46 +02004051psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4052{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004053 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004054 {
4055 /* The object has (apparently) been initialized but it is not
4056 * in use. It's ok to call abort on such an object, and there's
4057 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004058 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004059 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004060
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004061 /* Sanity check (shouldn't happen: operation->alg should
4062 * always have been initialized to a valid value). */
4063 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4064 return( PSA_ERROR_BAD_STATE );
4065
mohammad1603503973b2018-03-12 15:59:30 +02004066 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004067
Moran Peker41deec42018-04-04 15:43:05 +03004068 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004069 operation->key_set = 0;
4070 operation->iv_set = 0;
4071 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004072 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004073 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004074
Moran Peker395db872018-05-31 14:07:14 +03004075 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004076}
4077
Gilles Peskinea0655c32018-04-30 17:06:50 +02004078
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004079
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004080
mohammad16035955c982018-04-26 00:53:03 +03004081/****************************************************************/
4082/* AEAD */
4083/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004084
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085typedef struct
4086{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004087 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 const mbedtls_cipher_info_t *cipher_info;
4089 union
4090 {
4091#if defined(MBEDTLS_CCM_C)
4092 mbedtls_ccm_context ccm;
4093#endif /* MBEDTLS_CCM_C */
4094#if defined(MBEDTLS_GCM_C)
4095 mbedtls_gcm_context gcm;
4096#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004097#if defined(MBEDTLS_CHACHAPOLY_C)
4098 mbedtls_chachapoly_context chachapoly;
4099#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004100 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004101 psa_algorithm_t core_alg;
4102 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004103 uint8_t tag_length;
4104} aead_operation_t;
4105
Gilles Peskine30a9e412019-01-14 18:36:12 +01004106static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004107{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004108 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004109 {
4110#if defined(MBEDTLS_CCM_C)
4111 case PSA_ALG_CCM:
4112 mbedtls_ccm_free( &operation->ctx.ccm );
4113 break;
4114#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004115#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004116 case PSA_ALG_GCM:
4117 mbedtls_gcm_free( &operation->ctx.gcm );
4118 break;
4119#endif /* MBEDTLS_GCM_C */
4120 }
4121}
4122
4123static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004124 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125 psa_key_usage_t usage,
4126 psa_algorithm_t alg )
4127{
4128 psa_status_t status;
4129 size_t key_bits;
4130 mbedtls_cipher_id_t cipher_id;
4131
Gilles Peskine28f8f302019-07-24 13:30:31 +02004132 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004133 if( status != PSA_SUCCESS )
4134 return( status );
4135
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004136 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004137
4138 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004139 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004140 &cipher_id );
4141 if( operation->cipher_info == NULL )
4142 return( PSA_ERROR_NOT_SUPPORTED );
4143
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004144 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004145 {
4146#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004147 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4148 operation->core_alg = PSA_ALG_CCM;
4149 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004150 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4151 * The call to mbedtls_ccm_encrypt_and_tag or
4152 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004153 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004154 return( PSA_ERROR_INVALID_ARGUMENT );
4155 mbedtls_ccm_init( &operation->ctx.ccm );
4156 status = mbedtls_to_psa_error(
4157 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4158 operation->slot->data.raw.data,
4159 (unsigned int) key_bits ) );
4160 if( status != 0 )
4161 goto cleanup;
4162 break;
4163#endif /* MBEDTLS_CCM_C */
4164
4165#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004166 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4167 operation->core_alg = PSA_ALG_GCM;
4168 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004169 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4170 * The call to mbedtls_gcm_crypt_and_tag or
4171 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004172 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004173 return( PSA_ERROR_INVALID_ARGUMENT );
4174 mbedtls_gcm_init( &operation->ctx.gcm );
4175 status = mbedtls_to_psa_error(
4176 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4177 operation->slot->data.raw.data,
4178 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004179 if( status != 0 )
4180 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004181 break;
4182#endif /* MBEDTLS_GCM_C */
4183
Gilles Peskine26869f22019-05-06 15:25:00 +02004184#if defined(MBEDTLS_CHACHAPOLY_C)
4185 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4186 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4187 operation->full_tag_length = 16;
4188 /* We only support the default tag length. */
4189 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4190 return( PSA_ERROR_NOT_SUPPORTED );
4191 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4192 status = mbedtls_to_psa_error(
4193 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4194 operation->slot->data.raw.data ) );
4195 if( status != 0 )
4196 goto cleanup;
4197 break;
4198#endif /* MBEDTLS_CHACHAPOLY_C */
4199
Gilles Peskineedf9a652018-08-17 18:11:56 +02004200 default:
4201 return( PSA_ERROR_NOT_SUPPORTED );
4202 }
4203
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004204 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4205 {
4206 status = PSA_ERROR_INVALID_ARGUMENT;
4207 goto cleanup;
4208 }
4209 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004210
Gilles Peskineedf9a652018-08-17 18:11:56 +02004211 return( PSA_SUCCESS );
4212
4213cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004214 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004215 return( status );
4216}
4217
Gilles Peskinec5487a82018-12-03 18:08:14 +01004218psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004219 psa_algorithm_t alg,
4220 const uint8_t *nonce,
4221 size_t nonce_length,
4222 const uint8_t *additional_data,
4223 size_t additional_data_length,
4224 const uint8_t *plaintext,
4225 size_t plaintext_length,
4226 uint8_t *ciphertext,
4227 size_t ciphertext_size,
4228 size_t *ciphertext_length )
4229{
mohammad16035955c982018-04-26 00:53:03 +03004230 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004231 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004232 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004233
mohammad1603f08a5502018-06-03 15:05:47 +03004234 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004235
Gilles Peskinec5487a82018-12-03 18:08:14 +01004236 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004237 if( status != PSA_SUCCESS )
4238 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004239
Gilles Peskineedf9a652018-08-17 18:11:56 +02004240 /* For all currently supported modes, the tag is at the end of the
4241 * ciphertext. */
4242 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4243 {
4244 status = PSA_ERROR_BUFFER_TOO_SMALL;
4245 goto exit;
4246 }
4247 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004248
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004249#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004250 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004251 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004252 status = mbedtls_to_psa_error(
4253 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4254 MBEDTLS_GCM_ENCRYPT,
4255 plaintext_length,
4256 nonce, nonce_length,
4257 additional_data, additional_data_length,
4258 plaintext, ciphertext,
4259 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004260 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004261 else
4262#endif /* MBEDTLS_GCM_C */
4263#if defined(MBEDTLS_CCM_C)
4264 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004265 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004266 status = mbedtls_to_psa_error(
4267 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4268 plaintext_length,
4269 nonce, nonce_length,
4270 additional_data,
4271 additional_data_length,
4272 plaintext, ciphertext,
4273 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004274 }
mohammad16035c8845f2018-05-09 05:40:09 -07004275 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004276#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004277#if defined(MBEDTLS_CHACHAPOLY_C)
4278 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4279 {
4280 if( nonce_length != 12 || operation.tag_length != 16 )
4281 {
4282 status = PSA_ERROR_NOT_SUPPORTED;
4283 goto exit;
4284 }
4285 status = mbedtls_to_psa_error(
4286 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4287 plaintext_length,
4288 nonce,
4289 additional_data,
4290 additional_data_length,
4291 plaintext,
4292 ciphertext,
4293 tag ) );
4294 }
4295 else
4296#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004297 {
mohammad1603554faad2018-06-03 15:07:38 +03004298 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004299 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004300
Gilles Peskineedf9a652018-08-17 18:11:56 +02004301 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4302 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004303
Gilles Peskineedf9a652018-08-17 18:11:56 +02004304exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004305 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004306 if( status == PSA_SUCCESS )
4307 *ciphertext_length = plaintext_length + operation.tag_length;
4308 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004309}
4310
Gilles Peskineee652a32018-06-01 19:23:52 +02004311/* Locate the tag in a ciphertext buffer containing the encrypted data
4312 * followed by the tag. Return the length of the part preceding the tag in
4313 * *plaintext_length. This is the size of the plaintext in modes where
4314 * the encrypted data has the same size as the plaintext, such as
4315 * CCM and GCM. */
4316static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4317 const uint8_t *ciphertext,
4318 size_t ciphertext_length,
4319 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004320 const uint8_t **p_tag )
4321{
4322 size_t payload_length;
4323 if( tag_length > ciphertext_length )
4324 return( PSA_ERROR_INVALID_ARGUMENT );
4325 payload_length = ciphertext_length - tag_length;
4326 if( payload_length > plaintext_size )
4327 return( PSA_ERROR_BUFFER_TOO_SMALL );
4328 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004329 return( PSA_SUCCESS );
4330}
4331
Gilles Peskinec5487a82018-12-03 18:08:14 +01004332psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004333 psa_algorithm_t alg,
4334 const uint8_t *nonce,
4335 size_t nonce_length,
4336 const uint8_t *additional_data,
4337 size_t additional_data_length,
4338 const uint8_t *ciphertext,
4339 size_t ciphertext_length,
4340 uint8_t *plaintext,
4341 size_t plaintext_size,
4342 size_t *plaintext_length )
4343{
mohammad16035955c982018-04-26 00:53:03 +03004344 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004345 aead_operation_t operation;
4346 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004347
Gilles Peskineee652a32018-06-01 19:23:52 +02004348 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004349
Gilles Peskinec5487a82018-12-03 18:08:14 +01004350 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004351 if( status != PSA_SUCCESS )
4352 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004353
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004354 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4355 ciphertext, ciphertext_length,
4356 plaintext_size, &tag );
4357 if( status != PSA_SUCCESS )
4358 goto exit;
4359
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004360#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004361 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004362 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004363 status = mbedtls_to_psa_error(
4364 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4365 ciphertext_length - operation.tag_length,
4366 nonce, nonce_length,
4367 additional_data,
4368 additional_data_length,
4369 tag, operation.tag_length,
4370 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004371 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004372 else
4373#endif /* MBEDTLS_GCM_C */
4374#if defined(MBEDTLS_CCM_C)
4375 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004376 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004377 status = mbedtls_to_psa_error(
4378 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4379 ciphertext_length - operation.tag_length,
4380 nonce, nonce_length,
4381 additional_data,
4382 additional_data_length,
4383 ciphertext, plaintext,
4384 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004385 }
mohammad160339574652018-06-01 04:39:53 -07004386 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004387#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004388#if defined(MBEDTLS_CHACHAPOLY_C)
4389 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4390 {
4391 if( nonce_length != 12 || operation.tag_length != 16 )
4392 {
4393 status = PSA_ERROR_NOT_SUPPORTED;
4394 goto exit;
4395 }
4396 status = mbedtls_to_psa_error(
4397 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4398 ciphertext_length - operation.tag_length,
4399 nonce,
4400 additional_data,
4401 additional_data_length,
4402 tag,
4403 ciphertext,
4404 plaintext ) );
4405 }
4406 else
4407#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004408 {
mohammad1603554faad2018-06-03 15:07:38 +03004409 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004410 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004411
Gilles Peskineedf9a652018-08-17 18:11:56 +02004412 if( status != PSA_SUCCESS && plaintext_size != 0 )
4413 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004414
Gilles Peskineedf9a652018-08-17 18:11:56 +02004415exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004416 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004417 if( status == PSA_SUCCESS )
4418 *plaintext_length = ciphertext_length - operation.tag_length;
4419 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004420}
4421
Gilles Peskinea0655c32018-04-30 17:06:50 +02004422
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004423
Gilles Peskine20035e32018-02-03 22:44:14 +01004424/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004425/* Generators */
4426/****************************************************************/
4427
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004428#define HKDF_STATE_INIT 0 /* no input yet */
4429#define HKDF_STATE_STARTED 1 /* got salt */
4430#define HKDF_STATE_KEYED 2 /* got key */
4431#define HKDF_STATE_OUTPUT 3 /* output started */
4432
Gilles Peskinecbe66502019-05-16 16:59:18 +02004433static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004435{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4437 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004438 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440}
4441
4442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004444{
4445 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004447 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004448 {
4449 /* The object has (apparently) been initialized but it is not
4450 * in use. It's ok to call abort on such an object, and there's
4451 * nothing to do. */
4452 }
4453 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004454#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004455 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004456 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004457 mbedtls_free( operation->ctx.hkdf.info );
4458 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004459 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004460 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004461 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004462 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004463 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004464 if( operation->ctx.tls12_prf.seed != NULL )
4465 {
4466 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4467 operation->ctx.tls12_prf.seed_length );
4468 mbedtls_free( operation->ctx.tls12_prf.seed );
4469 }
4470
4471 if( operation->ctx.tls12_prf.label != NULL )
4472 {
4473 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4474 operation->ctx.tls12_prf.label_length );
4475 mbedtls_free( operation->ctx.tls12_prf.label );
4476 }
4477
4478 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4479
4480 /* We leave the fields Ai and output_block to be erased safely by the
4481 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004482 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004483 else
4484#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004485 {
4486 status = PSA_ERROR_BAD_STATE;
4487 }
Janos Follath083036a2019-06-11 10:22:26 +01004488 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004489 return( status );
4490}
4491
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004493 size_t *capacity)
4494{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004496 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004498 return PSA_ERROR_BAD_STATE;
4499 }
4500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004502 return( PSA_SUCCESS );
4503}
4504
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004506 size_t capacity )
4507{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004509 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004511 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004512 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004513 return( PSA_SUCCESS );
4514}
4515
Gilles Peskinebef7f142018-07-12 17:22:21 +02004516#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004518 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004519static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004520 psa_algorithm_t hash_alg,
4521 uint8_t *output,
4522 size_t output_length )
4523{
4524 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4525 psa_status_t status;
4526
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004527 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4528 return( PSA_ERROR_BAD_STATE );
4529 hkdf->state = HKDF_STATE_OUTPUT;
4530
Gilles Peskinebef7f142018-07-12 17:22:21 +02004531 while( output_length != 0 )
4532 {
4533 /* Copy what remains of the current block */
4534 uint8_t n = hash_length - hkdf->offset_in_block;
4535 if( n > output_length )
4536 n = (uint8_t) output_length;
4537 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4538 output += n;
4539 output_length -= n;
4540 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004541 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004542 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004543 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004544 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004546 * object was corrupted or if this function is called directly
4547 * inside the library. */
4548 if( hkdf->block_number == 0xff )
4549 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004550
4551 /* We need a new block */
4552 ++hkdf->block_number;
4553 hkdf->offset_in_block = 0;
4554 status = psa_hmac_setup_internal( &hkdf->hmac,
4555 hkdf->prk, hash_length,
4556 hash_alg );
4557 if( status != PSA_SUCCESS )
4558 return( status );
4559 if( hkdf->block_number != 1 )
4560 {
4561 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4562 hkdf->output_block,
4563 hash_length );
4564 if( status != PSA_SUCCESS )
4565 return( status );
4566 }
4567 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4568 hkdf->info,
4569 hkdf->info_length );
4570 if( status != PSA_SUCCESS )
4571 return( status );
4572 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4573 &hkdf->block_number, 1 );
4574 if( status != PSA_SUCCESS )
4575 return( status );
4576 status = psa_hmac_finish_internal( &hkdf->hmac,
4577 hkdf->output_block,
4578 sizeof( hkdf->output_block ) );
4579 if( status != PSA_SUCCESS )
4580 return( status );
4581 }
4582
4583 return( PSA_SUCCESS );
4584}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004585
Janos Follath7742fee2019-06-17 12:58:10 +01004586static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4587 psa_tls12_prf_key_derivation_t *tls12_prf,
4588 psa_algorithm_t alg )
4589{
4590 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4591 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004592 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4593 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004594
Janos Follath7742fee2019-06-17 12:58:10 +01004595 /* We can't be wanting more output after block 0xff, otherwise
4596 * the capacity check in psa_key_derivation_output_bytes() would have
4597 * prevented this call. It could happen only if the operation
4598 * object was corrupted or if this function is called directly
4599 * inside the library. */
4600 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004601 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004602
4603 /* We need a new block */
4604 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004605 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004606
4607 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4608 *
4609 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4610 *
4611 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4612 * HMAC_hash(secret, A(2) + seed) +
4613 * HMAC_hash(secret, A(3) + seed) + ...
4614 *
4615 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004616 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004617 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004618 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004619 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004620 * is currently extracted as `output_block` and where i is
4621 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004622 */
4623
Janos Follathea29bfb2019-06-19 12:21:20 +01004624 /* Save the hash context before using it, to preserve the hash state with
4625 * only the inner padding in it. We need this, because inner padding depends
4626 * on the key (secret in the RFC's terminology). */
4627 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4628 if( status != PSA_SUCCESS )
4629 goto cleanup;
4630
4631 /* Calculate A(i) where i = tls12_prf->block_number. */
4632 if( tls12_prf->block_number == 1 )
4633 {
4634 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4635 * the variable seed and in this instance means it in the context of the
4636 * P_hash function, where seed = label + seed.) */
4637 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4638 tls12_prf->label, tls12_prf->label_length );
4639 if( status != PSA_SUCCESS )
4640 goto cleanup;
4641 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4642 tls12_prf->seed, tls12_prf->seed_length );
4643 if( status != PSA_SUCCESS )
4644 goto cleanup;
4645 }
4646 else
4647 {
4648 /* A(i) = HMAC_hash(secret, A(i-1)) */
4649 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4650 tls12_prf->Ai, hash_length );
4651 if( status != PSA_SUCCESS )
4652 goto cleanup;
4653 }
4654
4655 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4656 tls12_prf->Ai, hash_length );
4657 if( status != PSA_SUCCESS )
4658 goto cleanup;
4659 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4660 if( status != PSA_SUCCESS )
4661 goto cleanup;
4662
4663 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4664 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4665 tls12_prf->Ai, hash_length );
4666 if( status != PSA_SUCCESS )
4667 goto cleanup;
4668 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4669 tls12_prf->label, tls12_prf->label_length );
4670 if( status != PSA_SUCCESS )
4671 goto cleanup;
4672 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4673 tls12_prf->seed, tls12_prf->seed_length );
4674 if( status != PSA_SUCCESS )
4675 goto cleanup;
4676 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4677 tls12_prf->output_block, hash_length );
4678 if( status != PSA_SUCCESS )
4679 goto cleanup;
4680 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4681 if( status != PSA_SUCCESS )
4682 goto cleanup;
4683
Janos Follath7742fee2019-06-17 12:58:10 +01004684
4685cleanup:
4686
Janos Follathea29bfb2019-06-19 12:21:20 +01004687 cleanup_status = psa_hash_abort( &backup );
4688 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4689 status = cleanup_status;
4690
4691 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004692}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004693
Janos Follath844eb0e2019-06-19 12:10:49 +01004694static psa_status_t psa_key_derivation_tls12_prf_read(
4695 psa_tls12_prf_key_derivation_t *tls12_prf,
4696 psa_algorithm_t alg,
4697 uint8_t *output,
4698 size_t output_length )
4699{
4700 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4701 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4702 psa_status_t status;
4703 uint8_t offset, length;
4704
4705 while( output_length != 0 )
4706 {
4707 /* Check if we have fully processed the current block. */
4708 if( tls12_prf->left_in_block == 0 )
4709 {
4710 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4711 alg );
4712 if( status != PSA_SUCCESS )
4713 return( status );
4714
4715 continue;
4716 }
4717
4718 if( tls12_prf->left_in_block > output_length )
4719 length = (uint8_t) output_length;
4720 else
4721 length = tls12_prf->left_in_block;
4722
4723 offset = hash_length - tls12_prf->left_in_block;
4724 memcpy( output, tls12_prf->output_block + offset, length );
4725 output += length;
4726 output_length -= length;
4727 tls12_prf->left_in_block -= length;
4728 }
4729
4730 return( PSA_SUCCESS );
4731}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004732#endif /* MBEDTLS_MD_C */
4733
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004734psa_status_t psa_key_derivation_output_bytes(
4735 psa_key_derivation_operation_t *operation,
4736 uint8_t *output,
4737 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738{
4739 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004743 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004745 return PSA_ERROR_BAD_STATE;
4746 }
4747
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004751 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004753 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004754 goto exit;
4755 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004759 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004760 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4761 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004763 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004764 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004765 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004767
Gilles Peskinebef7f142018-07-12 17:22:21 +02004768#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004769 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004770 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004771 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004773 output, output_length );
4774 }
Janos Follathadbec812019-06-14 11:05:39 +01004775 else
Janos Follathadbec812019-06-14 11:05:39 +01004776 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004777 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004778 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004780 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004781 output_length );
4782 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004783 else
4784#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004785 {
4786 return( PSA_ERROR_BAD_STATE );
4787 }
4788
4789exit:
4790 if( status != PSA_SUCCESS )
4791 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004792 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 * This allows us to differentiate between exhausted operations and
4794 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4795 * operations. */
4796 psa_algorithm_t alg = operation->alg;
4797 psa_key_derivation_abort( operation );
4798 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004799 memset( output, '!', output_length );
4800 }
4801 return( status );
4802}
4803
Gilles Peskine08542d82018-07-19 17:05:42 +02004804#if defined(MBEDTLS_DES_C)
4805static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4806{
4807 if( data_size >= 8 )
4808 mbedtls_des_key_set_parity( data );
4809 if( data_size >= 16 )
4810 mbedtls_des_key_set_parity( data + 8 );
4811 if( data_size >= 24 )
4812 mbedtls_des_key_set_parity( data + 16 );
4813}
4814#endif /* MBEDTLS_DES_C */
4815
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004816static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004817 psa_key_slot_t *slot,
4818 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004820{
4821 uint8_t *data = NULL;
4822 size_t bytes = PSA_BITS_TO_BYTES( bits );
4823 psa_status_t status;
4824
Gilles Peskine8e338702019-07-30 20:06:31 +02004825 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004826 return( PSA_ERROR_INVALID_ARGUMENT );
4827 if( bits % 8 != 0 )
4828 return( PSA_ERROR_INVALID_ARGUMENT );
4829 data = mbedtls_calloc( 1, bytes );
4830 if( data == NULL )
4831 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4832
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004834 if( status != PSA_SUCCESS )
4835 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004836#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004837 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004838 psa_des_set_key_parity( data, bytes );
4839#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004840 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004841
4842exit:
4843 mbedtls_free( data );
4844 return( status );
4845}
4846
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004847psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004849 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004850{
4851 psa_status_t status;
4852 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004853 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004854
4855 /* Reject any attempt to create a zero-length key so that we don't
4856 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4857 if( psa_get_key_bits( attributes ) == 0 )
4858 return( PSA_ERROR_INVALID_ARGUMENT );
4859
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004860 if( ! operation->can_output_key )
4861 return( PSA_ERROR_NOT_PERMITTED );
4862
Gilles Peskinedf179142019-07-15 22:02:14 +02004863 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4864 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004865#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4866 if( driver != NULL )
4867 {
4868 /* Deriving a key in a secure element is not implemented yet. */
4869 status = PSA_ERROR_NOT_SUPPORTED;
4870 }
4871#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 if( status == PSA_SUCCESS )
4873 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004874 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004875 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004876 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004877 }
4878 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004879 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004880 if( status != PSA_SUCCESS )
4881 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004882 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004883 *handle = 0;
4884 }
4885 return( status );
4886}
4887
Gilles Peskineeab56e42018-07-12 17:12:33 +02004888
4889
4890/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004891/* Key derivation */
4892/****************************************************************/
4893
Gilles Peskine969c5d62019-01-16 15:53:06 +01004894static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897{
Janos Follath5fe19732019-06-20 15:09:30 +01004898 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4899 * initialisers for this union leave some bytes unspecified.) */
4900 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4901
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4905 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4906 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4910 if( hash_size == 0 )
4911 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4913 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004914 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004915 {
4916 return( PSA_ERROR_NOT_SUPPORTED );
4917 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004919 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004920 }
4921#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004923 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924}
4925
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927 psa_algorithm_t alg )
4928{
4929 psa_status_t status;
4930
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932 return( PSA_ERROR_BAD_STATE );
4933
Gilles Peskine6843c292019-01-18 16:44:49 +01004934 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4935 return( PSA_ERROR_INVALID_ARGUMENT );
4936 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937 {
4938 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004940 }
4941 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4942 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004944 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 else
4946 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004947
4948 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004950 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004951}
4952
4953#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004954static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955 psa_algorithm_t hash_alg,
4956 psa_key_derivation_step_t step,
4957 const uint8_t *data,
4958 size_t data_length )
4959{
4960 psa_status_t status;
4961 switch( step )
4962 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004963 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004964 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004966 status = psa_hmac_setup_internal( &hkdf->hmac,
4967 data, data_length,
4968 hash_alg );
4969 if( status != PSA_SUCCESS )
4970 return( status );
4971 hkdf->state = HKDF_STATE_STARTED;
4972 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004973 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 /* If no salt was provided, use an empty salt. */
4975 if( hkdf->state == HKDF_STATE_INIT )
4976 {
4977 status = psa_hmac_setup_internal( &hkdf->hmac,
4978 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004979 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980 if( status != PSA_SUCCESS )
4981 return( status );
4982 hkdf->state = HKDF_STATE_STARTED;
4983 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004984 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004986 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4987 data, data_length );
4988 if( status != PSA_SUCCESS )
4989 return( status );
4990 status = psa_hmac_finish_internal( &hkdf->hmac,
4991 hkdf->prk,
4992 sizeof( hkdf->prk ) );
4993 if( status != PSA_SUCCESS )
4994 return( status );
4995 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4996 hkdf->block_number = 0;
4997 hkdf->state = HKDF_STATE_KEYED;
4998 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004999 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005000 if( hkdf->state == HKDF_STATE_OUTPUT )
5001 return( PSA_ERROR_BAD_STATE );
5002 if( hkdf->info_set )
5003 return( PSA_ERROR_BAD_STATE );
5004 hkdf->info_length = data_length;
5005 if( data_length != 0 )
5006 {
5007 hkdf->info = mbedtls_calloc( 1, data_length );
5008 if( hkdf->info == NULL )
5009 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5010 memcpy( hkdf->info, data, data_length );
5011 }
5012 hkdf->info_set = 1;
5013 return( PSA_SUCCESS );
5014 default:
5015 return( PSA_ERROR_INVALID_ARGUMENT );
5016 }
5017}
Janos Follathb03233e2019-06-11 15:30:30 +01005018
Janos Follathf08e2652019-06-13 09:05:41 +01005019static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5020 const uint8_t *data,
5021 size_t data_length )
5022{
5023 if( prf->state != TLS12_PRF_STATE_INIT )
5024 return( PSA_ERROR_BAD_STATE );
5025
Janos Follathd6dce9f2019-07-04 09:11:38 +01005026 if( data_length != 0 )
5027 {
5028 prf->seed = mbedtls_calloc( 1, data_length );
5029 if( prf->seed == NULL )
5030 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005031
Janos Follathd6dce9f2019-07-04 09:11:38 +01005032 memcpy( prf->seed, data, data_length );
5033 prf->seed_length = data_length;
5034 }
Janos Follathf08e2652019-06-13 09:05:41 +01005035
5036 prf->state = TLS12_PRF_STATE_SEED_SET;
5037
5038 return( PSA_SUCCESS );
5039}
5040
Janos Follath81550542019-06-13 14:26:34 +01005041static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5042 psa_algorithm_t hash_alg,
5043 const uint8_t *data,
5044 size_t data_length )
5045{
5046 psa_status_t status;
5047 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5048 return( PSA_ERROR_BAD_STATE );
5049
5050 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5051 if( status != PSA_SUCCESS )
5052 return( status );
5053
5054 prf->state = TLS12_PRF_STATE_KEY_SET;
5055
5056 return( PSA_SUCCESS );
5057}
5058
Janos Follath6660f0e2019-06-17 08:44:03 +01005059static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5060 psa_tls12_prf_key_derivation_t *prf,
5061 psa_algorithm_t hash_alg,
5062 const uint8_t *data,
5063 size_t data_length )
5064{
5065 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005066 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5067 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005068
5069 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5070 return( PSA_ERROR_INVALID_ARGUMENT );
5071
5072 /* Quoting RFC 4279, Section 2:
5073 *
5074 * The premaster secret is formed as follows: if the PSK is N octets
5075 * long, concatenate a uint16 with the value N, N zero octets, a second
5076 * uint16 with the value N, and the PSK itself.
5077 */
5078
Janos Follath40e13932019-06-26 13:22:29 +01005079 *cur++ = ( data_length >> 8 ) & 0xff;
5080 *cur++ = ( data_length >> 0 ) & 0xff;
5081 memset( cur, 0, data_length );
5082 cur += data_length;
5083 *cur++ = pms[0];
5084 *cur++ = pms[1];
5085 memcpy( cur, data, data_length );
5086 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005087
Janos Follath40e13932019-06-26 13:22:29 +01005088 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005089
5090 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5091 return( status );
5092}
5093
Janos Follath63028dd2019-06-13 09:15:47 +01005094static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5095 const uint8_t *data,
5096 size_t data_length )
5097{
5098 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5099 return( PSA_ERROR_BAD_STATE );
5100
Janos Follathd6dce9f2019-07-04 09:11:38 +01005101 if( data_length != 0 )
5102 {
5103 prf->label = mbedtls_calloc( 1, data_length );
5104 if( prf->label == NULL )
5105 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005106
Janos Follathd6dce9f2019-07-04 09:11:38 +01005107 memcpy( prf->label, data, data_length );
5108 prf->label_length = data_length;
5109 }
Janos Follath63028dd2019-06-13 09:15:47 +01005110
5111 prf->state = TLS12_PRF_STATE_LABEL_SET;
5112
5113 return( PSA_SUCCESS );
5114}
5115
Janos Follathb03233e2019-06-11 15:30:30 +01005116static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5117 psa_algorithm_t hash_alg,
5118 psa_key_derivation_step_t step,
5119 const uint8_t *data,
5120 size_t data_length )
5121{
Janos Follathb03233e2019-06-11 15:30:30 +01005122 switch( step )
5123 {
Janos Follathf08e2652019-06-13 09:05:41 +01005124 case PSA_KEY_DERIVATION_INPUT_SEED:
5125 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005126 case PSA_KEY_DERIVATION_INPUT_SECRET:
5127 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005128 case PSA_KEY_DERIVATION_INPUT_LABEL:
5129 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005130 default:
5131 return( PSA_ERROR_INVALID_ARGUMENT );
5132 }
5133}
Janos Follath6660f0e2019-06-17 08:44:03 +01005134
5135static psa_status_t psa_tls12_prf_psk_to_ms_input(
5136 psa_tls12_prf_key_derivation_t *prf,
5137 psa_algorithm_t hash_alg,
5138 psa_key_derivation_step_t step,
5139 const uint8_t *data,
5140 size_t data_length )
5141{
5142 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005143 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005144 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5145 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005146 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005147
5148 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5149}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005150#endif /* MBEDTLS_MD_C */
5151
Gilles Peskineb8965192019-09-24 16:21:10 +02005152/** Check whether the given key type is acceptable for the given
5153 * input step of a key derivation.
5154 *
5155 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5156 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5157 * Both secret and non-secret inputs can alternatively have the type
5158 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5159 * that the input was passed as a buffer rather than via a key object.
5160 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005161static int psa_key_derivation_check_input_type(
5162 psa_key_derivation_step_t step,
5163 psa_key_type_t key_type )
5164{
5165 switch( step )
5166 {
5167 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005168 if( key_type == PSA_KEY_TYPE_DERIVE )
5169 return( PSA_SUCCESS );
5170 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005171 return( PSA_SUCCESS );
5172 break;
5173 case PSA_KEY_DERIVATION_INPUT_LABEL:
5174 case PSA_KEY_DERIVATION_INPUT_SALT:
5175 case PSA_KEY_DERIVATION_INPUT_INFO:
5176 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005177 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5178 return( PSA_SUCCESS );
5179 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005180 return( PSA_SUCCESS );
5181 break;
5182 }
5183 return( PSA_ERROR_INVALID_ARGUMENT );
5184}
5185
Janos Follathb80a94e2019-06-12 15:54:46 +01005186static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005188 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005189 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005190 const uint8_t *data,
5191 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005192{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005193 psa_status_t status;
5194 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5195
5196 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005197 if( status != PSA_SUCCESS )
5198 goto exit;
5199
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005200#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005201 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 step, data, data_length );
5206 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005207 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 {
Janos Follathb03233e2019-06-11 15:30:30 +01005209 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5210 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5211 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005212 }
5213 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5214 {
5215 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5216 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5217 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005218 }
5219 else
5220#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 return( PSA_ERROR_BAD_STATE );
5224 }
5225
Gilles Peskine224b0d62019-09-23 18:13:17 +02005226exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005228 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 return( status );
5230}
5231
Janos Follath51f4a0f2019-06-14 11:35:55 +01005232psa_status_t psa_key_derivation_input_bytes(
5233 psa_key_derivation_operation_t *operation,
5234 psa_key_derivation_step_t step,
5235 const uint8_t *data,
5236 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005237{
Gilles Peskineb8965192019-09-24 16:21:10 +02005238 return( psa_key_derivation_input_internal( operation, step,
5239 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005240 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005241}
5242
Janos Follath51f4a0f2019-06-14 11:35:55 +01005243psa_status_t psa_key_derivation_input_key(
5244 psa_key_derivation_operation_t *operation,
5245 psa_key_derivation_step_t step,
5246 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247{
5248 psa_key_slot_t *slot;
5249 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005250
Gilles Peskine28f8f302019-07-24 13:30:31 +02005251 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005252 PSA_KEY_USAGE_DERIVE,
5253 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005254 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005255 {
5256 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005258 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005259
5260 /* Passing a key object as a SECRET input unlocks the permission
5261 * to output to a key object. */
5262 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5263 operation->can_output_key = 1;
5264
Janos Follathb80a94e2019-06-12 15:54:46 +01005265 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005266 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005267 slot->data.raw.data,
5268 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005269}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005270
5271
5272
5273/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005274/* Key agreement */
5275/****************************************************************/
5276
Gilles Peskinea05219c2018-11-16 16:02:56 +01005277#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005278static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5279 size_t peer_key_length,
5280 const mbedtls_ecp_keypair *our_key,
5281 uint8_t *shared_secret,
5282 size_t shared_secret_size,
5283 size_t *shared_secret_length )
5284{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285 mbedtls_ecp_keypair *their_key = NULL;
5286 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005287 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005288 size_t bits = 0;
5289 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005291
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005292 status = psa_import_ec_public_key( curve,
5293 peer_key, peer_key_length,
5294 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005295 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005296 goto exit;
5297
Jaeden Amero97271b32019-01-10 19:38:51 +00005298 status = mbedtls_to_psa_error(
5299 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5300 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005301 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005302 status = mbedtls_to_psa_error(
5303 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5304 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305 goto exit;
5306
Jaeden Amero97271b32019-01-10 19:38:51 +00005307 status = mbedtls_to_psa_error(
5308 mbedtls_ecdh_calc_secret( &ecdh,
5309 shared_secret_length,
5310 shared_secret, shared_secret_size,
5311 mbedtls_ctr_drbg_random,
5312 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005313 if( status != PSA_SUCCESS )
5314 goto exit;
5315 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5316 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005317
5318exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005319 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005320 mbedtls_ecp_keypair_free( their_key );
5321 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005322 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005323}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005324#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005325
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5327
Gilles Peskine0216fe12019-04-11 21:23:21 +02005328static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5329 psa_key_slot_t *private_key,
5330 const uint8_t *peer_key,
5331 size_t peer_key_length,
5332 uint8_t *shared_secret,
5333 size_t shared_secret_size,
5334 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005335{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005336 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005337 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005338#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005339 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005340 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005341 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005342 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5343 private_key->data.ecp,
5344 shared_secret, shared_secret_size,
5345 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005346#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005347 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005348 (void) private_key;
5349 (void) peer_key;
5350 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005351 (void) shared_secret;
5352 (void) shared_secret_size;
5353 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005354 return( PSA_ERROR_NOT_SUPPORTED );
5355 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005356}
5357
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005358/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005359 * to potentially free embedded data structures and wipe confidential data.
5360 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005362 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005363 psa_key_slot_t *private_key,
5364 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005365 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366{
5367 psa_status_t status;
5368 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5369 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005371
5372 /* Step 1: run the secret agreement algorithm to generate the shared
5373 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005374 status = psa_key_agreement_raw_internal( ka_alg,
5375 private_key,
5376 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005377 shared_secret,
5378 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005379 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005380 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381 goto exit;
5382
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005383 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005384 * the shared secret. A shared secret is permitted wherever a key
5385 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005386 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005387 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005388 shared_secret,
5389 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005390
Gilles Peskine12313cd2018-06-20 00:20:32 +02005391exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005392 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393 return( status );
5394}
5395
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005396psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005397 psa_key_derivation_step_t step,
5398 psa_key_handle_t private_key,
5399 const uint8_t *peer_key,
5400 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005401{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005402 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005403 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005405 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005406 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005407 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005408 if( status != PSA_SUCCESS )
5409 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005411 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005412 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005413 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005415 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005416}
5417
Gilles Peskinebe697d82019-05-16 18:00:41 +02005418psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5419 psa_key_handle_t private_key,
5420 const uint8_t *peer_key,
5421 size_t peer_key_length,
5422 uint8_t *output,
5423 size_t output_size,
5424 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005425{
5426 psa_key_slot_t *slot;
5427 psa_status_t status;
5428
5429 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5430 {
5431 status = PSA_ERROR_INVALID_ARGUMENT;
5432 goto exit;
5433 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005434 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005435 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005436 if( status != PSA_SUCCESS )
5437 goto exit;
5438
5439 status = psa_key_agreement_raw_internal( alg, slot,
5440 peer_key, peer_key_length,
5441 output, output_size,
5442 output_length );
5443
5444exit:
5445 if( status != PSA_SUCCESS )
5446 {
5447 /* If an error happens and is not handled properly, the output
5448 * may be used as a key to protect sensitive data. Arrange for such
5449 * a key to be random, which is likely to result in decryption or
5450 * verification errors. This is better than filling the buffer with
5451 * some constant data such as zeros, which would result in the data
5452 * being protected with a reproducible, easily knowable key.
5453 */
5454 psa_generate_random( output, output_size );
5455 *output_length = output_size;
5456 }
5457 return( status );
5458}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005459
5460
5461/****************************************************************/
5462/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005463/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005464
Gilles Peskine4c317f42018-07-12 01:24:09 +02005465psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005466 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005467{
Janos Follath24eed8d2019-11-22 13:21:35 +00005468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005469 GUARD_MODULE_INITIALIZED;
5470
Gilles Peskinef181eca2019-08-07 13:49:00 +02005471 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5472 {
5473 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5474 output,
5475 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5476 if( ret != 0 )
5477 return( mbedtls_to_psa_error( ret ) );
5478 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5479 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5480 }
5481
Gilles Peskinee59236f2018-01-27 23:32:46 +01005482 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5483 return( mbedtls_to_psa_error( ret ) );
5484}
5485
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005486#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5487#include "mbedtls/entropy_poll.h"
5488
Gilles Peskine7228da22019-07-15 11:06:15 +02005489psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005490 size_t seed_size )
5491{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005492 if( global_data.initialized )
5493 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005494
5495 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5496 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5497 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5498 return( PSA_ERROR_INVALID_ARGUMENT );
5499
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005500 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005501}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005502#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005503
Gilles Peskinee56e8782019-04-26 17:34:02 +02005504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5505static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5506 size_t domain_parameters_size,
5507 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005508{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005509 size_t i;
5510 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511
Gilles Peskinee56e8782019-04-26 17:34:02 +02005512 if( domain_parameters_size == 0 )
5513 {
5514 *exponent = 65537;
5515 return( PSA_SUCCESS );
5516 }
5517
5518 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5519 * support values that fit in a 32-bit integer, which is larger than
5520 * int on just about every platform anyway. */
5521 if( domain_parameters_size > sizeof( acc ) )
5522 return( PSA_ERROR_NOT_SUPPORTED );
5523 for( i = 0; i < domain_parameters_size; i++ )
5524 acc = ( acc << 8 ) | domain_parameters[i];
5525 if( acc > INT_MAX )
5526 return( PSA_ERROR_NOT_SUPPORTED );
5527 *exponent = acc;
5528 return( PSA_SUCCESS );
5529}
5530#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5531
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005532static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005533 psa_key_slot_t *slot, size_t bits,
5534 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535{
Gilles Peskine8e338702019-07-30 20:06:31 +02005536 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005537
Gilles Peskinee56e8782019-04-26 17:34:02 +02005538 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005539 return( PSA_ERROR_INVALID_ARGUMENT );
5540
Gilles Peskinee59236f2018-01-27 23:32:46 +01005541 if( key_type_is_raw_bytes( type ) )
5542 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005543 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005544 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5545 if( status != PSA_SUCCESS )
5546 return( status );
5547 status = psa_generate_random( slot->data.raw.data,
5548 slot->data.raw.bytes );
5549 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005550 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005551#if defined(MBEDTLS_DES_C)
5552 if( type == PSA_KEY_TYPE_DES )
5553 psa_des_set_key_parity( slot->data.raw.data,
5554 slot->data.raw.bytes );
5555#endif /* MBEDTLS_DES_C */
5556 }
5557 else
5558
5559#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005560 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005561 {
5562 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005564 int exponent;
5565 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5567 return( PSA_ERROR_NOT_SUPPORTED );
5568 /* Accept only byte-aligned keys, for the same reasons as
5569 * in psa_import_rsa_key(). */
5570 if( bits % 8 != 0 )
5571 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005572 status = psa_read_rsa_exponent( domain_parameters,
5573 domain_parameters_size,
5574 &exponent );
5575 if( status != PSA_SUCCESS )
5576 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005577 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5578 if( rsa == NULL )
5579 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5580 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5581 ret = mbedtls_rsa_gen_key( rsa,
5582 mbedtls_ctr_drbg_random,
5583 &global_data.ctr_drbg,
5584 (unsigned int) bits,
5585 exponent );
5586 if( ret != 0 )
5587 {
5588 mbedtls_rsa_free( rsa );
5589 mbedtls_free( rsa );
5590 return( mbedtls_to_psa_error( ret ) );
5591 }
5592 slot->data.rsa = rsa;
5593 }
5594 else
5595#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5596
5597#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005598 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005599 {
5600 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005601 mbedtls_ecp_group_id grp_id =
5602 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005603 const mbedtls_ecp_curve_info *curve_info =
5604 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5605 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005607 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005608 return( PSA_ERROR_NOT_SUPPORTED );
5609 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5610 return( PSA_ERROR_NOT_SUPPORTED );
5611 if( curve_info->bit_size != bits )
5612 return( PSA_ERROR_INVALID_ARGUMENT );
5613 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5614 if( ecp == NULL )
5615 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5616 mbedtls_ecp_keypair_init( ecp );
5617 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5618 mbedtls_ctr_drbg_random,
5619 &global_data.ctr_drbg );
5620 if( ret != 0 )
5621 {
5622 mbedtls_ecp_keypair_free( ecp );
5623 mbedtls_free( ecp );
5624 return( mbedtls_to_psa_error( ret ) );
5625 }
5626 slot->data.ecp = ecp;
5627 }
5628 else
5629#endif /* MBEDTLS_ECP_C */
5630
5631 return( PSA_ERROR_NOT_SUPPORTED );
5632
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005633 return( PSA_SUCCESS );
5634}
Darryl Green0c6575a2018-11-07 16:05:30 +00005635
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005636psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005637 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005638{
5639 psa_status_t status;
5640 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005641 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005642
Gilles Peskine0f84d622019-09-12 19:03:13 +02005643 /* Reject any attempt to create a zero-length key so that we don't
5644 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5645 if( psa_get_key_bits( attributes ) == 0 )
5646 return( PSA_ERROR_INVALID_ARGUMENT );
5647
Gilles Peskinedf179142019-07-15 22:02:14 +02005648 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5649 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005650 if( status != PSA_SUCCESS )
5651 goto exit;
5652
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005653#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5654 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005655 {
Gilles Peskine11792082019-08-06 18:36:36 +02005656 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5657 size_t pubkey_length = 0; /* We don't support this feature yet */
5658 if( drv->key_management == NULL ||
5659 drv->key_management->p_generate == NULL )
5660 {
5661 status = PSA_ERROR_NOT_SUPPORTED;
5662 goto exit;
5663 }
5664 status = drv->key_management->p_generate(
5665 psa_get_se_driver_context( driver ),
5666 slot->data.se.slot_number, attributes,
5667 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005668 }
Gilles Peskine11792082019-08-06 18:36:36 +02005669 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005670#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005672 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005673 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005674 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005675 }
Gilles Peskine11792082019-08-06 18:36:36 +02005676
5677exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005678 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005679 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005680 if( status != PSA_SUCCESS )
5681 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005682 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005683 *handle = 0;
5684 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005685 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005686}
5687
5688
Gilles Peskinee59236f2018-01-27 23:32:46 +01005689
5690/****************************************************************/
5691/* Module setup */
5692/****************************************************************/
5693
Gilles Peskine5e769522018-11-20 21:59:56 +01005694psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5695 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5696 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5697{
5698 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5699 return( PSA_ERROR_BAD_STATE );
5700 global_data.entropy_init = entropy_init;
5701 global_data.entropy_free = entropy_free;
5702 return( PSA_SUCCESS );
5703}
5704
Gilles Peskinee59236f2018-01-27 23:32:46 +01005705void mbedtls_psa_crypto_free( void )
5706{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005707 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005708 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5709 {
5710 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005711 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005712 }
5713 /* Wipe all remaining data, including configuration.
5714 * In particular, this sets all state indicator to the value
5715 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005716 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005717#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005718 /* Unregister all secure element drivers, so that we restart from
5719 * a pristine state. */
5720 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005721#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005722}
5723
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005724#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5725/** Recover a transaction that was interrupted by a power failure.
5726 *
5727 * This function is called during initialization, before psa_crypto_init()
5728 * returns. If this function returns a failure status, the initialization
5729 * fails.
5730 */
5731static psa_status_t psa_crypto_recover_transaction(
5732 const psa_crypto_transaction_t *transaction )
5733{
5734 switch( transaction->unknown.type )
5735 {
5736 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5737 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005738 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005739 * is implemented.
5740 * https://github.com/ARMmbed/mbed-crypto/issues/218
5741 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005742 default:
5743 /* We found an unsupported transaction in the storage.
5744 * We don't know what state the storage is in. Give up. */
5745 return( PSA_ERROR_STORAGE_FAILURE );
5746 }
5747}
5748#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5749
Gilles Peskinee59236f2018-01-27 23:32:46 +01005750psa_status_t psa_crypto_init( void )
5751{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005752 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005753 const unsigned char drbg_seed[] = "PSA";
5754
Gilles Peskinec6b69072018-11-20 21:42:52 +01005755 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005756 if( global_data.initialized != 0 )
5757 return( PSA_SUCCESS );
5758
Gilles Peskine5e769522018-11-20 21:59:56 +01005759 /* Set default configuration if
5760 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5761 if( global_data.entropy_init == NULL )
5762 global_data.entropy_init = mbedtls_entropy_init;
5763 if( global_data.entropy_free == NULL )
5764 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765
Gilles Peskinec6b69072018-11-20 21:42:52 +01005766 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005767 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005768#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5769 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5770 /* The PSA entropy injection feature depends on using NV seed as an entropy
5771 * source. Add NV seed as an entropy source for PSA entropy injection. */
5772 mbedtls_entropy_add_source( &global_data.entropy,
5773 mbedtls_nv_seed_poll, NULL,
5774 MBEDTLS_ENTROPY_BLOCK_SIZE,
5775 MBEDTLS_ENTROPY_SOURCE_STRONG );
5776#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005777 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005778 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005779 status = mbedtls_to_psa_error(
5780 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5781 mbedtls_entropy_func,
5782 &global_data.entropy,
5783 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5784 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005786 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787
Gilles Peskine66fb1262018-12-10 16:29:04 +01005788 status = psa_initialize_key_slots( );
5789 if( status != PSA_SUCCESS )
5790 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005791
Gilles Peskined9348f22019-10-01 15:22:29 +02005792#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5793 status = psa_init_all_se_drivers( );
5794 if( status != PSA_SUCCESS )
5795 goto exit;
5796#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5797
Gilles Peskine4b734222019-07-24 15:56:31 +02005798#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005799 status = psa_crypto_load_transaction( );
5800 if( status == PSA_SUCCESS )
5801 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005802 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5803 if( status != PSA_SUCCESS )
5804 goto exit;
5805 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005806 }
5807 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5808 {
5809 /* There's no transaction to complete. It's all good. */
5810 status = PSA_SUCCESS;
5811 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005812#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005813
Gilles Peskinec6b69072018-11-20 21:42:52 +01005814 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815 global_data.initialized = 1;
5816
5817exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005818 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005819 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005820 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005821}
5822
5823#endif /* MBEDTLS_PSA_CRYPTO_C */