blob: c82cae9fe4d769ab766cb98ea8bcff4106ae3e23 [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 Peskine34ef7f52018-06-18 20:47:51 +0200378static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
379{
380 switch( grpid )
381 {
382 case MBEDTLS_ECP_DP_SECP192R1:
383 return( PSA_ECC_CURVE_SECP192R1 );
384 case MBEDTLS_ECP_DP_SECP224R1:
385 return( PSA_ECC_CURVE_SECP224R1 );
386 case MBEDTLS_ECP_DP_SECP256R1:
387 return( PSA_ECC_CURVE_SECP256R1 );
388 case MBEDTLS_ECP_DP_SECP384R1:
389 return( PSA_ECC_CURVE_SECP384R1 );
390 case MBEDTLS_ECP_DP_SECP521R1:
391 return( PSA_ECC_CURVE_SECP521R1 );
392 case MBEDTLS_ECP_DP_BP256R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
394 case MBEDTLS_ECP_DP_BP384R1:
395 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
396 case MBEDTLS_ECP_DP_BP512R1:
397 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
398 case MBEDTLS_ECP_DP_CURVE25519:
399 return( PSA_ECC_CURVE_CURVE25519 );
400 case MBEDTLS_ECP_DP_SECP192K1:
401 return( PSA_ECC_CURVE_SECP192K1 );
402 case MBEDTLS_ECP_DP_SECP224K1:
403 return( PSA_ECC_CURVE_SECP224K1 );
404 case MBEDTLS_ECP_DP_SECP256K1:
405 return( PSA_ECC_CURVE_SECP256K1 );
406 case MBEDTLS_ECP_DP_CURVE448:
407 return( PSA_ECC_CURVE_CURVE448 );
408 default:
409 return( 0 );
410 }
411}
412
Gilles Peskine12313cd2018-06-20 00:20:32 +0200413static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
414{
415 switch( curve )
416 {
417 case PSA_ECC_CURVE_SECP192R1:
418 return( MBEDTLS_ECP_DP_SECP192R1 );
419 case PSA_ECC_CURVE_SECP224R1:
420 return( MBEDTLS_ECP_DP_SECP224R1 );
421 case PSA_ECC_CURVE_SECP256R1:
422 return( MBEDTLS_ECP_DP_SECP256R1 );
423 case PSA_ECC_CURVE_SECP384R1:
424 return( MBEDTLS_ECP_DP_SECP384R1 );
425 case PSA_ECC_CURVE_SECP521R1:
426 return( MBEDTLS_ECP_DP_SECP521R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
428 return( MBEDTLS_ECP_DP_BP256R1 );
429 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
430 return( MBEDTLS_ECP_DP_BP384R1 );
431 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
432 return( MBEDTLS_ECP_DP_BP512R1 );
433 case PSA_ECC_CURVE_CURVE25519:
434 return( MBEDTLS_ECP_DP_CURVE25519 );
435 case PSA_ECC_CURVE_SECP192K1:
436 return( MBEDTLS_ECP_DP_SECP192K1 );
437 case PSA_ECC_CURVE_SECP224K1:
438 return( MBEDTLS_ECP_DP_SECP224K1 );
439 case PSA_ECC_CURVE_SECP256K1:
440 return( MBEDTLS_ECP_DP_SECP256K1 );
441 case PSA_ECC_CURVE_CURVE448:
442 return( MBEDTLS_ECP_DP_CURVE448 );
443 default:
444 return( MBEDTLS_ECP_DP_NONE );
445 }
446}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100447#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200448
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200449static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
450 size_t bits,
451 struct raw_data *raw )
452{
453 /* Check that the bit size is acceptable for the key type */
454 switch( type )
455 {
456 case PSA_KEY_TYPE_RAW_DATA:
457#if defined(MBEDTLS_MD_C)
458 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200459#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200460 case PSA_KEY_TYPE_DERIVE:
461 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462#if defined(MBEDTLS_AES_C)
463 case PSA_KEY_TYPE_AES:
464 if( bits != 128 && bits != 192 && bits != 256 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_CAMELLIA_C)
469 case PSA_KEY_TYPE_CAMELLIA:
470 if( bits != 128 && bits != 192 && bits != 256 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_DES_C)
475 case PSA_KEY_TYPE_DES:
476 if( bits != 64 && bits != 128 && bits != 192 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
480#if defined(MBEDTLS_ARC4_C)
481 case PSA_KEY_TYPE_ARC4:
482 if( bits < 8 || bits > 2048 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200486#if defined(MBEDTLS_CHACHA20_C)
487 case PSA_KEY_TYPE_CHACHA20:
488 if( bits != 256 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
490 break;
491#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 default:
493 return( PSA_ERROR_NOT_SUPPORTED );
494 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200495 if( bits % 8 != 0 )
496 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497
498 /* Allocate memory for the key */
499 raw->bytes = PSA_BITS_TO_BYTES( bits );
500 raw->data = mbedtls_calloc( 1, raw->bytes );
501 if( raw->data == NULL )
502 {
503 raw->bytes = 0;
504 return( PSA_ERROR_INSUFFICIENT_MEMORY );
505 }
506 return( PSA_SUCCESS );
507}
508
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200509#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100510/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
511 * that are not a multiple of 8) well. For example, there is only
512 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
513 * way to return the exact bit size of a key.
514 * To keep things simple, reject non-byte-aligned key sizes. */
515static psa_status_t psa_check_rsa_key_byte_aligned(
516 const mbedtls_rsa_context *rsa )
517{
518 mbedtls_mpi n;
519 psa_status_t status;
520 mbedtls_mpi_init( &n );
521 status = mbedtls_to_psa_error(
522 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
523 if( status == PSA_SUCCESS )
524 {
525 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
526 status = PSA_ERROR_NOT_SUPPORTED;
527 }
528 mbedtls_mpi_free( &n );
529 return( status );
530}
531
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532static psa_status_t psa_import_rsa_key( psa_key_type_t type,
533 const uint8_t *data,
534 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535 mbedtls_rsa_context **p_rsa )
536{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537 psa_status_t status;
538 mbedtls_pk_context pk;
539 mbedtls_rsa_context *rsa;
540 size_t bits;
541
542 mbedtls_pk_init( &pk );
543
544 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200545 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200548 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
551 if( status != PSA_SUCCESS )
552 goto exit;
553
554 /* We have something that the pkparse module recognizes. If it is a
555 * valid RSA key, store it. */
556 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558 status = PSA_ERROR_INVALID_ARGUMENT;
559 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000561
562 rsa = mbedtls_pk_rsa( pk );
563 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
564 * supports non-byte-aligned key sizes, but not well. For example,
565 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
566 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
567 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
568 {
569 status = PSA_ERROR_NOT_SUPPORTED;
570 goto exit;
571 }
572 status = psa_check_rsa_key_byte_aligned( rsa );
573
574exit:
575 /* Free the content of the pk object only on error. */
576 if( status != PSA_SUCCESS )
577 {
578 mbedtls_pk_free( &pk );
579 return( status );
580 }
581
582 /* On success, store the content of the object in the RSA context. */
583 *p_rsa = rsa;
584
585 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200586}
587#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
588
Jaeden Ameroccdce902019-01-10 11:42:27 +0000589#if defined(MBEDTLS_ECP_C)
590
591/* Import a public key given as the uncompressed representation defined by SEC1
592 * 2.3.3 as the content of an ECPoint. */
593static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
594 const uint8_t *data,
595 size_t data_length,
596 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200597{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200598 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000599 mbedtls_ecp_keypair *ecp = NULL;
600 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
601
602 *p_ecp = NULL;
603 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
604 if( ecp == NULL )
605 return( PSA_ERROR_INSUFFICIENT_MEMORY );
606 mbedtls_ecp_keypair_init( ecp );
607
608 /* Load the group. */
609 status = mbedtls_to_psa_error(
610 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
611 if( status != PSA_SUCCESS )
612 goto exit;
613 /* Load the public value. */
614 status = mbedtls_to_psa_error(
615 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
616 data, data_length ) );
617 if( status != PSA_SUCCESS )
618 goto exit;
619
620 /* Check that the point is on the curve. */
621 status = mbedtls_to_psa_error(
622 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
623 if( status != PSA_SUCCESS )
624 goto exit;
625
626 *p_ecp = ecp;
627 return( PSA_SUCCESS );
628
629exit:
630 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 mbedtls_ecp_keypair_free( ecp );
633 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000635 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200636}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000637#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638
Gilles Peskinef76aa772018-10-29 19:24:33 +0100639#if defined(MBEDTLS_ECP_C)
640/* Import a private key given as a byte string which is the private value
641 * in big-endian order. */
642static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
643 const uint8_t *data,
644 size_t data_length,
645 mbedtls_ecp_keypair **p_ecp )
646{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100648 mbedtls_ecp_keypair *ecp = NULL;
649 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
650
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200651 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
652 return( PSA_ERROR_INVALID_ARGUMENT );
653
Gilles Peskinef76aa772018-10-29 19:24:33 +0100654 *p_ecp = NULL;
655 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
656 if( ecp == NULL )
657 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000658 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659
660 /* Load the group. */
661 status = mbedtls_to_psa_error(
662 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
663 if( status != PSA_SUCCESS )
664 goto exit;
665 /* Load the secret value. */
666 status = mbedtls_to_psa_error(
667 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
668 if( status != PSA_SUCCESS )
669 goto exit;
670 /* Validate the private key. */
671 status = mbedtls_to_psa_error(
672 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
673 if( status != PSA_SUCCESS )
674 goto exit;
675 /* Calculate the public key from the private key. */
676 status = mbedtls_to_psa_error(
677 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
678 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
679 if( status != PSA_SUCCESS )
680 goto exit;
681
682 *p_ecp = ecp;
683 return( PSA_SUCCESS );
684
685exit:
686 if( ecp != NULL )
687 {
688 mbedtls_ecp_keypair_free( ecp );
689 mbedtls_free( ecp );
690 }
691 return( status );
692}
693#endif /* defined(MBEDTLS_ECP_C) */
694
Gilles Peskineb46bef22019-07-30 21:32:04 +0200695
696/** Return the size of the key in the given slot, in bits.
697 *
698 * \param[in] slot A key slot.
699 *
700 * \return The key size in bits, read from the metadata in the slot.
701 */
702static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
703{
704 return( slot->attr.bits );
705}
706
707/** Calculate the size of the key in the given slot, in bits.
708 *
709 * \param[in] slot A key slot containing a transparent key.
710 *
711 * \return The key size in bits, calculated from the key data.
712 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200713static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 size_t bits = 0; /* return 0 on an empty slot */
716
Gilles Peskineb46bef22019-07-30 21:32:04 +0200717 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200718 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200720 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
721 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722#endif /* defined(MBEDTLS_RSA_C) */
723#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
725 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200727
728 /* We know that the size fits in psa_key_bits_t thanks to checks
729 * when the key was created. */
730 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200731}
732
Gilles Peskine8e338702019-07-30 20:06:31 +0200733/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100734 * previously. This function assumes that the slot does not contain
735 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100736psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
737 const uint8_t *data,
738 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200740 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741
Gilles Peskine8e338702019-07-30 20:06:31 +0200742 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 if( data_length > SIZE_MAX / 8 )
747 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200748 /* Enforce a size limit, and in particular ensure that the bit
749 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200750 if( bit_size > PSA_MAX_KEY_BITS )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200752 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200753 &slot->data.raw );
754 if( status != PSA_SUCCESS )
755 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200756 if( data_length != 0 )
757 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758 }
759 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100760#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200761 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100762 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200763 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764 data, data_length,
765 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100768 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000769 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200770 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000771 data, data_length,
772 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 }
774 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100775#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000776#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200777 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100778 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200779 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000780 data, data_length,
781 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 }
783 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100785 {
786 return( PSA_ERROR_NOT_SUPPORTED );
787 }
Darryl Green940d72c2018-07-13 13:18:51 +0100788
Gilles Peskineb46bef22019-07-30 21:32:04 +0200789 if( status == PSA_SUCCESS )
790 {
791 /* Write the actual key size to the slot.
792 * psa_start_key_creation() wrote the size declared by the
793 * caller, which may be 0 (meaning unspecified) or wrong. */
794 slot->attr.bits = psa_calculate_key_bits( slot );
795 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100796 return( status );
797}
798
Gilles Peskinef603c712019-01-19 13:40:11 +0100799/** Calculate the intersection of two algorithm usage policies.
800 *
801 * Return 0 (which allows no operation) on incompatibility.
802 */
803static psa_algorithm_t psa_key_policy_algorithm_intersection(
804 psa_algorithm_t alg1,
805 psa_algorithm_t alg2 )
806{
Gilles Peskine549ea862019-05-22 11:45:59 +0200807 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100808 if( alg1 == alg2 )
809 return( alg1 );
810 /* If the policies are from the same hash-and-sign family, check
811 * if one is a wildcard. If so the other has the specific algorithm. */
812 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
813 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
814 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
815 {
816 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
817 return( alg2 );
818 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
819 return( alg1 );
820 }
821 /* If the policies are incompatible, allow nothing. */
822 return( 0 );
823}
824
Gilles Peskined6f371b2019-05-10 19:33:38 +0200825static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
826 psa_algorithm_t requested_alg )
827{
Gilles Peskine549ea862019-05-22 11:45:59 +0200828 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200829 if( requested_alg == policy_alg )
830 return( 1 );
831 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200832 * and requested_alg is the same hash-and-sign family with any hash,
833 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200834 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
835 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
836 {
837 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
838 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
839 }
840 /* If it isn't permitted, it's forbidden. */
841 return( 0 );
842}
843
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100844/** Test whether a policy permits an algorithm.
845 *
846 * The caller must test usage flags separately.
847 */
848static int psa_key_policy_permits( const psa_key_policy_t *policy,
849 psa_algorithm_t alg )
850{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851 return( psa_key_algorithm_permits( policy->alg, alg ) ||
852 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100853}
854
Gilles Peskinef603c712019-01-19 13:40:11 +0100855/** Restrict a key policy based on a constraint.
856 *
857 * \param[in,out] policy The policy to restrict.
858 * \param[in] constraint The policy constraint to apply.
859 *
860 * \retval #PSA_SUCCESS
861 * \c *policy contains the intersection of the original value of
862 * \c *policy and \c *constraint.
863 * \retval #PSA_ERROR_INVALID_ARGUMENT
864 * \c *policy and \c *constraint are incompatible.
865 * \c *policy is unchanged.
866 */
867static psa_status_t psa_restrict_key_policy(
868 psa_key_policy_t *policy,
869 const psa_key_policy_t *constraint )
870{
871 psa_algorithm_t intersection_alg =
872 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200873 psa_algorithm_t intersection_alg2 =
874 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100875 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
876 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
878 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 policy->usage &= constraint->usage;
880 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100882 return( PSA_SUCCESS );
883}
884
Darryl Green06fd18d2018-07-16 11:21:11 +0100885/** Retrieve a slot which must contain a key. The key must have allow all the
886 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
887 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100888static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100889 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100890 psa_key_usage_t usage,
891 psa_algorithm_t alg )
892{
893 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100894 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100895
896 *p_slot = NULL;
897
Gilles Peskinec5487a82018-12-03 18:08:14 +0100898 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100899 if( status != PSA_SUCCESS )
900 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100901
902 /* Enforce that usage policy for the key slot contains all the flags
903 * required by the usage parameter. There is one exception: public
904 * keys can always be exported, so we treat public key objects as
905 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200906 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100907 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200908 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100909 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100910
911 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
914
915 *p_slot = slot;
916 return( PSA_SUCCESS );
917}
Darryl Green940d72c2018-07-13 13:18:51 +0100918
Gilles Peskine28f8f302019-07-24 13:30:31 +0200919/** Retrieve a slot which must contain a transparent key.
920 *
921 * A transparent key is a key for which the key material is directly
922 * available, as opposed to a key in a secure element.
923 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200924 * This is a temporary function to use instead of psa_get_key_from_slot()
925 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200926 */
927#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
928static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
929 psa_key_slot_t **p_slot,
930 psa_key_usage_t usage,
931 psa_algorithm_t alg )
932{
933 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
934 if( status != PSA_SUCCESS )
935 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200936 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200937 {
938 *p_slot = NULL;
939 return( PSA_ERROR_NOT_SUPPORTED );
940 }
941 return( PSA_SUCCESS );
942}
943#else /* MBEDTLS_PSA_CRYPTO_SE_C */
944/* With no secure element support, all keys are transparent. */
945#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
946 psa_get_key_from_slot( handle, p_slot, usage, alg )
947#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
948
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100949/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100950static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000951{
Gilles Peskine73167e12019-07-12 23:44:37 +0200952#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
953 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000954 {
955 /* No key material to clean. */
956 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200957 else
958#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200959 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000960 {
961 /* No key material to clean. */
962 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 mbedtls_free( slot->data.raw.data );
966 }
967 else
968#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200969 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000970 {
971 mbedtls_rsa_free( slot->data.rsa );
972 mbedtls_free( slot->data.rsa );
973 }
974 else
975#endif /* defined(MBEDTLS_RSA_C) */
976#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200977 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000978 {
979 mbedtls_ecp_keypair_free( slot->data.ecp );
980 mbedtls_free( slot->data.ecp );
981 }
982 else
983#endif /* defined(MBEDTLS_ECP_C) */
984 {
985 /* Shouldn't happen: the key type is not any type that we
986 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200987 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000988 }
989
990 return( PSA_SUCCESS );
991}
992
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993/** Completely wipe a slot in memory, including its policy.
994 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100995psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100996{
997 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200998 /* Multipart operations may still be using the key. This is safe
999 * because all multipart operation objects are independent from
1000 * the key slot: if they need to access the key after the setup
1001 * phase, they have a copy of the key. Note that this means that
1002 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003 /* At this point, key material and other type-specific content has
1004 * been wiped. Clear remaining metadata. We can call memset and not
1005 * zeroize because the metadata is not particularly sensitive. */
1006 memset( slot, 0, sizeof( *slot ) );
1007 return( status );
1008}
1009
Gilles Peskinec5487a82018-12-03 18:08:14 +01001010psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001012 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001013 psa_status_t status; /* status of the last operation */
1014 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001015#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1016 psa_se_drv_table_entry_t *driver;
1017#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018
Gilles Peskine1841cf42019-10-08 15:48:25 +02001019 if( handle == 0 )
1020 return( PSA_SUCCESS );
1021
Gilles Peskinec5487a82018-12-03 18:08:14 +01001022 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023 if( status != PSA_SUCCESS )
1024 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001025
1026#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001027 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001030 /* For a key in a secure element, we need to do three things:
1031 * remove the key file in internal storage, destroy the
1032 * key inside the secure element, and update the driver's
1033 * persistent data. Start a transaction that will encompass these
1034 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001035 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001036 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001038 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001039 status = psa_crypto_save_transaction( );
1040 if( status != PSA_SUCCESS )
1041 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001042 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001043 /* We should still try to destroy the key in the secure
1044 * element and the key metadata in storage. This is especially
1045 * important if the error is that the storage is full.
1046 * But how to do it exactly without risking an inconsistent
1047 * state after a reset?
1048 * https://github.com/ARMmbed/mbed-crypto/issues/215
1049 */
1050 overall_status = status;
1051 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001052 }
1053
Gilles Peskine354f7672019-07-12 23:46:38 +02001054 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001055 if( overall_status == PSA_SUCCESS )
1056 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001057 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001058#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1059
Darryl Greend49a4992018-06-18 17:27:26 +01001060#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001061 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001062 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001063 status = psa_destroy_persistent_key( slot->attr.id );
1064 if( overall_status == PSA_SUCCESS )
1065 overall_status = status;
1066
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001067 /* TODO: other slots may have a copy of the same key. We should
1068 * invalidate them.
1069 * https://github.com/ARMmbed/mbed-crypto/issues/214
1070 */
Darryl Greend49a4992018-06-18 17:27:26 +01001071 }
1072#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001073
Gilles Peskinefc762652019-07-22 19:30:34 +02001074#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1075 if( driver != NULL )
1076 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001077 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001078 if( overall_status == PSA_SUCCESS )
1079 overall_status = status;
1080 status = psa_crypto_stop_transaction( );
1081 if( overall_status == PSA_SUCCESS )
1082 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001083 }
1084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1085
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1087exit:
1088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001089 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001090 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1091 if( overall_status == PSA_SUCCESS )
1092 overall_status = status;
1093 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001094}
1095
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001096void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001097{
Gilles Peskineb699f072019-04-26 16:06:02 +02001098 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001099 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001100}
1101
Gilles Peskineb699f072019-04-26 16:06:02 +02001102psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1103 psa_key_type_t type,
1104 const uint8_t *data,
1105 size_t data_length )
1106{
1107 uint8_t *copy = NULL;
1108
1109 if( data_length != 0 )
1110 {
1111 copy = mbedtls_calloc( 1, data_length );
1112 if( copy == NULL )
1113 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1114 memcpy( copy, data, data_length );
1115 }
1116 /* After this point, this function is guaranteed to succeed, so it
1117 * can start modifying `*attributes`. */
1118
1119 if( attributes->domain_parameters != NULL )
1120 {
1121 mbedtls_free( attributes->domain_parameters );
1122 attributes->domain_parameters = NULL;
1123 attributes->domain_parameters_size = 0;
1124 }
1125
1126 attributes->domain_parameters = copy;
1127 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001128 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001129 return( PSA_SUCCESS );
1130}
1131
1132psa_status_t psa_get_key_domain_parameters(
1133 const psa_key_attributes_t *attributes,
1134 uint8_t *data, size_t data_size, size_t *data_length )
1135{
1136 if( attributes->domain_parameters_size > data_size )
1137 return( PSA_ERROR_BUFFER_TOO_SMALL );
1138 *data_length = attributes->domain_parameters_size;
1139 if( attributes->domain_parameters_size != 0 )
1140 memcpy( data, attributes->domain_parameters,
1141 attributes->domain_parameters_size );
1142 return( PSA_SUCCESS );
1143}
1144
1145#if defined(MBEDTLS_RSA_C)
1146static psa_status_t psa_get_rsa_public_exponent(
1147 const mbedtls_rsa_context *rsa,
1148 psa_key_attributes_t *attributes )
1149{
1150 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001152 uint8_t *buffer = NULL;
1153 size_t buflen;
1154 mbedtls_mpi_init( &mpi );
1155
1156 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1157 if( ret != 0 )
1158 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001159 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1160 {
1161 /* It's the default value, which is reported as an empty string,
1162 * so there's nothing to do. */
1163 goto exit;
1164 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001165
1166 buflen = mbedtls_mpi_size( &mpi );
1167 buffer = mbedtls_calloc( 1, buflen );
1168 if( buffer == NULL )
1169 {
1170 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1171 goto exit;
1172 }
1173 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1174 if( ret != 0 )
1175 goto exit;
1176 attributes->domain_parameters = buffer;
1177 attributes->domain_parameters_size = buflen;
1178
1179exit:
1180 mbedtls_mpi_free( &mpi );
1181 if( ret != 0 )
1182 mbedtls_free( buffer );
1183 return( mbedtls_to_psa_error( ret ) );
1184}
1185#endif /* MBEDTLS_RSA_C */
1186
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001187/** Retrieve all the publicly-accessible attributes of a key.
1188 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1190 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001192 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001193 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001194
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001195 psa_reset_key_attributes( attributes );
1196
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001197 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001198 if( status != PSA_SUCCESS )
1199 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001200
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001201 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001202 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1203 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1204
1205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1206 if( psa_key_slot_is_external( slot ) )
1207 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001209
Gilles Peskine8e338702019-07-30 20:06:31 +02001210 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 {
1212#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001213 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001215#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001216 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001217 * is not yet implemented.
1218 * https://github.com/ARMmbed/mbed-crypto/issues/216
1219 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001220 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001221 break;
1222#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1224 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001225#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001226 default:
1227 /* Nothing else to do. */
1228 break;
1229 }
1230
1231 if( status != PSA_SUCCESS )
1232 psa_reset_key_attributes( attributes );
1233 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234}
1235
Gilles Peskinec8000c02019-08-02 20:15:51 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237psa_status_t psa_get_key_slot_number(
1238 const psa_key_attributes_t *attributes,
1239 psa_key_slot_number_t *slot_number )
1240{
1241 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1242 {
1243 *slot_number = attributes->slot_number;
1244 return( PSA_SUCCESS );
1245 }
1246 else
1247 return( PSA_ERROR_INVALID_ARGUMENT );
1248}
1249#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1250
Jaeden Ameroccdce902019-01-10 11:42:27 +00001251#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001252static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1253 unsigned char *buf, size_t size )
1254{
Janos Follath24eed8d2019-11-22 13:21:35 +00001255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001256 unsigned char *c;
1257 size_t len = 0;
1258
1259 c = buf + size;
1260
1261 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1262
1263 return( (int) len );
1264}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001265#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001266
Gilles Peskinef603c712019-01-19 13:40:11 +01001267static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1268 uint8_t *data,
1269 size_t data_size,
1270 size_t *data_length,
1271 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001272{
Gilles Peskine5d309672019-07-12 23:47:28 +02001273#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1274 const psa_drv_se_t *drv;
1275 psa_drv_se_context_t *drv_context;
1276#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1277
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001278 *data_length = 0;
1279
Gilles Peskine8e338702019-07-30 20:06:31 +02001280 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001281 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001282
Gilles Peskinef9168942019-09-12 19:20:29 +02001283 /* Reject a zero-length output buffer now, since this can never be a
1284 * valid key representation. This way we know that data must be a valid
1285 * pointer and we can do things like memset(data, ..., data_size). */
1286 if( data_size == 0 )
1287 return( PSA_ERROR_BUFFER_TOO_SMALL );
1288
Gilles Peskine5d309672019-07-12 23:47:28 +02001289#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001290 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001291 {
1292 psa_drv_se_export_key_t method;
1293 if( drv->key_management == NULL )
1294 return( PSA_ERROR_NOT_SUPPORTED );
1295 method = ( export_public_key ?
1296 drv->key_management->p_export_public :
1297 drv->key_management->p_export );
1298 if( method == NULL )
1299 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001300 return( method( drv_context,
1301 slot->data.se.slot_number,
1302 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001303 }
1304#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1305
Gilles Peskine8e338702019-07-30 20:06:31 +02001306 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 {
1308 if( slot->data.raw.bytes > data_size )
1309 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001310 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1311 memset( data + slot->data.raw.bytes, 0,
1312 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313 *data_length = slot->data.raw.bytes;
1314 return( PSA_SUCCESS );
1315 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001317 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001318 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001319 psa_status_t status;
1320
Gilles Peskineb46bef22019-07-30 21:32:04 +02001321 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001322 if( bytes > data_size )
1323 return( PSA_ERROR_BUFFER_TOO_SMALL );
1324 status = mbedtls_to_psa_error(
1325 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1326 if( status != PSA_SUCCESS )
1327 return( status );
1328 memset( data + bytes, 0, data_size - bytes );
1329 *data_length = bytes;
1330 return( PSA_SUCCESS );
1331 }
1332#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001333 else
Moran Peker17e36e12018-05-02 12:55:20 +03001334 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001335#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001336 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1337 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001338 {
Moran Pekera998bc62018-04-16 18:16:20 +03001339 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001341 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001342 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001343#if defined(MBEDTLS_RSA_C)
1344 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001345 pk.pk_info = &mbedtls_rsa_info;
1346 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001347#else
1348 return( PSA_ERROR_NOT_SUPPORTED );
1349#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001350 }
1351 else
1352 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001353#if defined(MBEDTLS_ECP_C)
1354 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001355 pk.pk_info = &mbedtls_eckey_info;
1356 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001357#else
1358 return( PSA_ERROR_NOT_SUPPORTED );
1359#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001360 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001361 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001363 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001364 }
Moran Peker17e36e12018-05-02 12:55:20 +03001365 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001366 {
Moran Peker17e36e12018-05-02 12:55:20 +03001367 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001368 }
Moran Peker60364322018-04-29 11:34:58 +03001369 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001370 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001371 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001372 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001373 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001374 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1375 * Move the data to the beginning and erase remaining data
1376 * at the original location. */
1377 if( 2 * (size_t) ret <= data_size )
1378 {
1379 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001380 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001381 }
1382 else if( (size_t) ret < data_size )
1383 {
1384 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001385 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001386 }
Moran Pekera998bc62018-04-16 18:16:20 +03001387 *data_length = ret;
1388 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001389 }
1390 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001391#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001392 {
1393 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001394 it is valid for a special-purpose implementation to omit
1395 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001396 return( PSA_ERROR_NOT_SUPPORTED );
1397 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398 }
1399}
1400
Gilles Peskinec5487a82018-12-03 18:08:14 +01001401psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001402 uint8_t *data,
1403 size_t data_size,
1404 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001405{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001406 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001407 psa_status_t status;
1408
1409 /* Set the key to empty now, so that even when there are errors, we always
1410 * set data_length to a value between 0 and data_size. On error, setting
1411 * the key to empty is a good choice because an empty key representation is
1412 * unlikely to be accepted anywhere. */
1413 *data_length = 0;
1414
1415 /* Export requires the EXPORT flag. There is an exception for public keys,
1416 * which don't require any flag, but psa_get_key_from_slot takes
1417 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001418 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001419 if( status != PSA_SUCCESS )
1420 return( status );
1421 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001422 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424
Gilles Peskinec5487a82018-12-03 18:08:14 +01001425psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001426 uint8_t *data,
1427 size_t data_size,
1428 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001429{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001430 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001431 psa_status_t status;
1432
1433 /* Set the key to empty now, so that even when there are errors, we always
1434 * set data_length to a value between 0 and data_size. On error, setting
1435 * the key to empty is a good choice because an empty key representation is
1436 * unlikely to be accepted anywhere. */
1437 *data_length = 0;
1438
1439 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001440 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001441 if( status != PSA_SUCCESS )
1442 return( status );
1443 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001444 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001445}
1446
Gilles Peskine91e8c332019-08-02 19:19:39 +02001447#if defined(static_assert)
1448static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1449 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001450static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001451 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001452static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001453 "One or more key attribute flag is listed as both internal-only and external-only" );
1454#endif
1455
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001456/** Validate that a key policy is internally well-formed.
1457 *
1458 * This function only rejects invalid policies. It does not validate the
1459 * consistency of the policy with respect to other attributes of the key
1460 * such as the key type.
1461 */
1462static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001463{
1464 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001465 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001466 PSA_KEY_USAGE_ENCRYPT |
1467 PSA_KEY_USAGE_DECRYPT |
1468 PSA_KEY_USAGE_SIGN |
1469 PSA_KEY_USAGE_VERIFY |
1470 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1471 return( PSA_ERROR_INVALID_ARGUMENT );
1472
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 return( PSA_SUCCESS );
1474}
1475
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001476/** Validate the internal consistency of key attributes.
1477 *
1478 * This function only rejects invalid attribute values. If does not
1479 * validate the consistency of the attributes with any key data that may
1480 * be involved in the creation of the key.
1481 *
1482 * Call this function early in the key creation process.
1483 *
1484 * \param[in] attributes Key attributes for the new key.
1485 * \param[out] p_drv On any return, the driver for the key, if any.
1486 * NULL for a transparent key.
1487 *
1488 */
1489static psa_status_t psa_validate_key_attributes(
1490 const psa_key_attributes_t *attributes,
1491 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001492{
1493 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001494
1495 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001496 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001497 status = psa_validate_persistent_key_parameters(
1498 attributes->core.lifetime, attributes->core.id,
1499 p_drv, 1 );
1500 if( status != PSA_SUCCESS )
1501 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001502 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503
1504 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001505 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506 return( status );
1507
1508 /* Refuse to create overly large keys.
1509 * Note that this doesn't trigger on import if the attributes don't
1510 * explicitly specify a size (so psa_get_key_bits returns 0), so
1511 * psa_import_key() needs its own checks. */
1512 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1513 return( PSA_ERROR_NOT_SUPPORTED );
1514
Gilles Peskine91e8c332019-08-02 19:19:39 +02001515 /* Reject invalid flags. These should not be reachable through the API. */
1516 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1517 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1518 return( PSA_ERROR_INVALID_ARGUMENT );
1519
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001520 return( PSA_SUCCESS );
1521}
1522
Gilles Peskine4747d192019-04-17 15:05:45 +02001523/** Prepare a key slot to receive key material.
1524 *
1525 * This function allocates a key slot and sets its metadata.
1526 *
1527 * If this function fails, call psa_fail_key_creation().
1528 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001529 * This function is intended to be used as follows:
1530 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1531 * it with the specified attributes, and assign it a handle.
1532 * -# Populate the slot with the key material.
1533 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1534 * In case of failure at any step, stop the sequence and call
1535 * psa_fail_key_creation().
1536 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001537 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001538 * \param[in] attributes Key attributes for the new key.
1539 * \param[out] handle On success, a handle for the allocated slot.
1540 * \param[out] p_slot On success, a pointer to the prepared slot.
1541 * \param[out] p_drv On any return, the driver for the key, if any.
1542 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001543 *
1544 * \retval #PSA_SUCCESS
1545 * The key slot is ready to receive key material.
1546 * \return If this function fails, the key slot is an invalid state.
1547 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001548 */
1549static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001550 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001551 const psa_key_attributes_t *attributes,
1552 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001553 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001554 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001555{
1556 psa_status_t status;
1557 psa_key_slot_t *slot;
1558
Gilles Peskinedf179142019-07-15 22:02:14 +02001559 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 *p_drv = NULL;
1561
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001562 status = psa_validate_key_attributes( attributes, p_drv );
1563 if( status != PSA_SUCCESS )
1564 return( status );
1565
Gilles Peskineedbed562019-08-07 18:19:59 +02001566 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001567 if( status != PSA_SUCCESS )
1568 return( status );
1569 slot = *p_slot;
1570
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001571 /* We're storing the declared bit-size of the key. It's up to each
1572 * creation mechanism to verify that this information is correct.
1573 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001574 * an input (generate, device) but not for those where the bit-size
1575 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001576
1577 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001578
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001580 * external-only flags, query `attributes`. Thanks to the check
1581 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001582 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001583 * may have set. */
1584 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001585
Gilles Peskinecbaff462019-07-12 23:46:04 +02001586#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001587 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001588 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001589 * create the key file in internal storage, create the
1590 * key inside the secure element, and update the driver's
1591 * persistent data. Start a transaction that will encompass these
1592 * three actions. */
1593 /* The first thing to do is to find a slot number for the new key.
1594 * We save the slot number in persistent storage as part of the
1595 * transaction data. It will be needed to recover if the power
1596 * fails during the key creation process, to clean up on the secure
1597 * element side after restarting. Obtaining a slot number from the
1598 * secure element driver updates its persistent state, but we do not yet
1599 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001600 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001601 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001602 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001603 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001604 &slot->data.se.slot_number );
1605 if( status != PSA_SUCCESS )
1606 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001607 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001608 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001609 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001610 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001611 status = psa_crypto_save_transaction( );
1612 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001613 {
1614 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001615 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001616 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001617 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001618
1619 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1620 {
1621 /* Key registration only makes sense with a secure element. */
1622 return( PSA_ERROR_INVALID_ARGUMENT );
1623 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001624#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1625
Darryl Green0c6575a2018-11-07 16:05:30 +00001626 return( status );
1627}
Gilles Peskine4747d192019-04-17 15:05:45 +02001628
1629/** Finalize the creation of a key once its key material has been set.
1630 *
1631 * This entails writing the key to persistent storage.
1632 *
1633 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 * See the documentation of psa_start_key_creation() for the intended use
1635 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001637 * \param[in,out] slot Pointer to the slot with key material.
1638 * \param[in] driver The secure element driver for the key,
1639 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001640 *
1641 * \retval #PSA_SUCCESS
1642 * The key was successfully created. The handle is now valid.
1643 * \return If this function fails, the key slot is an invalid state.
1644 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001646static psa_status_t psa_finish_key_creation(
1647 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001648 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001649{
1650 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001651 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001652 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001653
1654#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001655 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001656 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001657#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1658 if( driver != NULL )
1659 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001660 psa_se_key_data_storage_t data;
1661#if defined(static_assert)
1662 static_assert( sizeof( slot->data.se.slot_number ) ==
1663 sizeof( data.slot_number ),
1664 "Slot number size does not match psa_se_key_data_storage_t" );
1665 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1666 "Bit-size size does not match psa_se_key_data_storage_t" );
1667#endif
1668 memcpy( &data.slot_number, &slot->data.se.slot_number,
1669 sizeof( slot->data.se.slot_number ) );
1670 memcpy( &data.bits, &slot->attr.bits,
1671 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001672 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001673 (uint8_t*) &data,
1674 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001675 }
1676 else
1677#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1678 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001679 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001680 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001681 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001682 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1683 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001684 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001685 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1686 status = psa_internal_export_key( slot,
1687 buffer, buffer_size, &length,
1688 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001689 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001690 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001691 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001692
Gilles Peskinef9168942019-09-12 19:20:29 +02001693 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001694 mbedtls_free( buffer );
1695 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001697#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1698
Gilles Peskinecbaff462019-07-12 23:46:04 +02001699#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001700 /* Finish the transaction for a key creation. This does not
1701 * happen when registering an existing key. Detect this case
1702 * by checking whether a transaction is in progress (actual
1703 * creation of a key in a secure element requires a transaction,
1704 * but registration doesn't use one). */
1705 if( driver != NULL &&
1706 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001707 {
1708 status = psa_save_se_persistent_data( driver );
1709 if( status != PSA_SUCCESS )
1710 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001711 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001712 return( status );
1713 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001714 status = psa_crypto_stop_transaction( );
1715 if( status != PSA_SUCCESS )
1716 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717 }
1718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1719
Gilles Peskine4747d192019-04-17 15:05:45 +02001720 return( status );
1721}
1722
1723/** Abort the creation of a key.
1724 *
1725 * You may call this function after calling psa_start_key_creation(),
1726 * or after psa_finish_key_creation() fails. In other circumstances, this
1727 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001728 * See the documentation of psa_start_key_creation() for the intended use
1729 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001731 * \param[in,out] slot Pointer to the slot with key material.
1732 * \param[in] driver The secure element driver for the key,
1733 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001735static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001736 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001737{
Gilles Peskine011e4282019-06-26 18:34:38 +02001738 (void) driver;
1739
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 if( slot == NULL )
1741 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001742
1743#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001744 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001745 * element, and the failure happened later (when saving metadata
1746 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001747 * element.
1748 * https://github.com/ARMmbed/mbed-crypto/issues/217
1749 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001750
Gilles Peskined7729582019-08-05 15:55:54 +02001751 /* Abort the ongoing transaction if any (there may not be one if
1752 * the creation process failed before starting one, or if the
1753 * key creation is a registration of a key in a secure element).
1754 * Earlier functions must already have done what it takes to undo any
1755 * partial creation. All that's left is to update the transaction data
1756 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001757 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001758#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1759
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 psa_wipe_key_slot( slot );
1761}
1762
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763/** Validate optional attributes during key creation.
1764 *
1765 * Some key attributes are optional during key creation. If they are
1766 * specified in the attributes structure, check that they are consistent
1767 * with the data in the slot.
1768 *
1769 * This function should be called near the end of key creation, after
1770 * the slot in memory is fully populated but before saving persistent data.
1771 */
1772static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 const psa_key_slot_t *slot,
1774 const psa_key_attributes_t *attributes )
1775{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001776 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001777 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001778 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001779 return( PSA_ERROR_INVALID_ARGUMENT );
1780 }
1781
1782 if( attributes->domain_parameters_size != 0 )
1783 {
1784#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001785 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001786 {
1787 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001789 mbedtls_mpi_init( &actual );
1790 mbedtls_mpi_init( &required );
1791 ret = mbedtls_rsa_export( slot->data.rsa,
1792 NULL, NULL, NULL, NULL, &actual );
1793 if( ret != 0 )
1794 goto rsa_exit;
1795 ret = mbedtls_mpi_read_binary( &required,
1796 attributes->domain_parameters,
1797 attributes->domain_parameters_size );
1798 if( ret != 0 )
1799 goto rsa_exit;
1800 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1801 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1802 rsa_exit:
1803 mbedtls_mpi_free( &actual );
1804 mbedtls_mpi_free( &required );
1805 if( ret != 0)
1806 return( mbedtls_to_psa_error( ret ) );
1807 }
1808 else
1809#endif
1810 {
1811 return( PSA_ERROR_INVALID_ARGUMENT );
1812 }
1813 }
1814
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001815 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001816 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001817 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001818 return( PSA_ERROR_INVALID_ARGUMENT );
1819 }
1820
1821 return( PSA_SUCCESS );
1822}
1823
Gilles Peskine4747d192019-04-17 15:05:45 +02001824psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001825 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001826 size_t data_length,
1827 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001828{
1829 psa_status_t status;
1830 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001831 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001832
Gilles Peskine0f84d622019-09-12 19:03:13 +02001833 /* Reject zero-length symmetric keys (including raw data key objects).
1834 * This also rejects any key which might be encoded as an empty string,
1835 * which is never valid. */
1836 if( data_length == 0 )
1837 return( PSA_ERROR_INVALID_ARGUMENT );
1838
Gilles Peskinedf179142019-07-15 22:02:14 +02001839 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1840 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001841 if( status != PSA_SUCCESS )
1842 goto exit;
1843
Gilles Peskine5d309672019-07-12 23:47:28 +02001844#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1845 if( driver != NULL )
1846 {
1847 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001848 /* The driver should set the number of key bits, however in
1849 * case it doesn't, we initialize bits to an invalid value. */
1850 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001851 if( drv->key_management == NULL ||
1852 drv->key_management->p_import == NULL )
1853 {
1854 status = PSA_ERROR_NOT_SUPPORTED;
1855 goto exit;
1856 }
1857 status = drv->key_management->p_import(
1858 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001859 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001860 &bits );
1861 if( status != PSA_SUCCESS )
1862 goto exit;
1863 if( bits > PSA_MAX_KEY_BITS )
1864 {
1865 status = PSA_ERROR_NOT_SUPPORTED;
1866 goto exit;
1867 }
1868 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001869 }
1870 else
1871#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1872 {
1873 status = psa_import_key_into_slot( slot, data, data_length );
1874 if( status != PSA_SUCCESS )
1875 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001876 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001877 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001878 if( status != PSA_SUCCESS )
1879 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880
Gilles Peskine011e4282019-06-26 18:34:38 +02001881 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001882exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001883 if( status != PSA_SUCCESS )
1884 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001885 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001886 *handle = 0;
1887 }
1888 return( status );
1889}
1890
Gilles Peskined7729582019-08-05 15:55:54 +02001891#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1892psa_status_t mbedtls_psa_register_se_key(
1893 const psa_key_attributes_t *attributes )
1894{
1895 psa_status_t status;
1896 psa_key_slot_t *slot = NULL;
1897 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001898 psa_key_handle_t handle = 0;
1899
1900 /* Leaving attributes unspecified is not currently supported.
1901 * It could make sense to query the key type and size from the
1902 * secure element, but not all secure elements support this
1903 * and the driver HAL doesn't currently support it. */
1904 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1905 return( PSA_ERROR_NOT_SUPPORTED );
1906 if( psa_get_key_bits( attributes ) == 0 )
1907 return( PSA_ERROR_NOT_SUPPORTED );
1908
1909 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1910 &handle, &slot, &driver );
1911 if( status != PSA_SUCCESS )
1912 goto exit;
1913
Gilles Peskined7729582019-08-05 15:55:54 +02001914 status = psa_finish_key_creation( slot, driver );
1915
1916exit:
1917 if( status != PSA_SUCCESS )
1918 {
1919 psa_fail_key_creation( slot, driver );
1920 }
1921 /* Registration doesn't keep the key in RAM. */
1922 psa_close_key( handle );
1923 return( status );
1924}
1925#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1926
Gilles Peskinef603c712019-01-19 13:40:11 +01001927static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001928 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001929{
1930 psa_status_t status;
1931 uint8_t *buffer = NULL;
1932 size_t buffer_size = 0;
1933 size_t length;
1934
Gilles Peskine8e338702019-07-30 20:06:31 +02001935 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001936 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001937 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001938 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001939 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001940 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1941 if( status != PSA_SUCCESS )
1942 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001943 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001944 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001945
1946exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001947 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001948 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001949 return( status );
1950}
1951
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001952psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1953 const psa_key_attributes_t *specified_attributes,
1954 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001955{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001956 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001957 psa_key_slot_t *source_slot = NULL;
1958 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001959 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001960 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001961
Gilles Peskine28f8f302019-07-24 13:30:31 +02001962 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001963 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001964 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965 goto exit;
1966
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001967 status = psa_validate_optional_attributes( source_slot,
1968 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001969 if( status != PSA_SUCCESS )
1970 goto exit;
1971
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001972 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001973 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 if( status != PSA_SUCCESS )
1975 goto exit;
1976
Gilles Peskinedf179142019-07-15 22:02:14 +02001977 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1978 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001979 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001980 if( status != PSA_SUCCESS )
1981 goto exit;
1982
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1984 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001985 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001986 /* Copying to a secure element is not implemented yet. */
1987 status = PSA_ERROR_NOT_SUPPORTED;
1988 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001989 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001990#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001991
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001992 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001993 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001995
Gilles Peskine011e4282019-06-26 18:34:38 +02001996 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001998 if( status != PSA_SUCCESS )
1999 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002000 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 *target_handle = 0;
2002 }
2003 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002004}
2005
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002006
2007
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002008/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002009/* Message digests */
2010/****************************************************************/
2011
Gilles Peskineb16841e2019-10-10 20:36:12 +02002012#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002013static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002014{
2015 switch( alg )
2016 {
2017#if defined(MBEDTLS_MD2_C)
2018 case PSA_ALG_MD2:
2019 return( &mbedtls_md2_info );
2020#endif
2021#if defined(MBEDTLS_MD4_C)
2022 case PSA_ALG_MD4:
2023 return( &mbedtls_md4_info );
2024#endif
2025#if defined(MBEDTLS_MD5_C)
2026 case PSA_ALG_MD5:
2027 return( &mbedtls_md5_info );
2028#endif
2029#if defined(MBEDTLS_RIPEMD160_C)
2030 case PSA_ALG_RIPEMD160:
2031 return( &mbedtls_ripemd160_info );
2032#endif
2033#if defined(MBEDTLS_SHA1_C)
2034 case PSA_ALG_SHA_1:
2035 return( &mbedtls_sha1_info );
2036#endif
2037#if defined(MBEDTLS_SHA256_C)
2038 case PSA_ALG_SHA_224:
2039 return( &mbedtls_sha224_info );
2040 case PSA_ALG_SHA_256:
2041 return( &mbedtls_sha256_info );
2042#endif
2043#if defined(MBEDTLS_SHA512_C)
2044 case PSA_ALG_SHA_384:
2045 return( &mbedtls_sha384_info );
2046 case PSA_ALG_SHA_512:
2047 return( &mbedtls_sha512_info );
2048#endif
2049 default:
2050 return( NULL );
2051 }
2052}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002053#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002054
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002055psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2056{
2057 switch( operation->alg )
2058 {
Gilles Peskine81736312018-06-26 15:04:31 +02002059 case 0:
2060 /* The object has (apparently) been initialized but it is not
2061 * in use. It's ok to call abort on such an object, and there's
2062 * nothing to do. */
2063 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002064#if defined(MBEDTLS_MD2_C)
2065 case PSA_ALG_MD2:
2066 mbedtls_md2_free( &operation->ctx.md2 );
2067 break;
2068#endif
2069#if defined(MBEDTLS_MD4_C)
2070 case PSA_ALG_MD4:
2071 mbedtls_md4_free( &operation->ctx.md4 );
2072 break;
2073#endif
2074#if defined(MBEDTLS_MD5_C)
2075 case PSA_ALG_MD5:
2076 mbedtls_md5_free( &operation->ctx.md5 );
2077 break;
2078#endif
2079#if defined(MBEDTLS_RIPEMD160_C)
2080 case PSA_ALG_RIPEMD160:
2081 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_SHA1_C)
2085 case PSA_ALG_SHA_1:
2086 mbedtls_sha1_free( &operation->ctx.sha1 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_SHA256_C)
2090 case PSA_ALG_SHA_224:
2091 case PSA_ALG_SHA_256:
2092 mbedtls_sha256_free( &operation->ctx.sha256 );
2093 break;
2094#endif
2095#if defined(MBEDTLS_SHA512_C)
2096 case PSA_ALG_SHA_384:
2097 case PSA_ALG_SHA_512:
2098 mbedtls_sha512_free( &operation->ctx.sha512 );
2099 break;
2100#endif
2101 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002102 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002103 }
2104 operation->alg = 0;
2105 return( PSA_SUCCESS );
2106}
2107
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002108psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 psa_algorithm_t alg )
2110{
Janos Follath24eed8d2019-11-22 13:21:35 +00002111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002112
2113 /* A context must be freshly initialized before it can be set up. */
2114 if( operation->alg != 0 )
2115 {
2116 return( PSA_ERROR_BAD_STATE );
2117 }
2118
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119 switch( alg )
2120 {
2121#if defined(MBEDTLS_MD2_C)
2122 case PSA_ALG_MD2:
2123 mbedtls_md2_init( &operation->ctx.md2 );
2124 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2125 break;
2126#endif
2127#if defined(MBEDTLS_MD4_C)
2128 case PSA_ALG_MD4:
2129 mbedtls_md4_init( &operation->ctx.md4 );
2130 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2131 break;
2132#endif
2133#if defined(MBEDTLS_MD5_C)
2134 case PSA_ALG_MD5:
2135 mbedtls_md5_init( &operation->ctx.md5 );
2136 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_RIPEMD160_C)
2140 case PSA_ALG_RIPEMD160:
2141 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2142 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_SHA1_C)
2146 case PSA_ALG_SHA_1:
2147 mbedtls_sha1_init( &operation->ctx.sha1 );
2148 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_SHA256_C)
2152 case PSA_ALG_SHA_224:
2153 mbedtls_sha256_init( &operation->ctx.sha256 );
2154 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2155 break;
2156 case PSA_ALG_SHA_256:
2157 mbedtls_sha256_init( &operation->ctx.sha256 );
2158 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2159 break;
2160#endif
2161#if defined(MBEDTLS_SHA512_C)
2162 case PSA_ALG_SHA_384:
2163 mbedtls_sha512_init( &operation->ctx.sha512 );
2164 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2165 break;
2166 case PSA_ALG_SHA_512:
2167 mbedtls_sha512_init( &operation->ctx.sha512 );
2168 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2169 break;
2170#endif
2171 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002172 return( PSA_ALG_IS_HASH( alg ) ?
2173 PSA_ERROR_NOT_SUPPORTED :
2174 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175 }
2176 if( ret == 0 )
2177 operation->alg = alg;
2178 else
2179 psa_hash_abort( operation );
2180 return( mbedtls_to_psa_error( ret ) );
2181}
2182
2183psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2184 const uint8_t *input,
2185 size_t input_length )
2186{
Janos Follath24eed8d2019-11-22 13:21:35 +00002187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002188
2189 /* Don't require hash implementations to behave correctly on a
2190 * zero-length input, which may have an invalid pointer. */
2191 if( input_length == 0 )
2192 return( PSA_SUCCESS );
2193
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 switch( operation->alg )
2195 {
2196#if defined(MBEDTLS_MD2_C)
2197 case PSA_ALG_MD2:
2198 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2199 input, input_length );
2200 break;
2201#endif
2202#if defined(MBEDTLS_MD4_C)
2203 case PSA_ALG_MD4:
2204 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2205 input, input_length );
2206 break;
2207#endif
2208#if defined(MBEDTLS_MD5_C)
2209 case PSA_ALG_MD5:
2210 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2211 input, input_length );
2212 break;
2213#endif
2214#if defined(MBEDTLS_RIPEMD160_C)
2215 case PSA_ALG_RIPEMD160:
2216 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2217 input, input_length );
2218 break;
2219#endif
2220#if defined(MBEDTLS_SHA1_C)
2221 case PSA_ALG_SHA_1:
2222 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2223 input, input_length );
2224 break;
2225#endif
2226#if defined(MBEDTLS_SHA256_C)
2227 case PSA_ALG_SHA_224:
2228 case PSA_ALG_SHA_256:
2229 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2230 input, input_length );
2231 break;
2232#endif
2233#if defined(MBEDTLS_SHA512_C)
2234 case PSA_ALG_SHA_384:
2235 case PSA_ALG_SHA_512:
2236 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2237 input, input_length );
2238 break;
2239#endif
2240 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002241 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002243
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 if( ret != 0 )
2245 psa_hash_abort( operation );
2246 return( mbedtls_to_psa_error( ret ) );
2247}
2248
2249psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2250 uint8_t *hash,
2251 size_t hash_size,
2252 size_t *hash_length )
2253{
itayzafrir40835d42018-08-02 13:14:17 +03002254 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002256 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257
2258 /* Fill the output buffer with something that isn't a valid hash
2259 * (barring an attack on the hash and deliberately-crafted input),
2260 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002261 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002262 /* If hash_size is 0 then hash may be NULL and then the
2263 * call to memset would have undefined behavior. */
2264 if( hash_size != 0 )
2265 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266
2267 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002268 {
2269 status = PSA_ERROR_BUFFER_TOO_SMALL;
2270 goto exit;
2271 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272
2273 switch( operation->alg )
2274 {
2275#if defined(MBEDTLS_MD2_C)
2276 case PSA_ALG_MD2:
2277 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2278 break;
2279#endif
2280#if defined(MBEDTLS_MD4_C)
2281 case PSA_ALG_MD4:
2282 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2283 break;
2284#endif
2285#if defined(MBEDTLS_MD5_C)
2286 case PSA_ALG_MD5:
2287 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2288 break;
2289#endif
2290#if defined(MBEDTLS_RIPEMD160_C)
2291 case PSA_ALG_RIPEMD160:
2292 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2293 break;
2294#endif
2295#if defined(MBEDTLS_SHA1_C)
2296 case PSA_ALG_SHA_1:
2297 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2298 break;
2299#endif
2300#if defined(MBEDTLS_SHA256_C)
2301 case PSA_ALG_SHA_224:
2302 case PSA_ALG_SHA_256:
2303 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2304 break;
2305#endif
2306#if defined(MBEDTLS_SHA512_C)
2307 case PSA_ALG_SHA_384:
2308 case PSA_ALG_SHA_512:
2309 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2310 break;
2311#endif
2312 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002313 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 }
itayzafrir40835d42018-08-02 13:14:17 +03002315 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316
itayzafrir40835d42018-08-02 13:14:17 +03002317exit:
2318 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002320 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 return( psa_hash_abort( operation ) );
2322 }
2323 else
2324 {
2325 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002326 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 }
2328}
2329
Gilles Peskine2d277862018-06-18 15:41:12 +02002330psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2331 const uint8_t *hash,
2332 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333{
2334 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2335 size_t actual_hash_length;
2336 psa_status_t status = psa_hash_finish( operation,
2337 actual_hash, sizeof( actual_hash ),
2338 &actual_hash_length );
2339 if( status != PSA_SUCCESS )
2340 return( status );
2341 if( actual_hash_length != hash_length )
2342 return( PSA_ERROR_INVALID_SIGNATURE );
2343 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2344 return( PSA_ERROR_INVALID_SIGNATURE );
2345 return( PSA_SUCCESS );
2346}
2347
Gilles Peskineeb35d782019-01-22 17:56:16 +01002348psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2349 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002350{
2351 if( target_operation->alg != 0 )
2352 return( PSA_ERROR_BAD_STATE );
2353
2354 switch( source_operation->alg )
2355 {
2356 case 0:
2357 return( PSA_ERROR_BAD_STATE );
2358#if defined(MBEDTLS_MD2_C)
2359 case PSA_ALG_MD2:
2360 mbedtls_md2_clone( &target_operation->ctx.md2,
2361 &source_operation->ctx.md2 );
2362 break;
2363#endif
2364#if defined(MBEDTLS_MD4_C)
2365 case PSA_ALG_MD4:
2366 mbedtls_md4_clone( &target_operation->ctx.md4,
2367 &source_operation->ctx.md4 );
2368 break;
2369#endif
2370#if defined(MBEDTLS_MD5_C)
2371 case PSA_ALG_MD5:
2372 mbedtls_md5_clone( &target_operation->ctx.md5,
2373 &source_operation->ctx.md5 );
2374 break;
2375#endif
2376#if defined(MBEDTLS_RIPEMD160_C)
2377 case PSA_ALG_RIPEMD160:
2378 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2379 &source_operation->ctx.ripemd160 );
2380 break;
2381#endif
2382#if defined(MBEDTLS_SHA1_C)
2383 case PSA_ALG_SHA_1:
2384 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2385 &source_operation->ctx.sha1 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_SHA256_C)
2389 case PSA_ALG_SHA_224:
2390 case PSA_ALG_SHA_256:
2391 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2392 &source_operation->ctx.sha256 );
2393 break;
2394#endif
2395#if defined(MBEDTLS_SHA512_C)
2396 case PSA_ALG_SHA_384:
2397 case PSA_ALG_SHA_512:
2398 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2399 &source_operation->ctx.sha512 );
2400 break;
2401#endif
2402 default:
2403 return( PSA_ERROR_NOT_SUPPORTED );
2404 }
2405
2406 target_operation->alg = source_operation->alg;
2407 return( PSA_SUCCESS );
2408}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409
2410
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412/* MAC */
2413/****************************************************************/
2414
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002415static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416 psa_algorithm_t alg,
2417 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002418 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002419 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002422 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002424 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002425 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002426
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2428 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002429 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002431 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002432 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 mode = MBEDTLS_MODE_STREAM;
2434 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 case PSA_ALG_CTR:
2436 mode = MBEDTLS_MODE_CTR;
2437 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002438 case PSA_ALG_CFB:
2439 mode = MBEDTLS_MODE_CFB;
2440 break;
2441 case PSA_ALG_OFB:
2442 mode = MBEDTLS_MODE_OFB;
2443 break;
2444 case PSA_ALG_CBC_NO_PADDING:
2445 mode = MBEDTLS_MODE_CBC;
2446 break;
2447 case PSA_ALG_CBC_PKCS7:
2448 mode = MBEDTLS_MODE_CBC;
2449 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002450 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 mode = MBEDTLS_MODE_CCM;
2452 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002453 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 mode = MBEDTLS_MODE_GCM;
2455 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002456 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2457 mode = MBEDTLS_MODE_CHACHAPOLY;
2458 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 default:
2460 return( NULL );
2461 }
2462 }
2463 else if( alg == PSA_ALG_CMAC )
2464 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465 else
2466 return( NULL );
2467
2468 switch( key_type )
2469 {
2470 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002471 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 break;
2473 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002474 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2475 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002477 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002479 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002480 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2481 * but two-key Triple-DES is functionally three-key Triple-DES
2482 * with K1=K3, so that's how we present it to mbedtls. */
2483 if( key_bits == 128 )
2484 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 break;
2486 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002487 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 break;
2489 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002490 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002492 case PSA_KEY_TYPE_CHACHA20:
2493 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2494 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 default:
2496 return( NULL );
2497 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002498 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002499 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500
Jaeden Amero23bbb752018-06-26 14:16:54 +01002501 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2502 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503}
2504
Gilles Peskinea05219c2018-11-16 16:02:56 +01002505#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002506static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002507{
Gilles Peskine2d277862018-06-18 15:41:12 +02002508 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002509 {
2510 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002511 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002512 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002513 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002514 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002515 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002516 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002517 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002518 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002519 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002520 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002521 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002522 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002523 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002524 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002525 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002526 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002527 return( 128 );
2528 default:
2529 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002530 }
2531}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002532#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002533
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002534/* Initialize the MAC operation structure. Once this function has been
2535 * called, psa_mac_abort can run and will do the right thing. */
2536static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2537 psa_algorithm_t alg )
2538{
2539 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2540
2541 operation->alg = alg;
2542 operation->key_set = 0;
2543 operation->iv_set = 0;
2544 operation->iv_required = 0;
2545 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002546 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547
2548#if defined(MBEDTLS_CMAC_C)
2549 if( alg == PSA_ALG_CMAC )
2550 {
2551 operation->iv_required = 0;
2552 mbedtls_cipher_init( &operation->ctx.cmac );
2553 status = PSA_SUCCESS;
2554 }
2555 else
2556#endif /* MBEDTLS_CMAC_C */
2557#if defined(MBEDTLS_MD_C)
2558 if( PSA_ALG_IS_HMAC( operation->alg ) )
2559 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002560 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2561 operation->ctx.hmac.hash_ctx.alg = 0;
2562 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563 }
2564 else
2565#endif /* MBEDTLS_MD_C */
2566 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002567 if( ! PSA_ALG_IS_MAC( alg ) )
2568 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569 }
2570
2571 if( status != PSA_SUCCESS )
2572 memset( operation, 0, sizeof( *operation ) );
2573 return( status );
2574}
2575
Gilles Peskine01126fa2018-07-12 17:04:55 +02002576#if defined(MBEDTLS_MD_C)
2577static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2578{
Gilles Peskine3f108122018-12-07 18:14:53 +01002579 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002580 return( psa_hash_abort( &hmac->hash_ctx ) );
2581}
2582#endif /* MBEDTLS_MD_C */
2583
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2585{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002586 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002588 /* The object has (apparently) been initialized but it is not
2589 * in use. It's ok to call abort on such an object, and there's
2590 * nothing to do. */
2591 return( PSA_SUCCESS );
2592 }
2593 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002595 if( operation->alg == PSA_ALG_CMAC )
2596 {
2597 mbedtls_cipher_free( &operation->ctx.cmac );
2598 }
2599 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002602 if( PSA_ALG_IS_HMAC( operation->alg ) )
2603 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002604 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002605 }
2606 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002608 {
2609 /* Sanity check (shouldn't happen: operation->alg should
2610 * always have been initialized to a valid value). */
2611 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 }
Moran Peker41deec42018-04-04 15:43:05 +03002613
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 operation->alg = 0;
2615 operation->key_set = 0;
2616 operation->iv_set = 0;
2617 operation->iv_required = 0;
2618 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002619 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002620
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002622
2623bad_state:
2624 /* If abort is called on an uninitialized object, we can't trust
2625 * anything. Wipe the object in case it contains confidential data.
2626 * This may result in a memory leak if a pointer gets overwritten,
2627 * but it's too late to do anything about this. */
2628 memset( operation, 0, sizeof( *operation ) );
2629 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630}
2631
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002632#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002633static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002634 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002635 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002636 const mbedtls_cipher_info_t *cipher_info )
2637{
Janos Follath24eed8d2019-11-22 13:21:35 +00002638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002639
2640 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002641
2642 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2643 if( ret != 0 )
2644 return( ret );
2645
2646 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2647 slot->data.raw.data,
2648 key_bits );
2649 return( ret );
2650}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002651#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652
Gilles Peskine248051a2018-06-20 16:09:38 +02002653#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002654static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2655 const uint8_t *key,
2656 size_t key_length,
2657 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002658{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002659 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002660 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002661 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002662 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002663 psa_status_t status;
2664
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002665 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2666 * overflow below. This should never trigger if the hash algorithm
2667 * is implemented correctly. */
2668 /* The size checks against the ipad and opad buffers cannot be written
2669 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2670 * because that triggers -Wlogical-op on GCC 7.3. */
2671 if( block_size > sizeof( ipad ) )
2672 return( PSA_ERROR_NOT_SUPPORTED );
2673 if( block_size > sizeof( hmac->opad ) )
2674 return( PSA_ERROR_NOT_SUPPORTED );
2675 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676 return( PSA_ERROR_NOT_SUPPORTED );
2677
Gilles Peskined223b522018-06-11 18:12:58 +02002678 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002679 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002680 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002682 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002684 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002685 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002686 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002687 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002688 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002689 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002690 }
Gilles Peskine96889972018-07-12 17:07:03 +02002691 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2692 * but it is permitted. It is common when HMAC is used in HKDF, for
2693 * example. Don't call `memcpy` in the 0-length because `key` could be
2694 * an invalid pointer which would make the behavior undefined. */
2695 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002696 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697
Gilles Peskined223b522018-06-11 18:12:58 +02002698 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2699 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002700 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002701 ipad[i] ^= 0x36;
2702 memset( ipad + key_length, 0x36, block_size - key_length );
2703
2704 /* Copy the key material from ipad to opad, flipping the requisite bits,
2705 * and filling the rest of opad with the requisite constant. */
2706 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002707 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2708 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002709
Gilles Peskine01126fa2018-07-12 17:04:55 +02002710 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002712 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002715
2716cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002717 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002718
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719 return( status );
2720}
Gilles Peskine248051a2018-06-20 16:09:38 +02002721#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722
Gilles Peskine89167cb2018-07-08 20:12:23 +02002723static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002724 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002725 psa_algorithm_t alg,
2726 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002729 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002731 psa_key_usage_t usage =
2732 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002733 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002734 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002736 /* A context must be freshly initialized before it can be set up. */
2737 if( operation->alg != 0 )
2738 {
2739 return( PSA_ERROR_BAD_STATE );
2740 }
2741
Gilles Peskined911eb72018-08-14 15:18:45 +02002742 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002743 if( status != PSA_SUCCESS )
2744 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002745 if( is_sign )
2746 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747
Gilles Peskine28f8f302019-07-24 13:30:31 +02002748 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002749 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002750 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002751 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002752
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002754 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002755 {
2756 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002757 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002758 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002760 if( cipher_info == NULL )
2761 {
2762 status = PSA_ERROR_NOT_SUPPORTED;
2763 goto exit;
2764 }
2765 operation->mac_size = cipher_info->block_size;
2766 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2767 status = mbedtls_to_psa_error( ret );
2768 }
2769 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002772 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002774 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002775 if( hash_alg == 0 )
2776 {
2777 status = PSA_ERROR_NOT_SUPPORTED;
2778 goto exit;
2779 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002780
2781 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2782 /* Sanity check. This shouldn't fail on a valid configuration. */
2783 if( operation->mac_size == 0 ||
2784 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2785 {
2786 status = PSA_ERROR_NOT_SUPPORTED;
2787 goto exit;
2788 }
2789
Gilles Peskine8e338702019-07-30 20:06:31 +02002790 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002791 {
2792 status = PSA_ERROR_INVALID_ARGUMENT;
2793 goto exit;
2794 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002795
Gilles Peskine01126fa2018-07-12 17:04:55 +02002796 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2797 slot->data.raw.data,
2798 slot->data.raw.bytes,
2799 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002800 }
2801 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002803 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002804 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002805 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002807
Gilles Peskined911eb72018-08-14 15:18:45 +02002808 if( truncated == 0 )
2809 {
2810 /* The "normal" case: untruncated algorithm. Nothing to do. */
2811 }
2812 else if( truncated < 4 )
2813 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002814 /* A very short MAC is too short for security since it can be
2815 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2816 * so we make this our minimum, even though 32 bits is still
2817 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002818 status = PSA_ERROR_NOT_SUPPORTED;
2819 }
2820 else if( truncated > operation->mac_size )
2821 {
2822 /* It's impossible to "truncate" to a larger length. */
2823 status = PSA_ERROR_INVALID_ARGUMENT;
2824 }
2825 else
2826 operation->mac_size = truncated;
2827
Gilles Peskinefbfac682018-07-08 20:51:54 +02002828exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002829 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002831 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002833 else
2834 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002835 operation->key_set = 1;
2836 }
2837 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838}
2839
Gilles Peskine89167cb2018-07-08 20:12:23 +02002840psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002841 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002842 psa_algorithm_t alg )
2843{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002844 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002845}
2846
2847psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002848 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002849 psa_algorithm_t alg )
2850{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002851 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002852}
2853
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2855 const uint8_t *input,
2856 size_t input_length )
2857{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002860 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002862 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002863 operation->has_input = 1;
2864
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002867 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2869 input, input_length );
2870 status = mbedtls_to_psa_error( ret );
2871 }
2872 else
2873#endif /* MBEDTLS_CMAC_C */
2874#if defined(MBEDTLS_MD_C)
2875 if( PSA_ALG_IS_HMAC( operation->alg ) )
2876 {
2877 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2878 input_length );
2879 }
2880 else
2881#endif /* MBEDTLS_MD_C */
2882 {
2883 /* This shouldn't happen if `operation` was initialized by
2884 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002885 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002886 }
2887
Gilles Peskinefbfac682018-07-08 20:51:54 +02002888 if( status != PSA_SUCCESS )
2889 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002890 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891}
2892
Gilles Peskine01126fa2018-07-12 17:04:55 +02002893#if defined(MBEDTLS_MD_C)
2894static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2895 uint8_t *mac,
2896 size_t mac_size )
2897{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002898 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002899 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2900 size_t hash_size = 0;
2901 size_t block_size = psa_get_hash_block_size( hash_alg );
2902 psa_status_t status;
2903
Gilles Peskine01126fa2018-07-12 17:04:55 +02002904 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2905 if( status != PSA_SUCCESS )
2906 return( status );
2907 /* From here on, tmp needs to be wiped. */
2908
2909 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2910 if( status != PSA_SUCCESS )
2911 goto exit;
2912
2913 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2914 if( status != PSA_SUCCESS )
2915 goto exit;
2916
2917 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2918 if( status != PSA_SUCCESS )
2919 goto exit;
2920
Gilles Peskined911eb72018-08-14 15:18:45 +02002921 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2922 if( status != PSA_SUCCESS )
2923 goto exit;
2924
2925 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926
2927exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002928 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002929 return( status );
2930}
2931#endif /* MBEDTLS_MD_C */
2932
mohammad16036df908f2018-04-02 08:34:15 -07002933static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002934 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002935 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002937 if( ! operation->key_set )
2938 return( PSA_ERROR_BAD_STATE );
2939 if( operation->iv_required && ! operation->iv_set )
2940 return( PSA_ERROR_BAD_STATE );
2941
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942 if( mac_size < operation->mac_size )
2943 return( PSA_ERROR_BUFFER_TOO_SMALL );
2944
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002946 if( operation->alg == PSA_ALG_CMAC )
2947 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002948 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2949 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2950 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002951 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002952 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002953 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002955 else
2956#endif /* MBEDTLS_CMAC_C */
2957#if defined(MBEDTLS_MD_C)
2958 if( PSA_ALG_IS_HMAC( operation->alg ) )
2959 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002960 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002961 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002962 }
2963 else
2964#endif /* MBEDTLS_MD_C */
2965 {
2966 /* This shouldn't happen if `operation` was initialized by
2967 * a setup function. */
2968 return( PSA_ERROR_BAD_STATE );
2969 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970}
2971
Gilles Peskineacd4be32018-07-08 19:56:25 +02002972psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2973 uint8_t *mac,
2974 size_t mac_size,
2975 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002976{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002977 psa_status_t status;
2978
Jaeden Amero252ef282019-02-15 14:05:35 +00002979 if( operation->alg == 0 )
2980 {
2981 return( PSA_ERROR_BAD_STATE );
2982 }
2983
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002984 /* Fill the output buffer with something that isn't a valid mac
2985 * (barring an attack on the mac and deliberately-crafted input),
2986 * in case the caller doesn't check the return status properly. */
2987 *mac_length = mac_size;
2988 /* If mac_size is 0 then mac may be NULL and then the
2989 * call to memset would have undefined behavior. */
2990 if( mac_size != 0 )
2991 memset( mac, '!', mac_size );
2992
Gilles Peskine89167cb2018-07-08 20:12:23 +02002993 if( ! operation->is_sign )
2994 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002995 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002996 }
mohammad16036df908f2018-04-02 08:34:15 -07002997
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002998 status = psa_mac_finish_internal( operation, mac, mac_size );
2999
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003000 if( status == PSA_SUCCESS )
3001 {
3002 status = psa_mac_abort( operation );
3003 if( status == PSA_SUCCESS )
3004 *mac_length = operation->mac_size;
3005 else
3006 memset( mac, '!', mac_size );
3007 }
3008 else
3009 psa_mac_abort( operation );
3010 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003011}
3012
Gilles Peskineacd4be32018-07-08 19:56:25 +02003013psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3014 const uint8_t *mac,
3015 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016{
Gilles Peskine828ed142018-06-18 23:25:51 +02003017 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003018 psa_status_t status;
3019
Jaeden Amero252ef282019-02-15 14:05:35 +00003020 if( operation->alg == 0 )
3021 {
3022 return( PSA_ERROR_BAD_STATE );
3023 }
3024
Gilles Peskine89167cb2018-07-08 20:12:23 +02003025 if( operation->is_sign )
3026 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003027 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003028 }
3029 if( operation->mac_size != mac_length )
3030 {
3031 status = PSA_ERROR_INVALID_SIGNATURE;
3032 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003033 }
mohammad16036df908f2018-04-02 08:34:15 -07003034
3035 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003036 actual_mac, sizeof( actual_mac ) );
3037
3038 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3039 status = PSA_ERROR_INVALID_SIGNATURE;
3040
3041cleanup:
3042 if( status == PSA_SUCCESS )
3043 status = psa_mac_abort( operation );
3044 else
3045 psa_mac_abort( operation );
3046
Gilles Peskine3f108122018-12-07 18:14:53 +01003047 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003048
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003049 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050}
3051
3052
Gilles Peskine20035e32018-02-03 22:44:14 +01003053
Gilles Peskine20035e32018-02-03 22:44:14 +01003054/****************************************************************/
3055/* Asymmetric cryptography */
3056/****************************************************************/
3057
Gilles Peskine2b450e32018-06-27 15:42:46 +02003058#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003059/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003060 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003061static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3062 size_t hash_length,
3063 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003064{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003065 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003066 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003067 *md_alg = mbedtls_md_get_type( md_info );
3068
3069 /* The Mbed TLS RSA module uses an unsigned int for hash length
3070 * parameters. Validate that it fits so that we don't risk an
3071 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003072#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003073 if( hash_length > UINT_MAX )
3074 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003075#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003076
3077#if defined(MBEDTLS_PKCS1_V15)
3078 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3079 * must be correct. */
3080 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3081 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003082 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003083 if( md_info == NULL )
3084 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003085 if( mbedtls_md_get_size( md_info ) != hash_length )
3086 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003087 }
3088#endif /* MBEDTLS_PKCS1_V15 */
3089
3090#if defined(MBEDTLS_PKCS1_V21)
3091 /* PSS requires a hash internally. */
3092 if( PSA_ALG_IS_RSA_PSS( alg ) )
3093 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003094 if( md_info == NULL )
3095 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003096 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003097#endif /* MBEDTLS_PKCS1_V21 */
3098
Gilles Peskine61b91d42018-06-08 16:09:36 +02003099 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003100}
3101
Gilles Peskine2b450e32018-06-27 15:42:46 +02003102static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3103 psa_algorithm_t alg,
3104 const uint8_t *hash,
3105 size_t hash_length,
3106 uint8_t *signature,
3107 size_t signature_size,
3108 size_t *signature_length )
3109{
3110 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003112 mbedtls_md_type_t md_alg;
3113
3114 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3115 if( status != PSA_SUCCESS )
3116 return( status );
3117
Gilles Peskine630a18a2018-06-29 17:49:35 +02003118 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003119 return( PSA_ERROR_BUFFER_TOO_SMALL );
3120
3121#if defined(MBEDTLS_PKCS1_V15)
3122 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3123 {
3124 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3125 MBEDTLS_MD_NONE );
3126 ret = mbedtls_rsa_pkcs1_sign( rsa,
3127 mbedtls_ctr_drbg_random,
3128 &global_data.ctr_drbg,
3129 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003130 md_alg,
3131 (unsigned int) hash_length,
3132 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003133 signature );
3134 }
3135 else
3136#endif /* MBEDTLS_PKCS1_V15 */
3137#if defined(MBEDTLS_PKCS1_V21)
3138 if( PSA_ALG_IS_RSA_PSS( alg ) )
3139 {
3140 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3141 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3142 mbedtls_ctr_drbg_random,
3143 &global_data.ctr_drbg,
3144 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003145 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003146 (unsigned int) hash_length,
3147 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003148 signature );
3149 }
3150 else
3151#endif /* MBEDTLS_PKCS1_V21 */
3152 {
3153 return( PSA_ERROR_INVALID_ARGUMENT );
3154 }
3155
3156 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003157 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003158 return( mbedtls_to_psa_error( ret ) );
3159}
3160
3161static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3162 psa_algorithm_t alg,
3163 const uint8_t *hash,
3164 size_t hash_length,
3165 const uint8_t *signature,
3166 size_t signature_length )
3167{
3168 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003170 mbedtls_md_type_t md_alg;
3171
3172 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3173 if( status != PSA_SUCCESS )
3174 return( status );
3175
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003176 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3177 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003178
3179#if defined(MBEDTLS_PKCS1_V15)
3180 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3181 {
3182 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3183 MBEDTLS_MD_NONE );
3184 ret = mbedtls_rsa_pkcs1_verify( rsa,
3185 mbedtls_ctr_drbg_random,
3186 &global_data.ctr_drbg,
3187 MBEDTLS_RSA_PUBLIC,
3188 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003189 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003190 hash,
3191 signature );
3192 }
3193 else
3194#endif /* MBEDTLS_PKCS1_V15 */
3195#if defined(MBEDTLS_PKCS1_V21)
3196 if( PSA_ALG_IS_RSA_PSS( alg ) )
3197 {
3198 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3199 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3200 mbedtls_ctr_drbg_random,
3201 &global_data.ctr_drbg,
3202 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003203 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003204 (unsigned int) hash_length,
3205 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003206 signature );
3207 }
3208 else
3209#endif /* MBEDTLS_PKCS1_V21 */
3210 {
3211 return( PSA_ERROR_INVALID_ARGUMENT );
3212 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003213
3214 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3215 * the rest of the signature is invalid". This has little use in
3216 * practice and PSA doesn't report this distinction. */
3217 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3218 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003219 return( mbedtls_to_psa_error( ret ) );
3220}
3221#endif /* MBEDTLS_RSA_C */
3222
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003223#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003224/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3225 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3226 * (even though these functions don't modify it). */
3227static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3228 psa_algorithm_t alg,
3229 const uint8_t *hash,
3230 size_t hash_length,
3231 uint8_t *signature,
3232 size_t signature_size,
3233 size_t *signature_length )
3234{
Janos Follath24eed8d2019-11-22 13:21:35 +00003235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003236 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003237 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003238 mbedtls_mpi_init( &r );
3239 mbedtls_mpi_init( &s );
3240
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003241 if( signature_size < 2 * curve_bytes )
3242 {
3243 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3244 goto cleanup;
3245 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003246
Gilles Peskinea05219c2018-11-16 16:02:56 +01003247#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003248 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3249 {
3250 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3251 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3252 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003253 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3254 &ecp->d, hash,
3255 hash_length, md_alg,
3256 mbedtls_ctr_drbg_random,
3257 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003258 }
3259 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003260#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003261 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003262 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003263 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3264 hash, hash_length,
3265 mbedtls_ctr_drbg_random,
3266 &global_data.ctr_drbg ) );
3267 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003268
3269 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3270 signature,
3271 curve_bytes ) );
3272 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3273 signature + curve_bytes,
3274 curve_bytes ) );
3275
3276cleanup:
3277 mbedtls_mpi_free( &r );
3278 mbedtls_mpi_free( &s );
3279 if( ret == 0 )
3280 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003281 return( mbedtls_to_psa_error( ret ) );
3282}
3283
3284static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3285 const uint8_t *hash,
3286 size_t hash_length,
3287 const uint8_t *signature,
3288 size_t signature_length )
3289{
Janos Follath24eed8d2019-11-22 13:21:35 +00003290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003291 mbedtls_mpi r, s;
3292 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3293 mbedtls_mpi_init( &r );
3294 mbedtls_mpi_init( &s );
3295
3296 if( signature_length != 2 * curve_bytes )
3297 return( PSA_ERROR_INVALID_SIGNATURE );
3298
3299 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3300 signature,
3301 curve_bytes ) );
3302 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3303 signature + curve_bytes,
3304 curve_bytes ) );
3305
3306 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3307 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003308
3309cleanup:
3310 mbedtls_mpi_free( &r );
3311 mbedtls_mpi_free( &s );
3312 return( mbedtls_to_psa_error( ret ) );
3313}
3314#endif /* MBEDTLS_ECDSA_C */
3315
Gilles Peskinec5487a82018-12-03 18:08:14 +01003316psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003317 psa_algorithm_t alg,
3318 const uint8_t *hash,
3319 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003320 uint8_t *signature,
3321 size_t signature_size,
3322 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003323{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003324 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003325 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3327 const psa_drv_se_t *drv;
3328 psa_drv_se_context_t *drv_context;
3329#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003330
3331 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003332 /* Immediately reject a zero-length signature buffer. This guarantees
3333 * that signature must be a valid pointer. (On the other hand, the hash
3334 * buffer can in principle be empty since it doesn't actually have
3335 * to be a hash.) */
3336 if( signature_size == 0 )
3337 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003338
Gilles Peskineedc64242019-08-07 21:05:07 +02003339 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003340 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003341 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003342 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003343 {
3344 status = PSA_ERROR_INVALID_ARGUMENT;
3345 goto exit;
3346 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003347
Gilles Peskineedc64242019-08-07 21:05:07 +02003348#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3349 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3350 {
3351 if( drv->asymmetric == NULL ||
3352 drv->asymmetric->p_sign == NULL )
3353 {
3354 status = PSA_ERROR_NOT_SUPPORTED;
3355 goto exit;
3356 }
3357 status = drv->asymmetric->p_sign( drv_context,
3358 slot->data.se.slot_number,
3359 alg,
3360 hash, hash_length,
3361 signature, signature_size,
3362 signature_length );
3363 }
3364 else
3365#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003366#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003367 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003368 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003369 status = psa_rsa_sign( slot->data.rsa,
3370 alg,
3371 hash, hash_length,
3372 signature, signature_size,
3373 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003374 }
3375 else
3376#endif /* defined(MBEDTLS_RSA_C) */
3377#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003378 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003379 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003380#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003381 if(
3382#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3383 PSA_ALG_IS_ECDSA( alg )
3384#else
3385 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3386#endif
3387 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003388 status = psa_ecdsa_sign( slot->data.ecp,
3389 alg,
3390 hash, hash_length,
3391 signature, signature_size,
3392 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003393 else
3394#endif /* defined(MBEDTLS_ECDSA_C) */
3395 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003396 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003397 }
itayzafrir5c753392018-05-08 11:18:38 +03003398 }
3399 else
3400#endif /* defined(MBEDTLS_ECP_C) */
3401 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003402 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003403 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003404
3405exit:
3406 /* Fill the unused part of the output buffer (the whole buffer on error,
3407 * the trailing part on success) with something that isn't a valid mac
3408 * (barring an attack on the mac and deliberately-crafted input),
3409 * in case the caller doesn't check the return status properly. */
3410 if( status == PSA_SUCCESS )
3411 memset( signature + *signature_length, '!',
3412 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003413 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003415 /* If signature_size is 0 then we have nothing to do. We must not call
3416 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003418}
3419
Gilles Peskinec5487a82018-12-03 18:08:14 +01003420psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003421 psa_algorithm_t alg,
3422 const uint8_t *hash,
3423 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003424 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003425 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003427 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3430 const psa_drv_se_t *drv;
3431 psa_drv_se_context_t *drv_context;
3432#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003433
Gilles Peskineedc64242019-08-07 21:05:07 +02003434 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003435 if( status != PSA_SUCCESS )
3436 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003437
Gilles Peskineedc64242019-08-07 21:05:07 +02003438#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3439 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3440 {
3441 if( drv->asymmetric == NULL ||
3442 drv->asymmetric->p_verify == NULL )
3443 return( PSA_ERROR_NOT_SUPPORTED );
3444 return( drv->asymmetric->p_verify( drv_context,
3445 slot->data.se.slot_number,
3446 alg,
3447 hash, hash_length,
3448 signature, signature_length ) );
3449 }
3450 else
3451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003452#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003453 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003454 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003455 return( psa_rsa_verify( slot->data.rsa,
3456 alg,
3457 hash, hash_length,
3458 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459 }
3460 else
3461#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003462#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003463 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003464 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003465#if defined(MBEDTLS_ECDSA_C)
3466 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003467 return( psa_ecdsa_verify( slot->data.ecp,
3468 hash, hash_length,
3469 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003470 else
3471#endif /* defined(MBEDTLS_ECDSA_C) */
3472 {
3473 return( PSA_ERROR_INVALID_ARGUMENT );
3474 }
itayzafrir5c753392018-05-08 11:18:38 +03003475 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003476 else
3477#endif /* defined(MBEDTLS_ECP_C) */
3478 {
3479 return( PSA_ERROR_NOT_SUPPORTED );
3480 }
3481}
3482
Gilles Peskine072ac562018-06-30 00:21:29 +02003483#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3484static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3485 mbedtls_rsa_context *rsa )
3486{
3487 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3488 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3489 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3490 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3491}
3492#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3493
Gilles Peskinec5487a82018-12-03 18:08:14 +01003494psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003495 psa_algorithm_t alg,
3496 const uint8_t *input,
3497 size_t input_length,
3498 const uint8_t *salt,
3499 size_t salt_length,
3500 uint8_t *output,
3501 size_t output_size,
3502 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003503{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003504 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003505 psa_status_t status;
3506
Darryl Green5cc689a2018-07-24 15:34:10 +01003507 (void) input;
3508 (void) input_length;
3509 (void) salt;
3510 (void) output;
3511 (void) output_size;
3512
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003513 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003514
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003515 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3516 return( PSA_ERROR_INVALID_ARGUMENT );
3517
Gilles Peskine28f8f302019-07-24 13:30:31 +02003518 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003519 if( status != PSA_SUCCESS )
3520 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003521 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3522 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003523 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524
3525#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003526 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527 {
3528 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003530 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003531 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003533 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003535 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3536 mbedtls_ctr_drbg_random,
3537 &global_data.ctr_drbg,
3538 MBEDTLS_RSA_PUBLIC,
3539 input_length,
3540 input,
3541 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 }
3543 else
3544#endif /* MBEDTLS_PKCS1_V15 */
3545#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003546 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003548 psa_rsa_oaep_set_padding_mode( alg, rsa );
3549 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3550 mbedtls_ctr_drbg_random,
3551 &global_data.ctr_drbg,
3552 MBEDTLS_RSA_PUBLIC,
3553 salt, salt_length,
3554 input_length,
3555 input,
3556 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 }
3558 else
3559#endif /* MBEDTLS_PKCS1_V21 */
3560 {
3561 return( PSA_ERROR_INVALID_ARGUMENT );
3562 }
3563 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003564 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565 return( mbedtls_to_psa_error( ret ) );
3566 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003568#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 {
3570 return( PSA_ERROR_NOT_SUPPORTED );
3571 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003572}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573
Gilles Peskinec5487a82018-12-03 18:08:14 +01003574psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003575 psa_algorithm_t alg,
3576 const uint8_t *input,
3577 size_t input_length,
3578 const uint8_t *salt,
3579 size_t salt_length,
3580 uint8_t *output,
3581 size_t output_size,
3582 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003584 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003585 psa_status_t status;
3586
Darryl Green5cc689a2018-07-24 15:34:10 +01003587 (void) input;
3588 (void) input_length;
3589 (void) salt;
3590 (void) output;
3591 (void) output_size;
3592
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003593 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003595 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3596 return( PSA_ERROR_INVALID_ARGUMENT );
3597
Gilles Peskine28f8f302019-07-24 13:30:31 +02003598 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003599 if( status != PSA_SUCCESS )
3600 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003601 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602 return( PSA_ERROR_INVALID_ARGUMENT );
3603
3604#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003605 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606 {
3607 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609
Gilles Peskine630a18a2018-06-29 17:49:35 +02003610 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611 return( PSA_ERROR_INVALID_ARGUMENT );
3612
3613#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003614 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003616 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3617 mbedtls_ctr_drbg_random,
3618 &global_data.ctr_drbg,
3619 MBEDTLS_RSA_PRIVATE,
3620 output_length,
3621 input,
3622 output,
3623 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624 }
3625 else
3626#endif /* MBEDTLS_PKCS1_V15 */
3627#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003628 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003630 psa_rsa_oaep_set_padding_mode( alg, rsa );
3631 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3632 mbedtls_ctr_drbg_random,
3633 &global_data.ctr_drbg,
3634 MBEDTLS_RSA_PRIVATE,
3635 salt, salt_length,
3636 output_length,
3637 input,
3638 output,
3639 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640 }
3641 else
3642#endif /* MBEDTLS_PKCS1_V21 */
3643 {
3644 return( PSA_ERROR_INVALID_ARGUMENT );
3645 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003646
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647 return( mbedtls_to_psa_error( ret ) );
3648 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003650#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651 {
3652 return( PSA_ERROR_NOT_SUPPORTED );
3653 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003654}
Gilles Peskine20035e32018-02-03 22:44:14 +01003655
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003656
3657
mohammad1603503973b2018-03-12 15:59:30 +02003658/****************************************************************/
3659/* Symmetric cryptography */
3660/****************************************************************/
3661
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003662/* Initialize the cipher operation structure. Once this function has been
3663 * called, psa_cipher_abort can run and will do the right thing. */
3664static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3665 psa_algorithm_t alg )
3666{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003667 if( ! PSA_ALG_IS_CIPHER( alg ) )
3668 {
3669 memset( operation, 0, sizeof( *operation ) );
3670 return( PSA_ERROR_INVALID_ARGUMENT );
3671 }
3672
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003673 operation->alg = alg;
3674 operation->key_set = 0;
3675 operation->iv_set = 0;
3676 operation->iv_required = 1;
3677 operation->iv_size = 0;
3678 operation->block_size = 0;
3679 mbedtls_cipher_init( &operation->ctx.cipher );
3680 return( PSA_SUCCESS );
3681}
3682
Gilles Peskinee553c652018-06-04 16:22:46 +02003683static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003684 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003685 psa_algorithm_t alg,
3686 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003687{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003688 int ret = 0;
3689 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003690 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003691 size_t key_bits;
3692 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003693 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3694 PSA_KEY_USAGE_ENCRYPT :
3695 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003696
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003697 /* A context must be freshly initialized before it can be set up. */
3698 if( operation->alg != 0 )
3699 {
3700 return( PSA_ERROR_BAD_STATE );
3701 }
3702
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003703 status = psa_cipher_init( operation, alg );
3704 if( status != PSA_SUCCESS )
3705 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003706
Gilles Peskine28f8f302019-07-24 13:30:31 +02003707 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003708 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003709 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003710 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003711
Gilles Peskine8e338702019-07-30 20:06:31 +02003712 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003713 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003714 {
3715 status = PSA_ERROR_NOT_SUPPORTED;
3716 goto exit;
3717 }
mohammad1603503973b2018-03-12 15:59:30 +02003718
mohammad1603503973b2018-03-12 15:59:30 +02003719 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003720 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003721 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003722
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003723#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003724 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003725 {
3726 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003727 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003728 memcpy( keys, slot->data.raw.data, 16 );
3729 memcpy( keys + 16, slot->data.raw.data, 8 );
3730 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3731 keys,
3732 192, cipher_operation );
3733 }
3734 else
3735#endif
3736 {
3737 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3738 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003739 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003740 }
Moran Peker41deec42018-04-04 15:43:05 +03003741 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003742 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003743
mohammad16038481e742018-03-18 13:57:31 +02003744#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003745 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003746 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003747 case PSA_ALG_CBC_NO_PADDING:
3748 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3749 MBEDTLS_PADDING_NONE );
3750 break;
3751 case PSA_ALG_CBC_PKCS7:
3752 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3753 MBEDTLS_PADDING_PKCS7 );
3754 break;
3755 default:
3756 /* The algorithm doesn't involve padding. */
3757 ret = 0;
3758 break;
3759 }
3760 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003761 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003762#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3763
mohammad1603503973b2018-03-12 15:59:30 +02003764 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003765 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003766 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003767 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003768 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003769 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003770 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003771#if defined(MBEDTLS_CHACHA20_C)
3772 else
3773 if( alg == PSA_ALG_CHACHA20 )
3774 operation->iv_size = 12;
3775#endif
mohammad1603503973b2018-03-12 15:59:30 +02003776
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003777exit:
3778 if( status == 0 )
3779 status = mbedtls_to_psa_error( ret );
3780 if( status != 0 )
3781 psa_cipher_abort( operation );
3782 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003783}
3784
Gilles Peskinefe119512018-07-08 21:39:34 +02003785psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003786 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003787 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003788{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003789 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003790}
3791
Gilles Peskinefe119512018-07-08 21:39:34 +02003792psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003793 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003794 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003795{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003796 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003797}
3798
Gilles Peskinefe119512018-07-08 21:39:34 +02003799psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003800 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003801 size_t iv_size,
3802 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003803{
itayzafrir534bd7c2018-08-02 13:56:32 +03003804 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003806 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003807 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003808 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003809 }
Moran Peker41deec42018-04-04 15:43:05 +03003810 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003811 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003812 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003813 goto exit;
3814 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003815 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3816 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003817 if( ret != 0 )
3818 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003820 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003821 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003822
mohammad16038481e742018-03-18 13:57:31 +02003823 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003824 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003825
Moran Peker395db872018-05-31 14:07:14 +03003826exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003827 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003828 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003829 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003830}
3831
Gilles Peskinefe119512018-07-08 21:39:34 +02003832psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003833 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003834 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003835{
itayzafrir534bd7c2018-08-02 13:56:32 +03003836 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003838 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003840 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003841 }
Moran Pekera28258c2018-05-29 16:25:04 +03003842 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003843 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003844 status = PSA_ERROR_INVALID_ARGUMENT;
3845 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003846 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003847 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3848 status = mbedtls_to_psa_error( ret );
3849exit:
3850 if( status == PSA_SUCCESS )
3851 operation->iv_set = 1;
3852 else
mohammad1603503973b2018-03-12 15:59:30 +02003853 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003854 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003855}
3856
Gilles Peskinee553c652018-06-04 16:22:46 +02003857psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3858 const uint8_t *input,
3859 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003860 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003861 size_t output_size,
3862 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003863{
itayzafrir534bd7c2018-08-02 13:56:32 +03003864 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003866 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003867
3868 if( operation->alg == 0 )
3869 {
3870 return( PSA_ERROR_BAD_STATE );
3871 }
3872
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003873 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003874 {
3875 /* Take the unprocessed partial block left over from previous
3876 * update calls, if any, plus the input to this call. Remove
3877 * the last partial block, if any. You get the data that will be
3878 * output in this call. */
3879 expected_output_size =
3880 ( operation->ctx.cipher.unprocessed_len + input_length )
3881 / operation->block_size * operation->block_size;
3882 }
3883 else
3884 {
3885 expected_output_size = input_length;
3886 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003887
Gilles Peskine89d789c2018-06-04 17:17:16 +02003888 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 {
3890 status = PSA_ERROR_BUFFER_TOO_SMALL;
3891 goto exit;
3892 }
mohammad160382759612018-03-12 18:16:40 +02003893
mohammad1603503973b2018-03-12 15:59:30 +02003894 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003895 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 status = mbedtls_to_psa_error( ret );
3897exit:
3898 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003899 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003900 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003901}
3902
Gilles Peskinee553c652018-06-04 16:22:46 +02003903psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3904 uint8_t *output,
3905 size_t output_size,
3906 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003907{
David Saadab4ecc272019-02-14 13:48:10 +02003908 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003909 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003910 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003911
mohammad1603503973b2018-03-12 15:59:30 +02003912 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003913 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003914 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003915 }
3916 if( operation->iv_required && ! operation->iv_set )
3917 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003918 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003919 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003920
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003921 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003922 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3923 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003924 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003925 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003926 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003927 }
3928
Janos Follath315b51c2018-07-09 16:04:51 +01003929 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3930 temp_output_buffer,
3931 output_length );
3932 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003933 {
Janos Follath315b51c2018-07-09 16:04:51 +01003934 status = mbedtls_to_psa_error( cipher_ret );
3935 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003936 }
Janos Follath315b51c2018-07-09 16:04:51 +01003937
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003938 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003939 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003940 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003941 memcpy( output, temp_output_buffer, *output_length );
3942 else
3943 {
Janos Follath315b51c2018-07-09 16:04:51 +01003944 status = PSA_ERROR_BUFFER_TOO_SMALL;
3945 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003946 }
mohammad1603503973b2018-03-12 15:59:30 +02003947
Gilles Peskine3f108122018-12-07 18:14:53 +01003948 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003949 status = psa_cipher_abort( operation );
3950
3951 return( status );
3952
3953error:
3954
3955 *output_length = 0;
3956
Gilles Peskine3f108122018-12-07 18:14:53 +01003957 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003958 (void) psa_cipher_abort( operation );
3959
3960 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003961}
3962
Gilles Peskinee553c652018-06-04 16:22:46 +02003963psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3964{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003965 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003966 {
3967 /* The object has (apparently) been initialized but it is not
3968 * in use. It's ok to call abort on such an object, and there's
3969 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003970 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003971 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003972
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003973 /* Sanity check (shouldn't happen: operation->alg should
3974 * always have been initialized to a valid value). */
3975 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3976 return( PSA_ERROR_BAD_STATE );
3977
mohammad1603503973b2018-03-12 15:59:30 +02003978 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003979
Moran Peker41deec42018-04-04 15:43:05 +03003980 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003981 operation->key_set = 0;
3982 operation->iv_set = 0;
3983 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003984 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003985 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003986
Moran Peker395db872018-05-31 14:07:14 +03003987 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003988}
3989
Gilles Peskinea0655c32018-04-30 17:06:50 +02003990
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003991
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003992
mohammad16035955c982018-04-26 00:53:03 +03003993/****************************************************************/
3994/* AEAD */
3995/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003996
Gilles Peskineedf9a652018-08-17 18:11:56 +02003997typedef struct
3998{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003999 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004000 const mbedtls_cipher_info_t *cipher_info;
4001 union
4002 {
4003#if defined(MBEDTLS_CCM_C)
4004 mbedtls_ccm_context ccm;
4005#endif /* MBEDTLS_CCM_C */
4006#if defined(MBEDTLS_GCM_C)
4007 mbedtls_gcm_context gcm;
4008#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004009#if defined(MBEDTLS_CHACHAPOLY_C)
4010 mbedtls_chachapoly_context chachapoly;
4011#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004012 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004013 psa_algorithm_t core_alg;
4014 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004015 uint8_t tag_length;
4016} aead_operation_t;
4017
Gilles Peskine30a9e412019-01-14 18:36:12 +01004018static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004019{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004020 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004021 {
4022#if defined(MBEDTLS_CCM_C)
4023 case PSA_ALG_CCM:
4024 mbedtls_ccm_free( &operation->ctx.ccm );
4025 break;
4026#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004027#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004028 case PSA_ALG_GCM:
4029 mbedtls_gcm_free( &operation->ctx.gcm );
4030 break;
4031#endif /* MBEDTLS_GCM_C */
4032 }
4033}
4034
4035static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004036 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004037 psa_key_usage_t usage,
4038 psa_algorithm_t alg )
4039{
4040 psa_status_t status;
4041 size_t key_bits;
4042 mbedtls_cipher_id_t cipher_id;
4043
Gilles Peskine28f8f302019-07-24 13:30:31 +02004044 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004045 if( status != PSA_SUCCESS )
4046 return( status );
4047
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004048 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049
4050 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004051 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052 &cipher_id );
4053 if( operation->cipher_info == NULL )
4054 return( PSA_ERROR_NOT_SUPPORTED );
4055
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004056 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057 {
4058#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004059 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4060 operation->core_alg = PSA_ALG_CCM;
4061 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004062 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4063 * The call to mbedtls_ccm_encrypt_and_tag or
4064 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004065 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004066 return( PSA_ERROR_INVALID_ARGUMENT );
4067 mbedtls_ccm_init( &operation->ctx.ccm );
4068 status = mbedtls_to_psa_error(
4069 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4070 operation->slot->data.raw.data,
4071 (unsigned int) key_bits ) );
4072 if( status != 0 )
4073 goto cleanup;
4074 break;
4075#endif /* MBEDTLS_CCM_C */
4076
4077#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004078 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4079 operation->core_alg = PSA_ALG_GCM;
4080 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004081 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4082 * The call to mbedtls_gcm_crypt_and_tag or
4083 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004084 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085 return( PSA_ERROR_INVALID_ARGUMENT );
4086 mbedtls_gcm_init( &operation->ctx.gcm );
4087 status = mbedtls_to_psa_error(
4088 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4089 operation->slot->data.raw.data,
4090 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004091 if( status != 0 )
4092 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004093 break;
4094#endif /* MBEDTLS_GCM_C */
4095
Gilles Peskine26869f22019-05-06 15:25:00 +02004096#if defined(MBEDTLS_CHACHAPOLY_C)
4097 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4098 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4099 operation->full_tag_length = 16;
4100 /* We only support the default tag length. */
4101 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4102 return( PSA_ERROR_NOT_SUPPORTED );
4103 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4104 status = mbedtls_to_psa_error(
4105 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4106 operation->slot->data.raw.data ) );
4107 if( status != 0 )
4108 goto cleanup;
4109 break;
4110#endif /* MBEDTLS_CHACHAPOLY_C */
4111
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112 default:
4113 return( PSA_ERROR_NOT_SUPPORTED );
4114 }
4115
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004116 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4117 {
4118 status = PSA_ERROR_INVALID_ARGUMENT;
4119 goto cleanup;
4120 }
4121 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004122
Gilles Peskineedf9a652018-08-17 18:11:56 +02004123 return( PSA_SUCCESS );
4124
4125cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004126 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 return( status );
4128}
4129
Gilles Peskinec5487a82018-12-03 18:08:14 +01004130psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004131 psa_algorithm_t alg,
4132 const uint8_t *nonce,
4133 size_t nonce_length,
4134 const uint8_t *additional_data,
4135 size_t additional_data_length,
4136 const uint8_t *plaintext,
4137 size_t plaintext_length,
4138 uint8_t *ciphertext,
4139 size_t ciphertext_size,
4140 size_t *ciphertext_length )
4141{
mohammad16035955c982018-04-26 00:53:03 +03004142 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004144 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004145
mohammad1603f08a5502018-06-03 15:05:47 +03004146 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004147
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004149 if( status != PSA_SUCCESS )
4150 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004151
Gilles Peskineedf9a652018-08-17 18:11:56 +02004152 /* For all currently supported modes, the tag is at the end of the
4153 * ciphertext. */
4154 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4155 {
4156 status = PSA_ERROR_BUFFER_TOO_SMALL;
4157 goto exit;
4158 }
4159 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004160
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004161#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004162 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004163 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004164 status = mbedtls_to_psa_error(
4165 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4166 MBEDTLS_GCM_ENCRYPT,
4167 plaintext_length,
4168 nonce, nonce_length,
4169 additional_data, additional_data_length,
4170 plaintext, ciphertext,
4171 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004172 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004173 else
4174#endif /* MBEDTLS_GCM_C */
4175#if defined(MBEDTLS_CCM_C)
4176 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004177 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004178 status = mbedtls_to_psa_error(
4179 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4180 plaintext_length,
4181 nonce, nonce_length,
4182 additional_data,
4183 additional_data_length,
4184 plaintext, ciphertext,
4185 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004186 }
mohammad16035c8845f2018-05-09 05:40:09 -07004187 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004188#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004189#if defined(MBEDTLS_CHACHAPOLY_C)
4190 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4191 {
4192 if( nonce_length != 12 || operation.tag_length != 16 )
4193 {
4194 status = PSA_ERROR_NOT_SUPPORTED;
4195 goto exit;
4196 }
4197 status = mbedtls_to_psa_error(
4198 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4199 plaintext_length,
4200 nonce,
4201 additional_data,
4202 additional_data_length,
4203 plaintext,
4204 ciphertext,
4205 tag ) );
4206 }
4207 else
4208#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004209 {
mohammad1603554faad2018-06-03 15:07:38 +03004210 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004211 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004212
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4214 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004215
Gilles Peskineedf9a652018-08-17 18:11:56 +02004216exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004217 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004218 if( status == PSA_SUCCESS )
4219 *ciphertext_length = plaintext_length + operation.tag_length;
4220 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004221}
4222
Gilles Peskineee652a32018-06-01 19:23:52 +02004223/* Locate the tag in a ciphertext buffer containing the encrypted data
4224 * followed by the tag. Return the length of the part preceding the tag in
4225 * *plaintext_length. This is the size of the plaintext in modes where
4226 * the encrypted data has the same size as the plaintext, such as
4227 * CCM and GCM. */
4228static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4229 const uint8_t *ciphertext,
4230 size_t ciphertext_length,
4231 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004232 const uint8_t **p_tag )
4233{
4234 size_t payload_length;
4235 if( tag_length > ciphertext_length )
4236 return( PSA_ERROR_INVALID_ARGUMENT );
4237 payload_length = ciphertext_length - tag_length;
4238 if( payload_length > plaintext_size )
4239 return( PSA_ERROR_BUFFER_TOO_SMALL );
4240 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004241 return( PSA_SUCCESS );
4242}
4243
Gilles Peskinec5487a82018-12-03 18:08:14 +01004244psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004245 psa_algorithm_t alg,
4246 const uint8_t *nonce,
4247 size_t nonce_length,
4248 const uint8_t *additional_data,
4249 size_t additional_data_length,
4250 const uint8_t *ciphertext,
4251 size_t ciphertext_length,
4252 uint8_t *plaintext,
4253 size_t plaintext_size,
4254 size_t *plaintext_length )
4255{
mohammad16035955c982018-04-26 00:53:03 +03004256 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004257 aead_operation_t operation;
4258 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004259
Gilles Peskineee652a32018-06-01 19:23:52 +02004260 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004261
Gilles Peskinec5487a82018-12-03 18:08:14 +01004262 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004263 if( status != PSA_SUCCESS )
4264 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004265
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004266 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4267 ciphertext, ciphertext_length,
4268 plaintext_size, &tag );
4269 if( status != PSA_SUCCESS )
4270 goto exit;
4271
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004272#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004273 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004274 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004275 status = mbedtls_to_psa_error(
4276 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4277 ciphertext_length - operation.tag_length,
4278 nonce, nonce_length,
4279 additional_data,
4280 additional_data_length,
4281 tag, operation.tag_length,
4282 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004283 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004284 else
4285#endif /* MBEDTLS_GCM_C */
4286#if defined(MBEDTLS_CCM_C)
4287 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004288 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004289 status = mbedtls_to_psa_error(
4290 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4291 ciphertext_length - operation.tag_length,
4292 nonce, nonce_length,
4293 additional_data,
4294 additional_data_length,
4295 ciphertext, plaintext,
4296 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004297 }
mohammad160339574652018-06-01 04:39:53 -07004298 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004299#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004300#if defined(MBEDTLS_CHACHAPOLY_C)
4301 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4302 {
4303 if( nonce_length != 12 || operation.tag_length != 16 )
4304 {
4305 status = PSA_ERROR_NOT_SUPPORTED;
4306 goto exit;
4307 }
4308 status = mbedtls_to_psa_error(
4309 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4310 ciphertext_length - operation.tag_length,
4311 nonce,
4312 additional_data,
4313 additional_data_length,
4314 tag,
4315 ciphertext,
4316 plaintext ) );
4317 }
4318 else
4319#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004320 {
mohammad1603554faad2018-06-03 15:07:38 +03004321 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004322 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004323
Gilles Peskineedf9a652018-08-17 18:11:56 +02004324 if( status != PSA_SUCCESS && plaintext_size != 0 )
4325 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004326
Gilles Peskineedf9a652018-08-17 18:11:56 +02004327exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004328 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004329 if( status == PSA_SUCCESS )
4330 *plaintext_length = ciphertext_length - operation.tag_length;
4331 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004332}
4333
Gilles Peskinea0655c32018-04-30 17:06:50 +02004334
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004335
Gilles Peskine20035e32018-02-03 22:44:14 +01004336/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004337/* Generators */
4338/****************************************************************/
4339
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004340#define HKDF_STATE_INIT 0 /* no input yet */
4341#define HKDF_STATE_STARTED 1 /* got salt */
4342#define HKDF_STATE_KEYED 2 /* got key */
4343#define HKDF_STATE_OUTPUT 3 /* output started */
4344
Gilles Peskinecbe66502019-05-16 16:59:18 +02004345static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004346 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004348 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4349 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004350 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004352}
4353
4354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004356{
4357 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004358 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004359 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004360 {
4361 /* The object has (apparently) been initialized but it is not
4362 * in use. It's ok to call abort on such an object, and there's
4363 * nothing to do. */
4364 }
4365 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004366#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004367 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004368 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 mbedtls_free( operation->ctx.hkdf.info );
4370 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004371 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004372 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004374 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004375 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004376 if( operation->ctx.tls12_prf.seed != NULL )
4377 {
4378 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4379 operation->ctx.tls12_prf.seed_length );
4380 mbedtls_free( operation->ctx.tls12_prf.seed );
4381 }
4382
4383 if( operation->ctx.tls12_prf.label != NULL )
4384 {
4385 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4386 operation->ctx.tls12_prf.label_length );
4387 mbedtls_free( operation->ctx.tls12_prf.label );
4388 }
4389
4390 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4391
4392 /* We leave the fields Ai and output_block to be erased safely by the
4393 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004394 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004395 else
4396#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004397 {
4398 status = PSA_ERROR_BAD_STATE;
4399 }
Janos Follath083036a2019-06-11 10:22:26 +01004400 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004401 return( status );
4402}
4403
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004405 size_t *capacity)
4406{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004407 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004408 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004409 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004410 return PSA_ERROR_BAD_STATE;
4411 }
4412
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004413 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004414 return( PSA_SUCCESS );
4415}
4416
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004417psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004418 size_t capacity )
4419{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004421 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004423 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004425 return( PSA_SUCCESS );
4426}
4427
Gilles Peskinebef7f142018-07-12 17:22:21 +02004428#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004431static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004432 psa_algorithm_t hash_alg,
4433 uint8_t *output,
4434 size_t output_length )
4435{
4436 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4437 psa_status_t status;
4438
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4440 return( PSA_ERROR_BAD_STATE );
4441 hkdf->state = HKDF_STATE_OUTPUT;
4442
Gilles Peskinebef7f142018-07-12 17:22:21 +02004443 while( output_length != 0 )
4444 {
4445 /* Copy what remains of the current block */
4446 uint8_t n = hash_length - hkdf->offset_in_block;
4447 if( n > output_length )
4448 n = (uint8_t) output_length;
4449 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4450 output += n;
4451 output_length -= n;
4452 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004453 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004454 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004455 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004456 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004457 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004458 * object was corrupted or if this function is called directly
4459 * inside the library. */
4460 if( hkdf->block_number == 0xff )
4461 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004462
4463 /* We need a new block */
4464 ++hkdf->block_number;
4465 hkdf->offset_in_block = 0;
4466 status = psa_hmac_setup_internal( &hkdf->hmac,
4467 hkdf->prk, hash_length,
4468 hash_alg );
4469 if( status != PSA_SUCCESS )
4470 return( status );
4471 if( hkdf->block_number != 1 )
4472 {
4473 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4474 hkdf->output_block,
4475 hash_length );
4476 if( status != PSA_SUCCESS )
4477 return( status );
4478 }
4479 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4480 hkdf->info,
4481 hkdf->info_length );
4482 if( status != PSA_SUCCESS )
4483 return( status );
4484 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4485 &hkdf->block_number, 1 );
4486 if( status != PSA_SUCCESS )
4487 return( status );
4488 status = psa_hmac_finish_internal( &hkdf->hmac,
4489 hkdf->output_block,
4490 sizeof( hkdf->output_block ) );
4491 if( status != PSA_SUCCESS )
4492 return( status );
4493 }
4494
4495 return( PSA_SUCCESS );
4496}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004497
Janos Follath7742fee2019-06-17 12:58:10 +01004498static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4499 psa_tls12_prf_key_derivation_t *tls12_prf,
4500 psa_algorithm_t alg )
4501{
4502 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4503 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004504 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4505 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004506
Janos Follath7742fee2019-06-17 12:58:10 +01004507 /* We can't be wanting more output after block 0xff, otherwise
4508 * the capacity check in psa_key_derivation_output_bytes() would have
4509 * prevented this call. It could happen only if the operation
4510 * object was corrupted or if this function is called directly
4511 * inside the library. */
4512 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004513 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004514
4515 /* We need a new block */
4516 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004517 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004518
4519 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4520 *
4521 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4522 *
4523 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4524 * HMAC_hash(secret, A(2) + seed) +
4525 * HMAC_hash(secret, A(3) + seed) + ...
4526 *
4527 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004528 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004529 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004530 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004531 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004532 * is currently extracted as `output_block` and where i is
4533 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004534 */
4535
Janos Follathea29bfb2019-06-19 12:21:20 +01004536 /* Save the hash context before using it, to preserve the hash state with
4537 * only the inner padding in it. We need this, because inner padding depends
4538 * on the key (secret in the RFC's terminology). */
4539 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4540 if( status != PSA_SUCCESS )
4541 goto cleanup;
4542
4543 /* Calculate A(i) where i = tls12_prf->block_number. */
4544 if( tls12_prf->block_number == 1 )
4545 {
4546 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4547 * the variable seed and in this instance means it in the context of the
4548 * P_hash function, where seed = label + seed.) */
4549 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4550 tls12_prf->label, tls12_prf->label_length );
4551 if( status != PSA_SUCCESS )
4552 goto cleanup;
4553 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4554 tls12_prf->seed, tls12_prf->seed_length );
4555 if( status != PSA_SUCCESS )
4556 goto cleanup;
4557 }
4558 else
4559 {
4560 /* A(i) = HMAC_hash(secret, A(i-1)) */
4561 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4562 tls12_prf->Ai, hash_length );
4563 if( status != PSA_SUCCESS )
4564 goto cleanup;
4565 }
4566
4567 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4568 tls12_prf->Ai, hash_length );
4569 if( status != PSA_SUCCESS )
4570 goto cleanup;
4571 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4572 if( status != PSA_SUCCESS )
4573 goto cleanup;
4574
4575 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4576 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4577 tls12_prf->Ai, hash_length );
4578 if( status != PSA_SUCCESS )
4579 goto cleanup;
4580 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4581 tls12_prf->label, tls12_prf->label_length );
4582 if( status != PSA_SUCCESS )
4583 goto cleanup;
4584 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4585 tls12_prf->seed, tls12_prf->seed_length );
4586 if( status != PSA_SUCCESS )
4587 goto cleanup;
4588 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4589 tls12_prf->output_block, hash_length );
4590 if( status != PSA_SUCCESS )
4591 goto cleanup;
4592 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4593 if( status != PSA_SUCCESS )
4594 goto cleanup;
4595
Janos Follath7742fee2019-06-17 12:58:10 +01004596
4597cleanup:
4598
Janos Follathea29bfb2019-06-19 12:21:20 +01004599 cleanup_status = psa_hash_abort( &backup );
4600 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4601 status = cleanup_status;
4602
4603 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004604}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004605
Janos Follath844eb0e2019-06-19 12:10:49 +01004606static psa_status_t psa_key_derivation_tls12_prf_read(
4607 psa_tls12_prf_key_derivation_t *tls12_prf,
4608 psa_algorithm_t alg,
4609 uint8_t *output,
4610 size_t output_length )
4611{
4612 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4613 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4614 psa_status_t status;
4615 uint8_t offset, length;
4616
4617 while( output_length != 0 )
4618 {
4619 /* Check if we have fully processed the current block. */
4620 if( tls12_prf->left_in_block == 0 )
4621 {
4622 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4623 alg );
4624 if( status != PSA_SUCCESS )
4625 return( status );
4626
4627 continue;
4628 }
4629
4630 if( tls12_prf->left_in_block > output_length )
4631 length = (uint8_t) output_length;
4632 else
4633 length = tls12_prf->left_in_block;
4634
4635 offset = hash_length - tls12_prf->left_in_block;
4636 memcpy( output, tls12_prf->output_block + offset, length );
4637 output += length;
4638 output_length -= length;
4639 tls12_prf->left_in_block -= length;
4640 }
4641
4642 return( PSA_SUCCESS );
4643}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004644#endif /* MBEDTLS_MD_C */
4645
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004646psa_status_t psa_key_derivation_output_bytes(
4647 psa_key_derivation_operation_t *operation,
4648 uint8_t *output,
4649 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004650{
4651 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004653
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004655 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004657 return PSA_ERROR_BAD_STATE;
4658 }
4659
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004661 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004665 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004666 goto exit;
4667 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004671 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004672 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4673 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004675 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004676 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004677 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004679
Gilles Peskinebef7f142018-07-12 17:22:21 +02004680#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004681 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004682 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004683 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004685 output, output_length );
4686 }
Janos Follathadbec812019-06-14 11:05:39 +01004687 else
Janos Follathadbec812019-06-14 11:05:39 +01004688 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004689 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004690 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004692 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004693 output_length );
4694 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004695 else
4696#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004697 {
4698 return( PSA_ERROR_BAD_STATE );
4699 }
4700
4701exit:
4702 if( status != PSA_SUCCESS )
4703 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004704 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 * This allows us to differentiate between exhausted operations and
4706 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4707 * operations. */
4708 psa_algorithm_t alg = operation->alg;
4709 psa_key_derivation_abort( operation );
4710 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004711 memset( output, '!', output_length );
4712 }
4713 return( status );
4714}
4715
Gilles Peskine08542d82018-07-19 17:05:42 +02004716#if defined(MBEDTLS_DES_C)
4717static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4718{
4719 if( data_size >= 8 )
4720 mbedtls_des_key_set_parity( data );
4721 if( data_size >= 16 )
4722 mbedtls_des_key_set_parity( data + 8 );
4723 if( data_size >= 24 )
4724 mbedtls_des_key_set_parity( data + 16 );
4725}
4726#endif /* MBEDTLS_DES_C */
4727
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004728static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004729 psa_key_slot_t *slot,
4730 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732{
4733 uint8_t *data = NULL;
4734 size_t bytes = PSA_BITS_TO_BYTES( bits );
4735 psa_status_t status;
4736
Gilles Peskine8e338702019-07-30 20:06:31 +02004737 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738 return( PSA_ERROR_INVALID_ARGUMENT );
4739 if( bits % 8 != 0 )
4740 return( PSA_ERROR_INVALID_ARGUMENT );
4741 data = mbedtls_calloc( 1, bytes );
4742 if( data == NULL )
4743 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004746 if( status != PSA_SUCCESS )
4747 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004748#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004749 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004750 psa_des_set_key_parity( data, bytes );
4751#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004752 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753
4754exit:
4755 mbedtls_free( data );
4756 return( status );
4757}
4758
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004759psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004761 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004762{
4763 psa_status_t status;
4764 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004765 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004766
4767 /* Reject any attempt to create a zero-length key so that we don't
4768 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4769 if( psa_get_key_bits( attributes ) == 0 )
4770 return( PSA_ERROR_INVALID_ARGUMENT );
4771
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004772 if( ! operation->can_output_key )
4773 return( PSA_ERROR_NOT_PERMITTED );
4774
Gilles Peskinedf179142019-07-15 22:02:14 +02004775 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4776 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4778 if( driver != NULL )
4779 {
4780 /* Deriving a key in a secure element is not implemented yet. */
4781 status = PSA_ERROR_NOT_SUPPORTED;
4782 }
4783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 if( status == PSA_SUCCESS )
4785 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004786 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004787 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 }
4790 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004791 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004792 if( status != PSA_SUCCESS )
4793 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004794 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004795 *handle = 0;
4796 }
4797 return( status );
4798}
4799
Gilles Peskineeab56e42018-07-12 17:12:33 +02004800
4801
4802/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004803/* Key derivation */
4804/****************************************************************/
4805
Gilles Peskine969c5d62019-01-16 15:53:06 +01004806static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004808 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004809{
Janos Follath5fe19732019-06-20 15:09:30 +01004810 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4811 * initialisers for this union leave some bytes unspecified.) */
4812 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4813
Gilles Peskine969c5d62019-01-16 15:53:06 +01004814 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004815#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004816 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4817 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4818 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004820 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004821 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4822 if( hash_size == 0 )
4823 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004824 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4825 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004826 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 {
4828 return( PSA_ERROR_NOT_SUPPORTED );
4829 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004832 }
4833#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004834 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004835 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004836}
4837
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004839 psa_algorithm_t alg )
4840{
4841 psa_status_t status;
4842
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004844 return( PSA_ERROR_BAD_STATE );
4845
Gilles Peskine6843c292019-01-18 16:44:49 +01004846 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4847 return( PSA_ERROR_INVALID_ARGUMENT );
4848 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004849 {
4850 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004852 }
4853 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4854 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857 else
4858 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004859
4860 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004862 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004863}
4864
4865#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004866static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004867 psa_algorithm_t hash_alg,
4868 psa_key_derivation_step_t step,
4869 const uint8_t *data,
4870 size_t data_length )
4871{
4872 psa_status_t status;
4873 switch( step )
4874 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004875 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004876 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004878 status = psa_hmac_setup_internal( &hkdf->hmac,
4879 data, data_length,
4880 hash_alg );
4881 if( status != PSA_SUCCESS )
4882 return( status );
4883 hkdf->state = HKDF_STATE_STARTED;
4884 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004885 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886 /* If no salt was provided, use an empty salt. */
4887 if( hkdf->state == HKDF_STATE_INIT )
4888 {
4889 status = psa_hmac_setup_internal( &hkdf->hmac,
4890 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004891 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892 if( status != PSA_SUCCESS )
4893 return( status );
4894 hkdf->state = HKDF_STATE_STARTED;
4895 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004896 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004898 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4899 data, data_length );
4900 if( status != PSA_SUCCESS )
4901 return( status );
4902 status = psa_hmac_finish_internal( &hkdf->hmac,
4903 hkdf->prk,
4904 sizeof( hkdf->prk ) );
4905 if( status != PSA_SUCCESS )
4906 return( status );
4907 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4908 hkdf->block_number = 0;
4909 hkdf->state = HKDF_STATE_KEYED;
4910 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004911 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004912 if( hkdf->state == HKDF_STATE_OUTPUT )
4913 return( PSA_ERROR_BAD_STATE );
4914 if( hkdf->info_set )
4915 return( PSA_ERROR_BAD_STATE );
4916 hkdf->info_length = data_length;
4917 if( data_length != 0 )
4918 {
4919 hkdf->info = mbedtls_calloc( 1, data_length );
4920 if( hkdf->info == NULL )
4921 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4922 memcpy( hkdf->info, data, data_length );
4923 }
4924 hkdf->info_set = 1;
4925 return( PSA_SUCCESS );
4926 default:
4927 return( PSA_ERROR_INVALID_ARGUMENT );
4928 }
4929}
Janos Follathb03233e2019-06-11 15:30:30 +01004930
Janos Follathf08e2652019-06-13 09:05:41 +01004931static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4932 const uint8_t *data,
4933 size_t data_length )
4934{
4935 if( prf->state != TLS12_PRF_STATE_INIT )
4936 return( PSA_ERROR_BAD_STATE );
4937
Janos Follathd6dce9f2019-07-04 09:11:38 +01004938 if( data_length != 0 )
4939 {
4940 prf->seed = mbedtls_calloc( 1, data_length );
4941 if( prf->seed == NULL )
4942 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004943
Janos Follathd6dce9f2019-07-04 09:11:38 +01004944 memcpy( prf->seed, data, data_length );
4945 prf->seed_length = data_length;
4946 }
Janos Follathf08e2652019-06-13 09:05:41 +01004947
4948 prf->state = TLS12_PRF_STATE_SEED_SET;
4949
4950 return( PSA_SUCCESS );
4951}
4952
Janos Follath81550542019-06-13 14:26:34 +01004953static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4954 psa_algorithm_t hash_alg,
4955 const uint8_t *data,
4956 size_t data_length )
4957{
4958 psa_status_t status;
4959 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4960 return( PSA_ERROR_BAD_STATE );
4961
4962 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4963 if( status != PSA_SUCCESS )
4964 return( status );
4965
4966 prf->state = TLS12_PRF_STATE_KEY_SET;
4967
4968 return( PSA_SUCCESS );
4969}
4970
Janos Follath6660f0e2019-06-17 08:44:03 +01004971static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4972 psa_tls12_prf_key_derivation_t *prf,
4973 psa_algorithm_t hash_alg,
4974 const uint8_t *data,
4975 size_t data_length )
4976{
4977 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004978 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4979 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004980
4981 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4982 return( PSA_ERROR_INVALID_ARGUMENT );
4983
4984 /* Quoting RFC 4279, Section 2:
4985 *
4986 * The premaster secret is formed as follows: if the PSK is N octets
4987 * long, concatenate a uint16 with the value N, N zero octets, a second
4988 * uint16 with the value N, and the PSK itself.
4989 */
4990
Janos Follath40e13932019-06-26 13:22:29 +01004991 *cur++ = ( data_length >> 8 ) & 0xff;
4992 *cur++ = ( data_length >> 0 ) & 0xff;
4993 memset( cur, 0, data_length );
4994 cur += data_length;
4995 *cur++ = pms[0];
4996 *cur++ = pms[1];
4997 memcpy( cur, data, data_length );
4998 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004999
Janos Follath40e13932019-06-26 13:22:29 +01005000 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005001
5002 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5003 return( status );
5004}
5005
Janos Follath63028dd2019-06-13 09:15:47 +01005006static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5007 const uint8_t *data,
5008 size_t data_length )
5009{
5010 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5011 return( PSA_ERROR_BAD_STATE );
5012
Janos Follathd6dce9f2019-07-04 09:11:38 +01005013 if( data_length != 0 )
5014 {
5015 prf->label = mbedtls_calloc( 1, data_length );
5016 if( prf->label == NULL )
5017 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005018
Janos Follathd6dce9f2019-07-04 09:11:38 +01005019 memcpy( prf->label, data, data_length );
5020 prf->label_length = data_length;
5021 }
Janos Follath63028dd2019-06-13 09:15:47 +01005022
5023 prf->state = TLS12_PRF_STATE_LABEL_SET;
5024
5025 return( PSA_SUCCESS );
5026}
5027
Janos Follathb03233e2019-06-11 15:30:30 +01005028static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5029 psa_algorithm_t hash_alg,
5030 psa_key_derivation_step_t step,
5031 const uint8_t *data,
5032 size_t data_length )
5033{
Janos Follathb03233e2019-06-11 15:30:30 +01005034 switch( step )
5035 {
Janos Follathf08e2652019-06-13 09:05:41 +01005036 case PSA_KEY_DERIVATION_INPUT_SEED:
5037 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005038 case PSA_KEY_DERIVATION_INPUT_SECRET:
5039 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005040 case PSA_KEY_DERIVATION_INPUT_LABEL:
5041 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005042 default:
5043 return( PSA_ERROR_INVALID_ARGUMENT );
5044 }
5045}
Janos Follath6660f0e2019-06-17 08:44:03 +01005046
5047static psa_status_t psa_tls12_prf_psk_to_ms_input(
5048 psa_tls12_prf_key_derivation_t *prf,
5049 psa_algorithm_t hash_alg,
5050 psa_key_derivation_step_t step,
5051 const uint8_t *data,
5052 size_t data_length )
5053{
5054 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005055 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005056 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5057 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005058 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005059
5060 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5061}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005062#endif /* MBEDTLS_MD_C */
5063
Gilles Peskineb8965192019-09-24 16:21:10 +02005064/** Check whether the given key type is acceptable for the given
5065 * input step of a key derivation.
5066 *
5067 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5068 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5069 * Both secret and non-secret inputs can alternatively have the type
5070 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5071 * that the input was passed as a buffer rather than via a key object.
5072 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005073static int psa_key_derivation_check_input_type(
5074 psa_key_derivation_step_t step,
5075 psa_key_type_t key_type )
5076{
5077 switch( step )
5078 {
5079 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005080 if( key_type == PSA_KEY_TYPE_DERIVE )
5081 return( PSA_SUCCESS );
5082 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005083 return( PSA_SUCCESS );
5084 break;
5085 case PSA_KEY_DERIVATION_INPUT_LABEL:
5086 case PSA_KEY_DERIVATION_INPUT_SALT:
5087 case PSA_KEY_DERIVATION_INPUT_INFO:
5088 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005089 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5090 return( PSA_SUCCESS );
5091 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005092 return( PSA_SUCCESS );
5093 break;
5094 }
5095 return( PSA_ERROR_INVALID_ARGUMENT );
5096}
5097
Janos Follathb80a94e2019-06-12 15:54:46 +01005098static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005100 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005101 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005102 const uint8_t *data,
5103 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005104{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005105 psa_status_t status;
5106 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5107
5108 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005109 if( status != PSA_SUCCESS )
5110 goto exit;
5111
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005113 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005116 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117 step, data, data_length );
5118 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005119 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120 {
Janos Follathb03233e2019-06-11 15:30:30 +01005121 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5122 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5123 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005124 }
5125 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5126 {
5127 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5128 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5129 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130 }
5131 else
5132#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135 return( PSA_ERROR_BAD_STATE );
5136 }
5137
Gilles Peskine224b0d62019-09-23 18:13:17 +02005138exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005139 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005141 return( status );
5142}
5143
Janos Follath51f4a0f2019-06-14 11:35:55 +01005144psa_status_t psa_key_derivation_input_bytes(
5145 psa_key_derivation_operation_t *operation,
5146 psa_key_derivation_step_t step,
5147 const uint8_t *data,
5148 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005149{
Gilles Peskineb8965192019-09-24 16:21:10 +02005150 return( psa_key_derivation_input_internal( operation, step,
5151 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005152 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005153}
5154
Janos Follath51f4a0f2019-06-14 11:35:55 +01005155psa_status_t psa_key_derivation_input_key(
5156 psa_key_derivation_operation_t *operation,
5157 psa_key_derivation_step_t step,
5158 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005159{
5160 psa_key_slot_t *slot;
5161 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005162
Gilles Peskine28f8f302019-07-24 13:30:31 +02005163 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005164 PSA_KEY_USAGE_DERIVE,
5165 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005166 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005167 {
5168 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005169 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005170 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005171
5172 /* Passing a key object as a SECRET input unlocks the permission
5173 * to output to a key object. */
5174 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5175 operation->can_output_key = 1;
5176
Janos Follathb80a94e2019-06-12 15:54:46 +01005177 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005178 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005179 slot->data.raw.data,
5180 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005182
5183
5184
5185/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005186/* Key agreement */
5187/****************************************************************/
5188
Gilles Peskinea05219c2018-11-16 16:02:56 +01005189#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005190static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5191 size_t peer_key_length,
5192 const mbedtls_ecp_keypair *our_key,
5193 uint8_t *shared_secret,
5194 size_t shared_secret_size,
5195 size_t *shared_secret_length )
5196{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005197 mbedtls_ecp_keypair *their_key = NULL;
5198 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005199 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005200 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005201
Jaeden Ameroccdce902019-01-10 11:42:27 +00005202 status = psa_import_ec_public_key(
5203 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5204 peer_key, peer_key_length,
5205 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005206 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005207 goto exit;
5208
Jaeden Amero97271b32019-01-10 19:38:51 +00005209 status = mbedtls_to_psa_error(
5210 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5211 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005212 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005213 status = mbedtls_to_psa_error(
5214 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5215 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005216 goto exit;
5217
Jaeden Amero97271b32019-01-10 19:38:51 +00005218 status = mbedtls_to_psa_error(
5219 mbedtls_ecdh_calc_secret( &ecdh,
5220 shared_secret_length,
5221 shared_secret, shared_secret_size,
5222 mbedtls_ctr_drbg_random,
5223 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005224
5225exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005226 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005227 mbedtls_ecp_keypair_free( their_key );
5228 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005229 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005230}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005231#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005232
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5234
Gilles Peskine0216fe12019-04-11 21:23:21 +02005235static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5236 psa_key_slot_t *private_key,
5237 const uint8_t *peer_key,
5238 size_t peer_key_length,
5239 uint8_t *shared_secret,
5240 size_t shared_secret_size,
5241 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005242{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005243 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005244 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005245#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005246 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005247 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005248 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005249 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5250 private_key->data.ecp,
5251 shared_secret, shared_secret_size,
5252 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005253#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005254 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005255 (void) private_key;
5256 (void) peer_key;
5257 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005258 (void) shared_secret;
5259 (void) shared_secret_size;
5260 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261 return( PSA_ERROR_NOT_SUPPORTED );
5262 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005263}
5264
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005265/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005266 * to potentially free embedded data structures and wipe confidential data.
5267 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005269 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005270 psa_key_slot_t *private_key,
5271 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273{
5274 psa_status_t status;
5275 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5276 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005278
5279 /* Step 1: run the secret agreement algorithm to generate the shared
5280 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005281 status = psa_key_agreement_raw_internal( ka_alg,
5282 private_key,
5283 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005284 shared_secret,
5285 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005286 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005287 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005288 goto exit;
5289
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005290 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005291 * the shared secret. A shared secret is permitted wherever a key
5292 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005293 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005294 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005295 shared_secret,
5296 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005297
Gilles Peskine12313cd2018-06-20 00:20:32 +02005298exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005299 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005300 return( status );
5301}
5302
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005304 psa_key_derivation_step_t step,
5305 psa_key_handle_t private_key,
5306 const uint8_t *peer_key,
5307 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005308{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005309 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005310 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005312 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005313 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005314 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005315 if( status != PSA_SUCCESS )
5316 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005318 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005319 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005320 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005322 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005323}
5324
Gilles Peskinebe697d82019-05-16 18:00:41 +02005325psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5326 psa_key_handle_t private_key,
5327 const uint8_t *peer_key,
5328 size_t peer_key_length,
5329 uint8_t *output,
5330 size_t output_size,
5331 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005332{
5333 psa_key_slot_t *slot;
5334 psa_status_t status;
5335
5336 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5337 {
5338 status = PSA_ERROR_INVALID_ARGUMENT;
5339 goto exit;
5340 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005341 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005342 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005343 if( status != PSA_SUCCESS )
5344 goto exit;
5345
5346 status = psa_key_agreement_raw_internal( alg, slot,
5347 peer_key, peer_key_length,
5348 output, output_size,
5349 output_length );
5350
5351exit:
5352 if( status != PSA_SUCCESS )
5353 {
5354 /* If an error happens and is not handled properly, the output
5355 * may be used as a key to protect sensitive data. Arrange for such
5356 * a key to be random, which is likely to result in decryption or
5357 * verification errors. This is better than filling the buffer with
5358 * some constant data such as zeros, which would result in the data
5359 * being protected with a reproducible, easily knowable key.
5360 */
5361 psa_generate_random( output, output_size );
5362 *output_length = output_size;
5363 }
5364 return( status );
5365}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005366
5367
5368/****************************************************************/
5369/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005370/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371
Gilles Peskine4c317f42018-07-12 01:24:09 +02005372psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005373 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005374{
Janos Follath24eed8d2019-11-22 13:21:35 +00005375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005376 GUARD_MODULE_INITIALIZED;
5377
Gilles Peskinef181eca2019-08-07 13:49:00 +02005378 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5379 {
5380 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5381 output,
5382 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5383 if( ret != 0 )
5384 return( mbedtls_to_psa_error( ret ) );
5385 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5386 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5387 }
5388
Gilles Peskinee59236f2018-01-27 23:32:46 +01005389 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5390 return( mbedtls_to_psa_error( ret ) );
5391}
5392
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005393#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5394#include "mbedtls/entropy_poll.h"
5395
Gilles Peskine7228da22019-07-15 11:06:15 +02005396psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005397 size_t seed_size )
5398{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005399 if( global_data.initialized )
5400 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005401
5402 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5403 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5404 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5405 return( PSA_ERROR_INVALID_ARGUMENT );
5406
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005407 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005408}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005409#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005410
Gilles Peskinee56e8782019-04-26 17:34:02 +02005411#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5412static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5413 size_t domain_parameters_size,
5414 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005415{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005416 size_t i;
5417 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005418
Gilles Peskinee56e8782019-04-26 17:34:02 +02005419 if( domain_parameters_size == 0 )
5420 {
5421 *exponent = 65537;
5422 return( PSA_SUCCESS );
5423 }
5424
5425 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5426 * support values that fit in a 32-bit integer, which is larger than
5427 * int on just about every platform anyway. */
5428 if( domain_parameters_size > sizeof( acc ) )
5429 return( PSA_ERROR_NOT_SUPPORTED );
5430 for( i = 0; i < domain_parameters_size; i++ )
5431 acc = ( acc << 8 ) | domain_parameters[i];
5432 if( acc > INT_MAX )
5433 return( PSA_ERROR_NOT_SUPPORTED );
5434 *exponent = acc;
5435 return( PSA_SUCCESS );
5436}
5437#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5438
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005439static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005440 psa_key_slot_t *slot, size_t bits,
5441 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005442{
Gilles Peskine8e338702019-07-30 20:06:31 +02005443 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005444
Gilles Peskinee56e8782019-04-26 17:34:02 +02005445 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005446 return( PSA_ERROR_INVALID_ARGUMENT );
5447
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448 if( key_type_is_raw_bytes( type ) )
5449 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005450 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005451 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5452 if( status != PSA_SUCCESS )
5453 return( status );
5454 status = psa_generate_random( slot->data.raw.data,
5455 slot->data.raw.bytes );
5456 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005457 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005458#if defined(MBEDTLS_DES_C)
5459 if( type == PSA_KEY_TYPE_DES )
5460 psa_des_set_key_parity( slot->data.raw.data,
5461 slot->data.raw.bytes );
5462#endif /* MBEDTLS_DES_C */
5463 }
5464 else
5465
5466#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005467 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005468 {
5469 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005471 int exponent;
5472 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005473 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5474 return( PSA_ERROR_NOT_SUPPORTED );
5475 /* Accept only byte-aligned keys, for the same reasons as
5476 * in psa_import_rsa_key(). */
5477 if( bits % 8 != 0 )
5478 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005479 status = psa_read_rsa_exponent( domain_parameters,
5480 domain_parameters_size,
5481 &exponent );
5482 if( status != PSA_SUCCESS )
5483 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005484 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5485 if( rsa == NULL )
5486 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5487 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5488 ret = mbedtls_rsa_gen_key( rsa,
5489 mbedtls_ctr_drbg_random,
5490 &global_data.ctr_drbg,
5491 (unsigned int) bits,
5492 exponent );
5493 if( ret != 0 )
5494 {
5495 mbedtls_rsa_free( rsa );
5496 mbedtls_free( rsa );
5497 return( mbedtls_to_psa_error( ret ) );
5498 }
5499 slot->data.rsa = rsa;
5500 }
5501 else
5502#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5503
5504#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005505 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506 {
5507 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5508 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5509 const mbedtls_ecp_curve_info *curve_info =
5510 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5511 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005513 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005514 return( PSA_ERROR_NOT_SUPPORTED );
5515 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5516 return( PSA_ERROR_NOT_SUPPORTED );
5517 if( curve_info->bit_size != bits )
5518 return( PSA_ERROR_INVALID_ARGUMENT );
5519 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5520 if( ecp == NULL )
5521 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5522 mbedtls_ecp_keypair_init( ecp );
5523 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5524 mbedtls_ctr_drbg_random,
5525 &global_data.ctr_drbg );
5526 if( ret != 0 )
5527 {
5528 mbedtls_ecp_keypair_free( ecp );
5529 mbedtls_free( ecp );
5530 return( mbedtls_to_psa_error( ret ) );
5531 }
5532 slot->data.ecp = ecp;
5533 }
5534 else
5535#endif /* MBEDTLS_ECP_C */
5536
5537 return( PSA_ERROR_NOT_SUPPORTED );
5538
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005539 return( PSA_SUCCESS );
5540}
Darryl Green0c6575a2018-11-07 16:05:30 +00005541
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005542psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005543 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544{
5545 psa_status_t status;
5546 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005547 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005548
Gilles Peskine0f84d622019-09-12 19:03:13 +02005549 /* Reject any attempt to create a zero-length key so that we don't
5550 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5551 if( psa_get_key_bits( attributes ) == 0 )
5552 return( PSA_ERROR_INVALID_ARGUMENT );
5553
Gilles Peskinedf179142019-07-15 22:02:14 +02005554 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5555 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005556 if( status != PSA_SUCCESS )
5557 goto exit;
5558
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005559#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5560 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005561 {
Gilles Peskine11792082019-08-06 18:36:36 +02005562 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5563 size_t pubkey_length = 0; /* We don't support this feature yet */
5564 if( drv->key_management == NULL ||
5565 drv->key_management->p_generate == NULL )
5566 {
5567 status = PSA_ERROR_NOT_SUPPORTED;
5568 goto exit;
5569 }
5570 status = drv->key_management->p_generate(
5571 psa_get_se_driver_context( driver ),
5572 slot->data.se.slot_number, attributes,
5573 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005574 }
Gilles Peskine11792082019-08-06 18:36:36 +02005575 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005576#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005577 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005578 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005579 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005580 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005581 }
Gilles Peskine11792082019-08-06 18:36:36 +02005582
5583exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005584 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005585 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005586 if( status != PSA_SUCCESS )
5587 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005588 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005589 *handle = 0;
5590 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005591 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592}
5593
5594
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595
5596/****************************************************************/
5597/* Module setup */
5598/****************************************************************/
5599
Gilles Peskine5e769522018-11-20 21:59:56 +01005600psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5601 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5602 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5603{
5604 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5605 return( PSA_ERROR_BAD_STATE );
5606 global_data.entropy_init = entropy_init;
5607 global_data.entropy_free = entropy_free;
5608 return( PSA_SUCCESS );
5609}
5610
Gilles Peskinee59236f2018-01-27 23:32:46 +01005611void mbedtls_psa_crypto_free( void )
5612{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005613 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005614 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5615 {
5616 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005617 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005618 }
5619 /* Wipe all remaining data, including configuration.
5620 * In particular, this sets all state indicator to the value
5621 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005622 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005623#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005624 /* Unregister all secure element drivers, so that we restart from
5625 * a pristine state. */
5626 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005627#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005628}
5629
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005630#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5631/** Recover a transaction that was interrupted by a power failure.
5632 *
5633 * This function is called during initialization, before psa_crypto_init()
5634 * returns. If this function returns a failure status, the initialization
5635 * fails.
5636 */
5637static psa_status_t psa_crypto_recover_transaction(
5638 const psa_crypto_transaction_t *transaction )
5639{
5640 switch( transaction->unknown.type )
5641 {
5642 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5643 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005644 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005645 * is implemented.
5646 * https://github.com/ARMmbed/mbed-crypto/issues/218
5647 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005648 default:
5649 /* We found an unsupported transaction in the storage.
5650 * We don't know what state the storage is in. Give up. */
5651 return( PSA_ERROR_STORAGE_FAILURE );
5652 }
5653}
5654#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5655
Gilles Peskinee59236f2018-01-27 23:32:46 +01005656psa_status_t psa_crypto_init( void )
5657{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005658 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005659 const unsigned char drbg_seed[] = "PSA";
5660
Gilles Peskinec6b69072018-11-20 21:42:52 +01005661 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005662 if( global_data.initialized != 0 )
5663 return( PSA_SUCCESS );
5664
Gilles Peskine5e769522018-11-20 21:59:56 +01005665 /* Set default configuration if
5666 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5667 if( global_data.entropy_init == NULL )
5668 global_data.entropy_init = mbedtls_entropy_init;
5669 if( global_data.entropy_free == NULL )
5670 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005671
Gilles Peskinec6b69072018-11-20 21:42:52 +01005672 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005673 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005674#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5675 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5676 /* The PSA entropy injection feature depends on using NV seed as an entropy
5677 * source. Add NV seed as an entropy source for PSA entropy injection. */
5678 mbedtls_entropy_add_source( &global_data.entropy,
5679 mbedtls_nv_seed_poll, NULL,
5680 MBEDTLS_ENTROPY_BLOCK_SIZE,
5681 MBEDTLS_ENTROPY_SOURCE_STRONG );
5682#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005683 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005684 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005685 status = mbedtls_to_psa_error(
5686 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5687 mbedtls_entropy_func,
5688 &global_data.entropy,
5689 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5690 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005691 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005692 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005693
Gilles Peskine66fb1262018-12-10 16:29:04 +01005694 status = psa_initialize_key_slots( );
5695 if( status != PSA_SUCCESS )
5696 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005697
Gilles Peskined9348f22019-10-01 15:22:29 +02005698#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5699 status = psa_init_all_se_drivers( );
5700 if( status != PSA_SUCCESS )
5701 goto exit;
5702#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5703
Gilles Peskine4b734222019-07-24 15:56:31 +02005704#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005705 status = psa_crypto_load_transaction( );
5706 if( status == PSA_SUCCESS )
5707 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005708 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5709 if( status != PSA_SUCCESS )
5710 goto exit;
5711 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005712 }
5713 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5714 {
5715 /* There's no transaction to complete. It's all good. */
5716 status = PSA_SUCCESS;
5717 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005718#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005719
Gilles Peskinec6b69072018-11-20 21:42:52 +01005720 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005721 global_data.initialized = 1;
5722
5723exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005724 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005725 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005726 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005727}
5728
5729#endif /* MBEDTLS_PSA_CRYPTO_C */