blob: bffddc995be107caca2898805b22d74713517b67 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020031#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010033#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010034/* Include internal declarations that are useful for implementing persistently
35 * stored keys. */
36#include "psa_crypto_storage.h"
37
Gilles Peskineb46bef22019-07-30 21:32:04 +020038#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010039#include <stdlib.h>
40#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020042#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#define mbedtls_calloc calloc
44#define mbedtls_free free
45#endif
46
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020048#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000049#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020050#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/blowfish.h"
52#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020053#include "mbedtls/chacha20.h"
54#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000073#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Gilles Peskine996deb12018-08-01 15:45:45 +020081#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
82
Gilles Peskine9ef733f2018-02-07 21:05:37 +010083/* constant-time buffer comparison */
84static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
85{
86 size_t i;
87 unsigned char diff = 0;
88
89 for( i = 0; i < n; i++ )
90 diff |= a[i] ^ b[i];
91
92 return( diff );
93}
94
95
96
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101static int key_type_is_raw_bytes( psa_key_type_t type )
102{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200103 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine2d277862018-06-18 15:41:12 +0200111typedef struct
112{
Gilles Peskine5e769522018-11-20 21:59:56 +0100113 void (* entropy_init )( mbedtls_entropy_context *ctx );
114 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100115 mbedtls_entropy_context entropy;
116 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100118 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119} psa_global_data_t;
120
121static psa_global_data_t global_data;
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
124 if( global_data.initialized == 0 ) \
125 return( PSA_ERROR_BAD_STATE );
126
Gilles Peskinee59236f2018-01-27 23:32:46 +0100127static psa_status_t mbedtls_to_psa_error( int ret )
128{
Gilles Peskinea5905292018-02-07 20:59:33 +0100129 /* If there's both a high-level code and low-level code, dispatch on
130 * the high-level code. */
131 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 {
133 case 0:
134 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100135
136 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
137 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
138 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
139 return( PSA_ERROR_NOT_SUPPORTED );
140 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
141 return( PSA_ERROR_HARDWARE_FAILURE );
142
143 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
147 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
148 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
149 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
150 case MBEDTLS_ERR_ASN1_INVALID_DATA:
151 return( PSA_ERROR_INVALID_ARGUMENT );
152 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
153 return( PSA_ERROR_INSUFFICIENT_MEMORY );
154 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
155 return( PSA_ERROR_BUFFER_TOO_SMALL );
156
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000158 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
160 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
161#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000168 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
170 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
171#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
173 return( PSA_ERROR_NOT_SUPPORTED );
174 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
175 return( PSA_ERROR_HARDWARE_FAILURE );
176
177 case MBEDTLS_ERR_CCM_BAD_INPUT:
178 return( PSA_ERROR_INVALID_ARGUMENT );
179 case MBEDTLS_ERR_CCM_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
181 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
182 return( PSA_ERROR_HARDWARE_FAILURE );
183
Gilles Peskine26869f22019-05-06 15:25:00 +0200184 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
185 return( PSA_ERROR_INVALID_ARGUMENT );
186
187 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
188 return( PSA_ERROR_BAD_STATE );
189 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
195 return( PSA_ERROR_INVALID_ARGUMENT );
196 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
198 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
199 return( PSA_ERROR_INVALID_PADDING );
200 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200205 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
213 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
214 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
215 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
216 return( PSA_ERROR_NOT_SUPPORTED );
217 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219
220 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
231 return( PSA_ERROR_INVALID_SIGNATURE );
232 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200233 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
237 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
238 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MD_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
249 return( PSA_ERROR_STORAGE_FAILURE );
250 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
260 return( PSA_ERROR_BUFFER_TOO_SMALL );
261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
284 return( PSA_ERROR_NOT_PERMITTED );
285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
294 return( PSA_ERROR_HARDWARE_FAILURE );
295
Gilles Peskineff2d2002019-05-06 15:26:23 +0200296 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
299 return( PSA_ERROR_NOT_SUPPORTED );
300
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303
304 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_RSA_INVALID_PADDING:
307 return( PSA_ERROR_INVALID_PADDING );
308 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
309 return( PSA_ERROR_HARDWARE_FAILURE );
310 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
313 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200314 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
316 return( PSA_ERROR_INVALID_SIGNATURE );
317 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
318 return( PSA_ERROR_BUFFER_TOO_SMALL );
319 case MBEDTLS_ERR_RSA_RNG_FAILED:
320 return( PSA_ERROR_INSUFFICIENT_MEMORY );
321 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
327 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
itayzafrir5c753392018-05-08 11:18:38 +0300336 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300337 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300338 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
340 return( PSA_ERROR_BUFFER_TOO_SMALL );
341 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
342 return( PSA_ERROR_NOT_SUPPORTED );
343 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
344 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
348 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000350 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
351 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300352
Gilles Peskinee59236f2018-01-27 23:32:46 +0100353 default:
David Saadab4ecc272019-02-14 13:48:10 +0200354 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 }
356}
357
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200358
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200359
360
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100361/****************************************************************/
362/* Key management */
363/****************************************************************/
364
Gilles Peskine73167e12019-07-12 23:44:37 +0200365#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
366static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
367{
Gilles Peskine8e338702019-07-30 20:06:31 +0200368 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200369}
370#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
371
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100372#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100373mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100374 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200375{
376 switch( curve )
377 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100378 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100379 switch( byte_length )
380 {
381 case PSA_BITS_TO_BYTES( 192 ):
382 return( MBEDTLS_ECP_DP_SECP192R1 );
383 case PSA_BITS_TO_BYTES( 224 ):
384 return( MBEDTLS_ECP_DP_SECP224R1 );
385 case PSA_BITS_TO_BYTES( 256 ):
386 return( MBEDTLS_ECP_DP_SECP256R1 );
387 case PSA_BITS_TO_BYTES( 384 ):
388 return( MBEDTLS_ECP_DP_SECP384R1 );
389 case PSA_BITS_TO_BYTES( 521 ):
390 return( MBEDTLS_ECP_DP_SECP521R1 );
391 default:
392 return( MBEDTLS_ECP_DP_NONE );
393 }
394 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100395
Paul Elliott8ff510a2020-06-02 17:19:28 +0100396 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100397 switch( byte_length )
398 {
399 case PSA_BITS_TO_BYTES( 256 ):
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_BITS_TO_BYTES( 384 ):
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_BITS_TO_BYTES( 512 ):
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 default:
406 return( MBEDTLS_ECP_DP_NONE );
407 }
408 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100409
Paul Elliott8ff510a2020-06-02 17:19:28 +0100410 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100411 switch( byte_length )
412 {
413 case PSA_BITS_TO_BYTES( 255 ):
414 return( MBEDTLS_ECP_DP_CURVE25519 );
415 case PSA_BITS_TO_BYTES( 448 ):
416 return( MBEDTLS_ECP_DP_CURVE448 );
417 default:
418 return( MBEDTLS_ECP_DP_NONE );
419 }
420 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100421
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 switch( byte_length )
424 {
425 case PSA_BITS_TO_BYTES( 192 ):
426 return( MBEDTLS_ECP_DP_SECP192K1 );
427 case PSA_BITS_TO_BYTES( 224 ):
428 return( MBEDTLS_ECP_DP_SECP224K1 );
429 case PSA_BITS_TO_BYTES( 256 ):
430 return( MBEDTLS_ECP_DP_SECP256K1 );
431 default:
432 return( MBEDTLS_ECP_DP_NONE );
433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Gilles Peskine12313cd2018-06-20 00:20:32 +0200436 default:
437 return( MBEDTLS_ECP_DP_NONE );
438 }
439}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100440#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200441
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200442static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
443 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200444{
445 /* Check that the bit size is acceptable for the key type */
446 switch( type )
447 {
448 case PSA_KEY_TYPE_RAW_DATA:
449#if defined(MBEDTLS_MD_C)
450 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200451#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200452 case PSA_KEY_TYPE_DERIVE:
453 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200454#if defined(MBEDTLS_AES_C)
455 case PSA_KEY_TYPE_AES:
456 if( bits != 128 && bits != 192 && bits != 256 )
457 return( PSA_ERROR_INVALID_ARGUMENT );
458 break;
459#endif
460#if defined(MBEDTLS_CAMELLIA_C)
461 case PSA_KEY_TYPE_CAMELLIA:
462 if( bits != 128 && bits != 192 && bits != 256 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466#if defined(MBEDTLS_DES_C)
467 case PSA_KEY_TYPE_DES:
468 if( bits != 64 && bits != 128 && bits != 192 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_ARC4_C)
473 case PSA_KEY_TYPE_ARC4:
474 if( bits < 8 || bits > 2048 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200478#if defined(MBEDTLS_CHACHA20_C)
479 case PSA_KEY_TYPE_CHACHA20:
480 if( bits != 256 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 default:
485 return( PSA_ERROR_NOT_SUPPORTED );
486 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200487 if( bits % 8 != 0 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 return( PSA_SUCCESS );
491}
492
Steven Cooremana01795d2020-07-24 22:48:15 +0200493#if defined(MBEDTLS_RSA_C)
494
495#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100496/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
497 * that are not a multiple of 8) well. For example, there is only
498 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
499 * way to return the exact bit size of a key.
500 * To keep things simple, reject non-byte-aligned key sizes. */
501static psa_status_t psa_check_rsa_key_byte_aligned(
502 const mbedtls_rsa_context *rsa )
503{
504 mbedtls_mpi n;
505 psa_status_t status;
506 mbedtls_mpi_init( &n );
507 status = mbedtls_to_psa_error(
508 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
509 if( status == PSA_SUCCESS )
510 {
511 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
512 status = PSA_ERROR_NOT_SUPPORTED;
513 }
514 mbedtls_mpi_free( &n );
515 return( status );
516}
Steven Cooremana01795d2020-07-24 22:48:15 +0200517#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100518
Steven Cooreman7f391872020-07-30 14:57:44 +0200519/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200520 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200521 * \param[in] type The type of key contained in \p data.
522 * \param[in] data The buffer from which to load the representation.
523 * \param[in] data_length The size in bytes of \p data.
524 * \param[out] p_rsa Returns a pointer to an RSA context on success.
525 * The caller is responsible for freeing both the
526 * contents of the context and the context itself
527 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200528 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200529static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200532 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533{
Steven Cooremana01795d2020-07-24 22:48:15 +0200534#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200536 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200538 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000539
540 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200541 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000542 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200543 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200546 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 if( status != PSA_SUCCESS )
548 goto exit;
549
550 /* We have something that the pkparse module recognizes. If it is a
551 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200552 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200553 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000554 status = PSA_ERROR_INVALID_ARGUMENT;
555 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000557
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
559 * supports non-byte-aligned key sizes, but not well. For example,
560 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200561 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000562 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
563 {
564 status = PSA_ERROR_NOT_SUPPORTED;
565 goto exit;
566 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200567 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 if( status != PSA_SUCCESS )
569 goto exit;
570
Steven Cooremana2371e52020-07-28 14:30:39 +0200571 /* Copy out the pointer to the RSA context, and reset the PK context
572 * such that pk_free doesn't free the RSA context we just grabbed. */
573 *p_rsa = mbedtls_pk_rsa( ctx );
574 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575
576exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200577 mbedtls_pk_free( &ctx );
578 return( status );
579#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200580 (void) data;
581 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200582 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200583 (void) rsa;
584 return( PSA_ERROR_NOT_SUPPORTED );
585#endif /* MBEDTLS_PK_PARSE_C */
586}
587
588/** Export an RSA key to export representation
589 *
590 * \param[in] type The type of key (public/private) to export
591 * \param[in] rsa The internal RSA representation from which to export
592 * \param[out] data The buffer to export to
593 * \param[in] data_size The length of the buffer to export to
594 * \param[out] data_length The amount of bytes written to \p data
595 */
596static psa_status_t psa_export_rsa_key( psa_key_type_t type,
597 mbedtls_rsa_context *rsa,
598 uint8_t *data,
599 size_t data_size,
600 size_t *data_length )
601{
602#if defined(MBEDTLS_PK_WRITE_C)
603 int ret;
604 mbedtls_pk_context pk;
605 uint8_t *pos = data + data_size;
606
607 mbedtls_pk_init( &pk );
608 pk.pk_info = &mbedtls_rsa_info;
609 pk.pk_ctx = rsa;
610
611 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200612 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
613 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200614 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
615 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
616 else
617 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
618
619 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200620 {
621 /* Clean up in case pk_write failed halfway through. */
622 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200623 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200624 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200625
626 /* The mbedtls_pk_xxx functions write to the end of the buffer.
627 * Move the data to the beginning and erase remaining data
628 * at the original location. */
629 if( 2 * (size_t) ret <= data_size )
630 {
631 memcpy( data, data + data_size - ret, ret );
632 memset( data + data_size - ret, 0, ret );
633 }
634 else if( (size_t) ret < data_size )
635 {
636 memmove( data, data + data_size - ret, ret );
637 memset( data + ret, 0, data_size - ret );
638 }
639
640 *data_length = ret;
641 return( PSA_SUCCESS );
642#else
643 (void) type;
644 (void) rsa;
645 (void) data;
646 (void) data_size;
647 (void) data_length;
648 return( PSA_ERROR_NOT_SUPPORTED );
649#endif /* MBEDTLS_PK_WRITE_C */
650}
651
652/** Import an RSA key from import representation to a slot
653 *
654 * \param[in,out] slot The slot where to store the export representation to
655 * \param[in] data The buffer containing the import representation
656 * \param[in] data_length The amount of bytes in \p data
657 */
658static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
659 const uint8_t *data,
660 size_t data_length )
661{
662 psa_status_t status;
663 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200664 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200665
Steven Cooremana01795d2020-07-24 22:48:15 +0200666 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200667 status = psa_load_rsa_representation( slot->attr.type,
668 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200669 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200671 if( status != PSA_SUCCESS )
672 goto exit;
673
674 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200675 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200676
Steven Cooreman75b74362020-07-28 14:30:13 +0200677 /* Re-export the data to PSA export format, such that we can store export
678 * representation in the key slot. Export representation in case of RSA is
679 * the smallest representation that's allowed as input, so a straight-up
680 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200681 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 if( output == NULL )
683 {
684 status = PSA_ERROR_INSUFFICIENT_MEMORY;
685 goto exit;
686 }
687
Steven Cooremana01795d2020-07-24 22:48:15 +0200688 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200689 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200690 output,
691 data_length,
692 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200693exit:
694 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200695 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200696 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200697
698 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000699 if( status != PSA_SUCCESS )
700 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200701 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000702 return( status );
703 }
704
Steven Cooremana01795d2020-07-24 22:48:15 +0200705 /* On success, store the allocated export-formatted key. */
706 slot->data.key.data = output;
707 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000708
709 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200710}
Steven Cooremana01795d2020-07-24 22:48:15 +0200711#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200712
Jaeden Ameroccdce902019-01-10 11:42:27 +0000713#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200714/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200715 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200716 * \param[in] type The type of key contained in \p data.
717 * \param[in] data The buffer from which to load the representation.
718 * \param[in] data_length The size in bytes of \p data.
719 * \param[out] p_ecp Returns a pointer to an ECP context on success.
720 * The caller is responsible for freeing both the
721 * contents of the context and the context itself
722 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200723 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200724static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
725 const uint8_t *data,
726 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200727 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100728{
729 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200730 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200731 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200732 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100733
Steven Cooreman4fed4552020-08-03 14:46:03 +0200734 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
735 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100736 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200737 /* A Weierstrass public key is represented as:
738 * - The byte 0x04;
739 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
740 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
741 * So its data length is 2m+1 where n is the key size in bits.
742 */
743 if( ( data_length & 1 ) == 0 )
744 return( PSA_ERROR_INVALID_ARGUMENT );
745 curve_size = data_length / 2;
746
747 /* Montgomery public keys are represented in compressed format, meaning
748 * their curve_size is equal to the amount of input. */
749
750 /* Private keys are represented in uncompressed private random integer
751 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100752 }
753
Steven Cooreman6d839f02020-07-30 11:36:45 +0200754 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200755 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200756 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200757 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200758 mbedtls_ecp_keypair_init( ecp );
759
Gilles Peskine4cd32772019-12-02 20:49:42 +0100760 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200761 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
762 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100763 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200764 {
765 status = PSA_ERROR_INVALID_ARGUMENT;
766 goto exit;
767 }
768
Steven Cooremanacda8342020-07-24 23:09:52 +0200769 status = mbedtls_to_psa_error(
770 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
771 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200772 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200773
Steven Cooreman6d839f02020-07-30 11:36:45 +0200774 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200775 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200776 {
777 /* Load the public value. */
778 status = mbedtls_to_psa_error(
779 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200780 data,
781 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200782 if( status != PSA_SUCCESS )
783 goto exit;
784
785 /* Check that the point is on the curve. */
786 status = mbedtls_to_psa_error(
787 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
788 if( status != PSA_SUCCESS )
789 goto exit;
790 }
791 else
792 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200793 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200794 status = mbedtls_to_psa_error(
795 mbedtls_ecp_read_key( ecp->grp.id,
796 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200797 data,
798 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200799 if( status != PSA_SUCCESS )
800 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200801 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200802
803 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200804exit:
805 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200806 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200807 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200808 mbedtls_free( ecp );
809 }
810
Steven Cooreman29149862020-08-05 15:43:42 +0200811 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100812}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000813
Steven Cooreman4fed4552020-08-03 14:46:03 +0200814/** Export an ECP key to export representation
815 *
816 * \param[in] type The type of key (public/private) to export
817 * \param[in] ecp The internal ECP representation from which to export
818 * \param[out] data The buffer to export to
819 * \param[in] data_size The length of the buffer to export to
820 * \param[out] data_length The amount of bytes written to \p data
821 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200822static psa_status_t psa_export_ecp_key( psa_key_type_t type,
823 mbedtls_ecp_keypair *ecp,
824 uint8_t *data,
825 size_t data_size,
826 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200827{
Steven Cooremanacda8342020-07-24 23:09:52 +0200828 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000829
Steven Cooremanacda8342020-07-24 23:09:52 +0200830 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200831 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 /* Check whether the public part is loaded */
833 if( mbedtls_ecp_is_zero( &ecp->Q ) )
834 {
835 /* Calculate the public key */
836 status = mbedtls_to_psa_error(
837 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
838 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
839 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200840 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200841 }
842
Steven Cooreman4fed4552020-08-03 14:46:03 +0200843 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200844 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
845 MBEDTLS_ECP_PF_UNCOMPRESSED,
846 data_length,
847 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200848 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 if( status != PSA_SUCCESS )
850 memset( data, 0, data_size );
851
Steven Cooreman29149862020-08-05 15:43:42 +0200852 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200853 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 else
855 {
Steven Cooreman29149862020-08-05 15:43:42 +0200856 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200857 return( PSA_ERROR_BUFFER_TOO_SMALL );
858
859 status = mbedtls_to_psa_error(
860 mbedtls_ecp_write_key( ecp,
861 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200862 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200863 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200864 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200865 else
866 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200867
868 return( status );
869 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200870}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871
Steven Cooreman4fed4552020-08-03 14:46:03 +0200872/** Import an ECP key from import representation to a slot
873 *
874 * \param[in,out] slot The slot where to store the export representation to
875 * \param[in] data The buffer containing the import representation
876 * \param[in] data_length The amount of bytes in \p data
877 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200878static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
879 const uint8_t *data,
880 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100881{
Steven Cooremanacda8342020-07-24 23:09:52 +0200882 psa_status_t status;
883 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200884 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100885
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200887 status = psa_load_ecp_representation( slot->attr.type,
888 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200889 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100891 if( status != PSA_SUCCESS )
892 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100893
Steven Cooremanacda8342020-07-24 23:09:52 +0200894 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200895 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200896 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200897 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200898
Steven Cooremanacda8342020-07-24 23:09:52 +0200899 /* Re-export the data to PSA export format. There is currently no support
900 * for other input formats then the export format, so this is a 1-1
901 * copy operation. */
902 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 if( output == NULL )
904 {
905 status = PSA_ERROR_INSUFFICIENT_MEMORY;
906 goto exit;
907 }
908
909 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200910 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200911 output,
912 data_length,
913 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100914exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200915 /* Always free the PK object (will also free contained ECP context) */
916 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200917 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200918
919 /* Free the allocated buffer only on error. */
920 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100921 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200922 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100924 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200925
926 /* On success, store the allocated export-formatted key. */
927 slot->data.key.data = output;
928 slot->data.key.bytes = data_length;
929
930 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100931}
932#endif /* defined(MBEDTLS_ECP_C) */
933
Gilles Peskineb46bef22019-07-30 21:32:04 +0200934/** Return the size of the key in the given slot, in bits.
935 *
936 * \param[in] slot A key slot.
937 *
938 * \return The key size in bits, read from the metadata in the slot.
939 */
940static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
941{
942 return( slot->attr.bits );
943}
944
Steven Cooreman75b74362020-07-28 14:30:13 +0200945/** Try to allocate a buffer to an empty key slot.
946 *
947 * \param[in,out] slot Key slot to attach buffer to.
948 * \param[in] buffer_length Requested size of the buffer.
949 *
950 * \retval #PSA_SUCCESS
951 * The buffer has been successfully allocated.
952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
953 * Not enough memory was available for allocation.
954 * \retval #PSA_ERROR_ALREADY_EXISTS
955 * Trying to allocate a buffer to a non-empty key slot.
956 */
957static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
958 size_t buffer_length )
959{
960 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200961 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200962
963 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
964 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200965 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200966
967 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200968 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200969}
970
Gilles Peskine8e338702019-07-30 20:06:31 +0200971/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100972 * previously. This function assumes that the slot does not contain
973 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100974psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
975 const uint8_t *data,
976 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200978 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200980 /* zero-length keys are never supported. */
981 if( data_length == 0 )
982 return( PSA_ERROR_NOT_SUPPORTED );
983
Gilles Peskine8e338702019-07-30 20:06:31 +0200984 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200986 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200987
Steven Cooreman75b74362020-07-28 14:30:13 +0200988 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
989 if( data_length > SIZE_MAX / 8 )
990 return( PSA_ERROR_NOT_SUPPORTED );
991
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200992 /* Enforce a size limit, and in particular ensure that the bit
993 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200994 if( bit_size > PSA_MAX_KEY_BITS )
995 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200996
997 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200998 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200999 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001000
1001 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001002 status = psa_allocate_buffer_to_slot( slot, data_length );
1003 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001004 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001005
1006 /* copy key into allocated buffer */
Steven Cooreman29149862020-08-05 15:43:42 +02001007 memcpy( slot->data.key.data, data, data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001008
1009 /* Write the actual key size to the slot.
1010 * psa_start_key_creation() wrote the size declared by the
1011 * caller, which may be 0 (meaning unspecified) or wrong. */
1012 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001014 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1015 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001016#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001017 status = psa_import_ecp_key( slot,
1018 data, data_length );
1019#else
1020 /* No drivers have been implemented yet, so without mbed TLS backing
1021 * there's no way to do ECP with the current library. */
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023#endif /* defined(MBEDTLS_ECP_C) */
1024 }
1025 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001026 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001027#if defined(MBEDTLS_RSA_C)
1028 status = psa_import_rsa_key( slot,
1029 data, data_length );
1030#else
1031 /* No drivers have been implemented yet, so without mbed TLS backing
1032 * there's no way to do RSA with the current library. */
1033 status = PSA_ERROR_NOT_SUPPORTED;
1034#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 }
1036 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001037 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001038 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001039 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001040 }
Darryl Green940d72c2018-07-13 13:18:51 +01001041
Darryl Green06fd18d2018-07-16 11:21:11 +01001042 return( status );
1043}
1044
Gilles Peskinef603c712019-01-19 13:40:11 +01001045/** Calculate the intersection of two algorithm usage policies.
1046 *
1047 * Return 0 (which allows no operation) on incompatibility.
1048 */
1049static psa_algorithm_t psa_key_policy_algorithm_intersection(
1050 psa_algorithm_t alg1,
1051 psa_algorithm_t alg2 )
1052{
Gilles Peskine549ea862019-05-22 11:45:59 +02001053 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001054 if( alg1 == alg2 )
1055 return( alg1 );
1056 /* If the policies are from the same hash-and-sign family, check
1057 * if one is a wildcard. If so the other has the specific algorithm. */
1058 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1059 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1060 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1061 {
1062 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1063 return( alg2 );
1064 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1065 return( alg1 );
1066 }
1067 /* If the policies are incompatible, allow nothing. */
1068 return( 0 );
1069}
1070
Gilles Peskined6f371b2019-05-10 19:33:38 +02001071static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1072 psa_algorithm_t requested_alg )
1073{
Gilles Peskine549ea862019-05-22 11:45:59 +02001074 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001075 if( requested_alg == policy_alg )
1076 return( 1 );
1077 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001078 * and requested_alg is the same hash-and-sign family with any hash,
1079 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001080 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1081 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1082 {
1083 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1084 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1085 }
1086 /* If it isn't permitted, it's forbidden. */
1087 return( 0 );
1088}
1089
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001090/** Test whether a policy permits an algorithm.
1091 *
1092 * The caller must test usage flags separately.
1093 */
1094static int psa_key_policy_permits( const psa_key_policy_t *policy,
1095 psa_algorithm_t alg )
1096{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001097 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1098 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001099}
1100
Gilles Peskinef603c712019-01-19 13:40:11 +01001101/** Restrict a key policy based on a constraint.
1102 *
1103 * \param[in,out] policy The policy to restrict.
1104 * \param[in] constraint The policy constraint to apply.
1105 *
1106 * \retval #PSA_SUCCESS
1107 * \c *policy contains the intersection of the original value of
1108 * \c *policy and \c *constraint.
1109 * \retval #PSA_ERROR_INVALID_ARGUMENT
1110 * \c *policy and \c *constraint are incompatible.
1111 * \c *policy is unchanged.
1112 */
1113static psa_status_t psa_restrict_key_policy(
1114 psa_key_policy_t *policy,
1115 const psa_key_policy_t *constraint )
1116{
1117 psa_algorithm_t intersection_alg =
1118 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001119 psa_algorithm_t intersection_alg2 =
1120 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001121 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1122 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001123 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1124 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001125 policy->usage &= constraint->usage;
1126 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001127 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001128 return( PSA_SUCCESS );
1129}
1130
Darryl Green06fd18d2018-07-16 11:21:11 +01001131/** Retrieve a slot which must contain a key. The key must have allow all the
1132 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1133 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001134static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001135 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001136 psa_key_usage_t usage,
1137 psa_algorithm_t alg )
1138{
1139 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001140 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001141
1142 *p_slot = NULL;
1143
Gilles Peskinec5487a82018-12-03 18:08:14 +01001144 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001145 if( status != PSA_SUCCESS )
1146 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001147
1148 /* Enforce that usage policy for the key slot contains all the flags
1149 * required by the usage parameter. There is one exception: public
1150 * keys can always be exported, so we treat public key objects as
1151 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001152 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001153 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001154 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001155 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001156
1157 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001158 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001159 return( PSA_ERROR_NOT_PERMITTED );
1160
1161 *p_slot = slot;
1162 return( PSA_SUCCESS );
1163}
Darryl Green940d72c2018-07-13 13:18:51 +01001164
Gilles Peskine28f8f302019-07-24 13:30:31 +02001165/** Retrieve a slot which must contain a transparent key.
1166 *
1167 * A transparent key is a key for which the key material is directly
1168 * available, as opposed to a key in a secure element.
1169 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001170 * This is a temporary function to use instead of psa_get_key_from_slot()
1171 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001172 */
1173#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1174static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1175 psa_key_slot_t **p_slot,
1176 psa_key_usage_t usage,
1177 psa_algorithm_t alg )
1178{
1179 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1180 if( status != PSA_SUCCESS )
1181 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001182 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001183 {
1184 *p_slot = NULL;
1185 return( PSA_ERROR_NOT_SUPPORTED );
1186 }
1187 return( PSA_SUCCESS );
1188}
1189#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1190/* With no secure element support, all keys are transparent. */
1191#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1192 psa_get_key_from_slot( handle, p_slot, usage, alg )
1193#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1194
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001195/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001196static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001197{
Gilles Peskine73167e12019-07-12 23:44:37 +02001198#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1199 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001200 {
1201 /* No key material to clean. */
1202 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001203 else
1204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001205 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001206 /* Data pointer will always be either a valid pointer or NULL in an
1207 * initialized slot, so we can just free it. */
1208 if( slot->data.key.data != NULL )
1209 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001210 mbedtls_free( slot->data.key.data );
1211 slot->data.key.data = NULL;
1212 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001213 }
Darryl Green40225ba2018-11-15 14:48:15 +00001214
1215 return( PSA_SUCCESS );
1216}
1217
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001218/** Completely wipe a slot in memory, including its policy.
1219 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001220psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001221{
1222 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001223 /* Multipart operations may still be using the key. This is safe
1224 * because all multipart operation objects are independent from
1225 * the key slot: if they need to access the key after the setup
1226 * phase, they have a copy of the key. Note that this means that
1227 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001228 /* At this point, key material and other type-specific content has
1229 * been wiped. Clear remaining metadata. We can call memset and not
1230 * zeroize because the metadata is not particularly sensitive. */
1231 memset( slot, 0, sizeof( *slot ) );
1232 return( status );
1233}
1234
Gilles Peskinec5487a82018-12-03 18:08:14 +01001235psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001237 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001238 psa_status_t status; /* status of the last operation */
1239 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1241 psa_se_drv_table_entry_t *driver;
1242#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001243
Gilles Peskine1841cf42019-10-08 15:48:25 +02001244 if( handle == 0 )
1245 return( PSA_SUCCESS );
1246
Gilles Peskinec5487a82018-12-03 18:08:14 +01001247 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001248 if( status != PSA_SUCCESS )
1249 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001250
1251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001252 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001253 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001254 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001255 /* For a key in a secure element, we need to do three things:
1256 * remove the key file in internal storage, destroy the
1257 * key inside the secure element, and update the driver's
1258 * persistent data. Start a transaction that will encompass these
1259 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001260 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001261 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001262 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001263 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001264 status = psa_crypto_save_transaction( );
1265 if( status != PSA_SUCCESS )
1266 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001267 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001268 /* We should still try to destroy the key in the secure
1269 * element and the key metadata in storage. This is especially
1270 * important if the error is that the storage is full.
1271 * But how to do it exactly without risking an inconsistent
1272 * state after a reset?
1273 * https://github.com/ARMmbed/mbed-crypto/issues/215
1274 */
1275 overall_status = status;
1276 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001277 }
1278
Gilles Peskine354f7672019-07-12 23:46:38 +02001279 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001280 if( overall_status == PSA_SUCCESS )
1281 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001282 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001283#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1284
Darryl Greend49a4992018-06-18 17:27:26 +01001285#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001286 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001287 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001288 status = psa_destroy_persistent_key( slot->attr.id );
1289 if( overall_status == PSA_SUCCESS )
1290 overall_status = status;
1291
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001292 /* TODO: other slots may have a copy of the same key. We should
1293 * invalidate them.
1294 * https://github.com/ARMmbed/mbed-crypto/issues/214
1295 */
Darryl Greend49a4992018-06-18 17:27:26 +01001296 }
1297#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001298
Gilles Peskinefc762652019-07-22 19:30:34 +02001299#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1300 if( driver != NULL )
1301 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001302 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303 if( overall_status == PSA_SUCCESS )
1304 overall_status = status;
1305 status = psa_crypto_stop_transaction( );
1306 if( overall_status == PSA_SUCCESS )
1307 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001308 }
1309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1310
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001311#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1312exit:
1313#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001314 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001315 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1316 if( overall_status == PSA_SUCCESS )
1317 overall_status = status;
1318 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319}
1320
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001321void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001322{
Gilles Peskineb699f072019-04-26 16:06:02 +02001323 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001325}
1326
Gilles Peskineb699f072019-04-26 16:06:02 +02001327psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1328 psa_key_type_t type,
1329 const uint8_t *data,
1330 size_t data_length )
1331{
1332 uint8_t *copy = NULL;
1333
1334 if( data_length != 0 )
1335 {
1336 copy = mbedtls_calloc( 1, data_length );
1337 if( copy == NULL )
1338 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1339 memcpy( copy, data, data_length );
1340 }
1341 /* After this point, this function is guaranteed to succeed, so it
1342 * can start modifying `*attributes`. */
1343
1344 if( attributes->domain_parameters != NULL )
1345 {
1346 mbedtls_free( attributes->domain_parameters );
1347 attributes->domain_parameters = NULL;
1348 attributes->domain_parameters_size = 0;
1349 }
1350
1351 attributes->domain_parameters = copy;
1352 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001353 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001354 return( PSA_SUCCESS );
1355}
1356
1357psa_status_t psa_get_key_domain_parameters(
1358 const psa_key_attributes_t *attributes,
1359 uint8_t *data, size_t data_size, size_t *data_length )
1360{
1361 if( attributes->domain_parameters_size > data_size )
1362 return( PSA_ERROR_BUFFER_TOO_SMALL );
1363 *data_length = attributes->domain_parameters_size;
1364 if( attributes->domain_parameters_size != 0 )
1365 memcpy( data, attributes->domain_parameters,
1366 attributes->domain_parameters_size );
1367 return( PSA_SUCCESS );
1368}
1369
1370#if defined(MBEDTLS_RSA_C)
1371static psa_status_t psa_get_rsa_public_exponent(
1372 const mbedtls_rsa_context *rsa,
1373 psa_key_attributes_t *attributes )
1374{
1375 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001377 uint8_t *buffer = NULL;
1378 size_t buflen;
1379 mbedtls_mpi_init( &mpi );
1380
1381 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1382 if( ret != 0 )
1383 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001384 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1385 {
1386 /* It's the default value, which is reported as an empty string,
1387 * so there's nothing to do. */
1388 goto exit;
1389 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001390
1391 buflen = mbedtls_mpi_size( &mpi );
1392 buffer = mbedtls_calloc( 1, buflen );
1393 if( buffer == NULL )
1394 {
1395 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1396 goto exit;
1397 }
1398 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1399 if( ret != 0 )
1400 goto exit;
1401 attributes->domain_parameters = buffer;
1402 attributes->domain_parameters_size = buflen;
1403
1404exit:
1405 mbedtls_mpi_free( &mpi );
1406 if( ret != 0 )
1407 mbedtls_free( buffer );
1408 return( mbedtls_to_psa_error( ret ) );
1409}
1410#endif /* MBEDTLS_RSA_C */
1411
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001412/** Retrieve all the publicly-accessible attributes of a key.
1413 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001414psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1415 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001416{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001417 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001418 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001419
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001420 psa_reset_key_attributes( attributes );
1421
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001422 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001423 if( status != PSA_SUCCESS )
1424 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001425
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001426 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001427 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1428 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1429
1430#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1431 if( psa_key_slot_is_external( slot ) )
1432 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1433#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001434
Gilles Peskine8e338702019-07-30 20:06:31 +02001435 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001436 {
1437#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001438 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001439 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001440#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001441 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001442 * is not yet implemented.
1443 * https://github.com/ARMmbed/mbed-crypto/issues/216
1444 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001446 break;
1447#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001448 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001449 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001450
Steven Cooreman4fed4552020-08-03 14:46:03 +02001451 status = psa_load_rsa_representation( slot->attr.type,
1452 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001453 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001454 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001455 if( status != PSA_SUCCESS )
1456 break;
1457
Steven Cooremana2371e52020-07-28 14:30:39 +02001458 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001459 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001460 mbedtls_rsa_free( rsa );
1461 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001462 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001463 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001464#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001465 default:
1466 /* Nothing else to do. */
1467 break;
1468 }
1469
1470 if( status != PSA_SUCCESS )
1471 psa_reset_key_attributes( attributes );
1472 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001473}
1474
Gilles Peskinec8000c02019-08-02 20:15:51 +02001475#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1476psa_status_t psa_get_key_slot_number(
1477 const psa_key_attributes_t *attributes,
1478 psa_key_slot_number_t *slot_number )
1479{
1480 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1481 {
1482 *slot_number = attributes->slot_number;
1483 return( PSA_SUCCESS );
1484 }
1485 else
1486 return( PSA_ERROR_INVALID_ARGUMENT );
1487}
1488#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1489
Steven Cooremana01795d2020-07-24 22:48:15 +02001490static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1491 uint8_t *data,
1492 size_t data_size,
1493 size_t *data_length )
1494{
1495 if( slot->data.key.bytes > data_size )
1496 return( PSA_ERROR_BUFFER_TOO_SMALL );
1497 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1498 memset( data + slot->data.key.bytes, 0,
1499 data_size - slot->data.key.bytes );
1500 *data_length = slot->data.key.bytes;
1501 return( PSA_SUCCESS );
1502}
1503
Gilles Peskinef603c712019-01-19 13:40:11 +01001504static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1505 uint8_t *data,
1506 size_t data_size,
1507 size_t *data_length,
1508 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001509{
Gilles Peskine5d309672019-07-12 23:47:28 +02001510#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1511 const psa_drv_se_t *drv;
1512 psa_drv_se_context_t *drv_context;
1513#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1514
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001515 *data_length = 0;
1516
Gilles Peskine8e338702019-07-30 20:06:31 +02001517 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001518 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001519
Gilles Peskinef9168942019-09-12 19:20:29 +02001520 /* Reject a zero-length output buffer now, since this can never be a
1521 * valid key representation. This way we know that data must be a valid
1522 * pointer and we can do things like memset(data, ..., data_size). */
1523 if( data_size == 0 )
1524 return( PSA_ERROR_BUFFER_TOO_SMALL );
1525
Gilles Peskine5d309672019-07-12 23:47:28 +02001526#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001527 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001528 {
1529 psa_drv_se_export_key_t method;
1530 if( drv->key_management == NULL )
1531 return( PSA_ERROR_NOT_SUPPORTED );
1532 method = ( export_public_key ?
1533 drv->key_management->p_export_public :
1534 drv->key_management->p_export );
1535 if( method == NULL )
1536 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001537 return( method( drv_context,
1538 slot->data.se.slot_number,
1539 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001540 }
1541#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1542
Gilles Peskine8e338702019-07-30 20:06:31 +02001543 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001545 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001546 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001547 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1548 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001549 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001550 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001551 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001552 /* Exporting public -> public */
1553 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1554 }
1555 else if( !export_public_key )
1556 {
1557 /* Exporting private -> private */
1558 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1559 }
1560 /* Need to export the public part of a private key,
1561 * so conversion is needed */
1562 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1563 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001564#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001565 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001566 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001567 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001568 slot->data.key.data,
1569 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001570 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001571 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001572 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001573
Steven Cooreman560c28a2020-07-24 23:20:24 +02001574 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001575 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001576 data,
1577 data_size,
1578 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001579
Steven Cooremana2371e52020-07-28 14:30:39 +02001580 mbedtls_rsa_free( rsa );
1581 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001582
Steven Cooreman560c28a2020-07-24 23:20:24 +02001583 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001584#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 /* We don't know how to convert a private RSA key to public. */
1586 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001587#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001588 }
1589 else
Moran Pekera998bc62018-04-16 18:16:20 +03001590 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001591#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001592 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001593 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001594 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001595 slot->data.key.data,
1596 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001597 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001598 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001599 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001600
1601 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1602 PSA_KEY_TYPE_ECC_GET_FAMILY(
1603 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001604 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001605 data,
1606 data_size,
1607 data_length );
1608
Steven Cooremana2371e52020-07-28 14:30:39 +02001609 mbedtls_ecp_keypair_free( ecp );
1610 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001611 return( status );
1612#else
1613 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001614 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001615#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001616 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001618 else
1619 {
1620 /* This shouldn't happen in the reference implementation, but
1621 it is valid for a special-purpose implementation to omit
1622 support for exporting certain key types. */
1623 return( PSA_ERROR_NOT_SUPPORTED );
1624 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625}
1626
Gilles Peskinec5487a82018-12-03 18:08:14 +01001627psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001628 uint8_t *data,
1629 size_t data_size,
1630 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001631{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001632 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001633 psa_status_t status;
1634
1635 /* Set the key to empty now, so that even when there are errors, we always
1636 * set data_length to a value between 0 and data_size. On error, setting
1637 * the key to empty is a good choice because an empty key representation is
1638 * unlikely to be accepted anywhere. */
1639 *data_length = 0;
1640
1641 /* Export requires the EXPORT flag. There is an exception for public keys,
1642 * which don't require any flag, but psa_get_key_from_slot takes
1643 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001644 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001645 if( status != PSA_SUCCESS )
1646 return( status );
1647 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001648 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001649}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650
Gilles Peskinec5487a82018-12-03 18:08:14 +01001651psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001652 uint8_t *data,
1653 size_t data_size,
1654 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001655{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001656 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001657 psa_status_t status;
1658
1659 /* Set the key to empty now, so that even when there are errors, we always
1660 * set data_length to a value between 0 and data_size. On error, setting
1661 * the key to empty is a good choice because an empty key representation is
1662 * unlikely to be accepted anywhere. */
1663 *data_length = 0;
1664
1665 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001666 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001667 if( status != PSA_SUCCESS )
1668 return( status );
1669 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001670 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001671}
1672
Gilles Peskine91e8c332019-08-02 19:19:39 +02001673#if defined(static_assert)
1674static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1675 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001676static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001677 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001678static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001679 "One or more key attribute flag is listed as both internal-only and external-only" );
1680#endif
1681
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001682/** Validate that a key policy is internally well-formed.
1683 *
1684 * This function only rejects invalid policies. It does not validate the
1685 * consistency of the policy with respect to other attributes of the key
1686 * such as the key type.
1687 */
1688static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001689{
1690 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001691 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 PSA_KEY_USAGE_ENCRYPT |
1693 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001694 PSA_KEY_USAGE_SIGN_HASH |
1695 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1697 return( PSA_ERROR_INVALID_ARGUMENT );
1698
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 return( PSA_SUCCESS );
1700}
1701
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001702/** Validate the internal consistency of key attributes.
1703 *
1704 * This function only rejects invalid attribute values. If does not
1705 * validate the consistency of the attributes with any key data that may
1706 * be involved in the creation of the key.
1707 *
1708 * Call this function early in the key creation process.
1709 *
1710 * \param[in] attributes Key attributes for the new key.
1711 * \param[out] p_drv On any return, the driver for the key, if any.
1712 * NULL for a transparent key.
1713 *
1714 */
1715static psa_status_t psa_validate_key_attributes(
1716 const psa_key_attributes_t *attributes,
1717 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001718{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001719 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001720
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001721 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1722 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001723 if( status != PSA_SUCCESS )
1724 return( status );
1725
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001726 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1727 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001728 if( status != PSA_SUCCESS )
1729 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001730
1731 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001732 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001733 return( status );
1734
1735 /* Refuse to create overly large keys.
1736 * Note that this doesn't trigger on import if the attributes don't
1737 * explicitly specify a size (so psa_get_key_bits returns 0), so
1738 * psa_import_key() needs its own checks. */
1739 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1740 return( PSA_ERROR_NOT_SUPPORTED );
1741
Gilles Peskine91e8c332019-08-02 19:19:39 +02001742 /* Reject invalid flags. These should not be reachable through the API. */
1743 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1744 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1745 return( PSA_ERROR_INVALID_ARGUMENT );
1746
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001747 return( PSA_SUCCESS );
1748}
1749
Gilles Peskine4747d192019-04-17 15:05:45 +02001750/** Prepare a key slot to receive key material.
1751 *
1752 * This function allocates a key slot and sets its metadata.
1753 *
1754 * If this function fails, call psa_fail_key_creation().
1755 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 * This function is intended to be used as follows:
1757 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1758 * it with the specified attributes, and assign it a handle.
1759 * -# Populate the slot with the key material.
1760 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1761 * In case of failure at any step, stop the sequence and call
1762 * psa_fail_key_creation().
1763 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001764 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001765 * \param[in] attributes Key attributes for the new key.
1766 * \param[out] handle On success, a handle for the allocated slot.
1767 * \param[out] p_slot On success, a pointer to the prepared slot.
1768 * \param[out] p_drv On any return, the driver for the key, if any.
1769 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001770 *
1771 * \retval #PSA_SUCCESS
1772 * The key slot is ready to receive key material.
1773 * \return If this function fails, the key slot is an invalid state.
1774 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 */
1776static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001777 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 const psa_key_attributes_t *attributes,
1779 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001780 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001781 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001782{
1783 psa_status_t status;
1784 psa_key_slot_t *slot;
1785
Gilles Peskinedf179142019-07-15 22:02:14 +02001786 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001787 *p_drv = NULL;
1788
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001789 status = psa_validate_key_attributes( attributes, p_drv );
1790 if( status != PSA_SUCCESS )
1791 return( status );
1792
Gilles Peskineedbed562019-08-07 18:19:59 +02001793 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001794 if( status != PSA_SUCCESS )
1795 return( status );
1796 slot = *p_slot;
1797
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001798 /* We're storing the declared bit-size of the key. It's up to each
1799 * creation mechanism to verify that this information is correct.
1800 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001801 * an input (generate, device) but not for those where the bit-size
1802 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001803
1804 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001805
Gilles Peskine91e8c332019-08-02 19:19:39 +02001806 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001807 * external-only flags, query `attributes`. Thanks to the check
1808 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001809 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001810 * may have set. */
1811 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001812
Gilles Peskinecbaff462019-07-12 23:46:04 +02001813#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001814 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001815 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001816 * create the key file in internal storage, create the
1817 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001818 * persistent data. This is done by starting a transaction that will
1819 * encompass these three actions.
1820 * For registering a volatile key, we just need to find an appropriate
1821 * slot number inside the SE. Since the key is designated volatile, creating
1822 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001823 /* The first thing to do is to find a slot number for the new key.
1824 * We save the slot number in persistent storage as part of the
1825 * transaction data. It will be needed to recover if the power
1826 * fails during the key creation process, to clean up on the secure
1827 * element side after restarting. Obtaining a slot number from the
1828 * secure element driver updates its persistent state, but we do not yet
1829 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001830 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001831 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001832 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001833 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001834 &slot->data.se.slot_number );
1835 if( status != PSA_SUCCESS )
1836 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001837
1838 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001839 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001840 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1841 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1842 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1843 psa_crypto_transaction.key.id = slot->attr.id;
1844 status = psa_crypto_save_transaction( );
1845 if( status != PSA_SUCCESS )
1846 {
1847 (void) psa_crypto_stop_transaction( );
1848 return( status );
1849 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001850 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001851 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001852
1853 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1854 {
1855 /* Key registration only makes sense with a secure element. */
1856 return( PSA_ERROR_INVALID_ARGUMENT );
1857 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001858#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1859
Darryl Green0c6575a2018-11-07 16:05:30 +00001860 return( status );
1861}
Gilles Peskine4747d192019-04-17 15:05:45 +02001862
1863/** Finalize the creation of a key once its key material has been set.
1864 *
1865 * This entails writing the key to persistent storage.
1866 *
1867 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001868 * See the documentation of psa_start_key_creation() for the intended use
1869 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001871 * \param[in,out] slot Pointer to the slot with key material.
1872 * \param[in] driver The secure element driver for the key,
1873 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001874 *
1875 * \retval #PSA_SUCCESS
1876 * The key was successfully created. The handle is now valid.
1877 * \return If this function fails, the key slot is an invalid state.
1878 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001880static psa_status_t psa_finish_key_creation(
1881 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001882 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001883{
1884 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001885 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001886 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001887
1888#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001889 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001890 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001891#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1892 if( driver != NULL )
1893 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001894 psa_se_key_data_storage_t data;
1895#if defined(static_assert)
1896 static_assert( sizeof( slot->data.se.slot_number ) ==
1897 sizeof( data.slot_number ),
1898 "Slot number size does not match psa_se_key_data_storage_t" );
1899 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1900 "Bit-size size does not match psa_se_key_data_storage_t" );
1901#endif
1902 memcpy( &data.slot_number, &slot->data.se.slot_number,
1903 sizeof( slot->data.se.slot_number ) );
1904 memcpy( &data.bits, &slot->attr.bits,
1905 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001906 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001907 (uint8_t*) &data,
1908 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001909 }
1910 else
1911#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1912 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001913 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001914 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001915 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001916 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1917 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001918 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001919 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1920 status = psa_internal_export_key( slot,
1921 buffer, buffer_size, &length,
1922 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001923 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001924 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001925 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001926
Gilles Peskinef9168942019-09-12 19:20:29 +02001927 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001928 mbedtls_free( buffer );
1929 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001930 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001931#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1932
Gilles Peskinecbaff462019-07-12 23:46:04 +02001933#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001934 /* Finish the transaction for a key creation. This does not
1935 * happen when registering an existing key. Detect this case
1936 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001937 * creation of a persistent key in a secure element requires a transaction,
1938 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001939 if( driver != NULL &&
1940 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001941 {
1942 status = psa_save_se_persistent_data( driver );
1943 if( status != PSA_SUCCESS )
1944 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001945 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001946 return( status );
1947 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001948 status = psa_crypto_stop_transaction( );
1949 if( status != PSA_SUCCESS )
1950 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001951 }
1952#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1953
Gilles Peskine4747d192019-04-17 15:05:45 +02001954 return( status );
1955}
1956
1957/** Abort the creation of a key.
1958 *
1959 * You may call this function after calling psa_start_key_creation(),
1960 * or after psa_finish_key_creation() fails. In other circumstances, this
1961 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001962 * See the documentation of psa_start_key_creation() for the intended use
1963 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001964 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001965 * \param[in,out] slot Pointer to the slot with key material.
1966 * \param[in] driver The secure element driver for the key,
1967 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001969static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001970 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001971{
Gilles Peskine011e4282019-06-26 18:34:38 +02001972 (void) driver;
1973
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 if( slot == NULL )
1975 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001976
1977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001978 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001979 * element, and the failure happened later (when saving metadata
1980 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001981 * element.
1982 * https://github.com/ARMmbed/mbed-crypto/issues/217
1983 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001984
Gilles Peskined7729582019-08-05 15:55:54 +02001985 /* Abort the ongoing transaction if any (there may not be one if
1986 * the creation process failed before starting one, or if the
1987 * key creation is a registration of a key in a secure element).
1988 * Earlier functions must already have done what it takes to undo any
1989 * partial creation. All that's left is to update the transaction data
1990 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001991 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001992#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1993
Gilles Peskine4747d192019-04-17 15:05:45 +02001994 psa_wipe_key_slot( slot );
1995}
1996
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001997/** Validate optional attributes during key creation.
1998 *
1999 * Some key attributes are optional during key creation. If they are
2000 * specified in the attributes structure, check that they are consistent
2001 * with the data in the slot.
2002 *
2003 * This function should be called near the end of key creation, after
2004 * the slot in memory is fully populated but before saving persistent data.
2005 */
2006static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 const psa_key_slot_t *slot,
2008 const psa_key_attributes_t *attributes )
2009{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002010 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002012 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013 return( PSA_ERROR_INVALID_ARGUMENT );
2014 }
2015
2016 if( attributes->domain_parameters_size != 0 )
2017 {
2018#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002019 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002021 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002022 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002024
Steven Cooreman7f391872020-07-30 14:57:44 +02002025 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002026 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002027 slot->data.key.data,
2028 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002029 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002030 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002031 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002032
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 mbedtls_mpi_init( &actual );
2034 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002035 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002037 mbedtls_rsa_free( rsa );
2038 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039 if( ret != 0 )
2040 goto rsa_exit;
2041 ret = mbedtls_mpi_read_binary( &required,
2042 attributes->domain_parameters,
2043 attributes->domain_parameters_size );
2044 if( ret != 0 )
2045 goto rsa_exit;
2046 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2047 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2048 rsa_exit:
2049 mbedtls_mpi_free( &actual );
2050 mbedtls_mpi_free( &required );
2051 if( ret != 0)
2052 return( mbedtls_to_psa_error( ret ) );
2053 }
2054 else
2055#endif
2056 {
2057 return( PSA_ERROR_INVALID_ARGUMENT );
2058 }
2059 }
2060
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002061 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002063 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064 return( PSA_ERROR_INVALID_ARGUMENT );
2065 }
2066
2067 return( PSA_SUCCESS );
2068}
2069
Gilles Peskine4747d192019-04-17 15:05:45 +02002070psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002071 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002072 size_t data_length,
2073 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002074{
2075 psa_status_t status;
2076 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002077 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002078
Gilles Peskine0f84d622019-09-12 19:03:13 +02002079 /* Reject zero-length symmetric keys (including raw data key objects).
2080 * This also rejects any key which might be encoded as an empty string,
2081 * which is never valid. */
2082 if( data_length == 0 )
2083 return( PSA_ERROR_INVALID_ARGUMENT );
2084
Gilles Peskinedf179142019-07-15 22:02:14 +02002085 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2086 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087 if( status != PSA_SUCCESS )
2088 goto exit;
2089
Gilles Peskine5d309672019-07-12 23:47:28 +02002090#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2091 if( driver != NULL )
2092 {
2093 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002094 /* The driver should set the number of key bits, however in
2095 * case it doesn't, we initialize bits to an invalid value. */
2096 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002097 if( drv->key_management == NULL ||
2098 drv->key_management->p_import == NULL )
2099 {
2100 status = PSA_ERROR_NOT_SUPPORTED;
2101 goto exit;
2102 }
2103 status = drv->key_management->p_import(
2104 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002105 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002106 &bits );
2107 if( status != PSA_SUCCESS )
2108 goto exit;
2109 if( bits > PSA_MAX_KEY_BITS )
2110 {
2111 status = PSA_ERROR_NOT_SUPPORTED;
2112 goto exit;
2113 }
2114 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002115 }
2116 else
2117#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2118 {
2119 status = psa_import_key_into_slot( slot, data, data_length );
2120 if( status != PSA_SUCCESS )
2121 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002122 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002123 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002124 if( status != PSA_SUCCESS )
2125 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002126
Gilles Peskine011e4282019-06-26 18:34:38 +02002127 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002129 if( status != PSA_SUCCESS )
2130 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002131 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002132 *handle = 0;
2133 }
2134 return( status );
2135}
2136
Gilles Peskined7729582019-08-05 15:55:54 +02002137#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2138psa_status_t mbedtls_psa_register_se_key(
2139 const psa_key_attributes_t *attributes )
2140{
2141 psa_status_t status;
2142 psa_key_slot_t *slot = NULL;
2143 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002144 psa_key_handle_t handle = 0;
2145
2146 /* Leaving attributes unspecified is not currently supported.
2147 * It could make sense to query the key type and size from the
2148 * secure element, but not all secure elements support this
2149 * and the driver HAL doesn't currently support it. */
2150 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2151 return( PSA_ERROR_NOT_SUPPORTED );
2152 if( psa_get_key_bits( attributes ) == 0 )
2153 return( PSA_ERROR_NOT_SUPPORTED );
2154
2155 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2156 &handle, &slot, &driver );
2157 if( status != PSA_SUCCESS )
2158 goto exit;
2159
Gilles Peskined7729582019-08-05 15:55:54 +02002160 status = psa_finish_key_creation( slot, driver );
2161
2162exit:
2163 if( status != PSA_SUCCESS )
2164 {
2165 psa_fail_key_creation( slot, driver );
2166 }
2167 /* Registration doesn't keep the key in RAM. */
2168 psa_close_key( handle );
2169 return( status );
2170}
2171#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2172
Gilles Peskinef603c712019-01-19 13:40:11 +01002173static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002174 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002175{
2176 psa_status_t status;
2177 uint8_t *buffer = NULL;
2178 size_t buffer_size = 0;
2179 size_t length;
2180
Gilles Peskine8e338702019-07-30 20:06:31 +02002181 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002182 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002183 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002184 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002185 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002186 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2187 if( status != PSA_SUCCESS )
2188 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002189 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002190 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002191
2192exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002193 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002194 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002195 return( status );
2196}
2197
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002198psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2199 const psa_key_attributes_t *specified_attributes,
2200 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002201{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002202 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203 psa_key_slot_t *source_slot = NULL;
2204 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002206 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002207
Gilles Peskine28f8f302019-07-24 13:30:31 +02002208 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002209 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002210 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002211 goto exit;
2212
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002213 status = psa_validate_optional_attributes( source_slot,
2214 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002215 if( status != PSA_SUCCESS )
2216 goto exit;
2217
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002218 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002219 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220 if( status != PSA_SUCCESS )
2221 goto exit;
2222
Gilles Peskinedf179142019-07-15 22:02:14 +02002223 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2224 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002225 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226 if( status != PSA_SUCCESS )
2227 goto exit;
2228
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002229#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2230 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002231 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002232 /* Copying to a secure element is not implemented yet. */
2233 status = PSA_ERROR_NOT_SUPPORTED;
2234 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002235 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002236#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002237
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002238 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002239 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002240 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002241
Gilles Peskine011e4282019-06-26 18:34:38 +02002242 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002243exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244 if( status != PSA_SUCCESS )
2245 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002246 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002247 *target_handle = 0;
2248 }
2249 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002250}
2251
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002252
2253
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002254/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002255/* Message digests */
2256/****************************************************************/
2257
Gilles Peskineb16841e2019-10-10 20:36:12 +02002258#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002259static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002260{
2261 switch( alg )
2262 {
2263#if defined(MBEDTLS_MD2_C)
2264 case PSA_ALG_MD2:
2265 return( &mbedtls_md2_info );
2266#endif
2267#if defined(MBEDTLS_MD4_C)
2268 case PSA_ALG_MD4:
2269 return( &mbedtls_md4_info );
2270#endif
2271#if defined(MBEDTLS_MD5_C)
2272 case PSA_ALG_MD5:
2273 return( &mbedtls_md5_info );
2274#endif
2275#if defined(MBEDTLS_RIPEMD160_C)
2276 case PSA_ALG_RIPEMD160:
2277 return( &mbedtls_ripemd160_info );
2278#endif
2279#if defined(MBEDTLS_SHA1_C)
2280 case PSA_ALG_SHA_1:
2281 return( &mbedtls_sha1_info );
2282#endif
2283#if defined(MBEDTLS_SHA256_C)
2284 case PSA_ALG_SHA_224:
2285 return( &mbedtls_sha224_info );
2286 case PSA_ALG_SHA_256:
2287 return( &mbedtls_sha256_info );
2288#endif
2289#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002290#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002291 case PSA_ALG_SHA_384:
2292 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002293#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002294 case PSA_ALG_SHA_512:
2295 return( &mbedtls_sha512_info );
2296#endif
2297 default:
2298 return( NULL );
2299 }
2300}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002301#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002302
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2304{
2305 switch( operation->alg )
2306 {
Gilles Peskine81736312018-06-26 15:04:31 +02002307 case 0:
2308 /* The object has (apparently) been initialized but it is not
2309 * in use. It's ok to call abort on such an object, and there's
2310 * nothing to do. */
2311 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002312#if defined(MBEDTLS_MD2_C)
2313 case PSA_ALG_MD2:
2314 mbedtls_md2_free( &operation->ctx.md2 );
2315 break;
2316#endif
2317#if defined(MBEDTLS_MD4_C)
2318 case PSA_ALG_MD4:
2319 mbedtls_md4_free( &operation->ctx.md4 );
2320 break;
2321#endif
2322#if defined(MBEDTLS_MD5_C)
2323 case PSA_ALG_MD5:
2324 mbedtls_md5_free( &operation->ctx.md5 );
2325 break;
2326#endif
2327#if defined(MBEDTLS_RIPEMD160_C)
2328 case PSA_ALG_RIPEMD160:
2329 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2330 break;
2331#endif
2332#if defined(MBEDTLS_SHA1_C)
2333 case PSA_ALG_SHA_1:
2334 mbedtls_sha1_free( &operation->ctx.sha1 );
2335 break;
2336#endif
2337#if defined(MBEDTLS_SHA256_C)
2338 case PSA_ALG_SHA_224:
2339 case PSA_ALG_SHA_256:
2340 mbedtls_sha256_free( &operation->ctx.sha256 );
2341 break;
2342#endif
2343#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002344#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002346#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 case PSA_ALG_SHA_512:
2348 mbedtls_sha512_free( &operation->ctx.sha512 );
2349 break;
2350#endif
2351 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002352 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353 }
2354 operation->alg = 0;
2355 return( PSA_SUCCESS );
2356}
2357
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002358psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359 psa_algorithm_t alg )
2360{
Janos Follath24eed8d2019-11-22 13:21:35 +00002361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002362
2363 /* A context must be freshly initialized before it can be set up. */
2364 if( operation->alg != 0 )
2365 {
2366 return( PSA_ERROR_BAD_STATE );
2367 }
2368
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369 switch( alg )
2370 {
2371#if defined(MBEDTLS_MD2_C)
2372 case PSA_ALG_MD2:
2373 mbedtls_md2_init( &operation->ctx.md2 );
2374 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2375 break;
2376#endif
2377#if defined(MBEDTLS_MD4_C)
2378 case PSA_ALG_MD4:
2379 mbedtls_md4_init( &operation->ctx.md4 );
2380 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2381 break;
2382#endif
2383#if defined(MBEDTLS_MD5_C)
2384 case PSA_ALG_MD5:
2385 mbedtls_md5_init( &operation->ctx.md5 );
2386 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2387 break;
2388#endif
2389#if defined(MBEDTLS_RIPEMD160_C)
2390 case PSA_ALG_RIPEMD160:
2391 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2392 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2393 break;
2394#endif
2395#if defined(MBEDTLS_SHA1_C)
2396 case PSA_ALG_SHA_1:
2397 mbedtls_sha1_init( &operation->ctx.sha1 );
2398 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2399 break;
2400#endif
2401#if defined(MBEDTLS_SHA256_C)
2402 case PSA_ALG_SHA_224:
2403 mbedtls_sha256_init( &operation->ctx.sha256 );
2404 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2405 break;
2406 case PSA_ALG_SHA_256:
2407 mbedtls_sha256_init( &operation->ctx.sha256 );
2408 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2409 break;
2410#endif
2411#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002412#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413 case PSA_ALG_SHA_384:
2414 mbedtls_sha512_init( &operation->ctx.sha512 );
2415 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2416 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002417#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002418 case PSA_ALG_SHA_512:
2419 mbedtls_sha512_init( &operation->ctx.sha512 );
2420 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2421 break;
2422#endif
2423 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002424 return( PSA_ALG_IS_HASH( alg ) ?
2425 PSA_ERROR_NOT_SUPPORTED :
2426 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002427 }
2428 if( ret == 0 )
2429 operation->alg = alg;
2430 else
2431 psa_hash_abort( operation );
2432 return( mbedtls_to_psa_error( ret ) );
2433}
2434
2435psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2436 const uint8_t *input,
2437 size_t input_length )
2438{
Janos Follath24eed8d2019-11-22 13:21:35 +00002439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002440
2441 /* Don't require hash implementations to behave correctly on a
2442 * zero-length input, which may have an invalid pointer. */
2443 if( input_length == 0 )
2444 return( PSA_SUCCESS );
2445
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002446 switch( operation->alg )
2447 {
2448#if defined(MBEDTLS_MD2_C)
2449 case PSA_ALG_MD2:
2450 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2451 input, input_length );
2452 break;
2453#endif
2454#if defined(MBEDTLS_MD4_C)
2455 case PSA_ALG_MD4:
2456 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2457 input, input_length );
2458 break;
2459#endif
2460#if defined(MBEDTLS_MD5_C)
2461 case PSA_ALG_MD5:
2462 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2463 input, input_length );
2464 break;
2465#endif
2466#if defined(MBEDTLS_RIPEMD160_C)
2467 case PSA_ALG_RIPEMD160:
2468 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2469 input, input_length );
2470 break;
2471#endif
2472#if defined(MBEDTLS_SHA1_C)
2473 case PSA_ALG_SHA_1:
2474 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2475 input, input_length );
2476 break;
2477#endif
2478#if defined(MBEDTLS_SHA256_C)
2479 case PSA_ALG_SHA_224:
2480 case PSA_ALG_SHA_256:
2481 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2482 input, input_length );
2483 break;
2484#endif
2485#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002486#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002488#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489 case PSA_ALG_SHA_512:
2490 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2491 input, input_length );
2492 break;
2493#endif
2494 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002495 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002497
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 if( ret != 0 )
2499 psa_hash_abort( operation );
2500 return( mbedtls_to_psa_error( ret ) );
2501}
2502
2503psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2504 uint8_t *hash,
2505 size_t hash_size,
2506 size_t *hash_length )
2507{
itayzafrir40835d42018-08-02 13:14:17 +03002508 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002510 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511
2512 /* Fill the output buffer with something that isn't a valid hash
2513 * (barring an attack on the hash and deliberately-crafted input),
2514 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002515 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002516 /* If hash_size is 0 then hash may be NULL and then the
2517 * call to memset would have undefined behavior. */
2518 if( hash_size != 0 )
2519 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520
2521 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002522 {
2523 status = PSA_ERROR_BUFFER_TOO_SMALL;
2524 goto exit;
2525 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002526
2527 switch( operation->alg )
2528 {
2529#if defined(MBEDTLS_MD2_C)
2530 case PSA_ALG_MD2:
2531 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2532 break;
2533#endif
2534#if defined(MBEDTLS_MD4_C)
2535 case PSA_ALG_MD4:
2536 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2537 break;
2538#endif
2539#if defined(MBEDTLS_MD5_C)
2540 case PSA_ALG_MD5:
2541 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2542 break;
2543#endif
2544#if defined(MBEDTLS_RIPEMD160_C)
2545 case PSA_ALG_RIPEMD160:
2546 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2547 break;
2548#endif
2549#if defined(MBEDTLS_SHA1_C)
2550 case PSA_ALG_SHA_1:
2551 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2552 break;
2553#endif
2554#if defined(MBEDTLS_SHA256_C)
2555 case PSA_ALG_SHA_224:
2556 case PSA_ALG_SHA_256:
2557 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2558 break;
2559#endif
2560#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002561#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002563#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564 case PSA_ALG_SHA_512:
2565 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2566 break;
2567#endif
2568 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002569 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002570 }
itayzafrir40835d42018-08-02 13:14:17 +03002571 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572
itayzafrir40835d42018-08-02 13:14:17 +03002573exit:
2574 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002576 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577 return( psa_hash_abort( operation ) );
2578 }
2579 else
2580 {
2581 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002582 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583 }
2584}
2585
Gilles Peskine2d277862018-06-18 15:41:12 +02002586psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2587 const uint8_t *hash,
2588 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002589{
2590 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2591 size_t actual_hash_length;
2592 psa_status_t status = psa_hash_finish( operation,
2593 actual_hash, sizeof( actual_hash ),
2594 &actual_hash_length );
2595 if( status != PSA_SUCCESS )
2596 return( status );
2597 if( actual_hash_length != hash_length )
2598 return( PSA_ERROR_INVALID_SIGNATURE );
2599 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2600 return( PSA_ERROR_INVALID_SIGNATURE );
2601 return( PSA_SUCCESS );
2602}
2603
Gilles Peskine0a749c82019-11-28 19:33:58 +01002604psa_status_t psa_hash_compute( psa_algorithm_t alg,
2605 const uint8_t *input, size_t input_length,
2606 uint8_t *hash, size_t hash_size,
2607 size_t *hash_length )
2608{
2609 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2611
2612 *hash_length = hash_size;
2613 status = psa_hash_setup( &operation, alg );
2614 if( status != PSA_SUCCESS )
2615 goto exit;
2616 status = psa_hash_update( &operation, input, input_length );
2617 if( status != PSA_SUCCESS )
2618 goto exit;
2619 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2620 if( status != PSA_SUCCESS )
2621 goto exit;
2622
2623exit:
2624 if( status == PSA_SUCCESS )
2625 status = psa_hash_abort( &operation );
2626 else
2627 psa_hash_abort( &operation );
2628 return( status );
2629}
2630
2631psa_status_t psa_hash_compare( psa_algorithm_t alg,
2632 const uint8_t *input, size_t input_length,
2633 const uint8_t *hash, size_t hash_length )
2634{
2635 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2636 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2637
2638 status = psa_hash_setup( &operation, alg );
2639 if( status != PSA_SUCCESS )
2640 goto exit;
2641 status = psa_hash_update( &operation, input, input_length );
2642 if( status != PSA_SUCCESS )
2643 goto exit;
2644 status = psa_hash_verify( &operation, hash, hash_length );
2645 if( status != PSA_SUCCESS )
2646 goto exit;
2647
2648exit:
2649 if( status == PSA_SUCCESS )
2650 status = psa_hash_abort( &operation );
2651 else
2652 psa_hash_abort( &operation );
2653 return( status );
2654}
2655
Gilles Peskineeb35d782019-01-22 17:56:16 +01002656psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2657 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002658{
2659 if( target_operation->alg != 0 )
2660 return( PSA_ERROR_BAD_STATE );
2661
2662 switch( source_operation->alg )
2663 {
2664 case 0:
2665 return( PSA_ERROR_BAD_STATE );
2666#if defined(MBEDTLS_MD2_C)
2667 case PSA_ALG_MD2:
2668 mbedtls_md2_clone( &target_operation->ctx.md2,
2669 &source_operation->ctx.md2 );
2670 break;
2671#endif
2672#if defined(MBEDTLS_MD4_C)
2673 case PSA_ALG_MD4:
2674 mbedtls_md4_clone( &target_operation->ctx.md4,
2675 &source_operation->ctx.md4 );
2676 break;
2677#endif
2678#if defined(MBEDTLS_MD5_C)
2679 case PSA_ALG_MD5:
2680 mbedtls_md5_clone( &target_operation->ctx.md5,
2681 &source_operation->ctx.md5 );
2682 break;
2683#endif
2684#if defined(MBEDTLS_RIPEMD160_C)
2685 case PSA_ALG_RIPEMD160:
2686 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2687 &source_operation->ctx.ripemd160 );
2688 break;
2689#endif
2690#if defined(MBEDTLS_SHA1_C)
2691 case PSA_ALG_SHA_1:
2692 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2693 &source_operation->ctx.sha1 );
2694 break;
2695#endif
2696#if defined(MBEDTLS_SHA256_C)
2697 case PSA_ALG_SHA_224:
2698 case PSA_ALG_SHA_256:
2699 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2700 &source_operation->ctx.sha256 );
2701 break;
2702#endif
2703#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002704#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002705 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002706#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002707 case PSA_ALG_SHA_512:
2708 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2709 &source_operation->ctx.sha512 );
2710 break;
2711#endif
2712 default:
2713 return( PSA_ERROR_NOT_SUPPORTED );
2714 }
2715
2716 target_operation->alg = source_operation->alg;
2717 return( PSA_SUCCESS );
2718}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719
2720
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722/* MAC */
2723/****************************************************************/
2724
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002725static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726 psa_algorithm_t alg,
2727 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002728 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002729 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002732 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002734 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002735 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002736
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2738 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002739 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002741 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002742 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 mode = MBEDTLS_MODE_STREAM;
2744 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745 case PSA_ALG_CTR:
2746 mode = MBEDTLS_MODE_CTR;
2747 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002748 case PSA_ALG_CFB:
2749 mode = MBEDTLS_MODE_CFB;
2750 break;
2751 case PSA_ALG_OFB:
2752 mode = MBEDTLS_MODE_OFB;
2753 break;
2754 case PSA_ALG_CBC_NO_PADDING:
2755 mode = MBEDTLS_MODE_CBC;
2756 break;
2757 case PSA_ALG_CBC_PKCS7:
2758 mode = MBEDTLS_MODE_CBC;
2759 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002760 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761 mode = MBEDTLS_MODE_CCM;
2762 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002763 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764 mode = MBEDTLS_MODE_GCM;
2765 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002766 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2767 mode = MBEDTLS_MODE_CHACHAPOLY;
2768 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769 default:
2770 return( NULL );
2771 }
2772 }
2773 else if( alg == PSA_ALG_CMAC )
2774 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002775 else
2776 return( NULL );
2777
2778 switch( key_type )
2779 {
2780 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002781 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782 break;
2783 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002784 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2785 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002787 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002789 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002790 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2791 * but two-key Triple-DES is functionally three-key Triple-DES
2792 * with K1=K3, so that's how we present it to mbedtls. */
2793 if( key_bits == 128 )
2794 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795 break;
2796 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002797 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798 break;
2799 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002800 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002802 case PSA_KEY_TYPE_CHACHA20:
2803 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2804 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002805 default:
2806 return( NULL );
2807 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002808 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002809 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810
Jaeden Amero23bbb752018-06-26 14:16:54 +01002811 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2812 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813}
2814
Gilles Peskinea05219c2018-11-16 16:02:56 +01002815#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002816static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002817{
Gilles Peskine2d277862018-06-18 15:41:12 +02002818 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002819 {
2820 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002821 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002822 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002823 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002824 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002825 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002826 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002827 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002828 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002829 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002830 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002831 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002832 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002833 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002834 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002835 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002836 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002837 return( 128 );
2838 default:
2839 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002840 }
2841}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002842#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002843
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002844/* Initialize the MAC operation structure. Once this function has been
2845 * called, psa_mac_abort can run and will do the right thing. */
2846static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2847 psa_algorithm_t alg )
2848{
2849 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2850
2851 operation->alg = alg;
2852 operation->key_set = 0;
2853 operation->iv_set = 0;
2854 operation->iv_required = 0;
2855 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002856 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002857
2858#if defined(MBEDTLS_CMAC_C)
2859 if( alg == PSA_ALG_CMAC )
2860 {
2861 operation->iv_required = 0;
2862 mbedtls_cipher_init( &operation->ctx.cmac );
2863 status = PSA_SUCCESS;
2864 }
2865 else
2866#endif /* MBEDTLS_CMAC_C */
2867#if defined(MBEDTLS_MD_C)
2868 if( PSA_ALG_IS_HMAC( operation->alg ) )
2869 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002870 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2871 operation->ctx.hmac.hash_ctx.alg = 0;
2872 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873 }
2874 else
2875#endif /* MBEDTLS_MD_C */
2876 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002877 if( ! PSA_ALG_IS_MAC( alg ) )
2878 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879 }
2880
2881 if( status != PSA_SUCCESS )
2882 memset( operation, 0, sizeof( *operation ) );
2883 return( status );
2884}
2885
Gilles Peskine01126fa2018-07-12 17:04:55 +02002886#if defined(MBEDTLS_MD_C)
2887static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2888{
Gilles Peskine3f108122018-12-07 18:14:53 +01002889 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002890 return( psa_hash_abort( &hmac->hash_ctx ) );
2891}
2892#endif /* MBEDTLS_MD_C */
2893
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2895{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002896 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002898 /* The object has (apparently) been initialized but it is not
2899 * in use. It's ok to call abort on such an object, and there's
2900 * nothing to do. */
2901 return( PSA_SUCCESS );
2902 }
2903 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002905 if( operation->alg == PSA_ALG_CMAC )
2906 {
2907 mbedtls_cipher_free( &operation->ctx.cmac );
2908 }
2909 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002910#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002912 if( PSA_ALG_IS_HMAC( operation->alg ) )
2913 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002914 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002915 }
2916 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002918 {
2919 /* Sanity check (shouldn't happen: operation->alg should
2920 * always have been initialized to a valid value). */
2921 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922 }
Moran Peker41deec42018-04-04 15:43:05 +03002923
Gilles Peskine8c9def32018-02-08 10:02:12 +01002924 operation->alg = 0;
2925 operation->key_set = 0;
2926 operation->iv_set = 0;
2927 operation->iv_required = 0;
2928 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002929 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002930
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002932
2933bad_state:
2934 /* If abort is called on an uninitialized object, we can't trust
2935 * anything. Wipe the object in case it contains confidential data.
2936 * This may result in a memory leak if a pointer gets overwritten,
2937 * but it's too late to do anything about this. */
2938 memset( operation, 0, sizeof( *operation ) );
2939 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940}
2941
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002942#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002943static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002944 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002945 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002946 const mbedtls_cipher_info_t *cipher_info )
2947{
Janos Follath24eed8d2019-11-22 13:21:35 +00002948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949
2950 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951
2952 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2953 if( ret != 0 )
2954 return( ret );
2955
2956 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002957 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958 key_bits );
2959 return( ret );
2960}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002961#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962
Gilles Peskine248051a2018-06-20 16:09:38 +02002963#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002964static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2965 const uint8_t *key,
2966 size_t key_length,
2967 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002968{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002969 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002970 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002971 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002972 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973 psa_status_t status;
2974
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002975 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2976 * overflow below. This should never trigger if the hash algorithm
2977 * is implemented correctly. */
2978 /* The size checks against the ipad and opad buffers cannot be written
2979 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2980 * because that triggers -Wlogical-op on GCC 7.3. */
2981 if( block_size > sizeof( ipad ) )
2982 return( PSA_ERROR_NOT_SUPPORTED );
2983 if( block_size > sizeof( hmac->opad ) )
2984 return( PSA_ERROR_NOT_SUPPORTED );
2985 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002986 return( PSA_ERROR_NOT_SUPPORTED );
2987
Gilles Peskined223b522018-06-11 18:12:58 +02002988 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002989 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002990 status = psa_hash_compute( hash_alg, key, key_length,
2991 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002992 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002993 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002994 }
Gilles Peskine96889972018-07-12 17:07:03 +02002995 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2996 * but it is permitted. It is common when HMAC is used in HKDF, for
2997 * example. Don't call `memcpy` in the 0-length because `key` could be
2998 * an invalid pointer which would make the behavior undefined. */
2999 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003000 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003001
Gilles Peskined223b522018-06-11 18:12:58 +02003002 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3003 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003004 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003005 ipad[i] ^= 0x36;
3006 memset( ipad + key_length, 0x36, block_size - key_length );
3007
3008 /* Copy the key material from ipad to opad, flipping the requisite bits,
3009 * and filling the rest of opad with the requisite constant. */
3010 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003011 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3012 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003013
Gilles Peskine01126fa2018-07-12 17:04:55 +02003014 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003015 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003016 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003017
Gilles Peskine01126fa2018-07-12 17:04:55 +02003018 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003019
3020cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003021 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003022
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003023 return( status );
3024}
Gilles Peskine248051a2018-06-20 16:09:38 +02003025#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003026
Gilles Peskine89167cb2018-07-08 20:12:23 +02003027static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003028 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003029 psa_algorithm_t alg,
3030 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003033 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003035 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003036 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003037 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003038 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003040 /* A context must be freshly initialized before it can be set up. */
3041 if( operation->alg != 0 )
3042 {
3043 return( PSA_ERROR_BAD_STATE );
3044 }
3045
Gilles Peskined911eb72018-08-14 15:18:45 +02003046 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003047 if( status != PSA_SUCCESS )
3048 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003049 if( is_sign )
3050 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051
Gilles Peskine28f8f302019-07-24 13:30:31 +02003052 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003053 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003054 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003055 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003056
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003058 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003059 {
3060 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003061 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003062 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 if( cipher_info == NULL )
3065 {
3066 status = PSA_ERROR_NOT_SUPPORTED;
3067 goto exit;
3068 }
3069 operation->mac_size = cipher_info->block_size;
3070 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3071 status = mbedtls_to_psa_error( ret );
3072 }
3073 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003076 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003077 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003078 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003079 if( hash_alg == 0 )
3080 {
3081 status = PSA_ERROR_NOT_SUPPORTED;
3082 goto exit;
3083 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003084
3085 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3086 /* Sanity check. This shouldn't fail on a valid configuration. */
3087 if( operation->mac_size == 0 ||
3088 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3089 {
3090 status = PSA_ERROR_NOT_SUPPORTED;
3091 goto exit;
3092 }
3093
Gilles Peskine8e338702019-07-30 20:06:31 +02003094 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003095 {
3096 status = PSA_ERROR_INVALID_ARGUMENT;
3097 goto exit;
3098 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003099
Gilles Peskine01126fa2018-07-12 17:04:55 +02003100 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003101 slot->data.key.data,
3102 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003103 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003104 }
3105 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003107 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003108 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003109 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003110 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003111
Gilles Peskined911eb72018-08-14 15:18:45 +02003112 if( truncated == 0 )
3113 {
3114 /* The "normal" case: untruncated algorithm. Nothing to do. */
3115 }
3116 else if( truncated < 4 )
3117 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003118 /* A very short MAC is too short for security since it can be
3119 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3120 * so we make this our minimum, even though 32 bits is still
3121 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003122 status = PSA_ERROR_NOT_SUPPORTED;
3123 }
3124 else if( truncated > operation->mac_size )
3125 {
3126 /* It's impossible to "truncate" to a larger length. */
3127 status = PSA_ERROR_INVALID_ARGUMENT;
3128 }
3129 else
3130 operation->mac_size = truncated;
3131
Gilles Peskinefbfac682018-07-08 20:51:54 +02003132exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003133 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003135 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003136 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003137 else
3138 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003139 operation->key_set = 1;
3140 }
3141 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142}
3143
Gilles Peskine89167cb2018-07-08 20:12:23 +02003144psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003145 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003146 psa_algorithm_t alg )
3147{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003148 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003149}
3150
3151psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003152 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003153 psa_algorithm_t alg )
3154{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003155 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003156}
3157
Gilles Peskine8c9def32018-02-08 10:02:12 +01003158psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3159 const uint8_t *input,
3160 size_t input_length )
3161{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003162 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003164 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003166 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003167 operation->has_input = 1;
3168
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003170 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003171 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003172 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3173 input, input_length );
3174 status = mbedtls_to_psa_error( ret );
3175 }
3176 else
3177#endif /* MBEDTLS_CMAC_C */
3178#if defined(MBEDTLS_MD_C)
3179 if( PSA_ALG_IS_HMAC( operation->alg ) )
3180 {
3181 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3182 input_length );
3183 }
3184 else
3185#endif /* MBEDTLS_MD_C */
3186 {
3187 /* This shouldn't happen if `operation` was initialized by
3188 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003189 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003190 }
3191
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192 if( status != PSA_SUCCESS )
3193 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003194 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003195}
3196
Gilles Peskine01126fa2018-07-12 17:04:55 +02003197#if defined(MBEDTLS_MD_C)
3198static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3199 uint8_t *mac,
3200 size_t mac_size )
3201{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003202 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003203 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3204 size_t hash_size = 0;
3205 size_t block_size = psa_get_hash_block_size( hash_alg );
3206 psa_status_t status;
3207
Gilles Peskine01126fa2018-07-12 17:04:55 +02003208 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3209 if( status != PSA_SUCCESS )
3210 return( status );
3211 /* From here on, tmp needs to be wiped. */
3212
3213 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3214 if( status != PSA_SUCCESS )
3215 goto exit;
3216
3217 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3218 if( status != PSA_SUCCESS )
3219 goto exit;
3220
3221 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3222 if( status != PSA_SUCCESS )
3223 goto exit;
3224
Gilles Peskined911eb72018-08-14 15:18:45 +02003225 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3226 if( status != PSA_SUCCESS )
3227 goto exit;
3228
3229 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003230
3231exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003232 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003233 return( status );
3234}
3235#endif /* MBEDTLS_MD_C */
3236
mohammad16036df908f2018-04-02 08:34:15 -07003237static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003238 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003239 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003240{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003241 if( ! operation->key_set )
3242 return( PSA_ERROR_BAD_STATE );
3243 if( operation->iv_required && ! operation->iv_set )
3244 return( PSA_ERROR_BAD_STATE );
3245
Gilles Peskine8c9def32018-02-08 10:02:12 +01003246 if( mac_size < operation->mac_size )
3247 return( PSA_ERROR_BUFFER_TOO_SMALL );
3248
Gilles Peskine8c9def32018-02-08 10:02:12 +01003249#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003250 if( operation->alg == PSA_ALG_CMAC )
3251 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003252 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3253 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3254 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003255 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003256 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003257 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003259 else
3260#endif /* MBEDTLS_CMAC_C */
3261#if defined(MBEDTLS_MD_C)
3262 if( PSA_ALG_IS_HMAC( operation->alg ) )
3263 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003264 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003265 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003266 }
3267 else
3268#endif /* MBEDTLS_MD_C */
3269 {
3270 /* This shouldn't happen if `operation` was initialized by
3271 * a setup function. */
3272 return( PSA_ERROR_BAD_STATE );
3273 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003274}
3275
Gilles Peskineacd4be32018-07-08 19:56:25 +02003276psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3277 uint8_t *mac,
3278 size_t mac_size,
3279 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003280{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003281 psa_status_t status;
3282
Jaeden Amero252ef282019-02-15 14:05:35 +00003283 if( operation->alg == 0 )
3284 {
3285 return( PSA_ERROR_BAD_STATE );
3286 }
3287
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003288 /* Fill the output buffer with something that isn't a valid mac
3289 * (barring an attack on the mac and deliberately-crafted input),
3290 * in case the caller doesn't check the return status properly. */
3291 *mac_length = mac_size;
3292 /* If mac_size is 0 then mac may be NULL and then the
3293 * call to memset would have undefined behavior. */
3294 if( mac_size != 0 )
3295 memset( mac, '!', mac_size );
3296
Gilles Peskine89167cb2018-07-08 20:12:23 +02003297 if( ! operation->is_sign )
3298 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003299 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003300 }
mohammad16036df908f2018-04-02 08:34:15 -07003301
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003302 status = psa_mac_finish_internal( operation, mac, mac_size );
3303
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003304 if( status == PSA_SUCCESS )
3305 {
3306 status = psa_mac_abort( operation );
3307 if( status == PSA_SUCCESS )
3308 *mac_length = operation->mac_size;
3309 else
3310 memset( mac, '!', mac_size );
3311 }
3312 else
3313 psa_mac_abort( operation );
3314 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003315}
3316
Gilles Peskineacd4be32018-07-08 19:56:25 +02003317psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3318 const uint8_t *mac,
3319 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003320{
Gilles Peskine828ed142018-06-18 23:25:51 +02003321 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003322 psa_status_t status;
3323
Jaeden Amero252ef282019-02-15 14:05:35 +00003324 if( operation->alg == 0 )
3325 {
3326 return( PSA_ERROR_BAD_STATE );
3327 }
3328
Gilles Peskine89167cb2018-07-08 20:12:23 +02003329 if( operation->is_sign )
3330 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003331 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003332 }
3333 if( operation->mac_size != mac_length )
3334 {
3335 status = PSA_ERROR_INVALID_SIGNATURE;
3336 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003337 }
mohammad16036df908f2018-04-02 08:34:15 -07003338
3339 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003340 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003341 if( status != PSA_SUCCESS )
3342 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003343
3344 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3345 status = PSA_ERROR_INVALID_SIGNATURE;
3346
3347cleanup:
3348 if( status == PSA_SUCCESS )
3349 status = psa_mac_abort( operation );
3350 else
3351 psa_mac_abort( operation );
3352
Gilles Peskine3f108122018-12-07 18:14:53 +01003353 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003354
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003355 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003356}
3357
3358
Gilles Peskine20035e32018-02-03 22:44:14 +01003359
Gilles Peskine20035e32018-02-03 22:44:14 +01003360/****************************************************************/
3361/* Asymmetric cryptography */
3362/****************************************************************/
3363
Gilles Peskine2b450e32018-06-27 15:42:46 +02003364#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003365/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003366 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003367static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3368 size_t hash_length,
3369 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003370{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003371 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003372 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003373 *md_alg = mbedtls_md_get_type( md_info );
3374
3375 /* The Mbed TLS RSA module uses an unsigned int for hash length
3376 * parameters. Validate that it fits so that we don't risk an
3377 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003378#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003379 if( hash_length > UINT_MAX )
3380 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003381#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003382
3383#if defined(MBEDTLS_PKCS1_V15)
3384 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3385 * must be correct. */
3386 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3387 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003388 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003389 if( md_info == NULL )
3390 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003391 if( mbedtls_md_get_size( md_info ) != hash_length )
3392 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003393 }
3394#endif /* MBEDTLS_PKCS1_V15 */
3395
3396#if defined(MBEDTLS_PKCS1_V21)
3397 /* PSS requires a hash internally. */
3398 if( PSA_ALG_IS_RSA_PSS( alg ) )
3399 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003400 if( md_info == NULL )
3401 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003402 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003403#endif /* MBEDTLS_PKCS1_V21 */
3404
Gilles Peskine61b91d42018-06-08 16:09:36 +02003405 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003406}
3407
Gilles Peskine2b450e32018-06-27 15:42:46 +02003408static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3409 psa_algorithm_t alg,
3410 const uint8_t *hash,
3411 size_t hash_length,
3412 uint8_t *signature,
3413 size_t signature_size,
3414 size_t *signature_length )
3415{
3416 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003418 mbedtls_md_type_t md_alg;
3419
3420 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3421 if( status != PSA_SUCCESS )
3422 return( status );
3423
Gilles Peskine630a18a2018-06-29 17:49:35 +02003424 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003425 return( PSA_ERROR_BUFFER_TOO_SMALL );
3426
3427#if defined(MBEDTLS_PKCS1_V15)
3428 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3429 {
3430 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3431 MBEDTLS_MD_NONE );
3432 ret = mbedtls_rsa_pkcs1_sign( rsa,
3433 mbedtls_ctr_drbg_random,
3434 &global_data.ctr_drbg,
3435 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003436 md_alg,
3437 (unsigned int) hash_length,
3438 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003439 signature );
3440 }
3441 else
3442#endif /* MBEDTLS_PKCS1_V15 */
3443#if defined(MBEDTLS_PKCS1_V21)
3444 if( PSA_ALG_IS_RSA_PSS( alg ) )
3445 {
3446 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3447 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3448 mbedtls_ctr_drbg_random,
3449 &global_data.ctr_drbg,
3450 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003451 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003452 (unsigned int) hash_length,
3453 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003454 signature );
3455 }
3456 else
3457#endif /* MBEDTLS_PKCS1_V21 */
3458 {
3459 return( PSA_ERROR_INVALID_ARGUMENT );
3460 }
3461
3462 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003463 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003464 return( mbedtls_to_psa_error( ret ) );
3465}
3466
3467static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3468 psa_algorithm_t alg,
3469 const uint8_t *hash,
3470 size_t hash_length,
3471 const uint8_t *signature,
3472 size_t signature_length )
3473{
3474 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003476 mbedtls_md_type_t md_alg;
3477
3478 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3479 if( status != PSA_SUCCESS )
3480 return( status );
3481
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003482 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3483 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003484
3485#if defined(MBEDTLS_PKCS1_V15)
3486 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3487 {
3488 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3489 MBEDTLS_MD_NONE );
3490 ret = mbedtls_rsa_pkcs1_verify( rsa,
3491 mbedtls_ctr_drbg_random,
3492 &global_data.ctr_drbg,
3493 MBEDTLS_RSA_PUBLIC,
3494 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003495 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003496 hash,
3497 signature );
3498 }
3499 else
3500#endif /* MBEDTLS_PKCS1_V15 */
3501#if defined(MBEDTLS_PKCS1_V21)
3502 if( PSA_ALG_IS_RSA_PSS( alg ) )
3503 {
3504 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3505 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3506 mbedtls_ctr_drbg_random,
3507 &global_data.ctr_drbg,
3508 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003509 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003510 (unsigned int) hash_length,
3511 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003512 signature );
3513 }
3514 else
3515#endif /* MBEDTLS_PKCS1_V21 */
3516 {
3517 return( PSA_ERROR_INVALID_ARGUMENT );
3518 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003519
3520 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3521 * the rest of the signature is invalid". This has little use in
3522 * practice and PSA doesn't report this distinction. */
3523 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3524 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003525 return( mbedtls_to_psa_error( ret ) );
3526}
3527#endif /* MBEDTLS_RSA_C */
3528
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003529#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003530/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3531 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3532 * (even though these functions don't modify it). */
3533static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3534 psa_algorithm_t alg,
3535 const uint8_t *hash,
3536 size_t hash_length,
3537 uint8_t *signature,
3538 size_t signature_size,
3539 size_t *signature_length )
3540{
Janos Follath24eed8d2019-11-22 13:21:35 +00003541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003542 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003543 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003544 mbedtls_mpi_init( &r );
3545 mbedtls_mpi_init( &s );
3546
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003547 if( signature_size < 2 * curve_bytes )
3548 {
3549 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3550 goto cleanup;
3551 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003552
Gilles Peskinea05219c2018-11-16 16:02:56 +01003553#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003554 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3555 {
3556 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3557 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3558 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003559 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3560 &ecp->d, hash,
3561 hash_length, md_alg,
3562 mbedtls_ctr_drbg_random,
3563 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003564 }
3565 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003566#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003567 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003568 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003569 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3570 hash, hash_length,
3571 mbedtls_ctr_drbg_random,
3572 &global_data.ctr_drbg ) );
3573 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003574
3575 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3576 signature,
3577 curve_bytes ) );
3578 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3579 signature + curve_bytes,
3580 curve_bytes ) );
3581
3582cleanup:
3583 mbedtls_mpi_free( &r );
3584 mbedtls_mpi_free( &s );
3585 if( ret == 0 )
3586 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003587 return( mbedtls_to_psa_error( ret ) );
3588}
3589
3590static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3591 const uint8_t *hash,
3592 size_t hash_length,
3593 const uint8_t *signature,
3594 size_t signature_length )
3595{
Janos Follath24eed8d2019-11-22 13:21:35 +00003596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003597 mbedtls_mpi r, s;
3598 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3599 mbedtls_mpi_init( &r );
3600 mbedtls_mpi_init( &s );
3601
3602 if( signature_length != 2 * curve_bytes )
3603 return( PSA_ERROR_INVALID_SIGNATURE );
3604
3605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3606 signature,
3607 curve_bytes ) );
3608 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3609 signature + curve_bytes,
3610 curve_bytes ) );
3611
Steven Cooremanacda8342020-07-24 23:09:52 +02003612 /* Check whether the public part is loaded. If not, load it. */
3613 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3614 {
3615 MBEDTLS_MPI_CHK(
3616 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3617 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3618 }
3619
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003620 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3621 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003622
3623cleanup:
3624 mbedtls_mpi_free( &r );
3625 mbedtls_mpi_free( &s );
3626 return( mbedtls_to_psa_error( ret ) );
3627}
3628#endif /* MBEDTLS_ECDSA_C */
3629
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003630psa_status_t psa_sign_hash( psa_key_handle_t handle,
3631 psa_algorithm_t alg,
3632 const uint8_t *hash,
3633 size_t hash_length,
3634 uint8_t *signature,
3635 size_t signature_size,
3636 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003637{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003638 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003639 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003640#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3641 const psa_drv_se_t *drv;
3642 psa_drv_se_context_t *drv_context;
3643#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003644
3645 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003646 /* Immediately reject a zero-length signature buffer. This guarantees
3647 * that signature must be a valid pointer. (On the other hand, the hash
3648 * buffer can in principle be empty since it doesn't actually have
3649 * to be a hash.) */
3650 if( signature_size == 0 )
3651 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003652
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003653 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003654 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003655 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003656 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003657 {
3658 status = PSA_ERROR_INVALID_ARGUMENT;
3659 goto exit;
3660 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003661
Gilles Peskineedc64242019-08-07 21:05:07 +02003662#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3663 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3664 {
3665 if( drv->asymmetric == NULL ||
3666 drv->asymmetric->p_sign == NULL )
3667 {
3668 status = PSA_ERROR_NOT_SUPPORTED;
3669 goto exit;
3670 }
3671 status = drv->asymmetric->p_sign( drv_context,
3672 slot->data.se.slot_number,
3673 alg,
3674 hash, hash_length,
3675 signature, signature_size,
3676 signature_length );
3677 }
3678 else
3679#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003680#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003681 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003682 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003683 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003684
Steven Cooreman4fed4552020-08-03 14:46:03 +02003685 status = psa_load_rsa_representation( slot->attr.type,
3686 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003687 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003688 &rsa );
3689 if( status != PSA_SUCCESS )
3690 goto exit;
3691
Steven Cooremana2371e52020-07-28 14:30:39 +02003692 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003693 alg,
3694 hash, hash_length,
3695 signature, signature_size,
3696 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003697
Steven Cooremana2371e52020-07-28 14:30:39 +02003698 mbedtls_rsa_free( rsa );
3699 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003700 }
3701 else
3702#endif /* defined(MBEDTLS_RSA_C) */
3703#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003704 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003705 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003706#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003707 if(
3708#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3709 PSA_ALG_IS_ECDSA( alg )
3710#else
3711 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3712#endif
3713 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003714 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003715 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003716 status = psa_load_ecp_representation( slot->attr.type,
3717 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003718 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003719 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003720 if( status != PSA_SUCCESS )
3721 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003722 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003723 alg,
3724 hash, hash_length,
3725 signature, signature_size,
3726 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003727 mbedtls_ecp_keypair_free( ecp );
3728 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003729 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003730 else
3731#endif /* defined(MBEDTLS_ECDSA_C) */
3732 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003733 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003734 }
itayzafrir5c753392018-05-08 11:18:38 +03003735 }
3736 else
3737#endif /* defined(MBEDTLS_ECP_C) */
3738 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003739 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003740 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003741
3742exit:
3743 /* Fill the unused part of the output buffer (the whole buffer on error,
3744 * the trailing part on success) with something that isn't a valid mac
3745 * (barring an attack on the mac and deliberately-crafted input),
3746 * in case the caller doesn't check the return status properly. */
3747 if( status == PSA_SUCCESS )
3748 memset( signature + *signature_length, '!',
3749 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003750 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003751 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003752 /* If signature_size is 0 then we have nothing to do. We must not call
3753 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003754 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003755}
3756
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003757psa_status_t psa_verify_hash( psa_key_handle_t handle,
3758 psa_algorithm_t alg,
3759 const uint8_t *hash,
3760 size_t hash_length,
3761 const uint8_t *signature,
3762 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003763{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003764 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003765 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003766#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3767 const psa_drv_se_t *drv;
3768 psa_drv_se_context_t *drv_context;
3769#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003770
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003771 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003772 if( status != PSA_SUCCESS )
3773 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003774
Gilles Peskineedc64242019-08-07 21:05:07 +02003775#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3776 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3777 {
3778 if( drv->asymmetric == NULL ||
3779 drv->asymmetric->p_verify == NULL )
3780 return( PSA_ERROR_NOT_SUPPORTED );
3781 return( drv->asymmetric->p_verify( drv_context,
3782 slot->data.se.slot_number,
3783 alg,
3784 hash, hash_length,
3785 signature, signature_length ) );
3786 }
3787 else
3788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003789#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003790 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003791 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003792 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003793
Steven Cooreman4fed4552020-08-03 14:46:03 +02003794 status = psa_load_rsa_representation( slot->attr.type,
3795 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003796 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003797 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003798 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003799 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003800
Steven Cooremana2371e52020-07-28 14:30:39 +02003801 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003802 alg,
3803 hash, hash_length,
3804 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003805 mbedtls_rsa_free( rsa );
3806 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003807 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808 }
3809 else
3810#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003811#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003812 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003813 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003814#if defined(MBEDTLS_ECDSA_C)
3815 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003816 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003817 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003818 status = psa_load_ecp_representation( slot->attr.type,
3819 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003820 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003821 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003822 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003823 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003824 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003825 hash, hash_length,
3826 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003827 mbedtls_ecp_keypair_free( ecp );
3828 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003829 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003830 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003831 else
3832#endif /* defined(MBEDTLS_ECDSA_C) */
3833 {
3834 return( PSA_ERROR_INVALID_ARGUMENT );
3835 }
itayzafrir5c753392018-05-08 11:18:38 +03003836 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003837 else
3838#endif /* defined(MBEDTLS_ECP_C) */
3839 {
3840 return( PSA_ERROR_NOT_SUPPORTED );
3841 }
3842}
3843
Gilles Peskine072ac562018-06-30 00:21:29 +02003844#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3845static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3846 mbedtls_rsa_context *rsa )
3847{
3848 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3849 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3850 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3851 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3852}
3853#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3854
Gilles Peskinec5487a82018-12-03 18:08:14 +01003855psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003856 psa_algorithm_t alg,
3857 const uint8_t *input,
3858 size_t input_length,
3859 const uint8_t *salt,
3860 size_t salt_length,
3861 uint8_t *output,
3862 size_t output_size,
3863 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003865 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003866 psa_status_t status;
3867
Darryl Green5cc689a2018-07-24 15:34:10 +01003868 (void) input;
3869 (void) input_length;
3870 (void) salt;
3871 (void) output;
3872 (void) output_size;
3873
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003874 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003876 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3877 return( PSA_ERROR_INVALID_ARGUMENT );
3878
Gilles Peskine28f8f302019-07-24 13:30:31 +02003879 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003880 if( status != PSA_SUCCESS )
3881 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003882 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3883 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885
3886#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003887 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003889 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003890 status = psa_load_rsa_representation( slot->attr.type,
3891 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003892 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003893 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003894 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003895 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003896
3897 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003898 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003899 status = PSA_ERROR_BUFFER_TOO_SMALL;
3900 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003901 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003903 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003904 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003905 status = mbedtls_to_psa_error(
3906 mbedtls_rsa_pkcs1_encrypt( rsa,
3907 mbedtls_ctr_drbg_random,
3908 &global_data.ctr_drbg,
3909 MBEDTLS_RSA_PUBLIC,
3910 input_length,
3911 input,
3912 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 }
3914 else
3915#endif /* MBEDTLS_PKCS1_V15 */
3916#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003917 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003919 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003920 status = mbedtls_to_psa_error(
3921 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3922 mbedtls_ctr_drbg_random,
3923 &global_data.ctr_drbg,
3924 MBEDTLS_RSA_PUBLIC,
3925 salt, salt_length,
3926 input_length,
3927 input,
3928 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 }
3930 else
3931#endif /* MBEDTLS_PKCS1_V21 */
3932 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003933 status = PSA_ERROR_INVALID_ARGUMENT;
3934 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003936rsa_exit:
3937 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003938 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003939
Steven Cooremana2371e52020-07-28 14:30:39 +02003940 mbedtls_rsa_free( rsa );
3941 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003942 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003945#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 {
3947 return( PSA_ERROR_NOT_SUPPORTED );
3948 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003949}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950
Gilles Peskinec5487a82018-12-03 18:08:14 +01003951psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003952 psa_algorithm_t alg,
3953 const uint8_t *input,
3954 size_t input_length,
3955 const uint8_t *salt,
3956 size_t salt_length,
3957 uint8_t *output,
3958 size_t output_size,
3959 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003961 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003962 psa_status_t status;
3963
Darryl Green5cc689a2018-07-24 15:34:10 +01003964 (void) input;
3965 (void) input_length;
3966 (void) salt;
3967 (void) output;
3968 (void) output_size;
3969
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003970 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003972 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3973 return( PSA_ERROR_INVALID_ARGUMENT );
3974
Gilles Peskine28f8f302019-07-24 13:30:31 +02003975 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003976 if( status != PSA_SUCCESS )
3977 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003978 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 return( PSA_ERROR_INVALID_ARGUMENT );
3980
3981#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003982 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003984 mbedtls_rsa_context *rsa = NULL;
3985 status = psa_load_rsa_representation( slot->attr.type,
3986 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003987 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003988 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003989 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003990 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991
Steven Cooremana2371e52020-07-28 14:30:39 +02003992 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003993 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003994 status = PSA_ERROR_INVALID_ARGUMENT;
3995 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003996 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997
3998#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003999 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004001 status = mbedtls_to_psa_error(
4002 mbedtls_rsa_pkcs1_decrypt( rsa,
4003 mbedtls_ctr_drbg_random,
4004 &global_data.ctr_drbg,
4005 MBEDTLS_RSA_PRIVATE,
4006 output_length,
4007 input,
4008 output,
4009 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010 }
4011 else
4012#endif /* MBEDTLS_PKCS1_V15 */
4013#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004014 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004016 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004017 status = mbedtls_to_psa_error(
4018 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4019 mbedtls_ctr_drbg_random,
4020 &global_data.ctr_drbg,
4021 MBEDTLS_RSA_PRIVATE,
4022 salt, salt_length,
4023 output_length,
4024 input,
4025 output,
4026 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 }
4028 else
4029#endif /* MBEDTLS_PKCS1_V21 */
4030 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004031 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004033
Steven Cooreman4fed4552020-08-03 14:46:03 +02004034rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004035 mbedtls_rsa_free( rsa );
4036 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004037 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004040#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041 {
4042 return( PSA_ERROR_NOT_SUPPORTED );
4043 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004044}
Gilles Peskine20035e32018-02-03 22:44:14 +01004045
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004046
4047
mohammad1603503973b2018-03-12 15:59:30 +02004048/****************************************************************/
4049/* Symmetric cryptography */
4050/****************************************************************/
4051
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004052/* Initialize the cipher operation structure. Once this function has been
4053 * called, psa_cipher_abort can run and will do the right thing. */
4054static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4055 psa_algorithm_t alg )
4056{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004057 if( ! PSA_ALG_IS_CIPHER( alg ) )
4058 {
4059 memset( operation, 0, sizeof( *operation ) );
4060 return( PSA_ERROR_INVALID_ARGUMENT );
4061 }
4062
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004063 operation->alg = alg;
4064 operation->key_set = 0;
4065 operation->iv_set = 0;
4066 operation->iv_required = 1;
4067 operation->iv_size = 0;
4068 operation->block_size = 0;
4069 mbedtls_cipher_init( &operation->ctx.cipher );
4070 return( PSA_SUCCESS );
4071}
4072
Gilles Peskinee553c652018-06-04 16:22:46 +02004073static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004074 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004075 psa_algorithm_t alg,
4076 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004077{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004078 int ret = 0;
4079 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004080 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004081 size_t key_bits;
4082 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004083 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4084 PSA_KEY_USAGE_ENCRYPT :
4085 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004086
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004087 /* A context must be freshly initialized before it can be set up. */
4088 if( operation->alg != 0 )
4089 {
4090 return( PSA_ERROR_BAD_STATE );
4091 }
4092
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004093 status = psa_cipher_init( operation, alg );
4094 if( status != PSA_SUCCESS )
4095 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004096
Gilles Peskine28f8f302019-07-24 13:30:31 +02004097 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004098 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004099 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004100 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004101
Gilles Peskine8e338702019-07-30 20:06:31 +02004102 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004103 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004104 {
4105 status = PSA_ERROR_NOT_SUPPORTED;
4106 goto exit;
4107 }
mohammad1603503973b2018-03-12 15:59:30 +02004108
mohammad1603503973b2018-03-12 15:59:30 +02004109 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004110 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004111 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004112
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004113#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004114 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004115 {
4116 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004117 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004118 memcpy( keys, slot->data.key.data, 16 );
4119 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004120 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4121 keys,
4122 192, cipher_operation );
4123 }
4124 else
4125#endif
4126 {
4127 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004128 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004129 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004130 }
Moran Peker41deec42018-04-04 15:43:05 +03004131 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004132 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004133
mohammad16038481e742018-03-18 13:57:31 +02004134#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004135 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004136 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004137 case PSA_ALG_CBC_NO_PADDING:
4138 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4139 MBEDTLS_PADDING_NONE );
4140 break;
4141 case PSA_ALG_CBC_PKCS7:
4142 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4143 MBEDTLS_PADDING_PKCS7 );
4144 break;
4145 default:
4146 /* The algorithm doesn't involve padding. */
4147 ret = 0;
4148 break;
4149 }
4150 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004151 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004152#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4153
mohammad1603503973b2018-03-12 15:59:30 +02004154 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004155 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004156 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004157 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004158 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004159 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004160 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004161#if defined(MBEDTLS_CHACHA20_C)
4162 else
4163 if( alg == PSA_ALG_CHACHA20 )
4164 operation->iv_size = 12;
4165#endif
mohammad1603503973b2018-03-12 15:59:30 +02004166
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004167exit:
4168 if( status == 0 )
4169 status = mbedtls_to_psa_error( ret );
4170 if( status != 0 )
4171 psa_cipher_abort( operation );
4172 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004173}
4174
Gilles Peskinefe119512018-07-08 21:39:34 +02004175psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004176 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004177 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004178{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004179 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004180}
4181
Gilles Peskinefe119512018-07-08 21:39:34 +02004182psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004183 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004184 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004185{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004186 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004187}
4188
Gilles Peskinefe119512018-07-08 21:39:34 +02004189psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004190 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004191 size_t iv_size,
4192 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004193{
itayzafrir534bd7c2018-08-02 13:56:32 +03004194 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004196 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004197 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004198 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004199 }
Moran Peker41deec42018-04-04 15:43:05 +03004200 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004201 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004202 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004203 goto exit;
4204 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004205 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4206 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004207 if( ret != 0 )
4208 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004209 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004210 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004211 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004212
mohammad16038481e742018-03-18 13:57:31 +02004213 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004214 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004215
Moran Peker395db872018-05-31 14:07:14 +03004216exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004217 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004218 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004219 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004220}
4221
Gilles Peskinefe119512018-07-08 21:39:34 +02004222psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004223 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004224 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004225{
itayzafrir534bd7c2018-08-02 13:56:32 +03004226 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004228 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004229 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004230 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004231 }
Moran Pekera28258c2018-05-29 16:25:04 +03004232 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004233 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004234 status = PSA_ERROR_INVALID_ARGUMENT;
4235 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004236 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004237 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4238 status = mbedtls_to_psa_error( ret );
4239exit:
4240 if( status == PSA_SUCCESS )
4241 operation->iv_set = 1;
4242 else
mohammad1603503973b2018-03-12 15:59:30 +02004243 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004245}
4246
Gilles Peskinee553c652018-06-04 16:22:46 +02004247psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4248 const uint8_t *input,
4249 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004250 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004251 size_t output_size,
4252 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004253{
itayzafrir534bd7c2018-08-02 13:56:32 +03004254 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004256 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004257
4258 if( operation->alg == 0 )
4259 {
4260 return( PSA_ERROR_BAD_STATE );
4261 }
4262
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004263 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004264 {
4265 /* Take the unprocessed partial block left over from previous
4266 * update calls, if any, plus the input to this call. Remove
4267 * the last partial block, if any. You get the data that will be
4268 * output in this call. */
4269 expected_output_size =
4270 ( operation->ctx.cipher.unprocessed_len + input_length )
4271 / operation->block_size * operation->block_size;
4272 }
4273 else
4274 {
4275 expected_output_size = input_length;
4276 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004277
Gilles Peskine89d789c2018-06-04 17:17:16 +02004278 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004279 {
4280 status = PSA_ERROR_BUFFER_TOO_SMALL;
4281 goto exit;
4282 }
mohammad160382759612018-03-12 18:16:40 +02004283
mohammad1603503973b2018-03-12 15:59:30 +02004284 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004285 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004286 status = mbedtls_to_psa_error( ret );
4287exit:
4288 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004289 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004290 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004291}
4292
Gilles Peskinee553c652018-06-04 16:22:46 +02004293psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4294 uint8_t *output,
4295 size_t output_size,
4296 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004297{
David Saadab4ecc272019-02-14 13:48:10 +02004298 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004299 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004300 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004301
mohammad1603503973b2018-03-12 15:59:30 +02004302 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004303 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004304 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004305 }
4306 if( operation->iv_required && ! operation->iv_set )
4307 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004308 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004309 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004310
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004311 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004312 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4313 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004314 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004315 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004316 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004317 }
4318
Janos Follath315b51c2018-07-09 16:04:51 +01004319 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4320 temp_output_buffer,
4321 output_length );
4322 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004323 {
Janos Follath315b51c2018-07-09 16:04:51 +01004324 status = mbedtls_to_psa_error( cipher_ret );
4325 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004326 }
Janos Follath315b51c2018-07-09 16:04:51 +01004327
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004328 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004329 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004330 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004331 memcpy( output, temp_output_buffer, *output_length );
4332 else
4333 {
Janos Follath315b51c2018-07-09 16:04:51 +01004334 status = PSA_ERROR_BUFFER_TOO_SMALL;
4335 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004336 }
mohammad1603503973b2018-03-12 15:59:30 +02004337
Gilles Peskine3f108122018-12-07 18:14:53 +01004338 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004339 status = psa_cipher_abort( operation );
4340
4341 return( status );
4342
4343error:
4344
4345 *output_length = 0;
4346
Gilles Peskine3f108122018-12-07 18:14:53 +01004347 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004348 (void) psa_cipher_abort( operation );
4349
4350 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004351}
4352
Gilles Peskinee553c652018-06-04 16:22:46 +02004353psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4354{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004355 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004356 {
4357 /* The object has (apparently) been initialized but it is not
4358 * in use. It's ok to call abort on such an object, and there's
4359 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004360 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004361 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004362
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004363 /* Sanity check (shouldn't happen: operation->alg should
4364 * always have been initialized to a valid value). */
4365 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4366 return( PSA_ERROR_BAD_STATE );
4367
mohammad1603503973b2018-03-12 15:59:30 +02004368 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004369
Moran Peker41deec42018-04-04 15:43:05 +03004370 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004371 operation->key_set = 0;
4372 operation->iv_set = 0;
4373 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004374 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004375 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004376
Moran Peker395db872018-05-31 14:07:14 +03004377 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004378}
4379
Gilles Peskinea0655c32018-04-30 17:06:50 +02004380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004381
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004382
mohammad16035955c982018-04-26 00:53:03 +03004383/****************************************************************/
4384/* AEAD */
4385/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004386
Gilles Peskineedf9a652018-08-17 18:11:56 +02004387typedef struct
4388{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004389 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004390 const mbedtls_cipher_info_t *cipher_info;
4391 union
4392 {
4393#if defined(MBEDTLS_CCM_C)
4394 mbedtls_ccm_context ccm;
4395#endif /* MBEDTLS_CCM_C */
4396#if defined(MBEDTLS_GCM_C)
4397 mbedtls_gcm_context gcm;
4398#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004399#if defined(MBEDTLS_CHACHAPOLY_C)
4400 mbedtls_chachapoly_context chachapoly;
4401#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004402 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004403 psa_algorithm_t core_alg;
4404 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405 uint8_t tag_length;
4406} aead_operation_t;
4407
Gilles Peskine30a9e412019-01-14 18:36:12 +01004408static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004409{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004410 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004411 {
4412#if defined(MBEDTLS_CCM_C)
4413 case PSA_ALG_CCM:
4414 mbedtls_ccm_free( &operation->ctx.ccm );
4415 break;
4416#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004417#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004418 case PSA_ALG_GCM:
4419 mbedtls_gcm_free( &operation->ctx.gcm );
4420 break;
4421#endif /* MBEDTLS_GCM_C */
4422 }
4423}
4424
4425static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004426 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004427 psa_key_usage_t usage,
4428 psa_algorithm_t alg )
4429{
4430 psa_status_t status;
4431 size_t key_bits;
4432 mbedtls_cipher_id_t cipher_id;
4433
Gilles Peskine28f8f302019-07-24 13:30:31 +02004434 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004435 if( status != PSA_SUCCESS )
4436 return( status );
4437
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004438 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004439
4440 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004441 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004442 &cipher_id );
4443 if( operation->cipher_info == NULL )
4444 return( PSA_ERROR_NOT_SUPPORTED );
4445
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004446 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004447 {
4448#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004449 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4450 operation->core_alg = PSA_ALG_CCM;
4451 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004452 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4453 * The call to mbedtls_ccm_encrypt_and_tag or
4454 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004455 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004456 return( PSA_ERROR_INVALID_ARGUMENT );
4457 mbedtls_ccm_init( &operation->ctx.ccm );
4458 status = mbedtls_to_psa_error(
4459 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004460 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004461 (unsigned int) key_bits ) );
4462 if( status != 0 )
4463 goto cleanup;
4464 break;
4465#endif /* MBEDTLS_CCM_C */
4466
4467#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004468 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4469 operation->core_alg = PSA_ALG_GCM;
4470 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004471 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4472 * The call to mbedtls_gcm_crypt_and_tag or
4473 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004474 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004475 return( PSA_ERROR_INVALID_ARGUMENT );
4476 mbedtls_gcm_init( &operation->ctx.gcm );
4477 status = mbedtls_to_psa_error(
4478 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004479 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004480 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004481 if( status != 0 )
4482 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004483 break;
4484#endif /* MBEDTLS_GCM_C */
4485
Gilles Peskine26869f22019-05-06 15:25:00 +02004486#if defined(MBEDTLS_CHACHAPOLY_C)
4487 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4488 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4489 operation->full_tag_length = 16;
4490 /* We only support the default tag length. */
4491 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4492 return( PSA_ERROR_NOT_SUPPORTED );
4493 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4494 status = mbedtls_to_psa_error(
4495 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004496 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004497 if( status != 0 )
4498 goto cleanup;
4499 break;
4500#endif /* MBEDTLS_CHACHAPOLY_C */
4501
Gilles Peskineedf9a652018-08-17 18:11:56 +02004502 default:
4503 return( PSA_ERROR_NOT_SUPPORTED );
4504 }
4505
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004506 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4507 {
4508 status = PSA_ERROR_INVALID_ARGUMENT;
4509 goto cleanup;
4510 }
4511 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004512
Gilles Peskineedf9a652018-08-17 18:11:56 +02004513 return( PSA_SUCCESS );
4514
4515cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004516 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004517 return( status );
4518}
4519
Gilles Peskinec5487a82018-12-03 18:08:14 +01004520psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004521 psa_algorithm_t alg,
4522 const uint8_t *nonce,
4523 size_t nonce_length,
4524 const uint8_t *additional_data,
4525 size_t additional_data_length,
4526 const uint8_t *plaintext,
4527 size_t plaintext_length,
4528 uint8_t *ciphertext,
4529 size_t ciphertext_size,
4530 size_t *ciphertext_length )
4531{
mohammad16035955c982018-04-26 00:53:03 +03004532 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004533 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004534 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004535
mohammad1603f08a5502018-06-03 15:05:47 +03004536 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004537
Gilles Peskinec5487a82018-12-03 18:08:14 +01004538 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004539 if( status != PSA_SUCCESS )
4540 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004541
Gilles Peskineedf9a652018-08-17 18:11:56 +02004542 /* For all currently supported modes, the tag is at the end of the
4543 * ciphertext. */
4544 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4545 {
4546 status = PSA_ERROR_BUFFER_TOO_SMALL;
4547 goto exit;
4548 }
4549 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004550
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004551#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004552 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004553 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004554 status = mbedtls_to_psa_error(
4555 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4556 MBEDTLS_GCM_ENCRYPT,
4557 plaintext_length,
4558 nonce, nonce_length,
4559 additional_data, additional_data_length,
4560 plaintext, ciphertext,
4561 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004562 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004563 else
4564#endif /* MBEDTLS_GCM_C */
4565#if defined(MBEDTLS_CCM_C)
4566 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004567 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004568 status = mbedtls_to_psa_error(
4569 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4570 plaintext_length,
4571 nonce, nonce_length,
4572 additional_data,
4573 additional_data_length,
4574 plaintext, ciphertext,
4575 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004576 }
mohammad16035c8845f2018-05-09 05:40:09 -07004577 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004578#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004579#if defined(MBEDTLS_CHACHAPOLY_C)
4580 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4581 {
4582 if( nonce_length != 12 || operation.tag_length != 16 )
4583 {
4584 status = PSA_ERROR_NOT_SUPPORTED;
4585 goto exit;
4586 }
4587 status = mbedtls_to_psa_error(
4588 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4589 plaintext_length,
4590 nonce,
4591 additional_data,
4592 additional_data_length,
4593 plaintext,
4594 ciphertext,
4595 tag ) );
4596 }
4597 else
4598#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004599 {
mohammad1603554faad2018-06-03 15:07:38 +03004600 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004601 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004602
Gilles Peskineedf9a652018-08-17 18:11:56 +02004603 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4604 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004605
Gilles Peskineedf9a652018-08-17 18:11:56 +02004606exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004607 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004608 if( status == PSA_SUCCESS )
4609 *ciphertext_length = plaintext_length + operation.tag_length;
4610 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004611}
4612
Gilles Peskineee652a32018-06-01 19:23:52 +02004613/* Locate the tag in a ciphertext buffer containing the encrypted data
4614 * followed by the tag. Return the length of the part preceding the tag in
4615 * *plaintext_length. This is the size of the plaintext in modes where
4616 * the encrypted data has the same size as the plaintext, such as
4617 * CCM and GCM. */
4618static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4619 const uint8_t *ciphertext,
4620 size_t ciphertext_length,
4621 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004622 const uint8_t **p_tag )
4623{
4624 size_t payload_length;
4625 if( tag_length > ciphertext_length )
4626 return( PSA_ERROR_INVALID_ARGUMENT );
4627 payload_length = ciphertext_length - tag_length;
4628 if( payload_length > plaintext_size )
4629 return( PSA_ERROR_BUFFER_TOO_SMALL );
4630 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004631 return( PSA_SUCCESS );
4632}
4633
Gilles Peskinec5487a82018-12-03 18:08:14 +01004634psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004635 psa_algorithm_t alg,
4636 const uint8_t *nonce,
4637 size_t nonce_length,
4638 const uint8_t *additional_data,
4639 size_t additional_data_length,
4640 const uint8_t *ciphertext,
4641 size_t ciphertext_length,
4642 uint8_t *plaintext,
4643 size_t plaintext_size,
4644 size_t *plaintext_length )
4645{
mohammad16035955c982018-04-26 00:53:03 +03004646 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004647 aead_operation_t operation;
4648 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004649
Gilles Peskineee652a32018-06-01 19:23:52 +02004650 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004651
Gilles Peskinec5487a82018-12-03 18:08:14 +01004652 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004653 if( status != PSA_SUCCESS )
4654 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004655
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004656 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4657 ciphertext, ciphertext_length,
4658 plaintext_size, &tag );
4659 if( status != PSA_SUCCESS )
4660 goto exit;
4661
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004662#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004663 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004664 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004665 status = mbedtls_to_psa_error(
4666 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4667 ciphertext_length - operation.tag_length,
4668 nonce, nonce_length,
4669 additional_data,
4670 additional_data_length,
4671 tag, operation.tag_length,
4672 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004673 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004674 else
4675#endif /* MBEDTLS_GCM_C */
4676#if defined(MBEDTLS_CCM_C)
4677 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004678 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679 status = mbedtls_to_psa_error(
4680 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4681 ciphertext_length - operation.tag_length,
4682 nonce, nonce_length,
4683 additional_data,
4684 additional_data_length,
4685 ciphertext, plaintext,
4686 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004687 }
mohammad160339574652018-06-01 04:39:53 -07004688 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004689#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004690#if defined(MBEDTLS_CHACHAPOLY_C)
4691 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4692 {
4693 if( nonce_length != 12 || operation.tag_length != 16 )
4694 {
4695 status = PSA_ERROR_NOT_SUPPORTED;
4696 goto exit;
4697 }
4698 status = mbedtls_to_psa_error(
4699 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4700 ciphertext_length - operation.tag_length,
4701 nonce,
4702 additional_data,
4703 additional_data_length,
4704 tag,
4705 ciphertext,
4706 plaintext ) );
4707 }
4708 else
4709#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004710 {
mohammad1603554faad2018-06-03 15:07:38 +03004711 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004712 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004713
Gilles Peskineedf9a652018-08-17 18:11:56 +02004714 if( status != PSA_SUCCESS && plaintext_size != 0 )
4715 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004716
Gilles Peskineedf9a652018-08-17 18:11:56 +02004717exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004718 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004719 if( status == PSA_SUCCESS )
4720 *plaintext_length = ciphertext_length - operation.tag_length;
4721 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004722}
4723
Gilles Peskinea0655c32018-04-30 17:06:50 +02004724
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004725
Gilles Peskine20035e32018-02-03 22:44:14 +01004726/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727/* Generators */
4728/****************************************************************/
4729
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004730#define HKDF_STATE_INIT 0 /* no input yet */
4731#define HKDF_STATE_STARTED 1 /* got salt */
4732#define HKDF_STATE_KEYED 2 /* got key */
4733#define HKDF_STATE_OUTPUT 3 /* output started */
4734
Gilles Peskinecbe66502019-05-16 16:59:18 +02004735static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004737{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4739 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004740 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004742}
4743
4744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004746{
4747 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 {
4751 /* The object has (apparently) been initialized but it is not
4752 * in use. It's ok to call abort on such an object, and there's
4753 * nothing to do. */
4754 }
4755 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004756#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004758 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 mbedtls_free( operation->ctx.hkdf.info );
4760 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004761 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004762 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004764 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004765 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004766 if( operation->ctx.tls12_prf.seed != NULL )
4767 {
4768 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4769 operation->ctx.tls12_prf.seed_length );
4770 mbedtls_free( operation->ctx.tls12_prf.seed );
4771 }
4772
4773 if( operation->ctx.tls12_prf.label != NULL )
4774 {
4775 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4776 operation->ctx.tls12_prf.label_length );
4777 mbedtls_free( operation->ctx.tls12_prf.label );
4778 }
4779
4780 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4781
4782 /* We leave the fields Ai and output_block to be erased safely by the
4783 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004784 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004785 else
4786#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004787 {
4788 status = PSA_ERROR_BAD_STATE;
4789 }
Janos Follath083036a2019-06-11 10:22:26 +01004790 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004791 return( status );
4792}
4793
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004795 size_t *capacity)
4796{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004797 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004798 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004800 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004801 }
4802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004804 return( PSA_SUCCESS );
4805}
4806
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004808 size_t capacity )
4809{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004811 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004813 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 return( PSA_SUCCESS );
4816}
4817
Gilles Peskinebef7f142018-07-12 17:22:21 +02004818#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004820 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004821static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004822 psa_algorithm_t hash_alg,
4823 uint8_t *output,
4824 size_t output_length )
4825{
4826 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4827 psa_status_t status;
4828
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4830 return( PSA_ERROR_BAD_STATE );
4831 hkdf->state = HKDF_STATE_OUTPUT;
4832
Gilles Peskinebef7f142018-07-12 17:22:21 +02004833 while( output_length != 0 )
4834 {
4835 /* Copy what remains of the current block */
4836 uint8_t n = hash_length - hkdf->offset_in_block;
4837 if( n > output_length )
4838 n = (uint8_t) output_length;
4839 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4840 output += n;
4841 output_length -= n;
4842 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004843 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004844 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004845 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004846 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004848 * object was corrupted or if this function is called directly
4849 * inside the library. */
4850 if( hkdf->block_number == 0xff )
4851 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004852
4853 /* We need a new block */
4854 ++hkdf->block_number;
4855 hkdf->offset_in_block = 0;
4856 status = psa_hmac_setup_internal( &hkdf->hmac,
4857 hkdf->prk, hash_length,
4858 hash_alg );
4859 if( status != PSA_SUCCESS )
4860 return( status );
4861 if( hkdf->block_number != 1 )
4862 {
4863 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4864 hkdf->output_block,
4865 hash_length );
4866 if( status != PSA_SUCCESS )
4867 return( status );
4868 }
4869 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4870 hkdf->info,
4871 hkdf->info_length );
4872 if( status != PSA_SUCCESS )
4873 return( status );
4874 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4875 &hkdf->block_number, 1 );
4876 if( status != PSA_SUCCESS )
4877 return( status );
4878 status = psa_hmac_finish_internal( &hkdf->hmac,
4879 hkdf->output_block,
4880 sizeof( hkdf->output_block ) );
4881 if( status != PSA_SUCCESS )
4882 return( status );
4883 }
4884
4885 return( PSA_SUCCESS );
4886}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004887
Janos Follath7742fee2019-06-17 12:58:10 +01004888static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4889 psa_tls12_prf_key_derivation_t *tls12_prf,
4890 psa_algorithm_t alg )
4891{
4892 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4893 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004894 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4895 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004896
Janos Follath7742fee2019-06-17 12:58:10 +01004897 /* We can't be wanting more output after block 0xff, otherwise
4898 * the capacity check in psa_key_derivation_output_bytes() would have
4899 * prevented this call. It could happen only if the operation
4900 * object was corrupted or if this function is called directly
4901 * inside the library. */
4902 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004903 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004904
4905 /* We need a new block */
4906 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004907 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004908
4909 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4910 *
4911 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4912 *
4913 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4914 * HMAC_hash(secret, A(2) + seed) +
4915 * HMAC_hash(secret, A(3) + seed) + ...
4916 *
4917 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004918 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004919 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004920 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004921 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004922 * is currently extracted as `output_block` and where i is
4923 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004924 */
4925
Janos Follathea29bfb2019-06-19 12:21:20 +01004926 /* Save the hash context before using it, to preserve the hash state with
4927 * only the inner padding in it. We need this, because inner padding depends
4928 * on the key (secret in the RFC's terminology). */
4929 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4930 if( status != PSA_SUCCESS )
4931 goto cleanup;
4932
4933 /* Calculate A(i) where i = tls12_prf->block_number. */
4934 if( tls12_prf->block_number == 1 )
4935 {
4936 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4937 * the variable seed and in this instance means it in the context of the
4938 * P_hash function, where seed = label + seed.) */
4939 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4940 tls12_prf->label, tls12_prf->label_length );
4941 if( status != PSA_SUCCESS )
4942 goto cleanup;
4943 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4944 tls12_prf->seed, tls12_prf->seed_length );
4945 if( status != PSA_SUCCESS )
4946 goto cleanup;
4947 }
4948 else
4949 {
4950 /* A(i) = HMAC_hash(secret, A(i-1)) */
4951 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4952 tls12_prf->Ai, hash_length );
4953 if( status != PSA_SUCCESS )
4954 goto cleanup;
4955 }
4956
4957 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4958 tls12_prf->Ai, hash_length );
4959 if( status != PSA_SUCCESS )
4960 goto cleanup;
4961 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4962 if( status != PSA_SUCCESS )
4963 goto cleanup;
4964
4965 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4966 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4967 tls12_prf->Ai, hash_length );
4968 if( status != PSA_SUCCESS )
4969 goto cleanup;
4970 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4971 tls12_prf->label, tls12_prf->label_length );
4972 if( status != PSA_SUCCESS )
4973 goto cleanup;
4974 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4975 tls12_prf->seed, tls12_prf->seed_length );
4976 if( status != PSA_SUCCESS )
4977 goto cleanup;
4978 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4979 tls12_prf->output_block, hash_length );
4980 if( status != PSA_SUCCESS )
4981 goto cleanup;
4982 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4983 if( status != PSA_SUCCESS )
4984 goto cleanup;
4985
Janos Follath7742fee2019-06-17 12:58:10 +01004986
4987cleanup:
4988
Janos Follathea29bfb2019-06-19 12:21:20 +01004989 cleanup_status = psa_hash_abort( &backup );
4990 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4991 status = cleanup_status;
4992
4993 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004994}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004995
Janos Follath844eb0e2019-06-19 12:10:49 +01004996static psa_status_t psa_key_derivation_tls12_prf_read(
4997 psa_tls12_prf_key_derivation_t *tls12_prf,
4998 psa_algorithm_t alg,
4999 uint8_t *output,
5000 size_t output_length )
5001{
5002 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5003 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5004 psa_status_t status;
5005 uint8_t offset, length;
5006
5007 while( output_length != 0 )
5008 {
5009 /* Check if we have fully processed the current block. */
5010 if( tls12_prf->left_in_block == 0 )
5011 {
5012 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5013 alg );
5014 if( status != PSA_SUCCESS )
5015 return( status );
5016
5017 continue;
5018 }
5019
5020 if( tls12_prf->left_in_block > output_length )
5021 length = (uint8_t) output_length;
5022 else
5023 length = tls12_prf->left_in_block;
5024
5025 offset = hash_length - tls12_prf->left_in_block;
5026 memcpy( output, tls12_prf->output_block + offset, length );
5027 output += length;
5028 output_length -= length;
5029 tls12_prf->left_in_block -= length;
5030 }
5031
5032 return( PSA_SUCCESS );
5033}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005034#endif /* MBEDTLS_MD_C */
5035
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005036psa_status_t psa_key_derivation_output_bytes(
5037 psa_key_derivation_operation_t *operation,
5038 uint8_t *output,
5039 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005040{
5041 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005043
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005045 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005047 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005048 }
5049
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005051 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005052 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005053 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005055 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005056 goto exit;
5057 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005059 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005061 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005062 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5063 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005065 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005066 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005067 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005069
Gilles Peskinebef7f142018-07-12 17:22:21 +02005070#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005071 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005072 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005073 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005075 output, output_length );
5076 }
Janos Follathadbec812019-06-14 11:05:39 +01005077 else
Janos Follathadbec812019-06-14 11:05:39 +01005078 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005079 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005080 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005082 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005083 output_length );
5084 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005085 else
5086#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005087 {
5088 return( PSA_ERROR_BAD_STATE );
5089 }
5090
5091exit:
5092 if( status != PSA_SUCCESS )
5093 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005094 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 * This allows us to differentiate between exhausted operations and
5096 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5097 * operations. */
5098 psa_algorithm_t alg = operation->alg;
5099 psa_key_derivation_abort( operation );
5100 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101 memset( output, '!', output_length );
5102 }
5103 return( status );
5104}
5105
Gilles Peskine08542d82018-07-19 17:05:42 +02005106#if defined(MBEDTLS_DES_C)
5107static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5108{
5109 if( data_size >= 8 )
5110 mbedtls_des_key_set_parity( data );
5111 if( data_size >= 16 )
5112 mbedtls_des_key_set_parity( data + 8 );
5113 if( data_size >= 24 )
5114 mbedtls_des_key_set_parity( data + 16 );
5115}
5116#endif /* MBEDTLS_DES_C */
5117
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005118static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005119 psa_key_slot_t *slot,
5120 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005122{
5123 uint8_t *data = NULL;
5124 size_t bytes = PSA_BITS_TO_BYTES( bits );
5125 psa_status_t status;
5126
Gilles Peskine8e338702019-07-30 20:06:31 +02005127 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005128 return( PSA_ERROR_INVALID_ARGUMENT );
5129 if( bits % 8 != 0 )
5130 return( PSA_ERROR_INVALID_ARGUMENT );
5131 data = mbedtls_calloc( 1, bytes );
5132 if( data == NULL )
5133 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136 if( status != PSA_SUCCESS )
5137 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005138#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005139 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005140 psa_des_set_key_parity( data, bytes );
5141#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005142 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143
5144exit:
5145 mbedtls_free( data );
5146 return( status );
5147}
5148
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005149psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005151 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005152{
5153 psa_status_t status;
5154 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005155 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005156
5157 /* Reject any attempt to create a zero-length key so that we don't
5158 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5159 if( psa_get_key_bits( attributes ) == 0 )
5160 return( PSA_ERROR_INVALID_ARGUMENT );
5161
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005162 if( ! operation->can_output_key )
5163 return( PSA_ERROR_NOT_PERMITTED );
5164
Gilles Peskinedf179142019-07-15 22:02:14 +02005165 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5166 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005167#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5168 if( driver != NULL )
5169 {
5170 /* Deriving a key in a secure element is not implemented yet. */
5171 status = PSA_ERROR_NOT_SUPPORTED;
5172 }
5173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005174 if( status == PSA_SUCCESS )
5175 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005176 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005177 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005179 }
5180 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005181 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005182 if( status != PSA_SUCCESS )
5183 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005184 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005185 *handle = 0;
5186 }
5187 return( status );
5188}
5189
Gilles Peskineeab56e42018-07-12 17:12:33 +02005190
5191
5192/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005193/* Key derivation */
5194/****************************************************************/
5195
Gilles Peskine969c5d62019-01-16 15:53:06 +01005196static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005198 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199{
Janos Follath5fe19732019-06-20 15:09:30 +01005200 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5201 * initialisers for this union leave some bytes unspecified.) */
5202 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5203
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005206 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5207 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5208 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5212 if( hash_size == 0 )
5213 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5215 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005216 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217 {
5218 return( PSA_ERROR_NOT_SUPPORTED );
5219 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005221 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222 }
5223#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005224 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005226}
5227
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005228psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005229 psa_algorithm_t alg )
5230{
5231 psa_status_t status;
5232
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005234 return( PSA_ERROR_BAD_STATE );
5235
Gilles Peskine6843c292019-01-18 16:44:49 +01005236 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5237 return( PSA_ERROR_INVALID_ARGUMENT );
5238 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005239 {
5240 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005242 }
5243 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5244 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005246 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247 else
5248 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005249
5250 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253}
5254
5255#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005256static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 psa_algorithm_t hash_alg,
5258 psa_key_derivation_step_t step,
5259 const uint8_t *data,
5260 size_t data_length )
5261{
5262 psa_status_t status;
5263 switch( step )
5264 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005265 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005266 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005267 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005268 status = psa_hmac_setup_internal( &hkdf->hmac,
5269 data, data_length,
5270 hash_alg );
5271 if( status != PSA_SUCCESS )
5272 return( status );
5273 hkdf->state = HKDF_STATE_STARTED;
5274 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005275 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276 /* If no salt was provided, use an empty salt. */
5277 if( hkdf->state == HKDF_STATE_INIT )
5278 {
5279 status = psa_hmac_setup_internal( &hkdf->hmac,
5280 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005281 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005282 if( status != PSA_SUCCESS )
5283 return( status );
5284 hkdf->state = HKDF_STATE_STARTED;
5285 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005286 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005288 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5289 data, data_length );
5290 if( status != PSA_SUCCESS )
5291 return( status );
5292 status = psa_hmac_finish_internal( &hkdf->hmac,
5293 hkdf->prk,
5294 sizeof( hkdf->prk ) );
5295 if( status != PSA_SUCCESS )
5296 return( status );
5297 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5298 hkdf->block_number = 0;
5299 hkdf->state = HKDF_STATE_KEYED;
5300 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005301 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005302 if( hkdf->state == HKDF_STATE_OUTPUT )
5303 return( PSA_ERROR_BAD_STATE );
5304 if( hkdf->info_set )
5305 return( PSA_ERROR_BAD_STATE );
5306 hkdf->info_length = data_length;
5307 if( data_length != 0 )
5308 {
5309 hkdf->info = mbedtls_calloc( 1, data_length );
5310 if( hkdf->info == NULL )
5311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5312 memcpy( hkdf->info, data, data_length );
5313 }
5314 hkdf->info_set = 1;
5315 return( PSA_SUCCESS );
5316 default:
5317 return( PSA_ERROR_INVALID_ARGUMENT );
5318 }
5319}
Janos Follathb03233e2019-06-11 15:30:30 +01005320
Janos Follathf08e2652019-06-13 09:05:41 +01005321static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5322 const uint8_t *data,
5323 size_t data_length )
5324{
5325 if( prf->state != TLS12_PRF_STATE_INIT )
5326 return( PSA_ERROR_BAD_STATE );
5327
Janos Follathd6dce9f2019-07-04 09:11:38 +01005328 if( data_length != 0 )
5329 {
5330 prf->seed = mbedtls_calloc( 1, data_length );
5331 if( prf->seed == NULL )
5332 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005333
Janos Follathd6dce9f2019-07-04 09:11:38 +01005334 memcpy( prf->seed, data, data_length );
5335 prf->seed_length = data_length;
5336 }
Janos Follathf08e2652019-06-13 09:05:41 +01005337
5338 prf->state = TLS12_PRF_STATE_SEED_SET;
5339
5340 return( PSA_SUCCESS );
5341}
5342
Janos Follath81550542019-06-13 14:26:34 +01005343static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5344 psa_algorithm_t hash_alg,
5345 const uint8_t *data,
5346 size_t data_length )
5347{
5348 psa_status_t status;
5349 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5350 return( PSA_ERROR_BAD_STATE );
5351
5352 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5353 if( status != PSA_SUCCESS )
5354 return( status );
5355
5356 prf->state = TLS12_PRF_STATE_KEY_SET;
5357
5358 return( PSA_SUCCESS );
5359}
5360
Janos Follath6660f0e2019-06-17 08:44:03 +01005361static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5362 psa_tls12_prf_key_derivation_t *prf,
5363 psa_algorithm_t hash_alg,
5364 const uint8_t *data,
5365 size_t data_length )
5366{
5367 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005368 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5369 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005370
5371 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5372 return( PSA_ERROR_INVALID_ARGUMENT );
5373
5374 /* Quoting RFC 4279, Section 2:
5375 *
5376 * The premaster secret is formed as follows: if the PSK is N octets
5377 * long, concatenate a uint16 with the value N, N zero octets, a second
5378 * uint16 with the value N, and the PSK itself.
5379 */
5380
Janos Follath40e13932019-06-26 13:22:29 +01005381 *cur++ = ( data_length >> 8 ) & 0xff;
5382 *cur++ = ( data_length >> 0 ) & 0xff;
5383 memset( cur, 0, data_length );
5384 cur += data_length;
5385 *cur++ = pms[0];
5386 *cur++ = pms[1];
5387 memcpy( cur, data, data_length );
5388 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005389
Janos Follath40e13932019-06-26 13:22:29 +01005390 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005391
5392 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5393 return( status );
5394}
5395
Janos Follath63028dd2019-06-13 09:15:47 +01005396static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5397 const uint8_t *data,
5398 size_t data_length )
5399{
5400 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5401 return( PSA_ERROR_BAD_STATE );
5402
Janos Follathd6dce9f2019-07-04 09:11:38 +01005403 if( data_length != 0 )
5404 {
5405 prf->label = mbedtls_calloc( 1, data_length );
5406 if( prf->label == NULL )
5407 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005408
Janos Follathd6dce9f2019-07-04 09:11:38 +01005409 memcpy( prf->label, data, data_length );
5410 prf->label_length = data_length;
5411 }
Janos Follath63028dd2019-06-13 09:15:47 +01005412
5413 prf->state = TLS12_PRF_STATE_LABEL_SET;
5414
5415 return( PSA_SUCCESS );
5416}
5417
Janos Follathb03233e2019-06-11 15:30:30 +01005418static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5419 psa_algorithm_t hash_alg,
5420 psa_key_derivation_step_t step,
5421 const uint8_t *data,
5422 size_t data_length )
5423{
Janos Follathb03233e2019-06-11 15:30:30 +01005424 switch( step )
5425 {
Janos Follathf08e2652019-06-13 09:05:41 +01005426 case PSA_KEY_DERIVATION_INPUT_SEED:
5427 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005428 case PSA_KEY_DERIVATION_INPUT_SECRET:
5429 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005430 case PSA_KEY_DERIVATION_INPUT_LABEL:
5431 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005432 default:
5433 return( PSA_ERROR_INVALID_ARGUMENT );
5434 }
5435}
Janos Follath6660f0e2019-06-17 08:44:03 +01005436
5437static psa_status_t psa_tls12_prf_psk_to_ms_input(
5438 psa_tls12_prf_key_derivation_t *prf,
5439 psa_algorithm_t hash_alg,
5440 psa_key_derivation_step_t step,
5441 const uint8_t *data,
5442 size_t data_length )
5443{
5444 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005445 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005446 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5447 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005448 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005449
5450 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5451}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005452#endif /* MBEDTLS_MD_C */
5453
Gilles Peskineb8965192019-09-24 16:21:10 +02005454/** Check whether the given key type is acceptable for the given
5455 * input step of a key derivation.
5456 *
5457 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5458 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5459 * Both secret and non-secret inputs can alternatively have the type
5460 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5461 * that the input was passed as a buffer rather than via a key object.
5462 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005463static int psa_key_derivation_check_input_type(
5464 psa_key_derivation_step_t step,
5465 psa_key_type_t key_type )
5466{
5467 switch( step )
5468 {
5469 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005470 if( key_type == PSA_KEY_TYPE_DERIVE )
5471 return( PSA_SUCCESS );
5472 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005473 return( PSA_SUCCESS );
5474 break;
5475 case PSA_KEY_DERIVATION_INPUT_LABEL:
5476 case PSA_KEY_DERIVATION_INPUT_SALT:
5477 case PSA_KEY_DERIVATION_INPUT_INFO:
5478 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005479 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5480 return( PSA_SUCCESS );
5481 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005482 return( PSA_SUCCESS );
5483 break;
5484 }
5485 return( PSA_ERROR_INVALID_ARGUMENT );
5486}
5487
Janos Follathb80a94e2019-06-12 15:54:46 +01005488static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005490 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005491 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005492 const uint8_t *data,
5493 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005494{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005495 psa_status_t status;
5496 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5497
5498 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005499 if( status != PSA_SUCCESS )
5500 goto exit;
5501
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005502#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005503 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005504 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005505 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005506 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005507 step, data, data_length );
5508 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005509 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005510 {
Janos Follathb03233e2019-06-11 15:30:30 +01005511 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5512 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5513 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005514 }
5515 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5516 {
5517 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5518 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5519 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005520 }
5521 else
5522#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005523 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005525 return( PSA_ERROR_BAD_STATE );
5526 }
5527
Gilles Peskine224b0d62019-09-23 18:13:17 +02005528exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005529 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005531 return( status );
5532}
5533
Janos Follath51f4a0f2019-06-14 11:35:55 +01005534psa_status_t psa_key_derivation_input_bytes(
5535 psa_key_derivation_operation_t *operation,
5536 psa_key_derivation_step_t step,
5537 const uint8_t *data,
5538 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005539{
Gilles Peskineb8965192019-09-24 16:21:10 +02005540 return( psa_key_derivation_input_internal( operation, step,
5541 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005542 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005543}
5544
Janos Follath51f4a0f2019-06-14 11:35:55 +01005545psa_status_t psa_key_derivation_input_key(
5546 psa_key_derivation_operation_t *operation,
5547 psa_key_derivation_step_t step,
5548 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005549{
5550 psa_key_slot_t *slot;
5551 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005552
Gilles Peskine28f8f302019-07-24 13:30:31 +02005553 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005554 PSA_KEY_USAGE_DERIVE,
5555 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005556 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005557 {
5558 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005559 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005560 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005561
5562 /* Passing a key object as a SECRET input unlocks the permission
5563 * to output to a key object. */
5564 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5565 operation->can_output_key = 1;
5566
Janos Follathb80a94e2019-06-12 15:54:46 +01005567 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005568 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005569 slot->data.key.data,
5570 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005572
5573
5574
5575/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005576/* Key agreement */
5577/****************************************************************/
5578
Gilles Peskinea05219c2018-11-16 16:02:56 +01005579#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005580static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5581 size_t peer_key_length,
5582 const mbedtls_ecp_keypair *our_key,
5583 uint8_t *shared_secret,
5584 size_t shared_secret_size,
5585 size_t *shared_secret_length )
5586{
Steven Cooremand4867872020-08-05 16:31:39 +02005587 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005588 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005589 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005590 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005591 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005592 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005593
Steven Cooreman4fed4552020-08-03 14:46:03 +02005594 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5595 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005596 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005597 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005598 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005599 goto exit;
5600
Jaeden Amero97271b32019-01-10 19:38:51 +00005601 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005602 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005603 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005604 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005605 status = mbedtls_to_psa_error(
5606 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5607 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005608 goto exit;
5609
Jaeden Amero97271b32019-01-10 19:38:51 +00005610 status = mbedtls_to_psa_error(
5611 mbedtls_ecdh_calc_secret( &ecdh,
5612 shared_secret_length,
5613 shared_secret, shared_secret_size,
5614 mbedtls_ctr_drbg_random,
5615 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005616 if( status != PSA_SUCCESS )
5617 goto exit;
5618 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5619 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005620
5621exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005622 if( status != PSA_SUCCESS )
5623 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005624 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005625 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005626 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005627
Jaeden Amero97271b32019-01-10 19:38:51 +00005628 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005629}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005630#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005631
Gilles Peskine01d718c2018-09-18 12:01:02 +02005632#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5633
Gilles Peskine0216fe12019-04-11 21:23:21 +02005634static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5635 psa_key_slot_t *private_key,
5636 const uint8_t *peer_key,
5637 size_t peer_key_length,
5638 uint8_t *shared_secret,
5639 size_t shared_secret_size,
5640 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005641{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005642 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005643 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005644#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005645 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005646 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005647 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005648 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005649 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005650 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005651 private_key->data.key.data,
5652 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005653 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005654 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005655 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005656 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005657 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005658 shared_secret, shared_secret_size,
5659 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005660 mbedtls_ecp_keypair_free( ecp );
5661 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005662 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005663#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005664 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005665 (void) private_key;
5666 (void) peer_key;
5667 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005668 (void) shared_secret;
5669 (void) shared_secret_size;
5670 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005671 return( PSA_ERROR_NOT_SUPPORTED );
5672 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005673}
5674
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005675/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005676 * to potentially free embedded data structures and wipe confidential data.
5677 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005679 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005680 psa_key_slot_t *private_key,
5681 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005682 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005683{
5684 psa_status_t status;
5685 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5686 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005688
5689 /* Step 1: run the secret agreement algorithm to generate the shared
5690 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005691 status = psa_key_agreement_raw_internal( ka_alg,
5692 private_key,
5693 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005694 shared_secret,
5695 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005696 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005697 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005698 goto exit;
5699
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005700 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005701 * the shared secret. A shared secret is permitted wherever a key
5702 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005703 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005704 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005705 shared_secret,
5706 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005707
Gilles Peskine12313cd2018-06-20 00:20:32 +02005708exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005709 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005710 return( status );
5711}
5712
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005714 psa_key_derivation_step_t step,
5715 psa_key_handle_t private_key,
5716 const uint8_t *peer_key,
5717 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005718{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005719 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005720 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005722 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005723 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005724 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005725 if( status != PSA_SUCCESS )
5726 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005727 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005728 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005730 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005732 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005733}
5734
Gilles Peskinebe697d82019-05-16 18:00:41 +02005735psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5736 psa_key_handle_t private_key,
5737 const uint8_t *peer_key,
5738 size_t peer_key_length,
5739 uint8_t *output,
5740 size_t output_size,
5741 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005742{
5743 psa_key_slot_t *slot;
5744 psa_status_t status;
5745
5746 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5747 {
5748 status = PSA_ERROR_INVALID_ARGUMENT;
5749 goto exit;
5750 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005751 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005752 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005753 if( status != PSA_SUCCESS )
5754 goto exit;
5755
5756 status = psa_key_agreement_raw_internal( alg, slot,
5757 peer_key, peer_key_length,
5758 output, output_size,
5759 output_length );
5760
5761exit:
5762 if( status != PSA_SUCCESS )
5763 {
5764 /* If an error happens and is not handled properly, the output
5765 * may be used as a key to protect sensitive data. Arrange for such
5766 * a key to be random, which is likely to result in decryption or
5767 * verification errors. This is better than filling the buffer with
5768 * some constant data such as zeros, which would result in the data
5769 * being protected with a reproducible, easily knowable key.
5770 */
5771 psa_generate_random( output, output_size );
5772 *output_length = output_size;
5773 }
5774 return( status );
5775}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005776
5777
5778/****************************************************************/
5779/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005780/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005781
Gilles Peskine4c317f42018-07-12 01:24:09 +02005782psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005783 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005784{
Janos Follath24eed8d2019-11-22 13:21:35 +00005785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005786 GUARD_MODULE_INITIALIZED;
5787
Gilles Peskinef181eca2019-08-07 13:49:00 +02005788 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5789 {
5790 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5791 output,
5792 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5793 if( ret != 0 )
5794 return( mbedtls_to_psa_error( ret ) );
5795 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5796 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5797 }
5798
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5800 return( mbedtls_to_psa_error( ret ) );
5801}
5802
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005803#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5804#include "mbedtls/entropy_poll.h"
5805
Gilles Peskine7228da22019-07-15 11:06:15 +02005806psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005807 size_t seed_size )
5808{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005809 if( global_data.initialized )
5810 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005811
5812 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5813 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5814 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5815 return( PSA_ERROR_INVALID_ARGUMENT );
5816
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005817 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005818}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005819#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005820
Gilles Peskinee56e8782019-04-26 17:34:02 +02005821#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5822static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5823 size_t domain_parameters_size,
5824 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005825{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005826 size_t i;
5827 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005828
Gilles Peskinee56e8782019-04-26 17:34:02 +02005829 if( domain_parameters_size == 0 )
5830 {
5831 *exponent = 65537;
5832 return( PSA_SUCCESS );
5833 }
5834
5835 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5836 * support values that fit in a 32-bit integer, which is larger than
5837 * int on just about every platform anyway. */
5838 if( domain_parameters_size > sizeof( acc ) )
5839 return( PSA_ERROR_NOT_SUPPORTED );
5840 for( i = 0; i < domain_parameters_size; i++ )
5841 acc = ( acc << 8 ) | domain_parameters[i];
5842 if( acc > INT_MAX )
5843 return( PSA_ERROR_NOT_SUPPORTED );
5844 *exponent = acc;
5845 return( PSA_SUCCESS );
5846}
5847#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5848
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005849static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005850 psa_key_slot_t *slot, size_t bits,
5851 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005852{
Gilles Peskine8e338702019-07-30 20:06:31 +02005853 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005854
Gilles Peskinee56e8782019-04-26 17:34:02 +02005855 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005856 return( PSA_ERROR_INVALID_ARGUMENT );
5857
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858 if( key_type_is_raw_bytes( type ) )
5859 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005860 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005861
5862 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005863 if( status != PSA_SUCCESS )
5864 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005865
5866 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005867 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5868 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005869 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005870
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005871 status = psa_generate_random( slot->data.key.data,
5872 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005874 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005875
5876 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005877#if defined(MBEDTLS_DES_C)
5878 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005879 psa_des_set_key_parity( slot->data.key.data,
5880 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005881#endif /* MBEDTLS_DES_C */
5882 }
5883 else
5884
5885#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005886 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005887 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005888 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005890 int exponent;
5891 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005892 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5893 return( PSA_ERROR_NOT_SUPPORTED );
5894 /* Accept only byte-aligned keys, for the same reasons as
5895 * in psa_import_rsa_key(). */
5896 if( bits % 8 != 0 )
5897 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005898 status = psa_read_rsa_exponent( domain_parameters,
5899 domain_parameters_size,
5900 &exponent );
5901 if( status != PSA_SUCCESS )
5902 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005903 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5904 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005905 mbedtls_ctr_drbg_random,
5906 &global_data.ctr_drbg,
5907 (unsigned int) bits,
5908 exponent );
5909 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005910 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005911
5912 /* Make sure to always have an export representation available */
5913 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5914
Steven Cooreman75b74362020-07-28 14:30:13 +02005915 status = psa_allocate_buffer_to_slot( slot, bytes );
5916 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005917 {
5918 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02005919 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005920 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005921
5922 status = psa_export_rsa_key( type,
5923 &rsa,
5924 slot->data.key.data,
5925 bytes,
5926 &slot->data.key.bytes );
5927 mbedtls_rsa_free( &rsa );
5928 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005929 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005930 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005931 }
5932 else
5933#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5934
5935#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005936 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005937 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005938 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005939 mbedtls_ecp_group_id grp_id =
5940 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005941 const mbedtls_ecp_curve_info *curve_info =
5942 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005943 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005945 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005946 return( PSA_ERROR_NOT_SUPPORTED );
5947 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5948 return( PSA_ERROR_NOT_SUPPORTED );
5949 if( curve_info->bit_size != bits )
5950 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005951 mbedtls_ecp_keypair_init( &ecp );
5952 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005953 mbedtls_ctr_drbg_random,
5954 &global_data.ctr_drbg );
5955 if( ret != 0 )
5956 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005957 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005958 return( mbedtls_to_psa_error( ret ) );
5959 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005960
5961
5962 /* Make sure to always have an export representation available */
5963 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005964 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5965 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005966 {
5967 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005968 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005969 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005970
5971 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005972 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5973
5974 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02005975 if( status != PSA_SUCCESS ) {
5976 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02005977 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02005978 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02005979 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005980 }
5981 else
5982#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02005983 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005984 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005985 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005986
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005987 return( PSA_SUCCESS );
5988}
Darryl Green0c6575a2018-11-07 16:05:30 +00005989
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005990psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005991 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005992{
5993 psa_status_t status;
5994 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005995 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005996
Gilles Peskine0f84d622019-09-12 19:03:13 +02005997 /* Reject any attempt to create a zero-length key so that we don't
5998 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5999 if( psa_get_key_bits( attributes ) == 0 )
6000 return( PSA_ERROR_INVALID_ARGUMENT );
6001
Gilles Peskinedf179142019-07-15 22:02:14 +02006002 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6003 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006004 if( status != PSA_SUCCESS )
6005 goto exit;
6006
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006007#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6008 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00006009 {
Gilles Peskine11792082019-08-06 18:36:36 +02006010 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6011 size_t pubkey_length = 0; /* We don't support this feature yet */
6012 if( drv->key_management == NULL ||
6013 drv->key_management->p_generate == NULL )
6014 {
6015 status = PSA_ERROR_NOT_SUPPORTED;
6016 goto exit;
6017 }
6018 status = drv->key_management->p_generate(
6019 psa_get_se_driver_context( driver ),
6020 slot->data.se.slot_number, attributes,
6021 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006022 }
Gilles Peskine11792082019-08-06 18:36:36 +02006023 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006024#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006025 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006026 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006027 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006028 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006029 }
Gilles Peskine11792082019-08-06 18:36:36 +02006030
6031exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006032 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006033 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006034 if( status != PSA_SUCCESS )
6035 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006036 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006037 *handle = 0;
6038 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006039 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006040}
6041
6042
Gilles Peskinee59236f2018-01-27 23:32:46 +01006043
6044/****************************************************************/
6045/* Module setup */
6046/****************************************************************/
6047
Gilles Peskine5e769522018-11-20 21:59:56 +01006048psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6049 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6050 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6051{
6052 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6053 return( PSA_ERROR_BAD_STATE );
6054 global_data.entropy_init = entropy_init;
6055 global_data.entropy_free = entropy_free;
6056 return( PSA_SUCCESS );
6057}
6058
Gilles Peskinee59236f2018-01-27 23:32:46 +01006059void mbedtls_psa_crypto_free( void )
6060{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006061 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006062 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6063 {
6064 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006065 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006066 }
6067 /* Wipe all remaining data, including configuration.
6068 * In particular, this sets all state indicator to the value
6069 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006070 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006072 /* Unregister all secure element drivers, so that we restart from
6073 * a pristine state. */
6074 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006075#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006076}
6077
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006078#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6079/** Recover a transaction that was interrupted by a power failure.
6080 *
6081 * This function is called during initialization, before psa_crypto_init()
6082 * returns. If this function returns a failure status, the initialization
6083 * fails.
6084 */
6085static psa_status_t psa_crypto_recover_transaction(
6086 const psa_crypto_transaction_t *transaction )
6087{
6088 switch( transaction->unknown.type )
6089 {
6090 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6091 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006092 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006093 * is implemented.
6094 * https://github.com/ARMmbed/mbed-crypto/issues/218
6095 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006096 default:
6097 /* We found an unsupported transaction in the storage.
6098 * We don't know what state the storage is in. Give up. */
6099 return( PSA_ERROR_STORAGE_FAILURE );
6100 }
6101}
6102#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6103
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104psa_status_t psa_crypto_init( void )
6105{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006106 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006107 const unsigned char drbg_seed[] = "PSA";
6108
Gilles Peskinec6b69072018-11-20 21:42:52 +01006109 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006110 if( global_data.initialized != 0 )
6111 return( PSA_SUCCESS );
6112
Gilles Peskine5e769522018-11-20 21:59:56 +01006113 /* Set default configuration if
6114 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6115 if( global_data.entropy_init == NULL )
6116 global_data.entropy_init = mbedtls_entropy_init;
6117 if( global_data.entropy_free == NULL )
6118 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006119
Gilles Peskinec6b69072018-11-20 21:42:52 +01006120 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006121 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006122#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6123 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6124 /* The PSA entropy injection feature depends on using NV seed as an entropy
6125 * source. Add NV seed as an entropy source for PSA entropy injection. */
6126 mbedtls_entropy_add_source( &global_data.entropy,
6127 mbedtls_nv_seed_poll, NULL,
6128 MBEDTLS_ENTROPY_BLOCK_SIZE,
6129 MBEDTLS_ENTROPY_SOURCE_STRONG );
6130#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006132 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006133 status = mbedtls_to_psa_error(
6134 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6135 mbedtls_entropy_func,
6136 &global_data.entropy,
6137 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6138 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006139 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006140 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006141
Gilles Peskine66fb1262018-12-10 16:29:04 +01006142 status = psa_initialize_key_slots( );
6143 if( status != PSA_SUCCESS )
6144 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006145
Gilles Peskined9348f22019-10-01 15:22:29 +02006146#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6147 status = psa_init_all_se_drivers( );
6148 if( status != PSA_SUCCESS )
6149 goto exit;
6150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6151
Gilles Peskine4b734222019-07-24 15:56:31 +02006152#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006153 status = psa_crypto_load_transaction( );
6154 if( status == PSA_SUCCESS )
6155 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006156 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6157 if( status != PSA_SUCCESS )
6158 goto exit;
6159 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006160 }
6161 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6162 {
6163 /* There's no transaction to complete. It's all good. */
6164 status = PSA_SUCCESS;
6165 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006166#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006167
Gilles Peskinec6b69072018-11-20 21:42:52 +01006168 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006169 global_data.initialized = 1;
6170
6171exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006172 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006173 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006174 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006175}
6176
6177#endif /* MBEDTLS_PSA_CRYPTO_C */