blob: c4dd36fefe660365acdc78819b15573d8f0d8da8 [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
Michael Thomas863b5d62020-02-10 19:41:16 +0000132psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
206 return( PSA_ERROR_BAD_STATE );
207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100378psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
379 size_t *bits )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200380{
381 switch( grpid )
382 {
383 case MBEDTLS_ECP_DP_SECP192R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100384 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100385 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200386 case MBEDTLS_ECP_DP_SECP224R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100387 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100388 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200389 case MBEDTLS_ECP_DP_SECP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100390 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100391 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200392 case MBEDTLS_ECP_DP_SECP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100393 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100394 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200395 case MBEDTLS_ECP_DP_SECP521R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100396 *bits = 521;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100397 return( PSA_ECC_CURVE_SECP_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200398 case MBEDTLS_ECP_DP_BP256R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100399 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100400 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200401 case MBEDTLS_ECP_DP_BP384R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100402 *bits = 384;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100403 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200404 case MBEDTLS_ECP_DP_BP512R1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100405 *bits = 512;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100406 return( PSA_ECC_CURVE_BRAINPOOL_P_R1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200407 case MBEDTLS_ECP_DP_CURVE25519:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100408 *bits = 255;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100409 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200410 case MBEDTLS_ECP_DP_SECP192K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100411 *bits = 192;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100412 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200413 case MBEDTLS_ECP_DP_SECP224K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100414 *bits = 224;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100415 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200416 case MBEDTLS_ECP_DP_SECP256K1:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100417 *bits = 256;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100418 return( PSA_ECC_CURVE_SECP_K1 );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200419 case MBEDTLS_ECP_DP_CURVE448:
Gilles Peskinec7ef5b32019-12-12 16:58:00 +0100420 *bits = 448;
Gilles Peskineb87b7192019-12-04 16:24:10 +0100421 return( PSA_ECC_CURVE_MONTGOMERY );
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200422 default:
423 return( 0 );
424 }
425}
426
Gilles Peskine5055b232019-12-12 17:49:31 +0100427mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
428 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429{
430 switch( curve )
431 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 case PSA_ECC_CURVE_SECP_R1:
433 switch( byte_length )
434 {
435 case PSA_BITS_TO_BYTES( 192 ):
436 return( MBEDTLS_ECP_DP_SECP192R1 );
437 case PSA_BITS_TO_BYTES( 224 ):
438 return( MBEDTLS_ECP_DP_SECP224R1 );
439 case PSA_BITS_TO_BYTES( 256 ):
440 return( MBEDTLS_ECP_DP_SECP256R1 );
441 case PSA_BITS_TO_BYTES( 384 ):
442 return( MBEDTLS_ECP_DP_SECP384R1 );
443 case PSA_BITS_TO_BYTES( 521 ):
444 return( MBEDTLS_ECP_DP_SECP521R1 );
445 default:
446 return( MBEDTLS_ECP_DP_NONE );
447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
450 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
451 switch( byte_length )
452 {
453 case PSA_BITS_TO_BYTES( 256 ):
454 return( MBEDTLS_ECP_DP_BP256R1 );
455 case PSA_BITS_TO_BYTES( 384 ):
456 return( MBEDTLS_ECP_DP_BP384R1 );
457 case PSA_BITS_TO_BYTES( 512 ):
458 return( MBEDTLS_ECP_DP_BP512R1 );
459 default:
460 return( MBEDTLS_ECP_DP_NONE );
461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
464 case PSA_ECC_CURVE_MONTGOMERY:
465 switch( byte_length )
466 {
467 case PSA_BITS_TO_BYTES( 255 ):
468 return( MBEDTLS_ECP_DP_CURVE25519 );
469 case PSA_BITS_TO_BYTES( 448 ):
470 return( MBEDTLS_ECP_DP_CURVE448 );
471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
476 case PSA_ECC_CURVE_SECP_K1:
477 switch( byte_length )
478 {
479 case PSA_BITS_TO_BYTES( 192 ):
480 return( MBEDTLS_ECP_DP_SECP192K1 );
481 case PSA_BITS_TO_BYTES( 224 ):
482 return( MBEDTLS_ECP_DP_SECP224K1 );
483 case PSA_BITS_TO_BYTES( 256 ):
484 return( MBEDTLS_ECP_DP_SECP256K1 );
485 default:
486 return( MBEDTLS_ECP_DP_NONE );
487 }
488 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100489
Gilles Peskine12313cd2018-06-20 00:20:32 +0200490 default:
491 return( MBEDTLS_ECP_DP_NONE );
492 }
493}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100494#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200495
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
497 size_t bits,
498 struct raw_data *raw )
499{
500 /* Check that the bit size is acceptable for the key type */
501 switch( type )
502 {
503 case PSA_KEY_TYPE_RAW_DATA:
504#if defined(MBEDTLS_MD_C)
505 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200506#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200507 case PSA_KEY_TYPE_DERIVE:
508 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509#if defined(MBEDTLS_AES_C)
510 case PSA_KEY_TYPE_AES:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_CAMELLIA_C)
516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_DES_C)
522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_ARC4_C)
528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200533#if defined(MBEDTLS_CHACHA20_C)
534 case PSA_KEY_TYPE_CHACHA20:
535 if( bits != 256 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
545 /* Allocate memory for the key */
546 raw->bytes = PSA_BITS_TO_BYTES( bits );
547 raw->data = mbedtls_calloc( 1, raw->bytes );
548 if( raw->data == NULL )
549 {
550 raw->bytes = 0;
551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
552 }
553 return( PSA_SUCCESS );
554}
555
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100557/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
558 * that are not a multiple of 8) well. For example, there is only
559 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
560 * way to return the exact bit size of a key.
561 * To keep things simple, reject non-byte-aligned key sizes. */
562static psa_status_t psa_check_rsa_key_byte_aligned(
563 const mbedtls_rsa_context *rsa )
564{
565 mbedtls_mpi n;
566 psa_status_t status;
567 mbedtls_mpi_init( &n );
568 status = mbedtls_to_psa_error(
569 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
570 if( status == PSA_SUCCESS )
571 {
572 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
573 status = PSA_ERROR_NOT_SUPPORTED;
574 }
575 mbedtls_mpi_free( &n );
576 return( status );
577}
578
Michael Thomas1ed98bd2020-02-18 22:11:30 +0000579psa_status_t psa_import_rsa_key( psa_key_type_t type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 const uint8_t *data,
581 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582 mbedtls_rsa_context **p_rsa )
583{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000584 psa_status_t status;
585 mbedtls_pk_context pk;
586 mbedtls_rsa_context *rsa;
587 size_t bits;
588
589 mbedtls_pk_init( &pk );
590
591 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200592 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000593 status = mbedtls_to_psa_error(
594 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200595 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000596 status = mbedtls_to_psa_error(
597 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
598 if( status != PSA_SUCCESS )
599 goto exit;
600
601 /* We have something that the pkparse module recognizes. If it is a
602 * valid RSA key, store it. */
603 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000605 status = PSA_ERROR_INVALID_ARGUMENT;
606 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000608
609 rsa = mbedtls_pk_rsa( pk );
610 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
611 * supports non-byte-aligned key sizes, but not well. For example,
612 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
613 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
614 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
615 {
616 status = PSA_ERROR_NOT_SUPPORTED;
617 goto exit;
618 }
619 status = psa_check_rsa_key_byte_aligned( rsa );
620
621exit:
622 /* Free the content of the pk object only on error. */
623 if( status != PSA_SUCCESS )
624 {
625 mbedtls_pk_free( &pk );
626 return( status );
627 }
628
629 /* On success, store the content of the object in the RSA context. */
630 *p_rsa = rsa;
631
632 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
634#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
635
Jaeden Ameroccdce902019-01-10 11:42:27 +0000636#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100637static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100638 size_t data_length,
639 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100640 mbedtls_ecp_keypair **p_ecp )
641{
642 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
643 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
644 if( *p_ecp == NULL )
645 return( PSA_ERROR_INSUFFICIENT_MEMORY );
646 mbedtls_ecp_keypair_init( *p_ecp );
647
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100648 if( is_public )
649 {
650 /* A public key is represented as:
651 * - The byte 0x04;
652 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
653 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
654 * So its data length is 2m+1 where n is the key size in bits.
655 */
656 if( ( data_length & 1 ) == 0 )
657 return( PSA_ERROR_INVALID_ARGUMENT );
658 data_length = data_length / 2;
659 }
660
Gilles Peskine4cd32772019-12-02 20:49:42 +0100661 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100662 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
663 if( grp_id == MBEDTLS_ECP_DP_NONE )
664 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100665 return( mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
667}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000668
669/* Import a public key given as the uncompressed representation defined by SEC1
670 * 2.3.3 as the content of an ECPoint. */
Michael Thomas863b5d62020-02-10 19:41:16 +0000671psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
Jaeden Ameroccdce902019-01-10 11:42:27 +0000672 const uint8_t *data,
673 size_t data_length,
674 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200675{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000677 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000678
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100679 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000680 if( status != PSA_SUCCESS )
681 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100682
Jaeden Ameroccdce902019-01-10 11:42:27 +0000683 /* Load the public value. */
684 status = mbedtls_to_psa_error(
685 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
686 data, data_length ) );
687 if( status != PSA_SUCCESS )
688 goto exit;
689
690 /* Check that the point is on the curve. */
691 status = mbedtls_to_psa_error(
692 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
693 if( status != PSA_SUCCESS )
694 goto exit;
695
696 *p_ecp = ecp;
697 return( PSA_SUCCESS );
698
699exit:
700 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000702 mbedtls_ecp_keypair_free( ecp );
703 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200704 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000705 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200706}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707
Gilles Peskinef76aa772018-10-29 19:24:33 +0100708/* Import a private key given as a byte string which is the private value
709 * in big-endian order. */
710static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
711 const uint8_t *data,
712 size_t data_length,
713 mbedtls_ecp_keypair **p_ecp )
714{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100716 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100718 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100719 if( status != PSA_SUCCESS )
720 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100721
Gilles Peskinef76aa772018-10-29 19:24:33 +0100722 /* Load the secret value. */
723 status = mbedtls_to_psa_error(
724 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
725 if( status != PSA_SUCCESS )
726 goto exit;
727 /* Validate the private key. */
728 status = mbedtls_to_psa_error(
729 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
730 if( status != PSA_SUCCESS )
731 goto exit;
732 /* Calculate the public key from the private key. */
733 status = mbedtls_to_psa_error(
734 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
735 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
736 if( status != PSA_SUCCESS )
737 goto exit;
738
739 *p_ecp = ecp;
740 return( PSA_SUCCESS );
741
742exit:
743 if( ecp != NULL )
744 {
745 mbedtls_ecp_keypair_free( ecp );
746 mbedtls_free( ecp );
747 }
748 return( status );
749}
750#endif /* defined(MBEDTLS_ECP_C) */
751
Gilles Peskineb46bef22019-07-30 21:32:04 +0200752
753/** Return the size of the key in the given slot, in bits.
754 *
755 * \param[in] slot A key slot.
756 *
757 * \return The key size in bits, read from the metadata in the slot.
758 */
759static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
760{
761 return( slot->attr.bits );
762}
763
764/** Calculate the size of the key in the given slot, in bits.
765 *
766 * \param[in] slot A key slot containing a transparent key.
767 *
768 * \return The key size in bits, calculated from the key data.
769 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200770static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200771{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200772 size_t bits = 0; /* return 0 on an empty slot */
773
Gilles Peskineb46bef22019-07-30 21:32:04 +0200774 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200775 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200776#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200777 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
778 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200779#endif /* defined(MBEDTLS_RSA_C) */
780#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200781 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
782 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200783#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200784
785 /* We know that the size fits in psa_key_bits_t thanks to checks
786 * when the key was created. */
787 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200788}
789
Gilles Peskine8e338702019-07-30 20:06:31 +0200790/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100791 * previously. This function assumes that the slot does not contain
792 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100793psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
794 const uint8_t *data,
795 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200797 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798
Gilles Peskine8e338702019-07-30 20:06:31 +0200799 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200801 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200802 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 if( data_length > SIZE_MAX / 8 )
804 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200805 /* Enforce a size limit, and in particular ensure that the bit
806 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200807 if( bit_size > PSA_MAX_KEY_BITS )
808 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200809 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200810 &slot->data.raw );
811 if( status != PSA_SUCCESS )
812 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200813 if( data_length != 0 )
814 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 }
816 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100817#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200818 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100819 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200820 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100821 data, data_length,
822 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100823 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200824 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100825 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000826 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200827 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000828 data, data_length,
829 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100830 }
831 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100832#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000833#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200834 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200836 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000837 data, data_length,
838 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839 }
840 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000841#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842 {
843 return( PSA_ERROR_NOT_SUPPORTED );
844 }
Darryl Green940d72c2018-07-13 13:18:51 +0100845
Gilles Peskineb46bef22019-07-30 21:32:04 +0200846 if( status == PSA_SUCCESS )
847 {
848 /* Write the actual key size to the slot.
849 * psa_start_key_creation() wrote the size declared by the
850 * caller, which may be 0 (meaning unspecified) or wrong. */
851 slot->attr.bits = psa_calculate_key_bits( slot );
852 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100853 return( status );
854}
855
Gilles Peskinef603c712019-01-19 13:40:11 +0100856/** Calculate the intersection of two algorithm usage policies.
857 *
858 * Return 0 (which allows no operation) on incompatibility.
859 */
860static psa_algorithm_t psa_key_policy_algorithm_intersection(
861 psa_algorithm_t alg1,
862 psa_algorithm_t alg2 )
863{
Gilles Peskine549ea862019-05-22 11:45:59 +0200864 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100865 if( alg1 == alg2 )
866 return( alg1 );
867 /* If the policies are from the same hash-and-sign family, check
868 * if one is a wildcard. If so the other has the specific algorithm. */
869 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
870 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
871 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
872 {
873 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
874 return( alg2 );
875 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
876 return( alg1 );
877 }
878 /* If the policies are incompatible, allow nothing. */
879 return( 0 );
880}
881
Gilles Peskined6f371b2019-05-10 19:33:38 +0200882static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
883 psa_algorithm_t requested_alg )
884{
Gilles Peskine549ea862019-05-22 11:45:59 +0200885 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 if( requested_alg == policy_alg )
887 return( 1 );
888 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200889 * and requested_alg is the same hash-and-sign family with any hash,
890 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
892 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
893 {
894 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
895 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
896 }
897 /* If it isn't permitted, it's forbidden. */
898 return( 0 );
899}
900
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100901/** Test whether a policy permits an algorithm.
902 *
903 * The caller must test usage flags separately.
904 */
905static int psa_key_policy_permits( const psa_key_policy_t *policy,
906 psa_algorithm_t alg )
907{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200908 return( psa_key_algorithm_permits( policy->alg, alg ) ||
909 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100910}
911
Gilles Peskinef603c712019-01-19 13:40:11 +0100912/** Restrict a key policy based on a constraint.
913 *
914 * \param[in,out] policy The policy to restrict.
915 * \param[in] constraint The policy constraint to apply.
916 *
917 * \retval #PSA_SUCCESS
918 * \c *policy contains the intersection of the original value of
919 * \c *policy and \c *constraint.
920 * \retval #PSA_ERROR_INVALID_ARGUMENT
921 * \c *policy and \c *constraint are incompatible.
922 * \c *policy is unchanged.
923 */
924static psa_status_t psa_restrict_key_policy(
925 psa_key_policy_t *policy,
926 const psa_key_policy_t *constraint )
927{
928 psa_algorithm_t intersection_alg =
929 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200930 psa_algorithm_t intersection_alg2 =
931 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100932 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
933 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200934 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
935 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 policy->usage &= constraint->usage;
937 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100939 return( PSA_SUCCESS );
940}
941
Darryl Green06fd18d2018-07-16 11:21:11 +0100942/** Retrieve a slot which must contain a key. The key must have allow all the
943 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
944 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100946 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100947 psa_key_usage_t usage,
948 psa_algorithm_t alg )
949{
950 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100952
953 *p_slot = NULL;
954
Gilles Peskinec5487a82018-12-03 18:08:14 +0100955 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100956 if( status != PSA_SUCCESS )
957 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100958
959 /* Enforce that usage policy for the key slot contains all the flags
960 * required by the usage parameter. There is one exception: public
961 * keys can always be exported, so we treat public key objects as
962 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100964 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200965 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100966 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100967
968 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200969 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100970 return( PSA_ERROR_NOT_PERMITTED );
971
972 *p_slot = slot;
973 return( PSA_SUCCESS );
974}
Darryl Green940d72c2018-07-13 13:18:51 +0100975
Gilles Peskine28f8f302019-07-24 13:30:31 +0200976/** Retrieve a slot which must contain a transparent key.
977 *
978 * A transparent key is a key for which the key material is directly
979 * available, as opposed to a key in a secure element.
980 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200981 * This is a temporary function to use instead of psa_get_key_from_slot()
982 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983 */
984#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
985static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
986 psa_key_slot_t **p_slot,
987 psa_key_usage_t usage,
988 psa_algorithm_t alg )
989{
990 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
991 if( status != PSA_SUCCESS )
992 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200993 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200994 {
995 *p_slot = NULL;
996 return( PSA_ERROR_NOT_SUPPORTED );
997 }
998 return( PSA_SUCCESS );
999}
1000#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1001/* With no secure element support, all keys are transparent. */
1002#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1003 psa_get_key_from_slot( handle, p_slot, usage, alg )
1004#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1005
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001007static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001008{
Gilles Peskine73167e12019-07-12 23:44:37 +02001009#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1010 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001011 {
1012 /* No key material to clean. */
1013 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001014 else
1015#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001016 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001017 {
1018 /* No key material to clean. */
1019 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001020 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001021 {
1022 mbedtls_free( slot->data.raw.data );
1023 }
1024 else
1025#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001026 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001027 {
1028 mbedtls_rsa_free( slot->data.rsa );
1029 mbedtls_free( slot->data.rsa );
1030 }
1031 else
1032#endif /* defined(MBEDTLS_RSA_C) */
1033#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001034 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001035 {
1036 mbedtls_ecp_keypair_free( slot->data.ecp );
1037 mbedtls_free( slot->data.ecp );
1038 }
1039 else
1040#endif /* defined(MBEDTLS_ECP_C) */
1041 {
1042 /* Shouldn't happen: the key type is not any type that we
1043 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001044 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001045 }
1046
1047 return( PSA_SUCCESS );
1048}
1049
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001050/** Completely wipe a slot in memory, including its policy.
1051 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001052psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001053{
1054 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001055 /* Multipart operations may still be using the key. This is safe
1056 * because all multipart operation objects are independent from
1057 * the key slot: if they need to access the key after the setup
1058 * phase, they have a copy of the key. Note that this means that
1059 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060 /* At this point, key material and other type-specific content has
1061 * been wiped. Clear remaining metadata. We can call memset and not
1062 * zeroize because the metadata is not particularly sensitive. */
1063 memset( slot, 0, sizeof( *slot ) );
1064 return( status );
1065}
1066
Gilles Peskinec5487a82018-12-03 18:08:14 +01001067psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001069 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001070 psa_status_t status; /* status of the last operation */
1071 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001072#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1073 psa_se_drv_table_entry_t *driver;
1074#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075
Gilles Peskine1841cf42019-10-08 15:48:25 +02001076 if( handle == 0 )
1077 return( PSA_SUCCESS );
1078
Gilles Peskinec5487a82018-12-03 18:08:14 +01001079 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080 if( status != PSA_SUCCESS )
1081 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001082
1083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001084 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001085 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001086 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001087 /* For a key in a secure element, we need to do three things:
1088 * remove the key file in internal storage, destroy the
1089 * key inside the secure element, and update the driver's
1090 * persistent data. Start a transaction that will encompass these
1091 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001092 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001093 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001094 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001095 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001096 status = psa_crypto_save_transaction( );
1097 if( status != PSA_SUCCESS )
1098 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001099 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001100 /* We should still try to destroy the key in the secure
1101 * element and the key metadata in storage. This is especially
1102 * important if the error is that the storage is full.
1103 * But how to do it exactly without risking an inconsistent
1104 * state after a reset?
1105 * https://github.com/ARMmbed/mbed-crypto/issues/215
1106 */
1107 overall_status = status;
1108 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001109 }
1110
Gilles Peskine354f7672019-07-12 23:46:38 +02001111 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001112 if( overall_status == PSA_SUCCESS )
1113 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001114 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001115#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1116
Darryl Greend49a4992018-06-18 17:27:26 +01001117#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001118 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001119 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001120 status = psa_destroy_persistent_key( slot->attr.id );
1121 if( overall_status == PSA_SUCCESS )
1122 overall_status = status;
1123
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001124 /* TODO: other slots may have a copy of the same key. We should
1125 * invalidate them.
1126 * https://github.com/ARMmbed/mbed-crypto/issues/214
1127 */
Darryl Greend49a4992018-06-18 17:27:26 +01001128 }
1129#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001130
Gilles Peskinefc762652019-07-22 19:30:34 +02001131#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1132 if( driver != NULL )
1133 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001134 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001135 if( overall_status == PSA_SUCCESS )
1136 overall_status = status;
1137 status = psa_crypto_stop_transaction( );
1138 if( overall_status == PSA_SUCCESS )
1139 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 }
1141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1142
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001143#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1144exit:
1145#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001146 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1148 if( overall_status == PSA_SUCCESS )
1149 overall_status = status;
1150 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001151}
1152
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001153void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001154{
Gilles Peskineb699f072019-04-26 16:06:02 +02001155 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001156 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001157}
1158
Gilles Peskineb699f072019-04-26 16:06:02 +02001159psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1160 psa_key_type_t type,
1161 const uint8_t *data,
1162 size_t data_length )
1163{
1164 uint8_t *copy = NULL;
1165
1166 if( data_length != 0 )
1167 {
1168 copy = mbedtls_calloc( 1, data_length );
1169 if( copy == NULL )
1170 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1171 memcpy( copy, data, data_length );
1172 }
1173 /* After this point, this function is guaranteed to succeed, so it
1174 * can start modifying `*attributes`. */
1175
1176 if( attributes->domain_parameters != NULL )
1177 {
1178 mbedtls_free( attributes->domain_parameters );
1179 attributes->domain_parameters = NULL;
1180 attributes->domain_parameters_size = 0;
1181 }
1182
1183 attributes->domain_parameters = copy;
1184 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001185 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 return( PSA_SUCCESS );
1187}
1188
1189psa_status_t psa_get_key_domain_parameters(
1190 const psa_key_attributes_t *attributes,
1191 uint8_t *data, size_t data_size, size_t *data_length )
1192{
1193 if( attributes->domain_parameters_size > data_size )
1194 return( PSA_ERROR_BUFFER_TOO_SMALL );
1195 *data_length = attributes->domain_parameters_size;
1196 if( attributes->domain_parameters_size != 0 )
1197 memcpy( data, attributes->domain_parameters,
1198 attributes->domain_parameters_size );
1199 return( PSA_SUCCESS );
1200}
1201
1202#if defined(MBEDTLS_RSA_C)
1203static psa_status_t psa_get_rsa_public_exponent(
1204 const mbedtls_rsa_context *rsa,
1205 psa_key_attributes_t *attributes )
1206{
1207 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 uint8_t *buffer = NULL;
1210 size_t buflen;
1211 mbedtls_mpi_init( &mpi );
1212
1213 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1214 if( ret != 0 )
1215 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001216 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1217 {
1218 /* It's the default value, which is reported as an empty string,
1219 * so there's nothing to do. */
1220 goto exit;
1221 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001222
1223 buflen = mbedtls_mpi_size( &mpi );
1224 buffer = mbedtls_calloc( 1, buflen );
1225 if( buffer == NULL )
1226 {
1227 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1228 goto exit;
1229 }
1230 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1231 if( ret != 0 )
1232 goto exit;
1233 attributes->domain_parameters = buffer;
1234 attributes->domain_parameters_size = buflen;
1235
1236exit:
1237 mbedtls_mpi_free( &mpi );
1238 if( ret != 0 )
1239 mbedtls_free( buffer );
1240 return( mbedtls_to_psa_error( ret ) );
1241}
1242#endif /* MBEDTLS_RSA_C */
1243
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001244/** Retrieve all the publicly-accessible attributes of a key.
1245 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001246psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1247 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001249 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001250 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001251
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001252 psa_reset_key_attributes( attributes );
1253
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001254 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001255 if( status != PSA_SUCCESS )
1256 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001257
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001258 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001259 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1260 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1261
1262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1263 if( psa_key_slot_is_external( slot ) )
1264 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001266
Gilles Peskine8e338702019-07-30 20:06:31 +02001267 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001268 {
1269#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001270 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001271 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001273 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001274 * is not yet implemented.
1275 * https://github.com/ARMmbed/mbed-crypto/issues/216
1276 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001278 break;
1279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1281 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001282#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001283 default:
1284 /* Nothing else to do. */
1285 break;
1286 }
1287
1288 if( status != PSA_SUCCESS )
1289 psa_reset_key_attributes( attributes );
1290 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291}
1292
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294psa_status_t psa_get_key_slot_number(
1295 const psa_key_attributes_t *attributes,
1296 psa_key_slot_number_t *slot_number )
1297{
1298 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1299 {
1300 *slot_number = attributes->slot_number;
1301 return( PSA_SUCCESS );
1302 }
1303 else
1304 return( PSA_ERROR_INVALID_ARGUMENT );
1305}
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Jaeden Ameroccdce902019-01-10 11:42:27 +00001308#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001309static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1310 unsigned char *buf, size_t size )
1311{
Janos Follath24eed8d2019-11-22 13:21:35 +00001312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001313 unsigned char *c;
1314 size_t len = 0;
1315
1316 c = buf + size;
1317
1318 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1319
1320 return( (int) len );
1321}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001322#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001323
Michael Thomas431507e2020-03-20 18:53:31 +00001324psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
Gilles Peskinef603c712019-01-19 13:40:11 +01001325 uint8_t *data,
1326 size_t data_size,
1327 size_t *data_length,
1328 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329{
Gilles Peskine5d309672019-07-12 23:47:28 +02001330#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1331 const psa_drv_se_t *drv;
1332 psa_drv_se_context_t *drv_context;
1333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1334
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001335 *data_length = 0;
1336
Gilles Peskine8e338702019-07-30 20:06:31 +02001337 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001338 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001339
Gilles Peskinef9168942019-09-12 19:20:29 +02001340 /* Reject a zero-length output buffer now, since this can never be a
1341 * valid key representation. This way we know that data must be a valid
1342 * pointer and we can do things like memset(data, ..., data_size). */
1343 if( data_size == 0 )
1344 return( PSA_ERROR_BUFFER_TOO_SMALL );
1345
Gilles Peskine5d309672019-07-12 23:47:28 +02001346#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001347 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001348 {
1349 psa_drv_se_export_key_t method;
1350 if( drv->key_management == NULL )
1351 return( PSA_ERROR_NOT_SUPPORTED );
1352 method = ( export_public_key ?
1353 drv->key_management->p_export_public :
1354 drv->key_management->p_export );
1355 if( method == NULL )
1356 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001357 return( method( drv_context,
1358 slot->data.se.slot_number,
1359 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001360 }
1361#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1362
Gilles Peskine8e338702019-07-30 20:06:31 +02001363 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364 {
1365 if( slot->data.raw.bytes > data_size )
1366 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001367 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1368 memset( data + slot->data.raw.bytes, 0,
1369 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370 *data_length = slot->data.raw.bytes;
1371 return( PSA_SUCCESS );
1372 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001373#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001374 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001375 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001376 psa_status_t status;
1377
Gilles Peskineb46bef22019-07-30 21:32:04 +02001378 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001379 if( bytes > data_size )
1380 return( PSA_ERROR_BUFFER_TOO_SMALL );
1381 status = mbedtls_to_psa_error(
1382 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1383 if( status != PSA_SUCCESS )
1384 return( status );
1385 memset( data + bytes, 0, data_size - bytes );
1386 *data_length = bytes;
1387 return( PSA_SUCCESS );
1388 }
1389#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001390 else
Moran Peker17e36e12018-05-02 12:55:20 +03001391 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001392#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001393 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1394 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001395 {
Moran Pekera998bc62018-04-16 18:16:20 +03001396 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001398 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001399 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001400#if defined(MBEDTLS_RSA_C)
1401 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001402 pk.pk_info = &mbedtls_rsa_info;
1403 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001404#else
1405 return( PSA_ERROR_NOT_SUPPORTED );
1406#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001407 }
1408 else
1409 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001410#if defined(MBEDTLS_ECP_C)
1411 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001412 pk.pk_info = &mbedtls_eckey_info;
1413 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001414#else
1415 return( PSA_ERROR_NOT_SUPPORTED );
1416#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001417 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001418 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001419 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001420 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001421 }
Moran Peker17e36e12018-05-02 12:55:20 +03001422 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001423 {
Moran Peker17e36e12018-05-02 12:55:20 +03001424 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001425 }
Moran Peker60364322018-04-29 11:34:58 +03001426 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001427 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001428 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001429 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001430 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001431 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1432 * Move the data to the beginning and erase remaining data
1433 * at the original location. */
1434 if( 2 * (size_t) ret <= data_size )
1435 {
1436 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001437 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001438 }
1439 else if( (size_t) ret < data_size )
1440 {
1441 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001442 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001443 }
Moran Pekera998bc62018-04-16 18:16:20 +03001444 *data_length = ret;
1445 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001446 }
1447 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001448#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001449 {
1450 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001451 it is valid for a special-purpose implementation to omit
1452 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001453 return( PSA_ERROR_NOT_SUPPORTED );
1454 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455 }
1456}
1457
Gilles Peskinec5487a82018-12-03 18:08:14 +01001458psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001459 uint8_t *data,
1460 size_t data_size,
1461 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001463 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001464 psa_status_t status;
1465
1466 /* Set the key to empty now, so that even when there are errors, we always
1467 * set data_length to a value between 0 and data_size. On error, setting
1468 * the key to empty is a good choice because an empty key representation is
1469 * unlikely to be accepted anywhere. */
1470 *data_length = 0;
1471
1472 /* Export requires the EXPORT flag. There is an exception for public keys,
1473 * which don't require any flag, but psa_get_key_from_slot takes
1474 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001475 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001476 if( status != PSA_SUCCESS )
1477 return( status );
1478 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001479 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481
Gilles Peskinec5487a82018-12-03 18:08:14 +01001482psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001483 uint8_t *data,
1484 size_t data_size,
1485 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001486{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001487 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001488 psa_status_t status;
1489
1490 /* Set the key to empty now, so that even when there are errors, we always
1491 * set data_length to a value between 0 and data_size. On error, setting
1492 * the key to empty is a good choice because an empty key representation is
1493 * unlikely to be accepted anywhere. */
1494 *data_length = 0;
1495
1496 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001497 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001498 if( status != PSA_SUCCESS )
1499 return( status );
1500 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001501 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001502}
1503
Gilles Peskine91e8c332019-08-02 19:19:39 +02001504#if defined(static_assert)
1505static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1506 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001507static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001508 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001509static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001510 "One or more key attribute flag is listed as both internal-only and external-only" );
1511#endif
1512
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513/** Validate that a key policy is internally well-formed.
1514 *
1515 * This function only rejects invalid policies. It does not validate the
1516 * consistency of the policy with respect to other attributes of the key
1517 * such as the key type.
1518 */
1519static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001520{
1521 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001522 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 PSA_KEY_USAGE_ENCRYPT |
1524 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001525 PSA_KEY_USAGE_SIGN_HASH |
1526 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001527 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1528 return( PSA_ERROR_INVALID_ARGUMENT );
1529
Gilles Peskine4747d192019-04-17 15:05:45 +02001530 return( PSA_SUCCESS );
1531}
1532
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001533/** Validate the internal consistency of key attributes.
1534 *
1535 * This function only rejects invalid attribute values. If does not
1536 * validate the consistency of the attributes with any key data that may
1537 * be involved in the creation of the key.
1538 *
1539 * Call this function early in the key creation process.
1540 *
1541 * \param[in] attributes Key attributes for the new key.
1542 * \param[out] p_drv On any return, the driver for the key, if any.
1543 * NULL for a transparent key.
1544 *
1545 */
1546static psa_status_t psa_validate_key_attributes(
1547 const psa_key_attributes_t *attributes,
1548 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001549{
1550 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001551
Michael Thomas153cd2f2020-01-22 16:54:38 +00001552 if( PSA_KEY_LIFETIME_IS_PERSISTENT (attributes->core.lifetime))
Darryl Green0c6575a2018-11-07 16:05:30 +00001553 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001554 status = psa_validate_persistent_key_parameters(
1555 attributes->core.lifetime, attributes->core.id,
1556 p_drv, 1 );
1557 if( status != PSA_SUCCESS )
1558 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001559 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001560
1561 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001562 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001563 return( status );
1564
1565 /* Refuse to create overly large keys.
1566 * Note that this doesn't trigger on import if the attributes don't
1567 * explicitly specify a size (so psa_get_key_bits returns 0), so
1568 * psa_import_key() needs its own checks. */
1569 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1570 return( PSA_ERROR_NOT_SUPPORTED );
1571
Gilles Peskine91e8c332019-08-02 19:19:39 +02001572 /* Reject invalid flags. These should not be reachable through the API. */
1573 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1574 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1575 return( PSA_ERROR_INVALID_ARGUMENT );
1576
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577 return( PSA_SUCCESS );
1578}
1579
Gilles Peskine4747d192019-04-17 15:05:45 +02001580/** Prepare a key slot to receive key material.
1581 *
1582 * This function allocates a key slot and sets its metadata.
1583 *
1584 * If this function fails, call psa_fail_key_creation().
1585 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001586 * This function is intended to be used as follows:
1587 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1588 * it with the specified attributes, and assign it a handle.
1589 * -# Populate the slot with the key material.
1590 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1591 * In case of failure at any step, stop the sequence and call
1592 * psa_fail_key_creation().
1593 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001594 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001595 * \param[in] attributes Key attributes for the new key.
1596 * \param[out] handle On success, a handle for the allocated slot.
1597 * \param[out] p_slot On success, a pointer to the prepared slot.
1598 * \param[out] p_drv On any return, the driver for the key, if any.
1599 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001600 *
1601 * \retval #PSA_SUCCESS
1602 * The key slot is ready to receive key material.
1603 * \return If this function fails, the key slot is an invalid state.
1604 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001605 */
1606static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001607 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001608 const psa_key_attributes_t *attributes,
1609 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001611 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001612{
1613 psa_status_t status;
1614 psa_key_slot_t *slot;
1615
Gilles Peskinedf179142019-07-15 22:02:14 +02001616 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 *p_drv = NULL;
1618
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001619 status = psa_validate_key_attributes( attributes, p_drv );
1620 if( status != PSA_SUCCESS )
1621 return( status );
1622
Gilles Peskineedbed562019-08-07 18:19:59 +02001623 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 if( status != PSA_SUCCESS )
1625 return( status );
1626 slot = *p_slot;
1627
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001628 /* We're storing the declared bit-size of the key. It's up to each
1629 * creation mechanism to verify that this information is correct.
1630 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001631 * an input (generate, device) but not for those where the bit-size
1632 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001633
1634 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001635
Gilles Peskine91e8c332019-08-02 19:19:39 +02001636 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001637 * external-only flags, query `attributes`. Thanks to the check
1638 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001639 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001640 * may have set. */
1641 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001642
Gilles Peskinecbaff462019-07-12 23:46:04 +02001643#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001644 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001645 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001646 * create the key file in internal storage, create the
1647 * key inside the secure element, and update the driver's
1648 * persistent data. Start a transaction that will encompass these
1649 * three actions. */
1650 /* The first thing to do is to find a slot number for the new key.
1651 * We save the slot number in persistent storage as part of the
1652 * transaction data. It will be needed to recover if the power
1653 * fails during the key creation process, to clean up on the secure
1654 * element side after restarting. Obtaining a slot number from the
1655 * secure element driver updates its persistent state, but we do not yet
1656 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001657 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001658 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001659 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001660 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001661 &slot->data.se.slot_number );
1662 if( status != PSA_SUCCESS )
1663 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001664 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001665 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001666 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001667 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001668 status = psa_crypto_save_transaction( );
1669 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001670 {
1671 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001672 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001673 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001674 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001675
1676 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1677 {
1678 /* Key registration only makes sense with a secure element. */
1679 return( PSA_ERROR_INVALID_ARGUMENT );
1680 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001681#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1682
Darryl Green0c6575a2018-11-07 16:05:30 +00001683 return( status );
1684}
Gilles Peskine4747d192019-04-17 15:05:45 +02001685
1686/** Finalize the creation of a key once its key material has been set.
1687 *
1688 * This entails writing the key to persistent storage.
1689 *
1690 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001691 * See the documentation of psa_start_key_creation() for the intended use
1692 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 * \param[in,out] slot Pointer to the slot with key material.
1695 * \param[in] driver The secure element driver for the key,
1696 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001697 *
1698 * \retval #PSA_SUCCESS
1699 * The key was successfully created. The handle is now valid.
1700 * \return If this function fails, the key slot is an invalid state.
1701 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 */
Michael Thomas863b5d62020-02-10 19:41:16 +00001703psa_status_t psa_finish_key_creation(
Gilles Peskine011e4282019-06-26 18:34:38 +02001704 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001705 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001706{
1707 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001708 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001710
1711#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Michael Thomasaf675252020-03-13 05:51:57 +00001712 if( PSA_KEY_LIFETIME_IS_PERSISTENT(slot->attr.lifetime ))
Gilles Peskine4747d192019-04-17 15:05:45 +02001713 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001714#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1715 if( driver != NULL )
1716 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001717 psa_se_key_data_storage_t data;
1718#if defined(static_assert)
1719 static_assert( sizeof( slot->data.se.slot_number ) ==
1720 sizeof( data.slot_number ),
1721 "Slot number size does not match psa_se_key_data_storage_t" );
1722 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1723 "Bit-size size does not match psa_se_key_data_storage_t" );
1724#endif
1725 memcpy( &data.slot_number, &slot->data.se.slot_number,
1726 sizeof( slot->data.se.slot_number ) );
1727 memcpy( &data.bits, &slot->attr.bits,
1728 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001729 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001730 (uint8_t*) &data,
1731 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001732 }
1733 else
1734#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas431507e2020-03-20 18:53:31 +00001735#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
1736 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
1737 {
1738 status = psa_finish_key_creation_vendor( slot );
1739 }
1740 else
1741#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskine1df83d42019-07-23 16:13:14 +02001742 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001743 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001744 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001745 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001746 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1747 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001748 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001749 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1750 status = psa_internal_export_key( slot,
1751 buffer, buffer_size, &length,
1752 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001753 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001754 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001755 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001756
Gilles Peskinef9168942019-09-12 19:20:29 +02001757 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001758 mbedtls_free( buffer );
1759 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001761#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1762
Gilles Peskinecbaff462019-07-12 23:46:04 +02001763#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001764 /* Finish the transaction for a key creation. This does not
1765 * happen when registering an existing key. Detect this case
1766 * by checking whether a transaction is in progress (actual
1767 * creation of a key in a secure element requires a transaction,
1768 * but registration doesn't use one). */
1769 if( driver != NULL &&
1770 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001771 {
1772 status = psa_save_se_persistent_data( driver );
1773 if( status != PSA_SUCCESS )
1774 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001775 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001776 return( status );
1777 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001778 status = psa_crypto_stop_transaction( );
1779 if( status != PSA_SUCCESS )
1780 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001781 }
1782#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1783
Gilles Peskine4747d192019-04-17 15:05:45 +02001784 return( status );
1785}
1786
1787/** Abort the creation of a key.
1788 *
1789 * You may call this function after calling psa_start_key_creation(),
1790 * or after psa_finish_key_creation() fails. In other circumstances, this
1791 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001792 * See the documentation of psa_start_key_creation() for the intended use
1793 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001794 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001795 * \param[in,out] slot Pointer to the slot with key material.
1796 * \param[in] driver The secure element driver for the key,
1797 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001798 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001799static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001800 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001801{
Gilles Peskine011e4282019-06-26 18:34:38 +02001802 (void) driver;
1803
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 if( slot == NULL )
1805 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001806
1807#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001808 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001809 * element, and the failure happened later (when saving metadata
1810 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001811 * element.
1812 * https://github.com/ARMmbed/mbed-crypto/issues/217
1813 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001814
Gilles Peskined7729582019-08-05 15:55:54 +02001815 /* Abort the ongoing transaction if any (there may not be one if
1816 * the creation process failed before starting one, or if the
1817 * key creation is a registration of a key in a secure element).
1818 * Earlier functions must already have done what it takes to undo any
1819 * partial creation. All that's left is to update the transaction data
1820 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001821 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001822#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1823
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 psa_wipe_key_slot( slot );
1825}
1826
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001827/** Validate optional attributes during key creation.
1828 *
1829 * Some key attributes are optional during key creation. If they are
1830 * specified in the attributes structure, check that they are consistent
1831 * with the data in the slot.
1832 *
1833 * This function should be called near the end of key creation, after
1834 * the slot in memory is fully populated but before saving persistent data.
1835 */
1836static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001837 const psa_key_slot_t *slot,
1838 const psa_key_attributes_t *attributes )
1839{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001840 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001841 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001842 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001843 return( PSA_ERROR_INVALID_ARGUMENT );
1844 }
1845
1846 if( attributes->domain_parameters_size != 0 )
1847 {
1848#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001849 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001850 {
1851 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853 mbedtls_mpi_init( &actual );
1854 mbedtls_mpi_init( &required );
1855 ret = mbedtls_rsa_export( slot->data.rsa,
1856 NULL, NULL, NULL, NULL, &actual );
1857 if( ret != 0 )
1858 goto rsa_exit;
1859 ret = mbedtls_mpi_read_binary( &required,
1860 attributes->domain_parameters,
1861 attributes->domain_parameters_size );
1862 if( ret != 0 )
1863 goto rsa_exit;
1864 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1865 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1866 rsa_exit:
1867 mbedtls_mpi_free( &actual );
1868 mbedtls_mpi_free( &required );
1869 if( ret != 0)
1870 return( mbedtls_to_psa_error( ret ) );
1871 }
1872 else
1873#endif
1874 {
1875 return( PSA_ERROR_INVALID_ARGUMENT );
1876 }
1877 }
1878
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001879 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001881 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001882 return( PSA_ERROR_INVALID_ARGUMENT );
1883 }
1884
1885 return( PSA_SUCCESS );
1886}
1887
Gilles Peskine4747d192019-04-17 15:05:45 +02001888psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001890 size_t data_length,
1891 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001892{
1893 psa_status_t status;
1894 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001895 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896
Gilles Peskine0f84d622019-09-12 19:03:13 +02001897 /* Reject zero-length symmetric keys (including raw data key objects).
1898 * This also rejects any key which might be encoded as an empty string,
1899 * which is never valid. */
1900 if( data_length == 0 )
1901 return( PSA_ERROR_INVALID_ARGUMENT );
1902
Gilles Peskinedf179142019-07-15 22:02:14 +02001903 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1904 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001905 if( status != PSA_SUCCESS )
1906 goto exit;
1907
Gilles Peskine5d309672019-07-12 23:47:28 +02001908#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1909 if( driver != NULL )
1910 {
1911 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001912 /* The driver should set the number of key bits, however in
1913 * case it doesn't, we initialize bits to an invalid value. */
1914 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001915 if( drv->key_management == NULL ||
1916 drv->key_management->p_import == NULL )
1917 {
1918 status = PSA_ERROR_NOT_SUPPORTED;
1919 goto exit;
1920 }
1921 status = drv->key_management->p_import(
1922 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001923 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001924 &bits );
1925 if( status != PSA_SUCCESS )
1926 goto exit;
1927 if( bits > PSA_MAX_KEY_BITS )
1928 {
1929 status = PSA_ERROR_NOT_SUPPORTED;
1930 goto exit;
1931 }
1932 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001933 }
1934 else
1935#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas863b5d62020-02-10 19:41:16 +00001936#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
1937 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
1938 {
Michael Thomas413973b2020-03-14 03:03:42 +00001939 status = psa_import_key_into_slot_vendor( slot, data, data_length, true);
Michael Thomas863b5d62020-02-10 19:41:16 +00001940 goto exit;
1941 }
1942 else
1943#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskine5d309672019-07-12 23:47:28 +02001944 {
1945 status = psa_import_key_into_slot( slot, data, data_length );
1946 if( status != PSA_SUCCESS )
1947 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001948 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001949 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001950 if( status != PSA_SUCCESS )
1951 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952
Gilles Peskine011e4282019-06-26 18:34:38 +02001953 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001955 if( status != PSA_SUCCESS )
1956 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001957 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001958 *handle = 0;
1959 }
1960 return( status );
1961}
1962
Gilles Peskined7729582019-08-05 15:55:54 +02001963#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1964psa_status_t mbedtls_psa_register_se_key(
1965 const psa_key_attributes_t *attributes )
1966{
1967 psa_status_t status;
1968 psa_key_slot_t *slot = NULL;
1969 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001970 psa_key_handle_t handle = 0;
1971
1972 /* Leaving attributes unspecified is not currently supported.
1973 * It could make sense to query the key type and size from the
1974 * secure element, but not all secure elements support this
1975 * and the driver HAL doesn't currently support it. */
1976 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1977 return( PSA_ERROR_NOT_SUPPORTED );
1978 if( psa_get_key_bits( attributes ) == 0 )
1979 return( PSA_ERROR_NOT_SUPPORTED );
1980
1981 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1982 &handle, &slot, &driver );
1983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
Gilles Peskined7729582019-08-05 15:55:54 +02001986 status = psa_finish_key_creation( slot, driver );
1987
1988exit:
1989 if( status != PSA_SUCCESS )
1990 {
1991 psa_fail_key_creation( slot, driver );
1992 }
1993 /* Registration doesn't keep the key in RAM. */
1994 psa_close_key( handle );
1995 return( status );
1996}
1997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1998
Gilles Peskinef603c712019-01-19 13:40:11 +01001999static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002001{
2002 psa_status_t status;
2003 uint8_t *buffer = NULL;
2004 size_t buffer_size = 0;
2005 size_t length;
2006
Gilles Peskine8e338702019-07-30 20:06:31 +02002007 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002008 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002009 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002010 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002011 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002012 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2013 if( status != PSA_SUCCESS )
2014 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002015 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002017
2018exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002019 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002020 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002021 return( status );
2022}
2023
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002024psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2025 const psa_key_attributes_t *specified_attributes,
2026 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002027{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002028 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002029 psa_key_slot_t *source_slot = NULL;
2030 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002031 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002032 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002033
Gilles Peskine28f8f302019-07-24 13:30:31 +02002034 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002035 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002036 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002037 goto exit;
2038
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002039 status = psa_validate_optional_attributes( source_slot,
2040 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 if( status != PSA_SUCCESS )
2042 goto exit;
2043
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002044 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002045 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002046 if( status != PSA_SUCCESS )
2047 goto exit;
2048
Gilles Peskinedf179142019-07-15 22:02:14 +02002049 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2050 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002051 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002052 if( status != PSA_SUCCESS )
2053 goto exit;
2054
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002055#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2056 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002057 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002058 /* Copying to a secure element is not implemented yet. */
2059 status = PSA_ERROR_NOT_SUPPORTED;
2060 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002061 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002062#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002063
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002064 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002065 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002067
Gilles Peskine011e4282019-06-26 18:34:38 +02002068 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002069exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002070 if( status != PSA_SUCCESS )
2071 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002072 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002073 *target_handle = 0;
2074 }
2075 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002076}
2077
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002078
2079
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002080/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002081/* Message digests */
2082/****************************************************************/
2083
Gilles Peskineb16841e2019-10-10 20:36:12 +02002084#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002085static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002086{
2087 switch( alg )
2088 {
2089#if defined(MBEDTLS_MD2_C)
2090 case PSA_ALG_MD2:
2091 return( &mbedtls_md2_info );
2092#endif
2093#if defined(MBEDTLS_MD4_C)
2094 case PSA_ALG_MD4:
2095 return( &mbedtls_md4_info );
2096#endif
2097#if defined(MBEDTLS_MD5_C)
2098 case PSA_ALG_MD5:
2099 return( &mbedtls_md5_info );
2100#endif
2101#if defined(MBEDTLS_RIPEMD160_C)
2102 case PSA_ALG_RIPEMD160:
2103 return( &mbedtls_ripemd160_info );
2104#endif
2105#if defined(MBEDTLS_SHA1_C)
2106 case PSA_ALG_SHA_1:
2107 return( &mbedtls_sha1_info );
2108#endif
2109#if defined(MBEDTLS_SHA256_C)
2110 case PSA_ALG_SHA_224:
2111 return( &mbedtls_sha224_info );
2112 case PSA_ALG_SHA_256:
2113 return( &mbedtls_sha256_info );
2114#endif
2115#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002116#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002117 case PSA_ALG_SHA_384:
2118 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002119#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002120 case PSA_ALG_SHA_512:
2121 return( &mbedtls_sha512_info );
2122#endif
2123 default:
2124 return( NULL );
2125 }
2126}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002127#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002128
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2130{
2131 switch( operation->alg )
2132 {
Gilles Peskine81736312018-06-26 15:04:31 +02002133 case 0:
2134 /* The object has (apparently) been initialized but it is not
2135 * in use. It's ok to call abort on such an object, and there's
2136 * nothing to do. */
2137 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002138#if defined(MBEDTLS_MD2_C)
2139 case PSA_ALG_MD2:
2140 mbedtls_md2_free( &operation->ctx.md2 );
2141 break;
2142#endif
2143#if defined(MBEDTLS_MD4_C)
2144 case PSA_ALG_MD4:
2145 mbedtls_md4_free( &operation->ctx.md4 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_MD5_C)
2149 case PSA_ALG_MD5:
2150 mbedtls_md5_free( &operation->ctx.md5 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_RIPEMD160_C)
2154 case PSA_ALG_RIPEMD160:
2155 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2156 break;
2157#endif
2158#if defined(MBEDTLS_SHA1_C)
2159 case PSA_ALG_SHA_1:
2160 mbedtls_sha1_free( &operation->ctx.sha1 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA256_C)
2164 case PSA_ALG_SHA_224:
2165 case PSA_ALG_SHA_256:
2166 mbedtls_sha256_free( &operation->ctx.sha256 );
2167 break;
2168#endif
2169#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002170#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002172#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002173 case PSA_ALG_SHA_512:
2174 mbedtls_sha512_free( &operation->ctx.sha512 );
2175 break;
2176#endif
2177 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002178 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179 }
2180 operation->alg = 0;
2181 return( PSA_SUCCESS );
2182}
2183
Gilles Peskineda8191d2018-07-08 19:46:38 +02002184psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185 psa_algorithm_t alg )
2186{
Janos Follath24eed8d2019-11-22 13:21:35 +00002187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002188
2189 /* A context must be freshly initialized before it can be set up. */
2190 if( operation->alg != 0 )
2191 {
2192 return( PSA_ERROR_BAD_STATE );
2193 }
2194
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 switch( alg )
2196 {
2197#if defined(MBEDTLS_MD2_C)
2198 case PSA_ALG_MD2:
2199 mbedtls_md2_init( &operation->ctx.md2 );
2200 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2201 break;
2202#endif
2203#if defined(MBEDTLS_MD4_C)
2204 case PSA_ALG_MD4:
2205 mbedtls_md4_init( &operation->ctx.md4 );
2206 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2207 break;
2208#endif
2209#if defined(MBEDTLS_MD5_C)
2210 case PSA_ALG_MD5:
2211 mbedtls_md5_init( &operation->ctx.md5 );
2212 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2213 break;
2214#endif
2215#if defined(MBEDTLS_RIPEMD160_C)
2216 case PSA_ALG_RIPEMD160:
2217 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2218 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2219 break;
2220#endif
2221#if defined(MBEDTLS_SHA1_C)
2222 case PSA_ALG_SHA_1:
2223 mbedtls_sha1_init( &operation->ctx.sha1 );
2224 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2225 break;
2226#endif
2227#if defined(MBEDTLS_SHA256_C)
2228 case PSA_ALG_SHA_224:
2229 mbedtls_sha256_init( &operation->ctx.sha256 );
2230 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2231 break;
2232 case PSA_ALG_SHA_256:
2233 mbedtls_sha256_init( &operation->ctx.sha256 );
2234 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2235 break;
2236#endif
2237#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002238#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 case PSA_ALG_SHA_384:
2240 mbedtls_sha512_init( &operation->ctx.sha512 );
2241 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2242 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002243#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 case PSA_ALG_SHA_512:
2245 mbedtls_sha512_init( &operation->ctx.sha512 );
2246 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2247 break;
2248#endif
2249 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002250 return( PSA_ALG_IS_HASH( alg ) ?
2251 PSA_ERROR_NOT_SUPPORTED :
2252 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253 }
2254 if( ret == 0 )
2255 operation->alg = alg;
2256 else
2257 psa_hash_abort( operation );
2258 return( mbedtls_to_psa_error( ret ) );
2259}
2260
2261psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2262 const uint8_t *input,
2263 size_t input_length )
2264{
Janos Follath24eed8d2019-11-22 13:21:35 +00002265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002266
2267 /* Don't require hash implementations to behave correctly on a
2268 * zero-length input, which may have an invalid pointer. */
2269 if( input_length == 0 )
2270 return( PSA_SUCCESS );
2271
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 switch( operation->alg )
2273 {
2274#if defined(MBEDTLS_MD2_C)
2275 case PSA_ALG_MD2:
2276 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2277 input, input_length );
2278 break;
2279#endif
2280#if defined(MBEDTLS_MD4_C)
2281 case PSA_ALG_MD4:
2282 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2283 input, input_length );
2284 break;
2285#endif
2286#if defined(MBEDTLS_MD5_C)
2287 case PSA_ALG_MD5:
2288 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2289 input, input_length );
2290 break;
2291#endif
2292#if defined(MBEDTLS_RIPEMD160_C)
2293 case PSA_ALG_RIPEMD160:
2294 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2295 input, input_length );
2296 break;
2297#endif
2298#if defined(MBEDTLS_SHA1_C)
2299 case PSA_ALG_SHA_1:
2300 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2301 input, input_length );
2302 break;
2303#endif
2304#if defined(MBEDTLS_SHA256_C)
2305 case PSA_ALG_SHA_224:
2306 case PSA_ALG_SHA_256:
2307 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2308 input, input_length );
2309 break;
2310#endif
2311#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002312#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002314#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315 case PSA_ALG_SHA_512:
2316 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2317 input, input_length );
2318 break;
2319#endif
2320 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002321 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002323
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 if( ret != 0 )
2325 psa_hash_abort( operation );
2326 return( mbedtls_to_psa_error( ret ) );
2327}
2328
2329psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2330 uint8_t *hash,
2331 size_t hash_size,
2332 size_t *hash_length )
2333{
itayzafrir40835d42018-08-02 13:14:17 +03002334 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002336 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337
2338 /* Fill the output buffer with something that isn't a valid hash
2339 * (barring an attack on the hash and deliberately-crafted input),
2340 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002341 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002342 /* If hash_size is 0 then hash may be NULL and then the
2343 * call to memset would have undefined behavior. */
2344 if( hash_size != 0 )
2345 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346
2347 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002348 {
2349 status = PSA_ERROR_BUFFER_TOO_SMALL;
2350 goto exit;
2351 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352
2353 switch( operation->alg )
2354 {
2355#if defined(MBEDTLS_MD2_C)
2356 case PSA_ALG_MD2:
2357 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2358 break;
2359#endif
2360#if defined(MBEDTLS_MD4_C)
2361 case PSA_ALG_MD4:
2362 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2363 break;
2364#endif
2365#if defined(MBEDTLS_MD5_C)
2366 case PSA_ALG_MD5:
2367 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2368 break;
2369#endif
2370#if defined(MBEDTLS_RIPEMD160_C)
2371 case PSA_ALG_RIPEMD160:
2372 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2373 break;
2374#endif
2375#if defined(MBEDTLS_SHA1_C)
2376 case PSA_ALG_SHA_1:
2377 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2378 break;
2379#endif
2380#if defined(MBEDTLS_SHA256_C)
2381 case PSA_ALG_SHA_224:
2382 case PSA_ALG_SHA_256:
2383 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2384 break;
2385#endif
2386#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002387#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002389#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390 case PSA_ALG_SHA_512:
2391 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2392 break;
2393#endif
2394 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002395 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396 }
itayzafrir40835d42018-08-02 13:14:17 +03002397 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398
itayzafrir40835d42018-08-02 13:14:17 +03002399exit:
2400 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002402 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403 return( psa_hash_abort( operation ) );
2404 }
2405 else
2406 {
2407 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002408 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409 }
2410}
2411
Gilles Peskine2d277862018-06-18 15:41:12 +02002412psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2413 const uint8_t *hash,
2414 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415{
2416 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2417 size_t actual_hash_length;
2418 psa_status_t status = psa_hash_finish( operation,
2419 actual_hash, sizeof( actual_hash ),
2420 &actual_hash_length );
2421 if( status != PSA_SUCCESS )
2422 return( status );
2423 if( actual_hash_length != hash_length )
2424 return( PSA_ERROR_INVALID_SIGNATURE );
2425 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2426 return( PSA_ERROR_INVALID_SIGNATURE );
2427 return( PSA_SUCCESS );
2428}
2429
Gilles Peskine0a749c82019-11-28 19:33:58 +01002430psa_status_t psa_hash_compute( psa_algorithm_t alg,
2431 const uint8_t *input, size_t input_length,
2432 uint8_t *hash, size_t hash_size,
2433 size_t *hash_length )
2434{
2435 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2437
2438 *hash_length = hash_size;
2439 status = psa_hash_setup( &operation, alg );
2440 if( status != PSA_SUCCESS )
2441 goto exit;
2442 status = psa_hash_update( &operation, input, input_length );
2443 if( status != PSA_SUCCESS )
2444 goto exit;
2445 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2446 if( status != PSA_SUCCESS )
2447 goto exit;
2448
2449exit:
2450 if( status == PSA_SUCCESS )
2451 status = psa_hash_abort( &operation );
2452 else
2453 psa_hash_abort( &operation );
2454 return( status );
2455}
2456
2457psa_status_t psa_hash_compare( psa_algorithm_t alg,
2458 const uint8_t *input, size_t input_length,
2459 const uint8_t *hash, size_t hash_length )
2460{
2461 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2463
2464 status = psa_hash_setup( &operation, alg );
2465 if( status != PSA_SUCCESS )
2466 goto exit;
2467 status = psa_hash_update( &operation, input, input_length );
2468 if( status != PSA_SUCCESS )
2469 goto exit;
2470 status = psa_hash_verify( &operation, hash, hash_length );
2471 if( status != PSA_SUCCESS )
2472 goto exit;
2473
2474exit:
2475 if( status == PSA_SUCCESS )
2476 status = psa_hash_abort( &operation );
2477 else
2478 psa_hash_abort( &operation );
2479 return( status );
2480}
2481
Gilles Peskineeb35d782019-01-22 17:56:16 +01002482psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2483 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002484{
2485 if( target_operation->alg != 0 )
2486 return( PSA_ERROR_BAD_STATE );
2487
2488 switch( source_operation->alg )
2489 {
2490 case 0:
2491 return( PSA_ERROR_BAD_STATE );
2492#if defined(MBEDTLS_MD2_C)
2493 case PSA_ALG_MD2:
2494 mbedtls_md2_clone( &target_operation->ctx.md2,
2495 &source_operation->ctx.md2 );
2496 break;
2497#endif
2498#if defined(MBEDTLS_MD4_C)
2499 case PSA_ALG_MD4:
2500 mbedtls_md4_clone( &target_operation->ctx.md4,
2501 &source_operation->ctx.md4 );
2502 break;
2503#endif
2504#if defined(MBEDTLS_MD5_C)
2505 case PSA_ALG_MD5:
2506 mbedtls_md5_clone( &target_operation->ctx.md5,
2507 &source_operation->ctx.md5 );
2508 break;
2509#endif
2510#if defined(MBEDTLS_RIPEMD160_C)
2511 case PSA_ALG_RIPEMD160:
2512 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2513 &source_operation->ctx.ripemd160 );
2514 break;
2515#endif
2516#if defined(MBEDTLS_SHA1_C)
2517 case PSA_ALG_SHA_1:
2518 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2519 &source_operation->ctx.sha1 );
2520 break;
2521#endif
2522#if defined(MBEDTLS_SHA256_C)
2523 case PSA_ALG_SHA_224:
2524 case PSA_ALG_SHA_256:
2525 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2526 &source_operation->ctx.sha256 );
2527 break;
2528#endif
2529#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002530#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002531 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002532#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002533 case PSA_ALG_SHA_512:
2534 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2535 &source_operation->ctx.sha512 );
2536 break;
2537#endif
2538 default:
2539 return( PSA_ERROR_NOT_SUPPORTED );
2540 }
2541
2542 target_operation->alg = source_operation->alg;
2543 return( PSA_SUCCESS );
2544}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002545
2546
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002547/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548/* MAC */
2549/****************************************************************/
2550
Michael Thomas863b5d62020-02-10 19:41:16 +00002551const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 psa_algorithm_t alg,
2553 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002554 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002555 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002558 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002560 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002561 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002562
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2564 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002565 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002567 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002568 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 mode = MBEDTLS_MODE_STREAM;
2570 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 case PSA_ALG_CTR:
2572 mode = MBEDTLS_MODE_CTR;
2573 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002574 case PSA_ALG_CFB:
2575 mode = MBEDTLS_MODE_CFB;
2576 break;
2577 case PSA_ALG_OFB:
2578 mode = MBEDTLS_MODE_OFB;
2579 break;
2580 case PSA_ALG_CBC_NO_PADDING:
2581 mode = MBEDTLS_MODE_CBC;
2582 break;
2583 case PSA_ALG_CBC_PKCS7:
2584 mode = MBEDTLS_MODE_CBC;
2585 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002586 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587 mode = MBEDTLS_MODE_CCM;
2588 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002589 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590 mode = MBEDTLS_MODE_GCM;
2591 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002592 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2593 mode = MBEDTLS_MODE_CHACHAPOLY;
2594 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595 default:
2596 return( NULL );
2597 }
2598 }
2599 else if( alg == PSA_ALG_CMAC )
2600 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 else
2602 return( NULL );
2603
2604 switch( key_type )
2605 {
2606 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002607 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608 break;
2609 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002610 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2611 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002613 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002615 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002616 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2617 * but two-key Triple-DES is functionally three-key Triple-DES
2618 * with K1=K3, so that's how we present it to mbedtls. */
2619 if( key_bits == 128 )
2620 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621 break;
2622 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002623 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 break;
2625 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002626 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002628 case PSA_KEY_TYPE_CHACHA20:
2629 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2630 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631 default:
2632 return( NULL );
2633 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002634 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002635 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636
Jaeden Amero23bbb752018-06-26 14:16:54 +01002637 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2638 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639}
2640
Gilles Peskinea05219c2018-11-16 16:02:56 +01002641#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002642static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002643{
Gilles Peskine2d277862018-06-18 15:41:12 +02002644 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002645 {
2646 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002647 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002648 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002649 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002650 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002651 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002652 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002653 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002654 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002655 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002656 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002657 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002658 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002659 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002660 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002661 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002662 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002663 return( 128 );
2664 default:
2665 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002666 }
2667}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002668#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002669
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002670/* Initialize the MAC operation structure. Once this function has been
2671 * called, psa_mac_abort can run and will do the right thing. */
2672static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2673 psa_algorithm_t alg )
2674{
2675 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2676
2677 operation->alg = alg;
2678 operation->key_set = 0;
2679 operation->iv_set = 0;
2680 operation->iv_required = 0;
2681 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002682 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002683
2684#if defined(MBEDTLS_CMAC_C)
2685 if( alg == PSA_ALG_CMAC )
2686 {
2687 operation->iv_required = 0;
2688 mbedtls_cipher_init( &operation->ctx.cmac );
2689 status = PSA_SUCCESS;
2690 }
2691 else
2692#endif /* MBEDTLS_CMAC_C */
2693#if defined(MBEDTLS_MD_C)
2694 if( PSA_ALG_IS_HMAC( operation->alg ) )
2695 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002696 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2697 operation->ctx.hmac.hash_ctx.alg = 0;
2698 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002699 }
2700 else
2701#endif /* MBEDTLS_MD_C */
2702 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002703 if( ! PSA_ALG_IS_MAC( alg ) )
2704 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002705 }
2706
2707 if( status != PSA_SUCCESS )
2708 memset( operation, 0, sizeof( *operation ) );
2709 return( status );
2710}
2711
Gilles Peskine01126fa2018-07-12 17:04:55 +02002712#if defined(MBEDTLS_MD_C)
2713static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2714{
Gilles Peskine3f108122018-12-07 18:14:53 +01002715 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002716 return( psa_hash_abort( &hmac->hash_ctx ) );
2717}
2718#endif /* MBEDTLS_MD_C */
2719
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2721{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002722 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002724 /* The object has (apparently) been initialized but it is not
2725 * in use. It's ok to call abort on such an object, and there's
2726 * nothing to do. */
2727 return( PSA_SUCCESS );
2728 }
2729 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002731 if( operation->alg == PSA_ALG_CMAC )
2732 {
2733 mbedtls_cipher_free( &operation->ctx.cmac );
2734 }
2735 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002738 if( PSA_ALG_IS_HMAC( operation->alg ) )
2739 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002740 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002741 }
2742 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002744 {
2745 /* Sanity check (shouldn't happen: operation->alg should
2746 * always have been initialized to a valid value). */
2747 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 }
Moran Peker41deec42018-04-04 15:43:05 +03002749
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750 operation->alg = 0;
2751 operation->key_set = 0;
2752 operation->iv_set = 0;
2753 operation->iv_required = 0;
2754 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002755 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002756
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002758
2759bad_state:
2760 /* If abort is called on an uninitialized object, we can't trust
2761 * anything. Wipe the object in case it contains confidential data.
2762 * This may result in a memory leak if a pointer gets overwritten,
2763 * but it's too late to do anything about this. */
2764 memset( operation, 0, sizeof( *operation ) );
2765 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766}
2767
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002768#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002769static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002771 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772 const mbedtls_cipher_info_t *cipher_info )
2773{
Janos Follath24eed8d2019-11-22 13:21:35 +00002774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002775
2776 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777
2778 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2779 if( ret != 0 )
2780 return( ret );
2781
2782 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2783 slot->data.raw.data,
2784 key_bits );
2785 return( ret );
2786}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002787#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788
Gilles Peskine248051a2018-06-20 16:09:38 +02002789#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002790static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2791 const uint8_t *key,
2792 size_t key_length,
2793 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002795 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002796 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002797 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002798 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002799 psa_status_t status;
2800
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002801 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2802 * overflow below. This should never trigger if the hash algorithm
2803 * is implemented correctly. */
2804 /* The size checks against the ipad and opad buffers cannot be written
2805 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2806 * because that triggers -Wlogical-op on GCC 7.3. */
2807 if( block_size > sizeof( ipad ) )
2808 return( PSA_ERROR_NOT_SUPPORTED );
2809 if( block_size > sizeof( hmac->opad ) )
2810 return( PSA_ERROR_NOT_SUPPORTED );
2811 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002812 return( PSA_ERROR_NOT_SUPPORTED );
2813
Gilles Peskined223b522018-06-11 18:12:58 +02002814 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002816 status = psa_hash_compute( hash_alg, key, key_length,
2817 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002818 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002819 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002820 }
Gilles Peskine96889972018-07-12 17:07:03 +02002821 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2822 * but it is permitted. It is common when HMAC is used in HKDF, for
2823 * example. Don't call `memcpy` in the 0-length because `key` could be
2824 * an invalid pointer which would make the behavior undefined. */
2825 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002826 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002827
Gilles Peskined223b522018-06-11 18:12:58 +02002828 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2829 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002830 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002831 ipad[i] ^= 0x36;
2832 memset( ipad + key_length, 0x36, block_size - key_length );
2833
2834 /* Copy the key material from ipad to opad, flipping the requisite bits,
2835 * and filling the rest of opad with the requisite constant. */
2836 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002837 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2838 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002839
Gilles Peskine01126fa2018-07-12 17:04:55 +02002840 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002841 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002842 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002843
Gilles Peskine01126fa2018-07-12 17:04:55 +02002844 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002845
2846cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002847 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002848
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002849 return( status );
2850}
Gilles Peskine248051a2018-06-20 16:09:38 +02002851#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002852
Gilles Peskine89167cb2018-07-08 20:12:23 +02002853static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002854 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002855 psa_algorithm_t alg,
2856 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002859 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002861 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002862 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002863 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002864 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002866 /* A context must be freshly initialized before it can be set up. */
2867 if( operation->alg != 0 )
2868 {
2869 return( PSA_ERROR_BAD_STATE );
2870 }
2871
Gilles Peskined911eb72018-08-14 15:18:45 +02002872 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873 if( status != PSA_SUCCESS )
2874 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002875 if( is_sign )
2876 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877
Gilles Peskine28f8f302019-07-24 13:30:31 +02002878 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002879 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002880 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002881 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002882
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002884 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002885 {
2886 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002887 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002888 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002890 if( cipher_info == NULL )
2891 {
2892 status = PSA_ERROR_NOT_SUPPORTED;
2893 goto exit;
2894 }
2895 operation->mac_size = cipher_info->block_size;
2896 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2897 status = mbedtls_to_psa_error( ret );
2898 }
2899 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002902 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002903 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002904 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002905 if( hash_alg == 0 )
2906 {
2907 status = PSA_ERROR_NOT_SUPPORTED;
2908 goto exit;
2909 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002910
2911 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2912 /* Sanity check. This shouldn't fail on a valid configuration. */
2913 if( operation->mac_size == 0 ||
2914 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2915 {
2916 status = PSA_ERROR_NOT_SUPPORTED;
2917 goto exit;
2918 }
2919
Gilles Peskine8e338702019-07-30 20:06:31 +02002920 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921 {
2922 status = PSA_ERROR_INVALID_ARGUMENT;
2923 goto exit;
2924 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002925
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2927 slot->data.raw.data,
2928 slot->data.raw.bytes,
2929 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 }
2931 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002933 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002934 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002935 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002937
Gilles Peskined911eb72018-08-14 15:18:45 +02002938 if( truncated == 0 )
2939 {
2940 /* The "normal" case: untruncated algorithm. Nothing to do. */
2941 }
2942 else if( truncated < 4 )
2943 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002944 /* A very short MAC is too short for security since it can be
2945 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2946 * so we make this our minimum, even though 32 bits is still
2947 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002948 status = PSA_ERROR_NOT_SUPPORTED;
2949 }
2950 else if( truncated > operation->mac_size )
2951 {
2952 /* It's impossible to "truncate" to a larger length. */
2953 status = PSA_ERROR_INVALID_ARGUMENT;
2954 }
2955 else
2956 operation->mac_size = truncated;
2957
Gilles Peskinefbfac682018-07-08 20:51:54 +02002958exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002959 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002961 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002963 else
2964 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002965 operation->key_set = 1;
2966 }
2967 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968}
2969
Gilles Peskine89167cb2018-07-08 20:12:23 +02002970psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002971 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002972 psa_algorithm_t alg )
2973{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002974 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002975}
2976
2977psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002978 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002979 psa_algorithm_t alg )
2980{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002981 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002982}
2983
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2985 const uint8_t *input,
2986 size_t input_length )
2987{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002988 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002990 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002992 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993 operation->has_input = 1;
2994
Gilles Peskine8c9def32018-02-08 10:02:12 +01002995#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002996 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002997 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002998 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2999 input, input_length );
3000 status = mbedtls_to_psa_error( ret );
3001 }
3002 else
3003#endif /* MBEDTLS_CMAC_C */
3004#if defined(MBEDTLS_MD_C)
3005 if( PSA_ALG_IS_HMAC( operation->alg ) )
3006 {
3007 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3008 input_length );
3009 }
3010 else
3011#endif /* MBEDTLS_MD_C */
3012 {
3013 /* This shouldn't happen if `operation` was initialized by
3014 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003015 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003016 }
3017
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 if( status != PSA_SUCCESS )
3019 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003020 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021}
3022
Gilles Peskine01126fa2018-07-12 17:04:55 +02003023#if defined(MBEDTLS_MD_C)
3024static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3025 uint8_t *mac,
3026 size_t mac_size )
3027{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003028 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003029 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3030 size_t hash_size = 0;
3031 size_t block_size = psa_get_hash_block_size( hash_alg );
3032 psa_status_t status;
3033
Gilles Peskine01126fa2018-07-12 17:04:55 +02003034 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3035 if( status != PSA_SUCCESS )
3036 return( status );
3037 /* From here on, tmp needs to be wiped. */
3038
3039 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3040 if( status != PSA_SUCCESS )
3041 goto exit;
3042
3043 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3044 if( status != PSA_SUCCESS )
3045 goto exit;
3046
3047 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3048 if( status != PSA_SUCCESS )
3049 goto exit;
3050
Gilles Peskined911eb72018-08-14 15:18:45 +02003051 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3052 if( status != PSA_SUCCESS )
3053 goto exit;
3054
3055 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003056
3057exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003058 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003059 return( status );
3060}
3061#endif /* MBEDTLS_MD_C */
3062
mohammad16036df908f2018-04-02 08:34:15 -07003063static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003064 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003065 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003066{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003067 if( ! operation->key_set )
3068 return( PSA_ERROR_BAD_STATE );
3069 if( operation->iv_required && ! operation->iv_set )
3070 return( PSA_ERROR_BAD_STATE );
3071
Gilles Peskine8c9def32018-02-08 10:02:12 +01003072 if( mac_size < operation->mac_size )
3073 return( PSA_ERROR_BUFFER_TOO_SMALL );
3074
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003076 if( operation->alg == PSA_ALG_CMAC )
3077 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003078 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3079 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3080 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003081 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003082 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003083 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003085 else
3086#endif /* MBEDTLS_CMAC_C */
3087#if defined(MBEDTLS_MD_C)
3088 if( PSA_ALG_IS_HMAC( operation->alg ) )
3089 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003090 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003091 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003092 }
3093 else
3094#endif /* MBEDTLS_MD_C */
3095 {
3096 /* This shouldn't happen if `operation` was initialized by
3097 * a setup function. */
3098 return( PSA_ERROR_BAD_STATE );
3099 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100}
3101
Gilles Peskineacd4be32018-07-08 19:56:25 +02003102psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3103 uint8_t *mac,
3104 size_t mac_size,
3105 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003106{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003107 psa_status_t status;
3108
Jaeden Amero252ef282019-02-15 14:05:35 +00003109 if( operation->alg == 0 )
3110 {
3111 return( PSA_ERROR_BAD_STATE );
3112 }
3113
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003114 /* Fill the output buffer with something that isn't a valid mac
3115 * (barring an attack on the mac and deliberately-crafted input),
3116 * in case the caller doesn't check the return status properly. */
3117 *mac_length = mac_size;
3118 /* If mac_size is 0 then mac may be NULL and then the
3119 * call to memset would have undefined behavior. */
3120 if( mac_size != 0 )
3121 memset( mac, '!', mac_size );
3122
Gilles Peskine89167cb2018-07-08 20:12:23 +02003123 if( ! operation->is_sign )
3124 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003125 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003126 }
mohammad16036df908f2018-04-02 08:34:15 -07003127
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003128 status = psa_mac_finish_internal( operation, mac, mac_size );
3129
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003130 if( status == PSA_SUCCESS )
3131 {
3132 status = psa_mac_abort( operation );
3133 if( status == PSA_SUCCESS )
3134 *mac_length = operation->mac_size;
3135 else
3136 memset( mac, '!', mac_size );
3137 }
3138 else
3139 psa_mac_abort( operation );
3140 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003141}
3142
Gilles Peskineacd4be32018-07-08 19:56:25 +02003143psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3144 const uint8_t *mac,
3145 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146{
Gilles Peskine828ed142018-06-18 23:25:51 +02003147 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003148 psa_status_t status;
3149
Jaeden Amero252ef282019-02-15 14:05:35 +00003150 if( operation->alg == 0 )
3151 {
3152 return( PSA_ERROR_BAD_STATE );
3153 }
3154
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155 if( operation->is_sign )
3156 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003157 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003158 }
3159 if( operation->mac_size != mac_length )
3160 {
3161 status = PSA_ERROR_INVALID_SIGNATURE;
3162 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003163 }
mohammad16036df908f2018-04-02 08:34:15 -07003164
3165 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003166 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003167 if( status != PSA_SUCCESS )
3168 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003169
3170 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3171 status = PSA_ERROR_INVALID_SIGNATURE;
3172
3173cleanup:
3174 if( status == PSA_SUCCESS )
3175 status = psa_mac_abort( operation );
3176 else
3177 psa_mac_abort( operation );
3178
Gilles Peskine3f108122018-12-07 18:14:53 +01003179 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003180
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003181 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182}
3183
3184
Gilles Peskine20035e32018-02-03 22:44:14 +01003185
Gilles Peskine20035e32018-02-03 22:44:14 +01003186/****************************************************************/
3187/* Asymmetric cryptography */
3188/****************************************************************/
3189
Gilles Peskine2b450e32018-06-27 15:42:46 +02003190#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003191/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003192 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003193static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3194 size_t hash_length,
3195 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003196{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003197 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003198 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003199 *md_alg = mbedtls_md_get_type( md_info );
3200
3201 /* The Mbed TLS RSA module uses an unsigned int for hash length
3202 * parameters. Validate that it fits so that we don't risk an
3203 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003204#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003205 if( hash_length > UINT_MAX )
3206 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003207#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003208
3209#if defined(MBEDTLS_PKCS1_V15)
3210 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3211 * must be correct. */
3212 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3213 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003214 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003215 if( md_info == NULL )
3216 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003217 if( mbedtls_md_get_size( md_info ) != hash_length )
3218 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003219 }
3220#endif /* MBEDTLS_PKCS1_V15 */
3221
3222#if defined(MBEDTLS_PKCS1_V21)
3223 /* PSS requires a hash internally. */
3224 if( PSA_ALG_IS_RSA_PSS( alg ) )
3225 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003226 if( md_info == NULL )
3227 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003228 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003229#endif /* MBEDTLS_PKCS1_V21 */
3230
Gilles Peskine61b91d42018-06-08 16:09:36 +02003231 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003232}
3233
Michael Thomas7ab51742020-02-12 03:48:56 +00003234psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003235 psa_algorithm_t alg,
3236 const uint8_t *hash,
3237 size_t hash_length,
3238 uint8_t *signature,
3239 size_t signature_size,
3240 size_t *signature_length )
3241{
3242 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003244 mbedtls_md_type_t md_alg;
3245
3246 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3247 if( status != PSA_SUCCESS )
3248 return( status );
3249
Gilles Peskine630a18a2018-06-29 17:49:35 +02003250 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003251 return( PSA_ERROR_BUFFER_TOO_SMALL );
3252
3253#if defined(MBEDTLS_PKCS1_V15)
3254 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3255 {
3256 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3257 MBEDTLS_MD_NONE );
3258 ret = mbedtls_rsa_pkcs1_sign( rsa,
3259 mbedtls_ctr_drbg_random,
3260 &global_data.ctr_drbg,
3261 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003262 md_alg,
3263 (unsigned int) hash_length,
3264 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003265 signature );
3266 }
3267 else
3268#endif /* MBEDTLS_PKCS1_V15 */
3269#if defined(MBEDTLS_PKCS1_V21)
3270 if( PSA_ALG_IS_RSA_PSS( alg ) )
3271 {
3272 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3273 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3274 mbedtls_ctr_drbg_random,
3275 &global_data.ctr_drbg,
3276 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003277 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003278 (unsigned int) hash_length,
3279 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003280 signature );
3281 }
3282 else
3283#endif /* MBEDTLS_PKCS1_V21 */
3284 {
3285 return( PSA_ERROR_INVALID_ARGUMENT );
3286 }
3287
3288 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003289 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003290 return( mbedtls_to_psa_error( ret ) );
3291}
3292
Michael Thomas7ab51742020-02-12 03:48:56 +00003293psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003294 psa_algorithm_t alg,
3295 const uint8_t *hash,
3296 size_t hash_length,
3297 const uint8_t *signature,
3298 size_t signature_length )
3299{
3300 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003302 mbedtls_md_type_t md_alg;
3303
3304 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3305 if( status != PSA_SUCCESS )
3306 return( status );
3307
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003308 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3309 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003310
3311#if defined(MBEDTLS_PKCS1_V15)
3312 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3313 {
3314 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3315 MBEDTLS_MD_NONE );
3316 ret = mbedtls_rsa_pkcs1_verify( rsa,
3317 mbedtls_ctr_drbg_random,
3318 &global_data.ctr_drbg,
3319 MBEDTLS_RSA_PUBLIC,
3320 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003321 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003322 hash,
3323 signature );
3324 }
3325 else
3326#endif /* MBEDTLS_PKCS1_V15 */
3327#if defined(MBEDTLS_PKCS1_V21)
3328 if( PSA_ALG_IS_RSA_PSS( alg ) )
3329 {
3330 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3331 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3332 mbedtls_ctr_drbg_random,
3333 &global_data.ctr_drbg,
3334 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003335 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003336 (unsigned int) hash_length,
3337 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003338 signature );
3339 }
3340 else
3341#endif /* MBEDTLS_PKCS1_V21 */
3342 {
3343 return( PSA_ERROR_INVALID_ARGUMENT );
3344 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003345
3346 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3347 * the rest of the signature is invalid". This has little use in
3348 * practice and PSA doesn't report this distinction. */
3349 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3350 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003351 return( mbedtls_to_psa_error( ret ) );
3352}
3353#endif /* MBEDTLS_RSA_C */
3354
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003355#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003356/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3357 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3358 * (even though these functions don't modify it). */
Michael Thomas257d3be2020-02-24 17:16:42 +00003359psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003360 psa_algorithm_t alg,
3361 const uint8_t *hash,
3362 size_t hash_length,
3363 uint8_t *signature,
3364 size_t signature_size,
3365 size_t *signature_length )
3366{
Janos Follath24eed8d2019-11-22 13:21:35 +00003367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003368 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003369 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003370 mbedtls_mpi_init( &r );
3371 mbedtls_mpi_init( &s );
3372
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003373 if( signature_size < 2 * curve_bytes )
3374 {
3375 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3376 goto cleanup;
3377 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378
Gilles Peskinea05219c2018-11-16 16:02:56 +01003379#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003380 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3381 {
3382 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3383 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3384 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003385 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3386 &ecp->d, hash,
3387 hash_length, md_alg,
3388 mbedtls_ctr_drbg_random,
3389 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003390 }
3391 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003392#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003393 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003394 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003395 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3396 hash, hash_length,
3397 mbedtls_ctr_drbg_random,
3398 &global_data.ctr_drbg ) );
3399 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003400
3401 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3402 signature,
3403 curve_bytes ) );
3404 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3405 signature + curve_bytes,
3406 curve_bytes ) );
3407
3408cleanup:
3409 mbedtls_mpi_free( &r );
3410 mbedtls_mpi_free( &s );
3411 if( ret == 0 )
3412 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003413 return( mbedtls_to_psa_error( ret ) );
3414}
3415
Michael Thomas257d3be2020-02-24 17:16:42 +00003416psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003417 const uint8_t *hash,
3418 size_t hash_length,
3419 const uint8_t *signature,
3420 size_t signature_length )
3421{
Janos Follath24eed8d2019-11-22 13:21:35 +00003422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003423 mbedtls_mpi r, s;
3424 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3425 mbedtls_mpi_init( &r );
3426 mbedtls_mpi_init( &s );
3427
3428 if( signature_length != 2 * curve_bytes )
3429 return( PSA_ERROR_INVALID_SIGNATURE );
3430
3431 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3432 signature,
3433 curve_bytes ) );
3434 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3435 signature + curve_bytes,
3436 curve_bytes ) );
3437
3438 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3439 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003440
3441cleanup:
3442 mbedtls_mpi_free( &r );
3443 mbedtls_mpi_free( &s );
3444 return( mbedtls_to_psa_error( ret ) );
3445}
3446#endif /* MBEDTLS_ECDSA_C */
3447
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003448psa_status_t psa_sign_hash( psa_key_handle_t handle,
3449 psa_algorithm_t alg,
3450 const uint8_t *hash,
3451 size_t hash_length,
3452 uint8_t *signature,
3453 size_t signature_size,
3454 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003455{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003456 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003457 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003458#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3459 const psa_drv_se_t *drv;
3460 psa_drv_se_context_t *drv_context;
3461#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003462
3463 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003464 /* Immediately reject a zero-length signature buffer. This guarantees
3465 * that signature must be a valid pointer. (On the other hand, the hash
3466 * buffer can in principle be empty since it doesn't actually have
3467 * to be a hash.) */
3468 if( signature_size == 0 )
3469 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003470
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003471 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003472 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003473 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003474 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003475 {
3476 status = PSA_ERROR_INVALID_ARGUMENT;
3477 goto exit;
3478 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003479
Gilles Peskineedc64242019-08-07 21:05:07 +02003480#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3481 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3482 {
3483 if( drv->asymmetric == NULL ||
3484 drv->asymmetric->p_sign == NULL )
3485 {
3486 status = PSA_ERROR_NOT_SUPPORTED;
3487 goto exit;
3488 }
3489 status = drv->asymmetric->p_sign( drv_context,
3490 slot->data.se.slot_number,
3491 alg,
3492 hash, hash_length,
3493 signature, signature_size,
3494 signature_length );
3495 }
3496 else
3497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003498#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003499 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003500 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003501 status = psa_rsa_sign( slot->data.rsa,
3502 alg,
3503 hash, hash_length,
3504 signature, signature_size,
3505 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003506 }
3507 else
3508#endif /* defined(MBEDTLS_RSA_C) */
3509#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003510 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003511 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003512#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003513 if(
3514#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3515 PSA_ALG_IS_ECDSA( alg )
3516#else
3517 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3518#endif
3519 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003520 status = psa_ecdsa_sign( slot->data.ecp,
3521 alg,
3522 hash, hash_length,
3523 signature, signature_size,
3524 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525 else
3526#endif /* defined(MBEDTLS_ECDSA_C) */
3527 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003528 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003529 }
itayzafrir5c753392018-05-08 11:18:38 +03003530 }
3531 else
3532#endif /* defined(MBEDTLS_ECP_C) */
3533 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003534 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003535 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003536
3537exit:
3538 /* Fill the unused part of the output buffer (the whole buffer on error,
3539 * the trailing part on success) with something that isn't a valid mac
3540 * (barring an attack on the mac and deliberately-crafted input),
3541 * in case the caller doesn't check the return status properly. */
3542 if( status == PSA_SUCCESS )
3543 memset( signature + *signature_length, '!',
3544 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003545 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003546 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003547 /* If signature_size is 0 then we have nothing to do. We must not call
3548 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003549 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003550}
3551
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003552psa_status_t psa_verify_hash( psa_key_handle_t handle,
3553 psa_algorithm_t alg,
3554 const uint8_t *hash,
3555 size_t hash_length,
3556 const uint8_t *signature,
3557 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003558{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003559 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003560 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003561#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3562 const psa_drv_se_t *drv;
3563 psa_drv_se_context_t *drv_context;
3564#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003565
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003566 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003567 if( status != PSA_SUCCESS )
3568 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003569
Gilles Peskineedc64242019-08-07 21:05:07 +02003570#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3571 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3572 {
3573 if( drv->asymmetric == NULL ||
3574 drv->asymmetric->p_verify == NULL )
3575 return( PSA_ERROR_NOT_SUPPORTED );
3576 return( drv->asymmetric->p_verify( drv_context,
3577 slot->data.se.slot_number,
3578 alg,
3579 hash, hash_length,
3580 signature, signature_length ) );
3581 }
3582 else
3583#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003584#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003585 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003587 return( psa_rsa_verify( slot->data.rsa,
3588 alg,
3589 hash, hash_length,
3590 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591 }
3592 else
3593#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003594#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003595 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003596 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003597#if defined(MBEDTLS_ECDSA_C)
3598 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003599 return( psa_ecdsa_verify( slot->data.ecp,
3600 hash, hash_length,
3601 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003602 else
3603#endif /* defined(MBEDTLS_ECDSA_C) */
3604 {
3605 return( PSA_ERROR_INVALID_ARGUMENT );
3606 }
itayzafrir5c753392018-05-08 11:18:38 +03003607 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003608 else
3609#endif /* defined(MBEDTLS_ECP_C) */
3610 {
3611 return( PSA_ERROR_NOT_SUPPORTED );
3612 }
3613}
3614
Gilles Peskine072ac562018-06-30 00:21:29 +02003615#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3616static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3617 mbedtls_rsa_context *rsa )
3618{
3619 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3620 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3621 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3622 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3623}
3624#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3625
Gilles Peskinec5487a82018-12-03 18:08:14 +01003626psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003627 psa_algorithm_t alg,
3628 const uint8_t *input,
3629 size_t input_length,
3630 const uint8_t *salt,
3631 size_t salt_length,
3632 uint8_t *output,
3633 size_t output_size,
3634 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003636 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003637 psa_status_t status;
3638
Darryl Green5cc689a2018-07-24 15:34:10 +01003639 (void) input;
3640 (void) input_length;
3641 (void) salt;
3642 (void) output;
3643 (void) output_size;
3644
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003645 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003647 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3648 return( PSA_ERROR_INVALID_ARGUMENT );
3649
Gilles Peskine28f8f302019-07-24 13:30:31 +02003650 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003651 if( status != PSA_SUCCESS )
3652 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003653 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3654 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003655 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003656
3657#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003658 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659 {
3660 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003662 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003663 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003665 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003666 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003667 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3668 mbedtls_ctr_drbg_random,
3669 &global_data.ctr_drbg,
3670 MBEDTLS_RSA_PUBLIC,
3671 input_length,
3672 input,
3673 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 }
3675 else
3676#endif /* MBEDTLS_PKCS1_V15 */
3677#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003678 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003680 psa_rsa_oaep_set_padding_mode( alg, rsa );
3681 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3682 mbedtls_ctr_drbg_random,
3683 &global_data.ctr_drbg,
3684 MBEDTLS_RSA_PUBLIC,
3685 salt, salt_length,
3686 input_length,
3687 input,
3688 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689 }
3690 else
3691#endif /* MBEDTLS_PKCS1_V21 */
3692 {
3693 return( PSA_ERROR_INVALID_ARGUMENT );
3694 }
3695 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003696 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697 return( mbedtls_to_psa_error( ret ) );
3698 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003700#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003701 {
3702 return( PSA_ERROR_NOT_SUPPORTED );
3703 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003704}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705
Gilles Peskinec5487a82018-12-03 18:08:14 +01003706psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003707 psa_algorithm_t alg,
3708 const uint8_t *input,
3709 size_t input_length,
3710 const uint8_t *salt,
3711 size_t salt_length,
3712 uint8_t *output,
3713 size_t output_size,
3714 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003716 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003717 psa_status_t status;
3718
Darryl Green5cc689a2018-07-24 15:34:10 +01003719 (void) input;
3720 (void) input_length;
3721 (void) salt;
3722 (void) output;
3723 (void) output_size;
3724
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003725 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003727 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3728 return( PSA_ERROR_INVALID_ARGUMENT );
3729
Gilles Peskine28f8f302019-07-24 13:30:31 +02003730 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003731 if( status != PSA_SUCCESS )
3732 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003733 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003734 return( PSA_ERROR_INVALID_ARGUMENT );
3735
3736#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003737 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003738 {
3739 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003741
Gilles Peskine630a18a2018-06-29 17:49:35 +02003742 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003743 return( PSA_ERROR_INVALID_ARGUMENT );
3744
3745#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003746 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003748 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3749 mbedtls_ctr_drbg_random,
3750 &global_data.ctr_drbg,
3751 MBEDTLS_RSA_PRIVATE,
3752 output_length,
3753 input,
3754 output,
3755 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003756 }
3757 else
3758#endif /* MBEDTLS_PKCS1_V15 */
3759#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003760 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003761 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003762 psa_rsa_oaep_set_padding_mode( alg, rsa );
3763 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3764 mbedtls_ctr_drbg_random,
3765 &global_data.ctr_drbg,
3766 MBEDTLS_RSA_PRIVATE,
3767 salt, salt_length,
3768 output_length,
3769 input,
3770 output,
3771 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003772 }
3773 else
3774#endif /* MBEDTLS_PKCS1_V21 */
3775 {
3776 return( PSA_ERROR_INVALID_ARGUMENT );
3777 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003778
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003779 return( mbedtls_to_psa_error( ret ) );
3780 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003781 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003782#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783 {
3784 return( PSA_ERROR_NOT_SUPPORTED );
3785 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003786}
Gilles Peskine20035e32018-02-03 22:44:14 +01003787
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003788
3789
mohammad1603503973b2018-03-12 15:59:30 +02003790/****************************************************************/
3791/* Symmetric cryptography */
3792/****************************************************************/
3793
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003794/* Initialize the cipher operation structure. Once this function has been
3795 * called, psa_cipher_abort can run and will do the right thing. */
3796static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3797 psa_algorithm_t alg )
3798{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003799 if( ! PSA_ALG_IS_CIPHER( alg ) )
3800 {
3801 memset( operation, 0, sizeof( *operation ) );
3802 return( PSA_ERROR_INVALID_ARGUMENT );
3803 }
3804
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003805 operation->alg = alg;
3806 operation->key_set = 0;
3807 operation->iv_set = 0;
3808 operation->iv_required = 1;
3809 operation->iv_size = 0;
3810 operation->block_size = 0;
3811 mbedtls_cipher_init( &operation->ctx.cipher );
3812 return( PSA_SUCCESS );
3813}
3814
Gilles Peskinee553c652018-06-04 16:22:46 +02003815static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003816 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003817 psa_algorithm_t alg,
3818 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003819{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003820 int ret = 0;
3821 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003822 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003823 size_t key_bits;
3824 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003825 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3826 PSA_KEY_USAGE_ENCRYPT :
3827 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003828
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003829 /* A context must be freshly initialized before it can be set up. */
3830 if( operation->alg != 0 )
3831 {
3832 return( PSA_ERROR_BAD_STATE );
3833 }
3834
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003835 status = psa_cipher_init( operation, alg );
3836 if( status != PSA_SUCCESS )
3837 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003838
Gilles Peskine28f8f302019-07-24 13:30:31 +02003839 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003840 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003841 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003842 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003843
Michael Thomas863b5d62020-02-10 19:41:16 +00003844#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
3845 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
3846 {
3847 status = psa_cipher_setup_vendor(operation, handle, alg, cipher_operation);
3848 goto exit;
3849 }
3850#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
3851
Gilles Peskine8e338702019-07-30 20:06:31 +02003852 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003853 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003854 {
3855 status = PSA_ERROR_NOT_SUPPORTED;
3856 goto exit;
3857 }
mohammad1603503973b2018-03-12 15:59:30 +02003858
mohammad1603503973b2018-03-12 15:59:30 +02003859 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003860 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003861 goto exit;
Michael Thomasaf675252020-03-13 05:51:57 +00003862
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003863#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003864 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003865 {
3866 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003867 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003868 memcpy( keys, slot->data.raw.data, 16 );
3869 memcpy( keys + 16, slot->data.raw.data, 8 );
3870 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3871 keys,
3872 192, cipher_operation );
3873 }
3874 else
3875#endif
3876 {
3877 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3878 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003879 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003880 }
Moran Peker41deec42018-04-04 15:43:05 +03003881 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003882 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003883
mohammad16038481e742018-03-18 13:57:31 +02003884#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003885 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003886 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003887 case PSA_ALG_CBC_NO_PADDING:
3888 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3889 MBEDTLS_PADDING_NONE );
3890 break;
3891 case PSA_ALG_CBC_PKCS7:
3892 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3893 MBEDTLS_PADDING_PKCS7 );
3894 break;
3895 default:
3896 /* The algorithm doesn't involve padding. */
3897 ret = 0;
3898 break;
3899 }
3900 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003901 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003902#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3903
mohammad1603503973b2018-03-12 15:59:30 +02003904 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003905 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003906 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003907 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003908 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003909 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003910 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003911#if defined(MBEDTLS_CHACHA20_C)
3912 else
3913 if( alg == PSA_ALG_CHACHA20 )
3914 operation->iv_size = 12;
3915#endif
mohammad1603503973b2018-03-12 15:59:30 +02003916
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003917exit:
3918 if( status == 0 )
3919 status = mbedtls_to_psa_error( ret );
3920 if( status != 0 )
3921 psa_cipher_abort( operation );
3922 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003923}
3924
Gilles Peskinefe119512018-07-08 21:39:34 +02003925psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003926 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003927 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003928{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003929 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003930}
3931
Gilles Peskinefe119512018-07-08 21:39:34 +02003932psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003933 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003934 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003935{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003936 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003937}
3938
Gilles Peskinefe119512018-07-08 21:39:34 +02003939psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003940 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003941 size_t iv_size,
3942 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003943{
itayzafrir534bd7c2018-08-02 13:56:32 +03003944 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003946 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003947 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003948 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003949 }
Moran Peker41deec42018-04-04 15:43:05 +03003950 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003951 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003952 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003953 goto exit;
3954 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003955 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3956 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003957 if( ret != 0 )
3958 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003959 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003960 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003961 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003962
mohammad16038481e742018-03-18 13:57:31 +02003963 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003964 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003965
Moran Peker395db872018-05-31 14:07:14 +03003966exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003967 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003968 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003969 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003970}
3971
Gilles Peskinefe119512018-07-08 21:39:34 +02003972psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003973 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003974 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003975{
itayzafrir534bd7c2018-08-02 13:56:32 +03003976 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003978 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003979 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003980 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003981 }
Moran Pekera28258c2018-05-29 16:25:04 +03003982 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003983 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003984 status = PSA_ERROR_INVALID_ARGUMENT;
3985 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003986 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003987 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3988 status = mbedtls_to_psa_error( ret );
3989exit:
3990 if( status == PSA_SUCCESS )
3991 operation->iv_set = 1;
3992 else
mohammad1603503973b2018-03-12 15:59:30 +02003993 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003994 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003995}
3996
Gilles Peskinee553c652018-06-04 16:22:46 +02003997psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3998 const uint8_t *input,
3999 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004000 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004001 size_t output_size,
4002 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004003{
itayzafrir534bd7c2018-08-02 13:56:32 +03004004 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004006 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004007
4008 if( operation->alg == 0 )
4009 {
4010 return( PSA_ERROR_BAD_STATE );
4011 }
4012
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004013 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004014 {
4015 /* Take the unprocessed partial block left over from previous
4016 * update calls, if any, plus the input to this call. Remove
4017 * the last partial block, if any. You get the data that will be
4018 * output in this call. */
4019 expected_output_size =
4020 ( operation->ctx.cipher.unprocessed_len + input_length )
4021 / operation->block_size * operation->block_size;
4022 }
4023 else
4024 {
4025 expected_output_size = input_length;
4026 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004027
Gilles Peskine89d789c2018-06-04 17:17:16 +02004028 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004029 {
4030 status = PSA_ERROR_BUFFER_TOO_SMALL;
4031 goto exit;
4032 }
mohammad160382759612018-03-12 18:16:40 +02004033
mohammad1603503973b2018-03-12 15:59:30 +02004034 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004035 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004036 status = mbedtls_to_psa_error( ret );
4037exit:
4038 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004039 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004040 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004041}
4042
Gilles Peskinee553c652018-06-04 16:22:46 +02004043psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4044 uint8_t *output,
4045 size_t output_size,
4046 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004047{
David Saadab4ecc272019-02-14 13:48:10 +02004048 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004049 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004050 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004051
mohammad1603503973b2018-03-12 15:59:30 +02004052 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004053 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004054 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004055 }
4056 if( operation->iv_required && ! operation->iv_set )
4057 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004058 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004059 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004060
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004061 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004062 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4063 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004064 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004065 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004066 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004067 }
4068
Janos Follath315b51c2018-07-09 16:04:51 +01004069 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4070 temp_output_buffer,
4071 output_length );
4072 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004073 {
Janos Follath315b51c2018-07-09 16:04:51 +01004074 status = mbedtls_to_psa_error( cipher_ret );
4075 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004076 }
Janos Follath315b51c2018-07-09 16:04:51 +01004077
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004078 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004079 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004080 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004081 memcpy( output, temp_output_buffer, *output_length );
4082 else
4083 {
Janos Follath315b51c2018-07-09 16:04:51 +01004084 status = PSA_ERROR_BUFFER_TOO_SMALL;
4085 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004086 }
mohammad1603503973b2018-03-12 15:59:30 +02004087
Gilles Peskine3f108122018-12-07 18:14:53 +01004088 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004089 status = psa_cipher_abort( operation );
4090
4091 return( status );
4092
4093error:
4094
4095 *output_length = 0;
4096
Gilles Peskine3f108122018-12-07 18:14:53 +01004097 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004098 (void) psa_cipher_abort( operation );
4099
4100 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004101}
4102
Gilles Peskinee553c652018-06-04 16:22:46 +02004103psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4104{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004105 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004106 {
4107 /* The object has (apparently) been initialized but it is not
4108 * in use. It's ok to call abort on such an object, and there's
4109 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004110 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004111 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004112
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004113 /* Sanity check (shouldn't happen: operation->alg should
4114 * always have been initialized to a valid value). */
4115 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4116 return( PSA_ERROR_BAD_STATE );
Michael Thomasaf675252020-03-13 05:51:57 +00004117
mohammad1603503973b2018-03-12 15:59:30 +02004118 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004119
Moran Peker41deec42018-04-04 15:43:05 +03004120 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004121 operation->key_set = 0;
4122 operation->iv_set = 0;
4123 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004124 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004125 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004126
Moran Peker395db872018-05-31 14:07:14 +03004127 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004128}
4129
Gilles Peskinea0655c32018-04-30 17:06:50 +02004130
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004131
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004132
mohammad16035955c982018-04-26 00:53:03 +03004133/****************************************************************/
4134/* AEAD */
4135/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004136
Gilles Peskineedf9a652018-08-17 18:11:56 +02004137typedef struct
4138{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004139 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004140 const mbedtls_cipher_info_t *cipher_info;
4141 union
4142 {
4143#if defined(MBEDTLS_CCM_C)
4144 mbedtls_ccm_context ccm;
4145#endif /* MBEDTLS_CCM_C */
4146#if defined(MBEDTLS_GCM_C)
4147 mbedtls_gcm_context gcm;
4148#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004149#if defined(MBEDTLS_CHACHAPOLY_C)
4150 mbedtls_chachapoly_context chachapoly;
4151#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004152 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004153 psa_algorithm_t core_alg;
4154 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004155 uint8_t tag_length;
4156} aead_operation_t;
4157
Gilles Peskine30a9e412019-01-14 18:36:12 +01004158static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004159{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004160 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 {
4162#if defined(MBEDTLS_CCM_C)
4163 case PSA_ALG_CCM:
4164 mbedtls_ccm_free( &operation->ctx.ccm );
4165 break;
4166#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004167#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004168 case PSA_ALG_GCM:
4169 mbedtls_gcm_free( &operation->ctx.gcm );
4170 break;
4171#endif /* MBEDTLS_GCM_C */
4172 }
4173}
4174
4175static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004176 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004177 psa_key_usage_t usage,
4178 psa_algorithm_t alg )
4179{
4180 psa_status_t status;
4181 size_t key_bits;
4182 mbedtls_cipher_id_t cipher_id;
4183
Gilles Peskine28f8f302019-07-24 13:30:31 +02004184 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004185 if( status != PSA_SUCCESS )
4186 return( status );
4187
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004188 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004189
4190 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004191 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004192 &cipher_id );
4193 if( operation->cipher_info == NULL )
4194 return( PSA_ERROR_NOT_SUPPORTED );
4195
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004196 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004197 {
4198#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004199 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4200 operation->core_alg = PSA_ALG_CCM;
4201 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004202 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4203 * The call to mbedtls_ccm_encrypt_and_tag or
4204 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004205 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004206 return( PSA_ERROR_INVALID_ARGUMENT );
4207 mbedtls_ccm_init( &operation->ctx.ccm );
4208 status = mbedtls_to_psa_error(
4209 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4210 operation->slot->data.raw.data,
4211 (unsigned int) key_bits ) );
4212 if( status != 0 )
4213 goto cleanup;
4214 break;
4215#endif /* MBEDTLS_CCM_C */
4216
4217#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004218 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4219 operation->core_alg = PSA_ALG_GCM;
4220 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004221 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4222 * The call to mbedtls_gcm_crypt_and_tag or
4223 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004224 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004225 return( PSA_ERROR_INVALID_ARGUMENT );
4226 mbedtls_gcm_init( &operation->ctx.gcm );
4227 status = mbedtls_to_psa_error(
4228 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4229 operation->slot->data.raw.data,
4230 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004231 if( status != 0 )
4232 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004233 break;
4234#endif /* MBEDTLS_GCM_C */
4235
Gilles Peskine26869f22019-05-06 15:25:00 +02004236#if defined(MBEDTLS_CHACHAPOLY_C)
4237 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4238 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4239 operation->full_tag_length = 16;
4240 /* We only support the default tag length. */
4241 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4242 return( PSA_ERROR_NOT_SUPPORTED );
4243 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4244 status = mbedtls_to_psa_error(
4245 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4246 operation->slot->data.raw.data ) );
4247 if( status != 0 )
4248 goto cleanup;
4249 break;
4250#endif /* MBEDTLS_CHACHAPOLY_C */
4251
Gilles Peskineedf9a652018-08-17 18:11:56 +02004252 default:
4253 return( PSA_ERROR_NOT_SUPPORTED );
4254 }
4255
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004256 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4257 {
4258 status = PSA_ERROR_INVALID_ARGUMENT;
4259 goto cleanup;
4260 }
4261 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004262
Gilles Peskineedf9a652018-08-17 18:11:56 +02004263 return( PSA_SUCCESS );
4264
4265cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004266 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004267 return( status );
4268}
4269
Gilles Peskinec5487a82018-12-03 18:08:14 +01004270psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004271 psa_algorithm_t alg,
4272 const uint8_t *nonce,
4273 size_t nonce_length,
4274 const uint8_t *additional_data,
4275 size_t additional_data_length,
4276 const uint8_t *plaintext,
4277 size_t plaintext_length,
4278 uint8_t *ciphertext,
4279 size_t ciphertext_size,
4280 size_t *ciphertext_length )
4281{
mohammad16035955c982018-04-26 00:53:03 +03004282 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004283 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004284 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004285
mohammad1603f08a5502018-06-03 15:05:47 +03004286 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004287
Gilles Peskinec5487a82018-12-03 18:08:14 +01004288 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004289 if( status != PSA_SUCCESS )
4290 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004291
Gilles Peskineedf9a652018-08-17 18:11:56 +02004292 /* For all currently supported modes, the tag is at the end of the
4293 * ciphertext. */
4294 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4295 {
4296 status = PSA_ERROR_BUFFER_TOO_SMALL;
4297 goto exit;
4298 }
4299 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004300
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004301#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004302 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004303 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004304 status = mbedtls_to_psa_error(
4305 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4306 MBEDTLS_GCM_ENCRYPT,
4307 plaintext_length,
4308 nonce, nonce_length,
4309 additional_data, additional_data_length,
4310 plaintext, ciphertext,
4311 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004312 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004313 else
4314#endif /* MBEDTLS_GCM_C */
4315#if defined(MBEDTLS_CCM_C)
4316 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004317 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004318 status = mbedtls_to_psa_error(
4319 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4320 plaintext_length,
4321 nonce, nonce_length,
4322 additional_data,
4323 additional_data_length,
4324 plaintext, ciphertext,
4325 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004326 }
mohammad16035c8845f2018-05-09 05:40:09 -07004327 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004328#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004329#if defined(MBEDTLS_CHACHAPOLY_C)
4330 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4331 {
4332 if( nonce_length != 12 || operation.tag_length != 16 )
4333 {
4334 status = PSA_ERROR_NOT_SUPPORTED;
4335 goto exit;
4336 }
4337 status = mbedtls_to_psa_error(
4338 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4339 plaintext_length,
4340 nonce,
4341 additional_data,
4342 additional_data_length,
4343 plaintext,
4344 ciphertext,
4345 tag ) );
4346 }
4347 else
4348#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004349 {
mohammad1603554faad2018-06-03 15:07:38 +03004350 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004351 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004352
Gilles Peskineedf9a652018-08-17 18:11:56 +02004353 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4354 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004355
Gilles Peskineedf9a652018-08-17 18:11:56 +02004356exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004357 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004358 if( status == PSA_SUCCESS )
4359 *ciphertext_length = plaintext_length + operation.tag_length;
4360 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004361}
4362
Gilles Peskineee652a32018-06-01 19:23:52 +02004363/* Locate the tag in a ciphertext buffer containing the encrypted data
4364 * followed by the tag. Return the length of the part preceding the tag in
4365 * *plaintext_length. This is the size of the plaintext in modes where
4366 * the encrypted data has the same size as the plaintext, such as
4367 * CCM and GCM. */
4368static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4369 const uint8_t *ciphertext,
4370 size_t ciphertext_length,
4371 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004372 const uint8_t **p_tag )
4373{
4374 size_t payload_length;
4375 if( tag_length > ciphertext_length )
4376 return( PSA_ERROR_INVALID_ARGUMENT );
4377 payload_length = ciphertext_length - tag_length;
4378 if( payload_length > plaintext_size )
4379 return( PSA_ERROR_BUFFER_TOO_SMALL );
4380 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004381 return( PSA_SUCCESS );
4382}
4383
Gilles Peskinec5487a82018-12-03 18:08:14 +01004384psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004385 psa_algorithm_t alg,
4386 const uint8_t *nonce,
4387 size_t nonce_length,
4388 const uint8_t *additional_data,
4389 size_t additional_data_length,
4390 const uint8_t *ciphertext,
4391 size_t ciphertext_length,
4392 uint8_t *plaintext,
4393 size_t plaintext_size,
4394 size_t *plaintext_length )
4395{
mohammad16035955c982018-04-26 00:53:03 +03004396 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397 aead_operation_t operation;
4398 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004399
Gilles Peskineee652a32018-06-01 19:23:52 +02004400 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004401
Gilles Peskinec5487a82018-12-03 18:08:14 +01004402 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004403 if( status != PSA_SUCCESS )
4404 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004405
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004406 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4407 ciphertext, ciphertext_length,
4408 plaintext_size, &tag );
4409 if( status != PSA_SUCCESS )
4410 goto exit;
4411
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004412#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004413 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004414 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004415 status = mbedtls_to_psa_error(
4416 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4417 ciphertext_length - operation.tag_length,
4418 nonce, nonce_length,
4419 additional_data,
4420 additional_data_length,
4421 tag, operation.tag_length,
4422 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004423 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004424 else
4425#endif /* MBEDTLS_GCM_C */
4426#if defined(MBEDTLS_CCM_C)
4427 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004428 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004429 status = mbedtls_to_psa_error(
4430 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4431 ciphertext_length - operation.tag_length,
4432 nonce, nonce_length,
4433 additional_data,
4434 additional_data_length,
4435 ciphertext, plaintext,
4436 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004437 }
mohammad160339574652018-06-01 04:39:53 -07004438 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004439#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004440#if defined(MBEDTLS_CHACHAPOLY_C)
4441 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4442 {
4443 if( nonce_length != 12 || operation.tag_length != 16 )
4444 {
4445 status = PSA_ERROR_NOT_SUPPORTED;
4446 goto exit;
4447 }
4448 status = mbedtls_to_psa_error(
4449 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4450 ciphertext_length - operation.tag_length,
4451 nonce,
4452 additional_data,
4453 additional_data_length,
4454 tag,
4455 ciphertext,
4456 plaintext ) );
4457 }
4458 else
4459#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004460 {
mohammad1603554faad2018-06-03 15:07:38 +03004461 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004462 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004463
Gilles Peskineedf9a652018-08-17 18:11:56 +02004464 if( status != PSA_SUCCESS && plaintext_size != 0 )
4465 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004466
Gilles Peskineedf9a652018-08-17 18:11:56 +02004467exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004468 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004469 if( status == PSA_SUCCESS )
4470 *plaintext_length = ciphertext_length - operation.tag_length;
4471 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004472}
4473
Gilles Peskinea0655c32018-04-30 17:06:50 +02004474
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004475
Gilles Peskine20035e32018-02-03 22:44:14 +01004476/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004477/* Generators */
4478/****************************************************************/
4479
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004480#define HKDF_STATE_INIT 0 /* no input yet */
4481#define HKDF_STATE_STARTED 1 /* got salt */
4482#define HKDF_STATE_KEYED 2 /* got key */
4483#define HKDF_STATE_OUTPUT 3 /* output started */
4484
Gilles Peskinecbe66502019-05-16 16:59:18 +02004485static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004487{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4489 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004490 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004492}
4493
4494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004496{
4497 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004499 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004500 {
4501 /* The object has (apparently) been initialized but it is not
4502 * in use. It's ok to call abort on such an object, and there's
4503 * nothing to do. */
4504 }
4505 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004506#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004507 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004508 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 mbedtls_free( operation->ctx.hkdf.info );
4510 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004511 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004512 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004514 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004515 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004516 if( operation->ctx.tls12_prf.seed != NULL )
4517 {
4518 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4519 operation->ctx.tls12_prf.seed_length );
4520 mbedtls_free( operation->ctx.tls12_prf.seed );
4521 }
4522
4523 if( operation->ctx.tls12_prf.label != NULL )
4524 {
4525 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4526 operation->ctx.tls12_prf.label_length );
4527 mbedtls_free( operation->ctx.tls12_prf.label );
4528 }
4529
4530 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4531
4532 /* We leave the fields Ai and output_block to be erased safely by the
4533 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004534 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004535 else
4536#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004537 {
4538 status = PSA_ERROR_BAD_STATE;
4539 }
Janos Follath083036a2019-06-11 10:22:26 +01004540 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004541 return( status );
4542}
4543
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004545 size_t *capacity)
4546{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004548 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004549 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004550 return PSA_ERROR_BAD_STATE;
4551 }
4552
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004554 return( PSA_SUCCESS );
4555}
4556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004558 size_t capacity )
4559{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004560 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004561 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004562 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004563 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004564 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004565 return( PSA_SUCCESS );
4566}
4567
Gilles Peskinebef7f142018-07-12 17:22:21 +02004568#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004570 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004571static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004572 psa_algorithm_t hash_alg,
4573 uint8_t *output,
4574 size_t output_length )
4575{
4576 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4577 psa_status_t status;
4578
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004579 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4580 return( PSA_ERROR_BAD_STATE );
4581 hkdf->state = HKDF_STATE_OUTPUT;
4582
Gilles Peskinebef7f142018-07-12 17:22:21 +02004583 while( output_length != 0 )
4584 {
4585 /* Copy what remains of the current block */
4586 uint8_t n = hash_length - hkdf->offset_in_block;
4587 if( n > output_length )
4588 n = (uint8_t) output_length;
4589 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4590 output += n;
4591 output_length -= n;
4592 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004593 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004594 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004595 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004596 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004598 * object was corrupted or if this function is called directly
4599 * inside the library. */
4600 if( hkdf->block_number == 0xff )
4601 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004602
4603 /* We need a new block */
4604 ++hkdf->block_number;
4605 hkdf->offset_in_block = 0;
4606 status = psa_hmac_setup_internal( &hkdf->hmac,
4607 hkdf->prk, hash_length,
4608 hash_alg );
4609 if( status != PSA_SUCCESS )
4610 return( status );
4611 if( hkdf->block_number != 1 )
4612 {
4613 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4614 hkdf->output_block,
4615 hash_length );
4616 if( status != PSA_SUCCESS )
4617 return( status );
4618 }
4619 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4620 hkdf->info,
4621 hkdf->info_length );
4622 if( status != PSA_SUCCESS )
4623 return( status );
4624 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4625 &hkdf->block_number, 1 );
4626 if( status != PSA_SUCCESS )
4627 return( status );
4628 status = psa_hmac_finish_internal( &hkdf->hmac,
4629 hkdf->output_block,
4630 sizeof( hkdf->output_block ) );
4631 if( status != PSA_SUCCESS )
4632 return( status );
4633 }
4634
4635 return( PSA_SUCCESS );
4636}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004637
Janos Follath7742fee2019-06-17 12:58:10 +01004638static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4639 psa_tls12_prf_key_derivation_t *tls12_prf,
4640 psa_algorithm_t alg )
4641{
4642 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4643 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004644 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4645 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004646
Janos Follath7742fee2019-06-17 12:58:10 +01004647 /* We can't be wanting more output after block 0xff, otherwise
4648 * the capacity check in psa_key_derivation_output_bytes() would have
4649 * prevented this call. It could happen only if the operation
4650 * object was corrupted or if this function is called directly
4651 * inside the library. */
4652 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004653 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004654
4655 /* We need a new block */
4656 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004657 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004658
4659 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4660 *
4661 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4662 *
4663 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4664 * HMAC_hash(secret, A(2) + seed) +
4665 * HMAC_hash(secret, A(3) + seed) + ...
4666 *
4667 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004668 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004669 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004670 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004671 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004672 * is currently extracted as `output_block` and where i is
4673 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004674 */
4675
Janos Follathea29bfb2019-06-19 12:21:20 +01004676 /* Save the hash context before using it, to preserve the hash state with
4677 * only the inner padding in it. We need this, because inner padding depends
4678 * on the key (secret in the RFC's terminology). */
4679 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4680 if( status != PSA_SUCCESS )
4681 goto cleanup;
4682
4683 /* Calculate A(i) where i = tls12_prf->block_number. */
4684 if( tls12_prf->block_number == 1 )
4685 {
4686 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4687 * the variable seed and in this instance means it in the context of the
4688 * P_hash function, where seed = label + seed.) */
4689 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4690 tls12_prf->label, tls12_prf->label_length );
4691 if( status != PSA_SUCCESS )
4692 goto cleanup;
4693 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4694 tls12_prf->seed, tls12_prf->seed_length );
4695 if( status != PSA_SUCCESS )
4696 goto cleanup;
4697 }
4698 else
4699 {
4700 /* A(i) = HMAC_hash(secret, A(i-1)) */
4701 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4702 tls12_prf->Ai, hash_length );
4703 if( status != PSA_SUCCESS )
4704 goto cleanup;
4705 }
4706
4707 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4708 tls12_prf->Ai, hash_length );
4709 if( status != PSA_SUCCESS )
4710 goto cleanup;
4711 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4712 if( status != PSA_SUCCESS )
4713 goto cleanup;
4714
4715 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4716 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4717 tls12_prf->Ai, hash_length );
4718 if( status != PSA_SUCCESS )
4719 goto cleanup;
4720 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4721 tls12_prf->label, tls12_prf->label_length );
4722 if( status != PSA_SUCCESS )
4723 goto cleanup;
4724 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4725 tls12_prf->seed, tls12_prf->seed_length );
4726 if( status != PSA_SUCCESS )
4727 goto cleanup;
4728 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4729 tls12_prf->output_block, hash_length );
4730 if( status != PSA_SUCCESS )
4731 goto cleanup;
4732 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4733 if( status != PSA_SUCCESS )
4734 goto cleanup;
4735
Janos Follath7742fee2019-06-17 12:58:10 +01004736
4737cleanup:
4738
Janos Follathea29bfb2019-06-19 12:21:20 +01004739 cleanup_status = psa_hash_abort( &backup );
4740 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4741 status = cleanup_status;
4742
4743 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004744}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004745
Janos Follath844eb0e2019-06-19 12:10:49 +01004746static psa_status_t psa_key_derivation_tls12_prf_read(
4747 psa_tls12_prf_key_derivation_t *tls12_prf,
4748 psa_algorithm_t alg,
4749 uint8_t *output,
4750 size_t output_length )
4751{
4752 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4753 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4754 psa_status_t status;
4755 uint8_t offset, length;
4756
4757 while( output_length != 0 )
4758 {
4759 /* Check if we have fully processed the current block. */
4760 if( tls12_prf->left_in_block == 0 )
4761 {
4762 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4763 alg );
4764 if( status != PSA_SUCCESS )
4765 return( status );
4766
4767 continue;
4768 }
4769
4770 if( tls12_prf->left_in_block > output_length )
4771 length = (uint8_t) output_length;
4772 else
4773 length = tls12_prf->left_in_block;
4774
4775 offset = hash_length - tls12_prf->left_in_block;
4776 memcpy( output, tls12_prf->output_block + offset, length );
4777 output += length;
4778 output_length -= length;
4779 tls12_prf->left_in_block -= length;
4780 }
4781
4782 return( PSA_SUCCESS );
4783}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004784#endif /* MBEDTLS_MD_C */
4785
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004786psa_status_t psa_key_derivation_output_bytes(
4787 psa_key_derivation_operation_t *operation,
4788 uint8_t *output,
4789 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790{
4791 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004793
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004795 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004797 return PSA_ERROR_BAD_STATE;
4798 }
4799
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004801 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004803 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004805 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 goto exit;
4807 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004811 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4813 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004816 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004817 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004819
Gilles Peskinebef7f142018-07-12 17:22:21 +02004820#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004822 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004825 output, output_length );
4826 }
Janos Follathadbec812019-06-14 11:05:39 +01004827 else
Janos Follathadbec812019-06-14 11:05:39 +01004828 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004830 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004832 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004833 output_length );
4834 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004835 else
4836#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004837 {
4838 return( PSA_ERROR_BAD_STATE );
4839 }
4840
4841exit:
4842 if( status != PSA_SUCCESS )
4843 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004844 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 * This allows us to differentiate between exhausted operations and
4846 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4847 * operations. */
4848 psa_algorithm_t alg = operation->alg;
4849 psa_key_derivation_abort( operation );
4850 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004851 memset( output, '!', output_length );
4852 }
4853 return( status );
4854}
4855
Gilles Peskine08542d82018-07-19 17:05:42 +02004856#if defined(MBEDTLS_DES_C)
4857static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4858{
4859 if( data_size >= 8 )
4860 mbedtls_des_key_set_parity( data );
4861 if( data_size >= 16 )
4862 mbedtls_des_key_set_parity( data + 8 );
4863 if( data_size >= 24 )
4864 mbedtls_des_key_set_parity( data + 16 );
4865}
4866#endif /* MBEDTLS_DES_C */
4867
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004868static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004869 psa_key_slot_t *slot,
4870 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004872{
4873 uint8_t *data = NULL;
4874 size_t bytes = PSA_BITS_TO_BYTES( bits );
4875 psa_status_t status;
4876
Gilles Peskine8e338702019-07-30 20:06:31 +02004877 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004878 return( PSA_ERROR_INVALID_ARGUMENT );
4879 if( bits % 8 != 0 )
4880 return( PSA_ERROR_INVALID_ARGUMENT );
4881 data = mbedtls_calloc( 1, bytes );
4882 if( data == NULL )
4883 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4884
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004886 if( status != PSA_SUCCESS )
4887 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004888#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004889 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004890 psa_des_set_key_parity( data, bytes );
4891#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004892 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004893
4894exit:
4895 mbedtls_free( data );
4896 return( status );
4897}
4898
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004899psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004901 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004902{
4903 psa_status_t status;
4904 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004905 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004906
4907 /* Reject any attempt to create a zero-length key so that we don't
4908 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4909 if( psa_get_key_bits( attributes ) == 0 )
4910 return( PSA_ERROR_INVALID_ARGUMENT );
4911
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004912 if( ! operation->can_output_key )
4913 return( PSA_ERROR_NOT_PERMITTED );
4914
Gilles Peskinedf179142019-07-15 22:02:14 +02004915 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4916 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004917#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4918 if( driver != NULL )
4919 {
4920 /* Deriving a key in a secure element is not implemented yet. */
4921 status = PSA_ERROR_NOT_SUPPORTED;
4922 }
4923#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004924 if( status == PSA_SUCCESS )
4925 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004926 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004927 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004929 }
4930 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004931 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004932 if( status != PSA_SUCCESS )
4933 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004934 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004935 *handle = 0;
4936 }
4937 return( status );
4938}
4939
Gilles Peskineeab56e42018-07-12 17:12:33 +02004940
4941
4942/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004943/* Key derivation */
4944/****************************************************************/
4945
Gilles Peskine969c5d62019-01-16 15:53:06 +01004946static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004948 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004949{
Janos Follath5fe19732019-06-20 15:09:30 +01004950 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4951 * initialisers for this union leave some bytes unspecified.) */
4952 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4953
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004956 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4957 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4958 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004959 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004960 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004961 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4962 if( hash_size == 0 )
4963 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004964 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4965 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004966 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 {
4968 return( PSA_ERROR_NOT_SUPPORTED );
4969 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004971 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004972 }
4973#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004975 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004976}
4977
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004979 psa_algorithm_t alg )
4980{
4981 psa_status_t status;
4982
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004984 return( PSA_ERROR_BAD_STATE );
4985
Gilles Peskine6843c292019-01-18 16:44:49 +01004986 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4987 return( PSA_ERROR_INVALID_ARGUMENT );
4988 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004989 {
4990 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004992 }
4993 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4994 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004996 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004997 else
4998 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004999
5000 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005002 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005003}
5004
5005#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005006static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005007 psa_algorithm_t hash_alg,
5008 psa_key_derivation_step_t step,
5009 const uint8_t *data,
5010 size_t data_length )
5011{
5012 psa_status_t status;
5013 switch( step )
5014 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005015 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005016 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005018 status = psa_hmac_setup_internal( &hkdf->hmac,
5019 data, data_length,
5020 hash_alg );
5021 if( status != PSA_SUCCESS )
5022 return( status );
5023 hkdf->state = HKDF_STATE_STARTED;
5024 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005025 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005026 /* If no salt was provided, use an empty salt. */
5027 if( hkdf->state == HKDF_STATE_INIT )
5028 {
5029 status = psa_hmac_setup_internal( &hkdf->hmac,
5030 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005031 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 if( status != PSA_SUCCESS )
5033 return( status );
5034 hkdf->state = HKDF_STATE_STARTED;
5035 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005036 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005037 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005038 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5039 data, data_length );
5040 if( status != PSA_SUCCESS )
5041 return( status );
5042 status = psa_hmac_finish_internal( &hkdf->hmac,
5043 hkdf->prk,
5044 sizeof( hkdf->prk ) );
5045 if( status != PSA_SUCCESS )
5046 return( status );
5047 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5048 hkdf->block_number = 0;
5049 hkdf->state = HKDF_STATE_KEYED;
5050 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005051 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005052 if( hkdf->state == HKDF_STATE_OUTPUT )
5053 return( PSA_ERROR_BAD_STATE );
5054 if( hkdf->info_set )
5055 return( PSA_ERROR_BAD_STATE );
5056 hkdf->info_length = data_length;
5057 if( data_length != 0 )
5058 {
5059 hkdf->info = mbedtls_calloc( 1, data_length );
5060 if( hkdf->info == NULL )
5061 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5062 memcpy( hkdf->info, data, data_length );
5063 }
5064 hkdf->info_set = 1;
5065 return( PSA_SUCCESS );
5066 default:
5067 return( PSA_ERROR_INVALID_ARGUMENT );
5068 }
5069}
Janos Follathb03233e2019-06-11 15:30:30 +01005070
Janos Follathf08e2652019-06-13 09:05:41 +01005071static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5072 const uint8_t *data,
5073 size_t data_length )
5074{
5075 if( prf->state != TLS12_PRF_STATE_INIT )
5076 return( PSA_ERROR_BAD_STATE );
5077
Janos Follathd6dce9f2019-07-04 09:11:38 +01005078 if( data_length != 0 )
5079 {
5080 prf->seed = mbedtls_calloc( 1, data_length );
5081 if( prf->seed == NULL )
5082 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005083
Janos Follathd6dce9f2019-07-04 09:11:38 +01005084 memcpy( prf->seed, data, data_length );
5085 prf->seed_length = data_length;
5086 }
Janos Follathf08e2652019-06-13 09:05:41 +01005087
5088 prf->state = TLS12_PRF_STATE_SEED_SET;
5089
5090 return( PSA_SUCCESS );
5091}
5092
Janos Follath81550542019-06-13 14:26:34 +01005093static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5094 psa_algorithm_t hash_alg,
5095 const uint8_t *data,
5096 size_t data_length )
5097{
5098 psa_status_t status;
5099 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5100 return( PSA_ERROR_BAD_STATE );
5101
5102 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5103 if( status != PSA_SUCCESS )
5104 return( status );
5105
5106 prf->state = TLS12_PRF_STATE_KEY_SET;
5107
5108 return( PSA_SUCCESS );
5109}
5110
Janos Follath6660f0e2019-06-17 08:44:03 +01005111static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5112 psa_tls12_prf_key_derivation_t *prf,
5113 psa_algorithm_t hash_alg,
5114 const uint8_t *data,
5115 size_t data_length )
5116{
5117 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005118 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5119 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005120
5121 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5122 return( PSA_ERROR_INVALID_ARGUMENT );
5123
5124 /* Quoting RFC 4279, Section 2:
5125 *
5126 * The premaster secret is formed as follows: if the PSK is N octets
5127 * long, concatenate a uint16 with the value N, N zero octets, a second
5128 * uint16 with the value N, and the PSK itself.
5129 */
5130
Janos Follath40e13932019-06-26 13:22:29 +01005131 *cur++ = ( data_length >> 8 ) & 0xff;
5132 *cur++ = ( data_length >> 0 ) & 0xff;
5133 memset( cur, 0, data_length );
5134 cur += data_length;
5135 *cur++ = pms[0];
5136 *cur++ = pms[1];
5137 memcpy( cur, data, data_length );
5138 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005139
Janos Follath40e13932019-06-26 13:22:29 +01005140 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005141
5142 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5143 return( status );
5144}
5145
Janos Follath63028dd2019-06-13 09:15:47 +01005146static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5147 const uint8_t *data,
5148 size_t data_length )
5149{
5150 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5151 return( PSA_ERROR_BAD_STATE );
5152
Janos Follathd6dce9f2019-07-04 09:11:38 +01005153 if( data_length != 0 )
5154 {
5155 prf->label = mbedtls_calloc( 1, data_length );
5156 if( prf->label == NULL )
5157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005158
Janos Follathd6dce9f2019-07-04 09:11:38 +01005159 memcpy( prf->label, data, data_length );
5160 prf->label_length = data_length;
5161 }
Janos Follath63028dd2019-06-13 09:15:47 +01005162
5163 prf->state = TLS12_PRF_STATE_LABEL_SET;
5164
5165 return( PSA_SUCCESS );
5166}
5167
Janos Follathb03233e2019-06-11 15:30:30 +01005168static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5169 psa_algorithm_t hash_alg,
5170 psa_key_derivation_step_t step,
5171 const uint8_t *data,
5172 size_t data_length )
5173{
Janos Follathb03233e2019-06-11 15:30:30 +01005174 switch( step )
5175 {
Janos Follathf08e2652019-06-13 09:05:41 +01005176 case PSA_KEY_DERIVATION_INPUT_SEED:
5177 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005178 case PSA_KEY_DERIVATION_INPUT_SECRET:
5179 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005180 case PSA_KEY_DERIVATION_INPUT_LABEL:
5181 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005182 default:
5183 return( PSA_ERROR_INVALID_ARGUMENT );
5184 }
5185}
Janos Follath6660f0e2019-06-17 08:44:03 +01005186
5187static psa_status_t psa_tls12_prf_psk_to_ms_input(
5188 psa_tls12_prf_key_derivation_t *prf,
5189 psa_algorithm_t hash_alg,
5190 psa_key_derivation_step_t step,
5191 const uint8_t *data,
5192 size_t data_length )
5193{
5194 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005195 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005196 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5197 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005198 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005199
5200 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5201}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202#endif /* MBEDTLS_MD_C */
5203
Gilles Peskineb8965192019-09-24 16:21:10 +02005204/** Check whether the given key type is acceptable for the given
5205 * input step of a key derivation.
5206 *
5207 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5208 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5209 * Both secret and non-secret inputs can alternatively have the type
5210 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5211 * that the input was passed as a buffer rather than via a key object.
5212 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005213static int psa_key_derivation_check_input_type(
5214 psa_key_derivation_step_t step,
5215 psa_key_type_t key_type )
5216{
5217 switch( step )
5218 {
5219 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005220 if( key_type == PSA_KEY_TYPE_DERIVE )
5221 return( PSA_SUCCESS );
5222 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005223 return( PSA_SUCCESS );
5224 break;
5225 case PSA_KEY_DERIVATION_INPUT_LABEL:
5226 case PSA_KEY_DERIVATION_INPUT_SALT:
5227 case PSA_KEY_DERIVATION_INPUT_INFO:
5228 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005229 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5230 return( PSA_SUCCESS );
5231 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005232 return( PSA_SUCCESS );
5233 break;
5234 }
5235 return( PSA_ERROR_INVALID_ARGUMENT );
5236}
5237
Janos Follathb80a94e2019-06-12 15:54:46 +01005238static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005240 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005241 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005242 const uint8_t *data,
5243 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005244{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005245 psa_status_t status;
5246 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5247
5248 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005249 if( status != PSA_SUCCESS )
5250 goto exit;
5251
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005253 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005254 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005256 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 step, data, data_length );
5258 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005259 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005260 {
Janos Follathb03233e2019-06-11 15:30:30 +01005261 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5262 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5263 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005264 }
5265 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5266 {
5267 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5268 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5269 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005270 }
5271 else
5272#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 return( PSA_ERROR_BAD_STATE );
5276 }
5277
Gilles Peskine224b0d62019-09-23 18:13:17 +02005278exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 return( status );
5282}
5283
Janos Follath51f4a0f2019-06-14 11:35:55 +01005284psa_status_t psa_key_derivation_input_bytes(
5285 psa_key_derivation_operation_t *operation,
5286 psa_key_derivation_step_t step,
5287 const uint8_t *data,
5288 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005289{
Gilles Peskineb8965192019-09-24 16:21:10 +02005290 return( psa_key_derivation_input_internal( operation, step,
5291 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005292 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005293}
5294
Janos Follath51f4a0f2019-06-14 11:35:55 +01005295psa_status_t psa_key_derivation_input_key(
5296 psa_key_derivation_operation_t *operation,
5297 psa_key_derivation_step_t step,
5298 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005299{
5300 psa_key_slot_t *slot;
5301 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005302
Gilles Peskine28f8f302019-07-24 13:30:31 +02005303 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005304 PSA_KEY_USAGE_DERIVE,
5305 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005306 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005307 {
5308 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005309 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005310 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005311
5312 /* Passing a key object as a SECRET input unlocks the permission
5313 * to output to a key object. */
5314 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5315 operation->can_output_key = 1;
5316
Janos Follathb80a94e2019-06-12 15:54:46 +01005317 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005318 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005319 slot->data.raw.data,
5320 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005322
5323
5324
5325/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326/* Key agreement */
5327/****************************************************************/
5328
Gilles Peskinea05219c2018-11-16 16:02:56 +01005329#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005330static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5331 size_t peer_key_length,
5332 const mbedtls_ecp_keypair *our_key,
5333 uint8_t *shared_secret,
5334 size_t shared_secret_size,
5335 size_t *shared_secret_length )
5336{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005337 mbedtls_ecp_keypair *their_key = NULL;
5338 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005339 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005340 size_t bits = 0;
5341 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005342 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005343
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005344 status = psa_import_ec_public_key( curve,
5345 peer_key, peer_key_length,
5346 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005347 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005348 goto exit;
5349
Jaeden Amero97271b32019-01-10 19:38:51 +00005350 status = mbedtls_to_psa_error(
5351 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5352 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005353 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005354 status = mbedtls_to_psa_error(
5355 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5356 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005357 goto exit;
5358
Jaeden Amero97271b32019-01-10 19:38:51 +00005359 status = mbedtls_to_psa_error(
5360 mbedtls_ecdh_calc_secret( &ecdh,
5361 shared_secret_length,
5362 shared_secret, shared_secret_size,
5363 mbedtls_ctr_drbg_random,
5364 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005365 if( status != PSA_SUCCESS )
5366 goto exit;
5367 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5368 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005369
5370exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005371 if( status != PSA_SUCCESS )
5372 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005373 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005374 mbedtls_ecp_keypair_free( their_key );
5375 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005376 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005377}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005378#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005379
Gilles Peskine01d718c2018-09-18 12:01:02 +02005380#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5381
Gilles Peskine0216fe12019-04-11 21:23:21 +02005382static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5383 psa_key_slot_t *private_key,
5384 const uint8_t *peer_key,
5385 size_t peer_key_length,
5386 uint8_t *shared_secret,
5387 size_t shared_secret_size,
5388 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005389{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005390 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005391 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005392#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005393 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005394 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005395 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005396 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5397 private_key->data.ecp,
5398 shared_secret, shared_secret_size,
5399 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005400#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005401 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005402 (void) private_key;
5403 (void) peer_key;
5404 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005405 (void) shared_secret;
5406 (void) shared_secret_size;
5407 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005408 return( PSA_ERROR_NOT_SUPPORTED );
5409 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005410}
5411
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005412/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005413 * to potentially free embedded data structures and wipe confidential data.
5414 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005415static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005416 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005417 psa_key_slot_t *private_key,
5418 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005419 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005420{
5421 psa_status_t status;
5422 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5423 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005425
5426 /* Step 1: run the secret agreement algorithm to generate the shared
5427 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005428 status = psa_key_agreement_raw_internal( ka_alg,
5429 private_key,
5430 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005431 shared_secret,
5432 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005433 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005434 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005435 goto exit;
5436
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005437 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005438 * the shared secret. A shared secret is permitted wherever a key
5439 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005440 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005441 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005442 shared_secret,
5443 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444
Gilles Peskine12313cd2018-06-20 00:20:32 +02005445exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005446 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005447 return( status );
5448}
5449
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005451 psa_key_derivation_step_t step,
5452 psa_key_handle_t private_key,
5453 const uint8_t *peer_key,
5454 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005455{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005456 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005457 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005459 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005460 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005461 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005462 if( status != PSA_SUCCESS )
5463 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005464 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005465 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005466 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005467 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005469 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005470}
5471
Gilles Peskinebe697d82019-05-16 18:00:41 +02005472psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5473 psa_key_handle_t private_key,
5474 const uint8_t *peer_key,
5475 size_t peer_key_length,
5476 uint8_t *output,
5477 size_t output_size,
5478 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005479{
5480 psa_key_slot_t *slot;
5481 psa_status_t status;
5482
5483 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5484 {
5485 status = PSA_ERROR_INVALID_ARGUMENT;
5486 goto exit;
5487 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005488 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005489 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005490 if( status != PSA_SUCCESS )
5491 goto exit;
5492
5493 status = psa_key_agreement_raw_internal( alg, slot,
5494 peer_key, peer_key_length,
5495 output, output_size,
5496 output_length );
5497
5498exit:
5499 if( status != PSA_SUCCESS )
5500 {
5501 /* If an error happens and is not handled properly, the output
5502 * may be used as a key to protect sensitive data. Arrange for such
5503 * a key to be random, which is likely to result in decryption or
5504 * verification errors. This is better than filling the buffer with
5505 * some constant data such as zeros, which would result in the data
5506 * being protected with a reproducible, easily knowable key.
5507 */
5508 psa_generate_random( output, output_size );
5509 *output_length = output_size;
5510 }
5511 return( status );
5512}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005513
5514
5515/****************************************************************/
5516/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005517/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005518
Gilles Peskine4c317f42018-07-12 01:24:09 +02005519psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005520 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005521{
Janos Follath24eed8d2019-11-22 13:21:35 +00005522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005523 GUARD_MODULE_INITIALIZED;
5524
Gilles Peskinef181eca2019-08-07 13:49:00 +02005525 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5526 {
5527 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5528 output,
5529 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5530 if( ret != 0 )
5531 return( mbedtls_to_psa_error( ret ) );
5532 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5533 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5534 }
5535
Gilles Peskinee59236f2018-01-27 23:32:46 +01005536 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5537 return( mbedtls_to_psa_error( ret ) );
5538}
5539
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005540#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5541#include "mbedtls/entropy_poll.h"
5542
Gilles Peskine7228da22019-07-15 11:06:15 +02005543psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005544 size_t seed_size )
5545{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005546 if( global_data.initialized )
5547 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005548
5549 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5550 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5551 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5552 return( PSA_ERROR_INVALID_ARGUMENT );
5553
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005554 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005555}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005556#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005557
Gilles Peskinee56e8782019-04-26 17:34:02 +02005558#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Michael Thomas863b5d62020-02-10 19:41:16 +00005559psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005560 size_t domain_parameters_size,
5561 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005562{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 size_t i;
5564 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005565
Gilles Peskinee56e8782019-04-26 17:34:02 +02005566 if( domain_parameters_size == 0 )
5567 {
5568 *exponent = 65537;
5569 return( PSA_SUCCESS );
5570 }
5571
5572 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5573 * support values that fit in a 32-bit integer, which is larger than
5574 * int on just about every platform anyway. */
5575 if( domain_parameters_size > sizeof( acc ) )
5576 return( PSA_ERROR_NOT_SUPPORTED );
5577 for( i = 0; i < domain_parameters_size; i++ )
5578 acc = ( acc << 8 ) | domain_parameters[i];
5579 if( acc > INT_MAX )
5580 return( PSA_ERROR_NOT_SUPPORTED );
5581 *exponent = acc;
5582 return( PSA_SUCCESS );
5583}
5584#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5585
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005586static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005587 psa_key_slot_t *slot, size_t bits,
5588 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005589{
Gilles Peskine8e338702019-07-30 20:06:31 +02005590 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005591
Gilles Peskinee56e8782019-04-26 17:34:02 +02005592 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593 return( PSA_ERROR_INVALID_ARGUMENT );
5594
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595 if( key_type_is_raw_bytes( type ) )
5596 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005597 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005598 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5599 if( status != PSA_SUCCESS )
5600 return( status );
5601 status = psa_generate_random( slot->data.raw.data,
5602 slot->data.raw.bytes );
5603 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005604 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005605#if defined(MBEDTLS_DES_C)
5606 if( type == PSA_KEY_TYPE_DES )
5607 psa_des_set_key_parity( slot->data.raw.data,
5608 slot->data.raw.bytes );
5609#endif /* MBEDTLS_DES_C */
5610 }
5611 else
5612
5613#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005614 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005615 {
5616 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005618 int exponent;
5619 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005620 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5621 return( PSA_ERROR_NOT_SUPPORTED );
5622 /* Accept only byte-aligned keys, for the same reasons as
5623 * in psa_import_rsa_key(). */
5624 if( bits % 8 != 0 )
5625 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005626 status = psa_read_rsa_exponent( domain_parameters,
5627 domain_parameters_size,
5628 &exponent );
5629 if( status != PSA_SUCCESS )
5630 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005631 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5632 if( rsa == NULL )
5633 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5634 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5635 ret = mbedtls_rsa_gen_key( rsa,
5636 mbedtls_ctr_drbg_random,
5637 &global_data.ctr_drbg,
5638 (unsigned int) bits,
5639 exponent );
5640 if( ret != 0 )
5641 {
5642 mbedtls_rsa_free( rsa );
5643 mbedtls_free( rsa );
5644 return( mbedtls_to_psa_error( ret ) );
5645 }
5646 slot->data.rsa = rsa;
5647 }
5648 else
5649#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5650
5651#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005652 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005653 {
5654 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005655 mbedtls_ecp_group_id grp_id =
5656 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005657 const mbedtls_ecp_curve_info *curve_info =
5658 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5659 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005661 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005662 return( PSA_ERROR_NOT_SUPPORTED );
5663 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5664 return( PSA_ERROR_NOT_SUPPORTED );
5665 if( curve_info->bit_size != bits )
5666 return( PSA_ERROR_INVALID_ARGUMENT );
5667 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5668 if( ecp == NULL )
5669 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5670 mbedtls_ecp_keypair_init( ecp );
5671 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5672 mbedtls_ctr_drbg_random,
5673 &global_data.ctr_drbg );
5674 if( ret != 0 )
5675 {
5676 mbedtls_ecp_keypair_free( ecp );
5677 mbedtls_free( ecp );
5678 return( mbedtls_to_psa_error( ret ) );
5679 }
5680 slot->data.ecp = ecp;
5681 }
5682 else
5683#endif /* MBEDTLS_ECP_C */
5684
5685 return( PSA_ERROR_NOT_SUPPORTED );
5686
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005687 return( PSA_SUCCESS );
5688}
Darryl Green0c6575a2018-11-07 16:05:30 +00005689
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005690psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005691 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005692{
5693 psa_status_t status;
5694 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005695 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005696
Gilles Peskine0f84d622019-09-12 19:03:13 +02005697 /* Reject any attempt to create a zero-length key so that we don't
5698 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5699 if( psa_get_key_bits( attributes ) == 0 )
5700 return( PSA_ERROR_INVALID_ARGUMENT );
5701
Gilles Peskinedf179142019-07-15 22:02:14 +02005702 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5703 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005704 if( status != PSA_SUCCESS )
5705 goto exit;
5706
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5708 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005709 {
Gilles Peskine11792082019-08-06 18:36:36 +02005710 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5711 size_t pubkey_length = 0; /* We don't support this feature yet */
5712 if( drv->key_management == NULL ||
5713 drv->key_management->p_generate == NULL )
5714 {
5715 status = PSA_ERROR_NOT_SUPPORTED;
5716 goto exit;
5717 }
5718 status = drv->key_management->p_generate(
5719 psa_get_se_driver_context( driver ),
5720 slot->data.se.slot_number, attributes,
5721 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005722 }
Gilles Peskine11792082019-08-06 18:36:36 +02005723 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005724#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas4c53fc72020-01-24 03:45:51 +00005725#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
Michael Thomas153cd2f2020-01-22 16:54:38 +00005726 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
Michael Thomas1d573ec2020-01-21 04:02:54 +00005727 {
5728 status = psa_generate_key_vendor(slot, attributes->core.bits,
5729 attributes->domain_parameters, attributes->domain_parameters_size);
Michael Thomasefcf4cf2020-02-24 17:46:31 +00005730 goto exit;
Michael Thomas1d573ec2020-01-21 04:02:54 +00005731 }
5732 else
Michael Thomas863b5d62020-02-10 19:41:16 +00005733#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005734 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005735 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005736 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005737 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005738 }
Gilles Peskine11792082019-08-06 18:36:36 +02005739exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005740 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005741 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005742 if( status != PSA_SUCCESS )
5743 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005744 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005745 *handle = 0;
5746 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005747 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005748}
5749
5750
Gilles Peskinee59236f2018-01-27 23:32:46 +01005751
5752/****************************************************************/
5753/* Module setup */
5754/****************************************************************/
5755
Gilles Peskine5e769522018-11-20 21:59:56 +01005756psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5757 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5758 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5759{
5760 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5761 return( PSA_ERROR_BAD_STATE );
5762 global_data.entropy_init = entropy_init;
5763 global_data.entropy_free = entropy_free;
5764 return( PSA_SUCCESS );
5765}
5766
Gilles Peskinee59236f2018-01-27 23:32:46 +01005767void mbedtls_psa_crypto_free( void )
5768{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005769 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005770 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5771 {
5772 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005773 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005774 }
5775 /* Wipe all remaining data, including configuration.
5776 * In particular, this sets all state indicator to the value
5777 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005778 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005780 /* Unregister all secure element drivers, so that we restart from
5781 * a pristine state. */
5782 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005784}
5785
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005786#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5787/** Recover a transaction that was interrupted by a power failure.
5788 *
5789 * This function is called during initialization, before psa_crypto_init()
5790 * returns. If this function returns a failure status, the initialization
5791 * fails.
5792 */
5793static psa_status_t psa_crypto_recover_transaction(
5794 const psa_crypto_transaction_t *transaction )
5795{
5796 switch( transaction->unknown.type )
5797 {
5798 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5799 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005800 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005801 * is implemented.
5802 * https://github.com/ARMmbed/mbed-crypto/issues/218
5803 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005804 default:
5805 /* We found an unsupported transaction in the storage.
5806 * We don't know what state the storage is in. Give up. */
5807 return( PSA_ERROR_STORAGE_FAILURE );
5808 }
5809}
5810#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5811
Gilles Peskinee59236f2018-01-27 23:32:46 +01005812psa_status_t psa_crypto_init( void )
5813{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005814 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815 const unsigned char drbg_seed[] = "PSA";
5816
Gilles Peskinec6b69072018-11-20 21:42:52 +01005817 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818 if( global_data.initialized != 0 )
5819 return( PSA_SUCCESS );
5820
Gilles Peskine5e769522018-11-20 21:59:56 +01005821 /* Set default configuration if
5822 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5823 if( global_data.entropy_init == NULL )
5824 global_data.entropy_init = mbedtls_entropy_init;
5825 if( global_data.entropy_free == NULL )
5826 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005827
Gilles Peskinec6b69072018-11-20 21:42:52 +01005828 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005829 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005830#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5831 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5832 /* The PSA entropy injection feature depends on using NV seed as an entropy
5833 * source. Add NV seed as an entropy source for PSA entropy injection. */
5834 mbedtls_entropy_add_source( &global_data.entropy,
5835 mbedtls_nv_seed_poll, NULL,
5836 MBEDTLS_ENTROPY_BLOCK_SIZE,
5837 MBEDTLS_ENTROPY_SOURCE_STRONG );
5838#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005839 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005840 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005841 status = mbedtls_to_psa_error(
5842 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5843 mbedtls_entropy_func,
5844 &global_data.entropy,
5845 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5846 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005848 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005849
Gilles Peskine66fb1262018-12-10 16:29:04 +01005850 status = psa_initialize_key_slots( );
5851 if( status != PSA_SUCCESS )
5852 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005853
Gilles Peskined9348f22019-10-01 15:22:29 +02005854#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5855 status = psa_init_all_se_drivers( );
5856 if( status != PSA_SUCCESS )
5857 goto exit;
5858#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5859
Gilles Peskine4b734222019-07-24 15:56:31 +02005860#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005861 status = psa_crypto_load_transaction( );
5862 if( status == PSA_SUCCESS )
5863 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005864 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5865 if( status != PSA_SUCCESS )
5866 goto exit;
5867 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005868 }
5869 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5870 {
5871 /* There's no transaction to complete. It's all good. */
5872 status = PSA_SUCCESS;
5873 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005874#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005875
Gilles Peskinec6b69072018-11-20 21:42:52 +01005876 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005877 global_data.initialized = 1;
5878
5879exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005880 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005881 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005882 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005883}
5884
5885#endif /* MBEDTLS_PSA_CRYPTO_C */