blob: ee616956af5d2193b8b9aa6a6078847fc519e832 [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/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010024
25#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030026
itayzafrir7723ab12019-02-14 10:28:02 +020027#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010028#include "psa/crypto.h"
29
Gilles Peskine039b90c2018-12-07 18:24:41 +010030#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010031#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb46bef22019-07-30 21:32:04 +020040#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041#include <stdlib.h>
42#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020044#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020055#include "mbedtls/chacha20.h"
56#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/cipher.h"
58#include "mbedtls/ccm.h"
59#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020062#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010064#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/error.h"
66#include "mbedtls/gcm.h"
67#include "mbedtls/md2.h"
68#include "mbedtls/md4.h"
69#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010070#include "mbedtls/md.h"
71#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010072#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010074#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000075#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskine996deb12018-08-01 15:45:45 +020083#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
84
Gilles Peskine9ef733f2018-02-07 21:05:37 +010085/* constant-time buffer comparison */
86static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
87{
88 size_t i;
89 unsigned char diff = 0;
90
91 for( i = 0; i < n; i++ )
92 diff |= a[i] ^ b[i];
93
94 return( diff );
95}
96
97
98
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103static int key_type_is_raw_bytes( psa_key_type_t type )
104{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200105 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine2d277862018-06-18 15:41:12 +0200113typedef struct
114{
Gilles Peskine5e769522018-11-20 21:59:56 +0100115 void (* entropy_init )( mbedtls_entropy_context *ctx );
116 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100117 mbedtls_entropy_context entropy;
118 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100120 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121} psa_global_data_t;
122
123static psa_global_data_t global_data;
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
126 if( global_data.initialized == 0 ) \
127 return( PSA_ERROR_BAD_STATE );
128
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129static psa_status_t mbedtls_to_psa_error( int ret )
130{
Gilles Peskinea5905292018-02-07 20:59:33 +0100131 /* If there's both a high-level code and low-level code, dispatch on
132 * the high-level code. */
133 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
162 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
190 return( PSA_ERROR_BAD_STATE );
191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
192 return( PSA_ERROR_INVALID_SIGNATURE );
193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200207 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
Gilles Peskineff2d2002019-05-06 15:26:23 +0200298 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
301 return( PSA_ERROR_NOT_SUPPORTED );
302
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305
306 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_INVALID_PADDING:
309 return( PSA_ERROR_INVALID_PADDING );
310 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
315 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200316 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
318 return( PSA_ERROR_INVALID_SIGNATURE );
319 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
320 return( PSA_ERROR_BUFFER_TOO_SMALL );
321 case MBEDTLS_ERR_RSA_RNG_FAILED:
322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
323 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
324 return( PSA_ERROR_NOT_SUPPORTED );
325 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
itayzafrir5c753392018-05-08 11:18:38 +0300338 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300340 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
344 return( PSA_ERROR_NOT_SUPPORTED );
345 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
346 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
350 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000352 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
353 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100376 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
378 switch( curve )
379 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100380 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100381 switch( byte_length )
382 {
383 case PSA_BITS_TO_BYTES( 192 ):
384 return( MBEDTLS_ECP_DP_SECP192R1 );
385 case PSA_BITS_TO_BYTES( 224 ):
386 return( MBEDTLS_ECP_DP_SECP224R1 );
387 case PSA_BITS_TO_BYTES( 256 ):
388 return( MBEDTLS_ECP_DP_SECP256R1 );
389 case PSA_BITS_TO_BYTES( 384 ):
390 return( MBEDTLS_ECP_DP_SECP384R1 );
391 case PSA_BITS_TO_BYTES( 521 ):
392 return( MBEDTLS_ECP_DP_SECP521R1 );
393 default:
394 return( MBEDTLS_ECP_DP_NONE );
395 }
396 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100397
Paul Elliott8ff510a2020-06-02 17:19:28 +0100398 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100399 switch( byte_length )
400 {
401 case PSA_BITS_TO_BYTES( 256 ):
402 return( MBEDTLS_ECP_DP_BP256R1 );
403 case PSA_BITS_TO_BYTES( 384 ):
404 return( MBEDTLS_ECP_DP_BP384R1 );
405 case PSA_BITS_TO_BYTES( 512 ):
406 return( MBEDTLS_ECP_DP_BP512R1 );
407 default:
408 return( MBEDTLS_ECP_DP_NONE );
409 }
410 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100411
Paul Elliott8ff510a2020-06-02 17:19:28 +0100412 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100413 switch( byte_length )
414 {
415 case PSA_BITS_TO_BYTES( 255 ):
416 return( MBEDTLS_ECP_DP_CURVE25519 );
417 case PSA_BITS_TO_BYTES( 448 ):
418 return( MBEDTLS_ECP_DP_CURVE448 );
419 default:
420 return( MBEDTLS_ECP_DP_NONE );
421 }
422 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100423
Paul Elliott8ff510a2020-06-02 17:19:28 +0100424 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100425 switch( byte_length )
426 {
427 case PSA_BITS_TO_BYTES( 192 ):
428 return( MBEDTLS_ECP_DP_SECP192K1 );
429 case PSA_BITS_TO_BYTES( 224 ):
430 return( MBEDTLS_ECP_DP_SECP224K1 );
431 case PSA_BITS_TO_BYTES( 256 ):
432 return( MBEDTLS_ECP_DP_SECP256K1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Gilles Peskine12313cd2018-06-20 00:20:32 +0200438 default:
439 return( MBEDTLS_ECP_DP_NONE );
440 }
441}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100442#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200443
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200444static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
445 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446{
447 /* Check that the bit size is acceptable for the key type */
448 switch( type )
449 {
450 case PSA_KEY_TYPE_RAW_DATA:
451#if defined(MBEDTLS_MD_C)
452 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200454 case PSA_KEY_TYPE_DERIVE:
455 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456#if defined(MBEDTLS_AES_C)
457 case PSA_KEY_TYPE_AES:
458 if( bits != 128 && bits != 192 && bits != 256 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462#if defined(MBEDTLS_CAMELLIA_C)
463 case PSA_KEY_TYPE_CAMELLIA:
464 if( bits != 128 && bits != 192 && bits != 256 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_DES_C)
469 case PSA_KEY_TYPE_DES:
470 if( bits != 64 && bits != 128 && bits != 192 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_ARC4_C)
475 case PSA_KEY_TYPE_ARC4:
476 if( bits < 8 || bits > 2048 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200480#if defined(MBEDTLS_CHACHA20_C)
481 case PSA_KEY_TYPE_CHACHA20:
482 if( bits != 256 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 default:
487 return( PSA_ERROR_NOT_SUPPORTED );
488 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200489 if( bits % 8 != 0 )
490 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 return( PSA_SUCCESS );
493}
494
Steven Cooremana01795d2020-07-24 22:48:15 +0200495#if defined(MBEDTLS_RSA_C)
496
497#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100498/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
499 * that are not a multiple of 8) well. For example, there is only
500 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
501 * way to return the exact bit size of a key.
502 * To keep things simple, reject non-byte-aligned key sizes. */
503static psa_status_t psa_check_rsa_key_byte_aligned(
504 const mbedtls_rsa_context *rsa )
505{
506 mbedtls_mpi n;
507 psa_status_t status;
508 mbedtls_mpi_init( &n );
509 status = mbedtls_to_psa_error(
510 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
511 if( status == PSA_SUCCESS )
512 {
513 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
514 status = PSA_ERROR_NOT_SUPPORTED;
515 }
516 mbedtls_mpi_free( &n );
517 return( status );
518}
Steven Cooremana01795d2020-07-24 22:48:15 +0200519#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100520
Steven Cooremana01795d2020-07-24 22:48:15 +0200521/** Load the contents of a key slot into an internal RSA representation
522 *
523 * \param[in] slot The slot from which to load the representation
524 * \param[out] rsa The internal RSA representation to hold the key. Must be
525 * allocated and initialized. If it already holds a
526 * different key, it will be overwritten and cause a memory
527 * leak.
528 */
529static psa_status_t psa_load_rsa_representation( const psa_key_slot_t *slot,
530 mbedtls_rsa_context *rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531{
Steven Cooremana01795d2020-07-24 22:48:15 +0200532#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200534 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200536 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537
538 /* Parse the data. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 if( PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540 status = mbedtls_to_psa_error(
Steven Cooremana01795d2020-07-24 22:48:15 +0200541 mbedtls_pk_parse_key( &ctx, slot->data.key.data, slot->data.key.bytes, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200542 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooremana01795d2020-07-24 22:48:15 +0200544 mbedtls_pk_parse_public_key( &ctx, slot->data.key.data, slot->data.key.bytes ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 if( status != PSA_SUCCESS )
546 goto exit;
547
548 /* We have something that the pkparse module recognizes. If it is a
549 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200550 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000552 status = PSA_ERROR_INVALID_ARGUMENT;
553 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
557 * supports non-byte-aligned key sizes, but not well. For example,
558 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200559 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 {
562 status = PSA_ERROR_NOT_SUPPORTED;
563 goto exit;
564 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
566
567 if( status != PSA_SUCCESS )
568 goto exit;
569
570 /* Copy the PK-contained RSA context to the one provided as function input */
571 status = mbedtls_to_psa_error(
572 mbedtls_rsa_copy( rsa, mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000573
574exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200575 mbedtls_pk_free( &ctx );
576 return( status );
577#else
578 (void) slot;
579 (void) rsa;
580 return( PSA_ERROR_NOT_SUPPORTED );
581#endif /* MBEDTLS_PK_PARSE_C */
582}
583
584/** Export an RSA key to export representation
585 *
586 * \param[in] type The type of key (public/private) to export
587 * \param[in] rsa The internal RSA representation from which to export
588 * \param[out] data The buffer to export to
589 * \param[in] data_size The length of the buffer to export to
590 * \param[out] data_length The amount of bytes written to \p data
591 */
592static psa_status_t psa_export_rsa_key( psa_key_type_t type,
593 mbedtls_rsa_context *rsa,
594 uint8_t *data,
595 size_t data_size,
596 size_t *data_length )
597{
598#if defined(MBEDTLS_PK_WRITE_C)
599 int ret;
600 mbedtls_pk_context pk;
601 uint8_t *pos = data + data_size;
602
603 mbedtls_pk_init( &pk );
604 pk.pk_info = &mbedtls_rsa_info;
605 pk.pk_ctx = rsa;
606
607 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
608 * representation of respectively the non-encrypted PKCS#1 RSAPrivateKey
609 * or the RFC3279 RSAPublicKey for a private key or a public key. */
610 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
611 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
612 else
613 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
614
615 if( ret < 0 )
616 return mbedtls_to_psa_error( ret );
617
618 /* The mbedtls_pk_xxx functions write to the end of the buffer.
619 * Move the data to the beginning and erase remaining data
620 * at the original location. */
621 if( 2 * (size_t) ret <= data_size )
622 {
623 memcpy( data, data + data_size - ret, ret );
624 memset( data + data_size - ret, 0, ret );
625 }
626 else if( (size_t) ret < data_size )
627 {
628 memmove( data, data + data_size - ret, ret );
629 memset( data + ret, 0, data_size - ret );
630 }
631
632 *data_length = ret;
633 return( PSA_SUCCESS );
634#else
635 (void) type;
636 (void) rsa;
637 (void) data;
638 (void) data_size;
639 (void) data_length;
640 return( PSA_ERROR_NOT_SUPPORTED );
641#endif /* MBEDTLS_PK_WRITE_C */
642}
643
644/** Import an RSA key from import representation to a slot
645 *
646 * \param[in,out] slot The slot where to store the export representation to
647 * \param[in] data The buffer containing the import representation
648 * \param[in] data_length The amount of bytes in \p data
649 */
650static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
651 const uint8_t *data,
652 size_t data_length )
653{
654 psa_status_t status;
655 uint8_t* output = NULL;
656 mbedtls_rsa_context rsa;
657 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
658
659 /* Temporarily load input into slot. The cast here is safe since it'll
660 * only be used for load_rsa_representation, which doesn't modify the
661 * buffer. */
662 slot->data.key.data = (uint8_t *)data;
663 slot->data.key.bytes = data_length;
664
665 /* Parse input */
666 status = psa_load_rsa_representation( slot, &rsa );
667 if( status != PSA_SUCCESS )
668 goto exit;
669
670 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
671 mbedtls_rsa_get_len( &rsa ) );
672
673 /* Re-export the data to PSA export format, which in case of RSA is the
674 * smallest representation we can parse. */
675 output = mbedtls_calloc( 1, data_length );
676
677 if( output == NULL )
678 {
679 status = PSA_ERROR_INSUFFICIENT_MEMORY;
680 goto exit;
681 }
682
683 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
684 * representation of respectively the non-encrypted PKCS#1 RSAPrivateKey
685 * or the RFC3279 RSAPublicKey for a private key or a public key. That
686 * means we have no other choice then to run an import to verify the key
687 * size. */
688 status = psa_export_rsa_key( slot->attr.type,
689 &rsa,
690 output,
691 data_length,
692 &data_length);
693
694exit:
695 /* Always free the RSA object */
696 mbedtls_rsa_free( &rsa );
697
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 );
702 slot->data.key.data = NULL;
703 slot->data.key.bytes = 0;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000704 return( status );
705 }
706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 /* On success, store the allocated export-formatted key. */
708 slot->data.key.data = output;
709 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000710
711 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200712}
Steven Cooremana01795d2020-07-24 22:48:15 +0200713#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200714
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_ECP_C)
Steven Cooremanacda8342020-07-24 23:09:52 +0200716/* Load the key slot contents into an mbedTLS internal representation object.
717 * Note: caller is responsible for freeing the object properly */
718static psa_status_t psa_load_ecp_representation( const psa_key_slot_t *slot,
719 mbedtls_ecp_keypair *ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100720{
721 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200722 size_t data_length = slot->data.key.bytes;
723 psa_status_t status;
724 mbedtls_ecp_keypair_init( ecp );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100725
Steven Cooremanacda8342020-07-24 23:09:52 +0200726 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100727 {
728 /* A public key is represented as:
729 * - The byte 0x04;
730 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
731 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
732 * So its data length is 2m+1 where n is the key size in bits.
733 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200734 if( ( slot->data.key.bytes & 1 ) == 0 )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100735 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200736 data_length = slot->data.key.bytes / 2;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 }
738
Gilles Peskine4cd32772019-12-02 20:49:42 +0100739 /* Load the group. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200740 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type),
741 data_length );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100742 if( grp_id == MBEDTLS_ECP_DP_NONE )
743 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200744 status = mbedtls_to_psa_error(
745 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
746 if( status != PSA_SUCCESS )
747 return( status );
748
749 /* Load the key material */
750 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
751 {
752 /* Load the public value. */
753 status = mbedtls_to_psa_error(
754 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
755 slot->data.key.data,
756 slot->data.key.bytes ) );
757 if( status != PSA_SUCCESS )
758 goto exit;
759
760 /* Check that the point is on the curve. */
761 status = mbedtls_to_psa_error(
762 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
763 if( status != PSA_SUCCESS )
764 goto exit;
765 }
766 else
767 {
768 /* Load the secret value. */
769 status = mbedtls_to_psa_error(
770 mbedtls_ecp_read_key( ecp->grp.id,
771 ecp,
772 slot->data.key.data,
773 slot->data.key.bytes ) );
774
775 if( status != PSA_SUCCESS )
776 goto exit;
777 /* Validate the private key. */
778 status = mbedtls_to_psa_error(
779 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
780 if( status != PSA_SUCCESS )
781 goto exit;
782 }
783exit:
784 if( status != PSA_SUCCESS )
785 mbedtls_ecp_keypair_free( ecp );
786 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100787}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000788
Steven Cooremanacda8342020-07-24 23:09:52 +0200789static psa_status_t psa_export_ecp_key( psa_key_type_t type,
790 mbedtls_ecp_keypair *ecp,
791 uint8_t *data,
792 size_t data_size,
793 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200794{
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000796
Steven Cooremanacda8342020-07-24 23:09:52 +0200797 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200798 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200799 /* Check whether the public part is loaded */
800 if( mbedtls_ecp_is_zero( &ecp->Q ) )
801 {
802 /* Calculate the public key */
803 status = mbedtls_to_psa_error(
804 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
805 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
806 if( status != PSA_SUCCESS )
807 return status;
808 }
809
810 return( mbedtls_to_psa_error(
811 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
812 MBEDTLS_ECP_PF_UNCOMPRESSED,
813 data_length,
814 data,
815 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200816 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200817 else
818 {
819 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
820 return( PSA_ERROR_BUFFER_TOO_SMALL );
821
822 status = mbedtls_to_psa_error(
823 mbedtls_ecp_write_key( ecp,
824 data,
825 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
826 if( status == PSA_SUCCESS )
827 {
828 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
829 }
830
831 return( status );
832 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200833}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200834
Steven Cooremanacda8342020-07-24 23:09:52 +0200835static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
836 const uint8_t *data,
837 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100838{
Steven Cooremanacda8342020-07-24 23:09:52 +0200839 psa_status_t status;
840 uint8_t* output = NULL;
841 mbedtls_ecp_keypair ecp;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100842
Steven Cooremanacda8342020-07-24 23:09:52 +0200843 /* Temporarily load input into slot. The cast here is safe since it'll
844 * only be used for load_ecp_representation, which doesn't modify the
845 * buffer. */
846 slot->data.key.data = (uint8_t *)data;
847 slot->data.key.bytes = data_length;
848
849 /* Parse input */
850 status = psa_load_ecp_representation( slot, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100851 if( status != PSA_SUCCESS )
852 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100853
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
855 slot->attr.bits = (psa_key_bits_t) ecp.grp.nbits + 1;
856 else
857 slot->attr.bits = (psa_key_bits_t) ecp.grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200858
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 /* Re-export the data to PSA export format. There is currently no support
860 * for other input formats then the export format, so this is a 1-1
861 * copy operation. */
862 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100863
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( output == NULL )
865 {
866 status = PSA_ERROR_INSUFFICIENT_MEMORY;
867 goto exit;
868 }
869
870 status = psa_export_ecp_key( slot->attr.type,
871 &ecp,
872 output,
873 data_length,
874 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100875
876exit:
Steven Cooremanacda8342020-07-24 23:09:52 +0200877 /* Always free the PK object (will also free contained RSA context) */
878 mbedtls_ecp_keypair_free( &ecp );
879
880 /* Free the allocated buffer only on error. */
881 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 mbedtls_free( output );
884 slot->data.key.data = NULL;
885 slot->data.key.bytes = 0;
886 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100887 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200888
889 /* On success, store the allocated export-formatted key. */
890 slot->data.key.data = output;
891 slot->data.key.bytes = data_length;
892
893 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100894}
895#endif /* defined(MBEDTLS_ECP_C) */
896
Gilles Peskineb46bef22019-07-30 21:32:04 +0200897/** Return the size of the key in the given slot, in bits.
898 *
899 * \param[in] slot A key slot.
900 *
901 * \return The key size in bits, read from the metadata in the slot.
902 */
903static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
904{
905 return( slot->attr.bits );
906}
907
908/** Calculate the size of the key in the given slot, in bits.
909 *
910 * \param[in] slot A key slot containing a transparent key.
911 *
912 * \return The key size in bits, calculated from the key data.
913 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200914static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200915{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200916 size_t bits = 0; /* return 0 on an empty slot */
917
Gilles Peskineb46bef22019-07-30 21:32:04 +0200918 if( key_type_is_raw_bytes( slot->attr.type ) )
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200919 bits = PSA_BYTES_TO_BITS( slot->data.key.bytes );
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200920
921 /* We know that the size fits in psa_key_bits_t thanks to checks
922 * when the key was created. */
923 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200924}
925
Gilles Peskine8e338702019-07-30 20:06:31 +0200926/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100927 * previously. This function assumes that the slot does not contain
928 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100929psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
930 const uint8_t *data,
931 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100932{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200933 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100934
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200935 /* zero-length keys are never supported. */
936 if( data_length == 0 )
937 return( PSA_ERROR_NOT_SUPPORTED );
938
939 /* Ensure that the bytes-to-bit conversion never overflows. */
940 if( data_length > SIZE_MAX / 8 )
941 return( PSA_ERROR_NOT_SUPPORTED );
942
Gilles Peskine8e338702019-07-30 20:06:31 +0200943 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200945 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200946
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200947 /* Enforce a size limit, and in particular ensure that the bit
948 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200949 if( bit_size > PSA_MAX_KEY_BITS )
950 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200951
952 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200953 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200954 return status;
955
956 /* Allocate memory for the key */
957 slot->data.key.data = mbedtls_calloc( 1, data_length );
958 if( slot->data.key.data == NULL )
959 {
960 return( PSA_ERROR_INSUFFICIENT_MEMORY );
961 }
962 slot->data.key.bytes = data_length;
963
964 /* copy key into allocated buffer */
965 memcpy(slot->data.key.data, data, data_length);
966
967 /* Write the actual key size to the slot.
968 * psa_start_key_creation() wrote the size declared by the
969 * caller, which may be 0 (meaning unspecified) or wrong. */
970 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 }
972 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100973#if defined(MBEDTLS_ECP_C)
Steven Cooremanacda8342020-07-24 23:09:52 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100975 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200976 status = psa_import_ecp_key( slot, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 }
978 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100979#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000980#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200983 status = psa_import_rsa_key( slot, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 }
985 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000986#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 {
988 return( PSA_ERROR_NOT_SUPPORTED );
989 }
Darryl Green940d72c2018-07-13 13:18:51 +0100990
Gilles Peskineb46bef22019-07-30 21:32:04 +0200991 if( status == PSA_SUCCESS )
992 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200993 if( !PSA_KEY_TYPE_IS_RSA( slot->attr.type ) &&
994 !PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Steven Cooremana01795d2020-07-24 22:48:15 +0200995 {
996 /* Write the actual key size to the slot.
997 * psa_start_key_creation() wrote the size declared by the
998 * caller, which may be 0 (meaning unspecified) or wrong. */
999 slot->attr.bits = psa_calculate_key_bits( slot );
1000 }
Gilles Peskineb46bef22019-07-30 21:32:04 +02001001 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001002 return( status );
1003}
1004
Gilles Peskinef603c712019-01-19 13:40:11 +01001005/** Calculate the intersection of two algorithm usage policies.
1006 *
1007 * Return 0 (which allows no operation) on incompatibility.
1008 */
1009static psa_algorithm_t psa_key_policy_algorithm_intersection(
1010 psa_algorithm_t alg1,
1011 psa_algorithm_t alg2 )
1012{
Gilles Peskine549ea862019-05-22 11:45:59 +02001013 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001014 if( alg1 == alg2 )
1015 return( alg1 );
1016 /* If the policies are from the same hash-and-sign family, check
1017 * if one is a wildcard. If so the other has the specific algorithm. */
1018 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1019 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1020 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1021 {
1022 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1023 return( alg2 );
1024 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1025 return( alg1 );
1026 }
1027 /* If the policies are incompatible, allow nothing. */
1028 return( 0 );
1029}
1030
Gilles Peskined6f371b2019-05-10 19:33:38 +02001031static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1032 psa_algorithm_t requested_alg )
1033{
Gilles Peskine549ea862019-05-22 11:45:59 +02001034 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001035 if( requested_alg == policy_alg )
1036 return( 1 );
1037 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001038 * and requested_alg is the same hash-and-sign family with any hash,
1039 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001040 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1041 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1042 {
1043 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1044 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1045 }
1046 /* If it isn't permitted, it's forbidden. */
1047 return( 0 );
1048}
1049
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001050/** Test whether a policy permits an algorithm.
1051 *
1052 * The caller must test usage flags separately.
1053 */
1054static int psa_key_policy_permits( const psa_key_policy_t *policy,
1055 psa_algorithm_t alg )
1056{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001057 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1058 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001059}
1060
Gilles Peskinef603c712019-01-19 13:40:11 +01001061/** Restrict a key policy based on a constraint.
1062 *
1063 * \param[in,out] policy The policy to restrict.
1064 * \param[in] constraint The policy constraint to apply.
1065 *
1066 * \retval #PSA_SUCCESS
1067 * \c *policy contains the intersection of the original value of
1068 * \c *policy and \c *constraint.
1069 * \retval #PSA_ERROR_INVALID_ARGUMENT
1070 * \c *policy and \c *constraint are incompatible.
1071 * \c *policy is unchanged.
1072 */
1073static psa_status_t psa_restrict_key_policy(
1074 psa_key_policy_t *policy,
1075 const psa_key_policy_t *constraint )
1076{
1077 psa_algorithm_t intersection_alg =
1078 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001079 psa_algorithm_t intersection_alg2 =
1080 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001081 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1082 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001083 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1084 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001085 policy->usage &= constraint->usage;
1086 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001087 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001088 return( PSA_SUCCESS );
1089}
1090
Darryl Green06fd18d2018-07-16 11:21:11 +01001091/** Retrieve a slot which must contain a key. The key must have allow all the
1092 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1093 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001094static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001095 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001096 psa_key_usage_t usage,
1097 psa_algorithm_t alg )
1098{
1099 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001100 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001101
1102 *p_slot = NULL;
1103
Gilles Peskinec5487a82018-12-03 18:08:14 +01001104 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001105 if( status != PSA_SUCCESS )
1106 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001107
1108 /* Enforce that usage policy for the key slot contains all the flags
1109 * required by the usage parameter. There is one exception: public
1110 * keys can always be exported, so we treat public key objects as
1111 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001112 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001113 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001114 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001115 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001116
1117 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001118 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001119 return( PSA_ERROR_NOT_PERMITTED );
1120
1121 *p_slot = slot;
1122 return( PSA_SUCCESS );
1123}
Darryl Green940d72c2018-07-13 13:18:51 +01001124
Gilles Peskine28f8f302019-07-24 13:30:31 +02001125/** Retrieve a slot which must contain a transparent key.
1126 *
1127 * A transparent key is a key for which the key material is directly
1128 * available, as opposed to a key in a secure element.
1129 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001130 * This is a temporary function to use instead of psa_get_key_from_slot()
1131 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132 */
1133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1134static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1135 psa_key_slot_t **p_slot,
1136 psa_key_usage_t usage,
1137 psa_algorithm_t alg )
1138{
1139 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1140 if( status != PSA_SUCCESS )
1141 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001142 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001143 {
1144 *p_slot = NULL;
1145 return( PSA_ERROR_NOT_SUPPORTED );
1146 }
1147 return( PSA_SUCCESS );
1148}
1149#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1150/* With no secure element support, all keys are transparent. */
1151#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1152 psa_get_key_from_slot( handle, p_slot, usage, alg )
1153#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1154
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001155/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001156static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001157{
Gilles Peskine73167e12019-07-12 23:44:37 +02001158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1159 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001160 {
1161 /* No key material to clean. */
1162 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001163 else
1164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001165 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001166 {
1167 /* No key material to clean. */
1168 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001169 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001170 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001171 mbedtls_free( slot->data.key.data );
Darryl Green40225ba2018-11-15 14:48:15 +00001172 }
1173 else
1174#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001175 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001176 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001177 mbedtls_free( slot->data.key.data );
1178 slot->data.key.data = NULL;
1179 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001180 }
1181 else
1182#endif /* defined(MBEDTLS_RSA_C) */
1183#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001184 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001185 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001186 mbedtls_free( slot->data.key.data );
1187 slot->data.key.data = NULL;
1188 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001189 }
1190 else
1191#endif /* defined(MBEDTLS_ECP_C) */
1192 {
1193 /* Shouldn't happen: the key type is not any type that we
1194 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001195 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001196 }
1197
1198 return( PSA_SUCCESS );
1199}
1200
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001201/** Completely wipe a slot in memory, including its policy.
1202 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001203psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001204{
1205 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001206 /* Multipart operations may still be using the key. This is safe
1207 * because all multipart operation objects are independent from
1208 * the key slot: if they need to access the key after the setup
1209 * phase, they have a copy of the key. Note that this means that
1210 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001211 /* At this point, key material and other type-specific content has
1212 * been wiped. Clear remaining metadata. We can call memset and not
1213 * zeroize because the metadata is not particularly sensitive. */
1214 memset( slot, 0, sizeof( *slot ) );
1215 return( status );
1216}
1217
Gilles Peskinec5487a82018-12-03 18:08:14 +01001218psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001219{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001220 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001221 psa_status_t status; /* status of the last operation */
1222 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1224 psa_se_drv_table_entry_t *driver;
1225#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226
Gilles Peskine1841cf42019-10-08 15:48:25 +02001227 if( handle == 0 )
1228 return( PSA_SUCCESS );
1229
Gilles Peskinec5487a82018-12-03 18:08:14 +01001230 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231 if( status != PSA_SUCCESS )
1232 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001233
1234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001235 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001236 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001237 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001238 /* For a key in a secure element, we need to do three things:
1239 * remove the key file in internal storage, destroy the
1240 * key inside the secure element, and update the driver's
1241 * persistent data. Start a transaction that will encompass these
1242 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001243 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001245 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001246 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001247 status = psa_crypto_save_transaction( );
1248 if( status != PSA_SUCCESS )
1249 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001250 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001251 /* We should still try to destroy the key in the secure
1252 * element and the key metadata in storage. This is especially
1253 * important if the error is that the storage is full.
1254 * But how to do it exactly without risking an inconsistent
1255 * state after a reset?
1256 * https://github.com/ARMmbed/mbed-crypto/issues/215
1257 */
1258 overall_status = status;
1259 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001260 }
1261
Gilles Peskine354f7672019-07-12 23:46:38 +02001262 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001263 if( overall_status == PSA_SUCCESS )
1264 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001265 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001266#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1267
Darryl Greend49a4992018-06-18 17:27:26 +01001268#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001269 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001270 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001271 status = psa_destroy_persistent_key( slot->attr.id );
1272 if( overall_status == PSA_SUCCESS )
1273 overall_status = status;
1274
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001275 /* TODO: other slots may have a copy of the same key. We should
1276 * invalidate them.
1277 * https://github.com/ARMmbed/mbed-crypto/issues/214
1278 */
Darryl Greend49a4992018-06-18 17:27:26 +01001279 }
1280#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001281
Gilles Peskinefc762652019-07-22 19:30:34 +02001282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1283 if( driver != NULL )
1284 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001285 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001286 if( overall_status == PSA_SUCCESS )
1287 overall_status = status;
1288 status = psa_crypto_stop_transaction( );
1289 if( overall_status == PSA_SUCCESS )
1290 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001291 }
1292#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1293
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1295exit:
1296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001297 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1299 if( overall_status == PSA_SUCCESS )
1300 overall_status = status;
1301 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302}
1303
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001304void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001305{
Gilles Peskineb699f072019-04-26 16:06:02 +02001306 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001307 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001308}
1309
Gilles Peskineb699f072019-04-26 16:06:02 +02001310psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1311 psa_key_type_t type,
1312 const uint8_t *data,
1313 size_t data_length )
1314{
1315 uint8_t *copy = NULL;
1316
1317 if( data_length != 0 )
1318 {
1319 copy = mbedtls_calloc( 1, data_length );
1320 if( copy == NULL )
1321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1322 memcpy( copy, data, data_length );
1323 }
1324 /* After this point, this function is guaranteed to succeed, so it
1325 * can start modifying `*attributes`. */
1326
1327 if( attributes->domain_parameters != NULL )
1328 {
1329 mbedtls_free( attributes->domain_parameters );
1330 attributes->domain_parameters = NULL;
1331 attributes->domain_parameters_size = 0;
1332 }
1333
1334 attributes->domain_parameters = copy;
1335 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001336 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001337 return( PSA_SUCCESS );
1338}
1339
1340psa_status_t psa_get_key_domain_parameters(
1341 const psa_key_attributes_t *attributes,
1342 uint8_t *data, size_t data_size, size_t *data_length )
1343{
1344 if( attributes->domain_parameters_size > data_size )
1345 return( PSA_ERROR_BUFFER_TOO_SMALL );
1346 *data_length = attributes->domain_parameters_size;
1347 if( attributes->domain_parameters_size != 0 )
1348 memcpy( data, attributes->domain_parameters,
1349 attributes->domain_parameters_size );
1350 return( PSA_SUCCESS );
1351}
1352
1353#if defined(MBEDTLS_RSA_C)
1354static psa_status_t psa_get_rsa_public_exponent(
1355 const mbedtls_rsa_context *rsa,
1356 psa_key_attributes_t *attributes )
1357{
1358 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001360 uint8_t *buffer = NULL;
1361 size_t buflen;
1362 mbedtls_mpi_init( &mpi );
1363
1364 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1365 if( ret != 0 )
1366 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001367 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1368 {
1369 /* It's the default value, which is reported as an empty string,
1370 * so there's nothing to do. */
1371 goto exit;
1372 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001373
1374 buflen = mbedtls_mpi_size( &mpi );
1375 buffer = mbedtls_calloc( 1, buflen );
1376 if( buffer == NULL )
1377 {
1378 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1379 goto exit;
1380 }
1381 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1382 if( ret != 0 )
1383 goto exit;
1384 attributes->domain_parameters = buffer;
1385 attributes->domain_parameters_size = buflen;
1386
1387exit:
1388 mbedtls_mpi_free( &mpi );
1389 if( ret != 0 )
1390 mbedtls_free( buffer );
1391 return( mbedtls_to_psa_error( ret ) );
1392}
1393#endif /* MBEDTLS_RSA_C */
1394
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001395/** Retrieve all the publicly-accessible attributes of a key.
1396 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1398 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001400 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001401 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001403 psa_reset_key_attributes( attributes );
1404
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001405 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001406 if( status != PSA_SUCCESS )
1407 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001408
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001409 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001410 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1411 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1412
1413#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1414 if( psa_key_slot_is_external( slot ) )
1415 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1416#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001417
Gilles Peskine8e338702019-07-30 20:06:31 +02001418 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001419 {
1420#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001421 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001422 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001423#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001424 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001425 * is not yet implemented.
1426 * https://github.com/ARMmbed/mbed-crypto/issues/216
1427 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001429 break;
1430#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001431 {
1432 mbedtls_rsa_context rsa;
1433 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1434
1435 status = psa_load_rsa_representation( slot, &rsa );
1436 if( status != PSA_SUCCESS )
1437 break;
1438
1439 status = psa_get_rsa_public_exponent( &rsa,
1440 attributes );
1441 mbedtls_rsa_free( &rsa );
1442 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001443 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001444#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001445 default:
1446 /* Nothing else to do. */
1447 break;
1448 }
1449
1450 if( status != PSA_SUCCESS )
1451 psa_reset_key_attributes( attributes );
1452 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453}
1454
Gilles Peskinec8000c02019-08-02 20:15:51 +02001455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1456psa_status_t psa_get_key_slot_number(
1457 const psa_key_attributes_t *attributes,
1458 psa_key_slot_number_t *slot_number )
1459{
1460 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1461 {
1462 *slot_number = attributes->slot_number;
1463 return( PSA_SUCCESS );
1464 }
1465 else
1466 return( PSA_ERROR_INVALID_ARGUMENT );
1467}
1468#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1469
Steven Cooremana01795d2020-07-24 22:48:15 +02001470static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1471 uint8_t *data,
1472 size_t data_size,
1473 size_t *data_length )
1474{
1475 if( slot->data.key.bytes > data_size )
1476 return( PSA_ERROR_BUFFER_TOO_SMALL );
1477 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1478 memset( data + slot->data.key.bytes, 0,
1479 data_size - slot->data.key.bytes );
1480 *data_length = slot->data.key.bytes;
1481 return( PSA_SUCCESS );
1482}
1483
Gilles Peskinef603c712019-01-19 13:40:11 +01001484static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1485 uint8_t *data,
1486 size_t data_size,
1487 size_t *data_length,
1488 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489{
Gilles Peskine5d309672019-07-12 23:47:28 +02001490#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1491 const psa_drv_se_t *drv;
1492 psa_drv_se_context_t *drv_context;
1493#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1494
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001495 *data_length = 0;
1496
Gilles Peskine8e338702019-07-30 20:06:31 +02001497 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001498 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001499
Gilles Peskinef9168942019-09-12 19:20:29 +02001500 /* Reject a zero-length output buffer now, since this can never be a
1501 * valid key representation. This way we know that data must be a valid
1502 * pointer and we can do things like memset(data, ..., data_size). */
1503 if( data_size == 0 )
1504 return( PSA_ERROR_BUFFER_TOO_SMALL );
1505
Gilles Peskine5d309672019-07-12 23:47:28 +02001506#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001507 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001508 {
1509 psa_drv_se_export_key_t method;
1510 if( drv->key_management == NULL )
1511 return( PSA_ERROR_NOT_SUPPORTED );
1512 method = ( export_public_key ?
1513 drv->key_management->p_export_public :
1514 drv->key_management->p_export );
1515 if( method == NULL )
1516 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001517 return( method( drv_context,
1518 slot->data.se.slot_number,
1519 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001520 }
1521#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1522
Gilles Peskine8e338702019-07-30 20:06:31 +02001523 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001525 if( slot->data.key.bytes > data_size )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001527 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1528 memset( data + slot->data.key.bytes, 0,
1529 data_size - slot->data.key.bytes );
1530 *data_length = slot->data.key.bytes;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531 return( PSA_SUCCESS );
1532 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001533#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001534 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001535 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001536 /* Exporting private -> private */
1537 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001538 }
1539#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540 else
Moran Peker17e36e12018-05-02 12:55:20 +03001541 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001542 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1543 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001544 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001545 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001546 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001547#if defined(MBEDTLS_RSA_C)
Steven Cooremana01795d2020-07-24 22:48:15 +02001548 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
1549 {
1550 /* Exporting public -> public */
1551 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1552 }
1553 else if( !export_public_key )
1554 {
1555 /* Exporting private -> private */
1556 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1557 }
1558
1559 /* Exporting private -> public */
1560 mbedtls_rsa_context rsa;
1561 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1562
1563 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1564 if( status != PSA_SUCCESS )
1565 return status;
1566
1567 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1568 &rsa,
1569 data,
1570 data_size,
1571 data_length );
1572
1573 mbedtls_rsa_free( &rsa );
1574
1575 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001576#else
Steven Cooremana01795d2020-07-24 22:48:15 +02001577 /* We don't know how to convert a private RSA key to public. */
Darryl Green9e2d7a02018-07-24 16:33:30 +01001578 return( PSA_ERROR_NOT_SUPPORTED );
1579#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001580 }
1581 else
1582 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001583#if defined(MBEDTLS_ECP_C)
Steven Cooremanacda8342020-07-24 23:09:52 +02001584 mbedtls_ecp_keypair ecp;
1585 psa_status_t status = psa_load_ecp_representation( slot, &ecp );
1586 if( status != PSA_SUCCESS )
1587 return status;
1588
1589 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1590 PSA_KEY_TYPE_ECC_GET_FAMILY(
1591 slot->attr.type ) ),
1592 &ecp,
1593 data,
1594 data_size,
1595 data_length );
1596
1597 mbedtls_ecp_keypair_free( &ecp );
1598 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001599#else
Steven Cooremanacda8342020-07-24 23:09:52 +02001600 /* We don't know how to convert a private ECC key to public. */
Darryl Green9e2d7a02018-07-24 16:33:30 +01001601 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02001602#endif /* defined(MBEDTLS_ECP_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001603 }
Gilles Peskine969ac722018-01-28 18:16:59 +01001604 }
1605 else
Moran Pekera998bc62018-04-16 18:16:20 +03001606 {
1607 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001608 it is valid for a special-purpose implementation to omit
1609 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001610 return( PSA_ERROR_NOT_SUPPORTED );
1611 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001612 }
1613}
1614
Gilles Peskinec5487a82018-12-03 18:08:14 +01001615psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001616 uint8_t *data,
1617 size_t data_size,
1618 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001619{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001620 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001621 psa_status_t status;
1622
1623 /* Set the key to empty now, so that even when there are errors, we always
1624 * set data_length to a value between 0 and data_size. On error, setting
1625 * the key to empty is a good choice because an empty key representation is
1626 * unlikely to be accepted anywhere. */
1627 *data_length = 0;
1628
1629 /* Export requires the EXPORT flag. There is an exception for public keys,
1630 * which don't require any flag, but psa_get_key_from_slot takes
1631 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001632 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001633 if( status != PSA_SUCCESS )
1634 return( status );
1635 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001636 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001638
Gilles Peskinec5487a82018-12-03 18:08:14 +01001639psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001640 uint8_t *data,
1641 size_t data_size,
1642 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001643{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001644 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001645 psa_status_t status;
1646
1647 /* Set the key to empty now, so that even when there are errors, we always
1648 * set data_length to a value between 0 and data_size. On error, setting
1649 * the key to empty is a good choice because an empty key representation is
1650 * unlikely to be accepted anywhere. */
1651 *data_length = 0;
1652
1653 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001654 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001655 if( status != PSA_SUCCESS )
1656 return( status );
1657 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001658 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001659}
1660
Gilles Peskine91e8c332019-08-02 19:19:39 +02001661#if defined(static_assert)
1662static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1663 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001664static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001665 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001666static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001667 "One or more key attribute flag is listed as both internal-only and external-only" );
1668#endif
1669
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001670/** Validate that a key policy is internally well-formed.
1671 *
1672 * This function only rejects invalid policies. It does not validate the
1673 * consistency of the policy with respect to other attributes of the key
1674 * such as the key type.
1675 */
1676static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001677{
1678 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001679 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 PSA_KEY_USAGE_ENCRYPT |
1681 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001682 PSA_KEY_USAGE_SIGN_HASH |
1683 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1685 return( PSA_ERROR_INVALID_ARGUMENT );
1686
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 return( PSA_SUCCESS );
1688}
1689
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001690/** Validate the internal consistency of key attributes.
1691 *
1692 * This function only rejects invalid attribute values. If does not
1693 * validate the consistency of the attributes with any key data that may
1694 * be involved in the creation of the key.
1695 *
1696 * Call this function early in the key creation process.
1697 *
1698 * \param[in] attributes Key attributes for the new key.
1699 * \param[out] p_drv On any return, the driver for the key, if any.
1700 * NULL for a transparent key.
1701 *
1702 */
1703static psa_status_t psa_validate_key_attributes(
1704 const psa_key_attributes_t *attributes,
1705 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001706{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001707 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001708
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001709 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1710 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001711 if( status != PSA_SUCCESS )
1712 return( status );
1713
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001714 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1715 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001716 if( status != PSA_SUCCESS )
1717 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001718
1719 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001720 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001721 return( status );
1722
1723 /* Refuse to create overly large keys.
1724 * Note that this doesn't trigger on import if the attributes don't
1725 * explicitly specify a size (so psa_get_key_bits returns 0), so
1726 * psa_import_key() needs its own checks. */
1727 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1728 return( PSA_ERROR_NOT_SUPPORTED );
1729
Gilles Peskine91e8c332019-08-02 19:19:39 +02001730 /* Reject invalid flags. These should not be reachable through the API. */
1731 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1732 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1733 return( PSA_ERROR_INVALID_ARGUMENT );
1734
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001735 return( PSA_SUCCESS );
1736}
1737
Gilles Peskine4747d192019-04-17 15:05:45 +02001738/** Prepare a key slot to receive key material.
1739 *
1740 * This function allocates a key slot and sets its metadata.
1741 *
1742 * If this function fails, call psa_fail_key_creation().
1743 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001744 * This function is intended to be used as follows:
1745 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1746 * it with the specified attributes, and assign it a handle.
1747 * -# Populate the slot with the key material.
1748 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1749 * In case of failure at any step, stop the sequence and call
1750 * psa_fail_key_creation().
1751 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001752 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 * \param[in] attributes Key attributes for the new key.
1754 * \param[out] handle On success, a handle for the allocated slot.
1755 * \param[out] p_slot On success, a pointer to the prepared slot.
1756 * \param[out] p_drv On any return, the driver for the key, if any.
1757 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001758 *
1759 * \retval #PSA_SUCCESS
1760 * The key slot is ready to receive key material.
1761 * \return If this function fails, the key slot is an invalid state.
1762 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 */
1764static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001765 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 const psa_key_attributes_t *attributes,
1767 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001769 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001770{
1771 psa_status_t status;
1772 psa_key_slot_t *slot;
1773
Gilles Peskinedf179142019-07-15 22:02:14 +02001774 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 *p_drv = NULL;
1776
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001777 status = psa_validate_key_attributes( attributes, p_drv );
1778 if( status != PSA_SUCCESS )
1779 return( status );
1780
Gilles Peskineedbed562019-08-07 18:19:59 +02001781 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 if( status != PSA_SUCCESS )
1783 return( status );
1784 slot = *p_slot;
1785
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001786 /* We're storing the declared bit-size of the key. It's up to each
1787 * creation mechanism to verify that this information is correct.
1788 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001789 * an input (generate, device) but not for those where the bit-size
1790 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001791
1792 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001793
Gilles Peskine91e8c332019-08-02 19:19:39 +02001794 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001795 * external-only flags, query `attributes`. Thanks to the check
1796 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001797 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001798 * may have set. */
1799 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001800
Gilles Peskinecbaff462019-07-12 23:46:04 +02001801#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001802 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001803 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001804 * create the key file in internal storage, create the
1805 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001806 * persistent data. This is done by starting a transaction that will
1807 * encompass these three actions.
1808 * For registering a volatile key, we just need to find an appropriate
1809 * slot number inside the SE. Since the key is designated volatile, creating
1810 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001811 /* The first thing to do is to find a slot number for the new key.
1812 * We save the slot number in persistent storage as part of the
1813 * transaction data. It will be needed to recover if the power
1814 * fails during the key creation process, to clean up on the secure
1815 * element side after restarting. Obtaining a slot number from the
1816 * secure element driver updates its persistent state, but we do not yet
1817 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001818 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001819 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001820 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001821 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822 &slot->data.se.slot_number );
1823 if( status != PSA_SUCCESS )
1824 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001825
1826 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001827 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001828 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1829 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1830 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1831 psa_crypto_transaction.key.id = slot->attr.id;
1832 status = psa_crypto_save_transaction( );
1833 if( status != PSA_SUCCESS )
1834 {
1835 (void) psa_crypto_stop_transaction( );
1836 return( status );
1837 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001838 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001839 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001840
1841 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1842 {
1843 /* Key registration only makes sense with a secure element. */
1844 return( PSA_ERROR_INVALID_ARGUMENT );
1845 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001846#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1847
Darryl Green0c6575a2018-11-07 16:05:30 +00001848 return( status );
1849}
Gilles Peskine4747d192019-04-17 15:05:45 +02001850
1851/** Finalize the creation of a key once its key material has been set.
1852 *
1853 * This entails writing the key to persistent storage.
1854 *
1855 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001856 * See the documentation of psa_start_key_creation() for the intended use
1857 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001859 * \param[in,out] slot Pointer to the slot with key material.
1860 * \param[in] driver The secure element driver for the key,
1861 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 *
1863 * \retval #PSA_SUCCESS
1864 * The key was successfully created. The handle is now valid.
1865 * \return If this function fails, the key slot is an invalid state.
1866 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001868static psa_status_t psa_finish_key_creation(
1869 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001870 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001871{
1872 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001873 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001874 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001875
1876#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001877 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001879#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1880 if( driver != NULL )
1881 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001882 psa_se_key_data_storage_t data;
1883#if defined(static_assert)
1884 static_assert( sizeof( slot->data.se.slot_number ) ==
1885 sizeof( data.slot_number ),
1886 "Slot number size does not match psa_se_key_data_storage_t" );
1887 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1888 "Bit-size size does not match psa_se_key_data_storage_t" );
1889#endif
1890 memcpy( &data.slot_number, &slot->data.se.slot_number,
1891 sizeof( slot->data.se.slot_number ) );
1892 memcpy( &data.bits, &slot->attr.bits,
1893 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001894 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001895 (uint8_t*) &data,
1896 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001897 }
1898 else
1899#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1900 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001901 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001902 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001903 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001904 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1905 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001906 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001907 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1908 status = psa_internal_export_key( slot,
1909 buffer, buffer_size, &length,
1910 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001911 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001912 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001913 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001914
Gilles Peskinef9168942019-09-12 19:20:29 +02001915 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001916 mbedtls_free( buffer );
1917 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001918 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001919#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1920
Gilles Peskinecbaff462019-07-12 23:46:04 +02001921#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001922 /* Finish the transaction for a key creation. This does not
1923 * happen when registering an existing key. Detect this case
1924 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001925 * creation of a persistent key in a secure element requires a transaction,
1926 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001927 if( driver != NULL &&
1928 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929 {
1930 status = psa_save_se_persistent_data( driver );
1931 if( status != PSA_SUCCESS )
1932 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001933 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001934 return( status );
1935 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001936 status = psa_crypto_stop_transaction( );
1937 if( status != PSA_SUCCESS )
1938 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001939 }
1940#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1941
Gilles Peskine4747d192019-04-17 15:05:45 +02001942 return( status );
1943}
1944
1945/** Abort the creation of a key.
1946 *
1947 * You may call this function after calling psa_start_key_creation(),
1948 * or after psa_finish_key_creation() fails. In other circumstances, this
1949 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001950 * See the documentation of psa_start_key_creation() for the intended use
1951 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001952 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001953 * \param[in,out] slot Pointer to the slot with key material.
1954 * \param[in] driver The secure element driver for the key,
1955 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001956 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001957static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001958 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001959{
Gilles Peskine011e4282019-06-26 18:34:38 +02001960 (void) driver;
1961
Gilles Peskine4747d192019-04-17 15:05:45 +02001962 if( slot == NULL )
1963 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001964
1965#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001966 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 * element, and the failure happened later (when saving metadata
1968 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001969 * element.
1970 * https://github.com/ARMmbed/mbed-crypto/issues/217
1971 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001972
Gilles Peskined7729582019-08-05 15:55:54 +02001973 /* Abort the ongoing transaction if any (there may not be one if
1974 * the creation process failed before starting one, or if the
1975 * key creation is a registration of a key in a secure element).
1976 * Earlier functions must already have done what it takes to undo any
1977 * partial creation. All that's left is to update the transaction data
1978 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001979 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001980#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1981
Gilles Peskine4747d192019-04-17 15:05:45 +02001982 psa_wipe_key_slot( slot );
1983}
1984
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001985/** Validate optional attributes during key creation.
1986 *
1987 * Some key attributes are optional during key creation. If they are
1988 * specified in the attributes structure, check that they are consistent
1989 * with the data in the slot.
1990 *
1991 * This function should be called near the end of key creation, after
1992 * the slot in memory is fully populated but before saving persistent data.
1993 */
1994static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 const psa_key_slot_t *slot,
1996 const psa_key_attributes_t *attributes )
1997{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001998 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002000 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 return( PSA_ERROR_INVALID_ARGUMENT );
2002 }
2003
2004 if( attributes->domain_parameters_size != 0 )
2005 {
2006#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002007 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 {
Steven Cooremana01795d2020-07-24 22:48:15 +02002009 mbedtls_rsa_context rsa;
2010 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2011
2012 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
2013 if( status != PSA_SUCCESS )
2014 return status;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00002016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 mbedtls_mpi_init( &actual );
2018 mbedtls_mpi_init( &required );
Steven Cooremana01795d2020-07-24 22:48:15 +02002019 ret = mbedtls_rsa_export( &rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana01795d2020-07-24 22:48:15 +02002021 mbedtls_rsa_free( &rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 if( ret != 0 )
2023 goto rsa_exit;
2024 ret = mbedtls_mpi_read_binary( &required,
2025 attributes->domain_parameters,
2026 attributes->domain_parameters_size );
2027 if( ret != 0 )
2028 goto rsa_exit;
2029 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2030 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2031 rsa_exit:
2032 mbedtls_mpi_free( &actual );
2033 mbedtls_mpi_free( &required );
2034 if( ret != 0)
2035 return( mbedtls_to_psa_error( ret ) );
2036 }
2037 else
2038#endif
2039 {
2040 return( PSA_ERROR_INVALID_ARGUMENT );
2041 }
2042 }
2043
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002044 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002046 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 return( PSA_ERROR_INVALID_ARGUMENT );
2048 }
2049
2050 return( PSA_SUCCESS );
2051}
2052
Gilles Peskine4747d192019-04-17 15:05:45 +02002053psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002054 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002055 size_t data_length,
2056 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002057{
2058 psa_status_t status;
2059 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002060 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061
Gilles Peskine0f84d622019-09-12 19:03:13 +02002062 /* Reject zero-length symmetric keys (including raw data key objects).
2063 * This also rejects any key which might be encoded as an empty string,
2064 * which is never valid. */
2065 if( data_length == 0 )
2066 return( PSA_ERROR_INVALID_ARGUMENT );
2067
Gilles Peskinedf179142019-07-15 22:02:14 +02002068 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2069 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 if( status != PSA_SUCCESS )
2071 goto exit;
2072
Gilles Peskine5d309672019-07-12 23:47:28 +02002073#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2074 if( driver != NULL )
2075 {
2076 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002077 /* The driver should set the number of key bits, however in
2078 * case it doesn't, we initialize bits to an invalid value. */
2079 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002080 if( drv->key_management == NULL ||
2081 drv->key_management->p_import == NULL )
2082 {
2083 status = PSA_ERROR_NOT_SUPPORTED;
2084 goto exit;
2085 }
2086 status = drv->key_management->p_import(
2087 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002088 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002089 &bits );
2090 if( status != PSA_SUCCESS )
2091 goto exit;
2092 if( bits > PSA_MAX_KEY_BITS )
2093 {
2094 status = PSA_ERROR_NOT_SUPPORTED;
2095 goto exit;
2096 }
2097 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002098 }
2099 else
2100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2101 {
2102 status = psa_import_key_into_slot( slot, data, data_length );
2103 if( status != PSA_SUCCESS )
2104 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002105 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002106 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002107 if( status != PSA_SUCCESS )
2108 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109
Gilles Peskine011e4282019-06-26 18:34:38 +02002110 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002112 if( status != PSA_SUCCESS )
2113 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002114 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002115 *handle = 0;
2116 }
2117 return( status );
2118}
2119
Gilles Peskined7729582019-08-05 15:55:54 +02002120#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2121psa_status_t mbedtls_psa_register_se_key(
2122 const psa_key_attributes_t *attributes )
2123{
2124 psa_status_t status;
2125 psa_key_slot_t *slot = NULL;
2126 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002127 psa_key_handle_t handle = 0;
2128
2129 /* Leaving attributes unspecified is not currently supported.
2130 * It could make sense to query the key type and size from the
2131 * secure element, but not all secure elements support this
2132 * and the driver HAL doesn't currently support it. */
2133 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2134 return( PSA_ERROR_NOT_SUPPORTED );
2135 if( psa_get_key_bits( attributes ) == 0 )
2136 return( PSA_ERROR_NOT_SUPPORTED );
2137
2138 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2139 &handle, &slot, &driver );
2140 if( status != PSA_SUCCESS )
2141 goto exit;
2142
Gilles Peskined7729582019-08-05 15:55:54 +02002143 status = psa_finish_key_creation( slot, driver );
2144
2145exit:
2146 if( status != PSA_SUCCESS )
2147 {
2148 psa_fail_key_creation( slot, driver );
2149 }
2150 /* Registration doesn't keep the key in RAM. */
2151 psa_close_key( handle );
2152 return( status );
2153}
2154#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2155
Gilles Peskinef603c712019-01-19 13:40:11 +01002156static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002157 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002158{
2159 psa_status_t status;
2160 uint8_t *buffer = NULL;
2161 size_t buffer_size = 0;
2162 size_t length;
2163
Gilles Peskine8e338702019-07-30 20:06:31 +02002164 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002165 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002166 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002167 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002168 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002169 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2170 if( status != PSA_SUCCESS )
2171 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002172 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002173 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002174
2175exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002176 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002177 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002178 return( status );
2179}
2180
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2182 const psa_key_attributes_t *specified_attributes,
2183 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002184{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002186 psa_key_slot_t *source_slot = NULL;
2187 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002188 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002189 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002190
Gilles Peskine28f8f302019-07-24 13:30:31 +02002191 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002192 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002193 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002194 goto exit;
2195
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002196 status = psa_validate_optional_attributes( source_slot,
2197 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002198 if( status != PSA_SUCCESS )
2199 goto exit;
2200
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002201 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002202 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 if( status != PSA_SUCCESS )
2204 goto exit;
2205
Gilles Peskinedf179142019-07-15 22:02:14 +02002206 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2207 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002208 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209 if( status != PSA_SUCCESS )
2210 goto exit;
2211
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002212#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2213 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002214 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002215 /* Copying to a secure element is not implemented yet. */
2216 status = PSA_ERROR_NOT_SUPPORTED;
2217 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002218 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002219#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002220
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002221 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002222 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002223 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002224
Gilles Peskine011e4282019-06-26 18:34:38 +02002225 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002227 if( status != PSA_SUCCESS )
2228 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002229 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230 *target_handle = 0;
2231 }
2232 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002233}
2234
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002235
2236
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002237/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002238/* Message digests */
2239/****************************************************************/
2240
Gilles Peskineb16841e2019-10-10 20:36:12 +02002241#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002242static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002243{
2244 switch( alg )
2245 {
2246#if defined(MBEDTLS_MD2_C)
2247 case PSA_ALG_MD2:
2248 return( &mbedtls_md2_info );
2249#endif
2250#if defined(MBEDTLS_MD4_C)
2251 case PSA_ALG_MD4:
2252 return( &mbedtls_md4_info );
2253#endif
2254#if defined(MBEDTLS_MD5_C)
2255 case PSA_ALG_MD5:
2256 return( &mbedtls_md5_info );
2257#endif
2258#if defined(MBEDTLS_RIPEMD160_C)
2259 case PSA_ALG_RIPEMD160:
2260 return( &mbedtls_ripemd160_info );
2261#endif
2262#if defined(MBEDTLS_SHA1_C)
2263 case PSA_ALG_SHA_1:
2264 return( &mbedtls_sha1_info );
2265#endif
2266#if defined(MBEDTLS_SHA256_C)
2267 case PSA_ALG_SHA_224:
2268 return( &mbedtls_sha224_info );
2269 case PSA_ALG_SHA_256:
2270 return( &mbedtls_sha256_info );
2271#endif
2272#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002273#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002274 case PSA_ALG_SHA_384:
2275 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002276#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002277 case PSA_ALG_SHA_512:
2278 return( &mbedtls_sha512_info );
2279#endif
2280 default:
2281 return( NULL );
2282 }
2283}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002284#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002285
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2287{
2288 switch( operation->alg )
2289 {
Gilles Peskine81736312018-06-26 15:04:31 +02002290 case 0:
2291 /* The object has (apparently) been initialized but it is not
2292 * in use. It's ok to call abort on such an object, and there's
2293 * nothing to do. */
2294 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295#if defined(MBEDTLS_MD2_C)
2296 case PSA_ALG_MD2:
2297 mbedtls_md2_free( &operation->ctx.md2 );
2298 break;
2299#endif
2300#if defined(MBEDTLS_MD4_C)
2301 case PSA_ALG_MD4:
2302 mbedtls_md4_free( &operation->ctx.md4 );
2303 break;
2304#endif
2305#if defined(MBEDTLS_MD5_C)
2306 case PSA_ALG_MD5:
2307 mbedtls_md5_free( &operation->ctx.md5 );
2308 break;
2309#endif
2310#if defined(MBEDTLS_RIPEMD160_C)
2311 case PSA_ALG_RIPEMD160:
2312 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2313 break;
2314#endif
2315#if defined(MBEDTLS_SHA1_C)
2316 case PSA_ALG_SHA_1:
2317 mbedtls_sha1_free( &operation->ctx.sha1 );
2318 break;
2319#endif
2320#if defined(MBEDTLS_SHA256_C)
2321 case PSA_ALG_SHA_224:
2322 case PSA_ALG_SHA_256:
2323 mbedtls_sha256_free( &operation->ctx.sha256 );
2324 break;
2325#endif
2326#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002327#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002329#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 case PSA_ALG_SHA_512:
2331 mbedtls_sha512_free( &operation->ctx.sha512 );
2332 break;
2333#endif
2334 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002335 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 }
2337 operation->alg = 0;
2338 return( PSA_SUCCESS );
2339}
2340
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002341psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 psa_algorithm_t alg )
2343{
Janos Follath24eed8d2019-11-22 13:21:35 +00002344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002345
2346 /* A context must be freshly initialized before it can be set up. */
2347 if( operation->alg != 0 )
2348 {
2349 return( PSA_ERROR_BAD_STATE );
2350 }
2351
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352 switch( alg )
2353 {
2354#if defined(MBEDTLS_MD2_C)
2355 case PSA_ALG_MD2:
2356 mbedtls_md2_init( &operation->ctx.md2 );
2357 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2358 break;
2359#endif
2360#if defined(MBEDTLS_MD4_C)
2361 case PSA_ALG_MD4:
2362 mbedtls_md4_init( &operation->ctx.md4 );
2363 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2364 break;
2365#endif
2366#if defined(MBEDTLS_MD5_C)
2367 case PSA_ALG_MD5:
2368 mbedtls_md5_init( &operation->ctx.md5 );
2369 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2370 break;
2371#endif
2372#if defined(MBEDTLS_RIPEMD160_C)
2373 case PSA_ALG_RIPEMD160:
2374 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2375 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2376 break;
2377#endif
2378#if defined(MBEDTLS_SHA1_C)
2379 case PSA_ALG_SHA_1:
2380 mbedtls_sha1_init( &operation->ctx.sha1 );
2381 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2382 break;
2383#endif
2384#if defined(MBEDTLS_SHA256_C)
2385 case PSA_ALG_SHA_224:
2386 mbedtls_sha256_init( &operation->ctx.sha256 );
2387 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2388 break;
2389 case PSA_ALG_SHA_256:
2390 mbedtls_sha256_init( &operation->ctx.sha256 );
2391 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002395#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396 case PSA_ALG_SHA_384:
2397 mbedtls_sha512_init( &operation->ctx.sha512 );
2398 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2399 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002400#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401 case PSA_ALG_SHA_512:
2402 mbedtls_sha512_init( &operation->ctx.sha512 );
2403 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2404 break;
2405#endif
2406 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002407 return( PSA_ALG_IS_HASH( alg ) ?
2408 PSA_ERROR_NOT_SUPPORTED :
2409 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410 }
2411 if( ret == 0 )
2412 operation->alg = alg;
2413 else
2414 psa_hash_abort( operation );
2415 return( mbedtls_to_psa_error( ret ) );
2416}
2417
2418psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2419 const uint8_t *input,
2420 size_t input_length )
2421{
Janos Follath24eed8d2019-11-22 13:21:35 +00002422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002423
2424 /* Don't require hash implementations to behave correctly on a
2425 * zero-length input, which may have an invalid pointer. */
2426 if( input_length == 0 )
2427 return( PSA_SUCCESS );
2428
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 switch( operation->alg )
2430 {
2431#if defined(MBEDTLS_MD2_C)
2432 case PSA_ALG_MD2:
2433 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2434 input, input_length );
2435 break;
2436#endif
2437#if defined(MBEDTLS_MD4_C)
2438 case PSA_ALG_MD4:
2439 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2440 input, input_length );
2441 break;
2442#endif
2443#if defined(MBEDTLS_MD5_C)
2444 case PSA_ALG_MD5:
2445 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2446 input, input_length );
2447 break;
2448#endif
2449#if defined(MBEDTLS_RIPEMD160_C)
2450 case PSA_ALG_RIPEMD160:
2451 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2452 input, input_length );
2453 break;
2454#endif
2455#if defined(MBEDTLS_SHA1_C)
2456 case PSA_ALG_SHA_1:
2457 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2458 input, input_length );
2459 break;
2460#endif
2461#if defined(MBEDTLS_SHA256_C)
2462 case PSA_ALG_SHA_224:
2463 case PSA_ALG_SHA_256:
2464 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2465 input, input_length );
2466 break;
2467#endif
2468#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002469#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002470 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002471#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002472 case PSA_ALG_SHA_512:
2473 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2474 input, input_length );
2475 break;
2476#endif
2477 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002478 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002479 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002480
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002481 if( ret != 0 )
2482 psa_hash_abort( operation );
2483 return( mbedtls_to_psa_error( ret ) );
2484}
2485
2486psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2487 uint8_t *hash,
2488 size_t hash_size,
2489 size_t *hash_length )
2490{
itayzafrir40835d42018-08-02 13:14:17 +03002491 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002493 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494
2495 /* Fill the output buffer with something that isn't a valid hash
2496 * (barring an attack on the hash and deliberately-crafted input),
2497 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002498 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002499 /* If hash_size is 0 then hash may be NULL and then the
2500 * call to memset would have undefined behavior. */
2501 if( hash_size != 0 )
2502 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503
2504 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002505 {
2506 status = PSA_ERROR_BUFFER_TOO_SMALL;
2507 goto exit;
2508 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509
2510 switch( operation->alg )
2511 {
2512#if defined(MBEDTLS_MD2_C)
2513 case PSA_ALG_MD2:
2514 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2515 break;
2516#endif
2517#if defined(MBEDTLS_MD4_C)
2518 case PSA_ALG_MD4:
2519 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2520 break;
2521#endif
2522#if defined(MBEDTLS_MD5_C)
2523 case PSA_ALG_MD5:
2524 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2525 break;
2526#endif
2527#if defined(MBEDTLS_RIPEMD160_C)
2528 case PSA_ALG_RIPEMD160:
2529 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2530 break;
2531#endif
2532#if defined(MBEDTLS_SHA1_C)
2533 case PSA_ALG_SHA_1:
2534 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2535 break;
2536#endif
2537#if defined(MBEDTLS_SHA256_C)
2538 case PSA_ALG_SHA_224:
2539 case PSA_ALG_SHA_256:
2540 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2541 break;
2542#endif
2543#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002544#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002545 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002546#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002547 case PSA_ALG_SHA_512:
2548 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2549 break;
2550#endif
2551 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002552 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 }
itayzafrir40835d42018-08-02 13:14:17 +03002554 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002555
itayzafrir40835d42018-08-02 13:14:17 +03002556exit:
2557 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002559 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560 return( psa_hash_abort( operation ) );
2561 }
2562 else
2563 {
2564 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002565 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566 }
2567}
2568
Gilles Peskine2d277862018-06-18 15:41:12 +02002569psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2570 const uint8_t *hash,
2571 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572{
2573 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2574 size_t actual_hash_length;
2575 psa_status_t status = psa_hash_finish( operation,
2576 actual_hash, sizeof( actual_hash ),
2577 &actual_hash_length );
2578 if( status != PSA_SUCCESS )
2579 return( status );
2580 if( actual_hash_length != hash_length )
2581 return( PSA_ERROR_INVALID_SIGNATURE );
2582 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2583 return( PSA_ERROR_INVALID_SIGNATURE );
2584 return( PSA_SUCCESS );
2585}
2586
Gilles Peskine0a749c82019-11-28 19:33:58 +01002587psa_status_t psa_hash_compute( psa_algorithm_t alg,
2588 const uint8_t *input, size_t input_length,
2589 uint8_t *hash, size_t hash_size,
2590 size_t *hash_length )
2591{
2592 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2594
2595 *hash_length = hash_size;
2596 status = psa_hash_setup( &operation, alg );
2597 if( status != PSA_SUCCESS )
2598 goto exit;
2599 status = psa_hash_update( &operation, input, input_length );
2600 if( status != PSA_SUCCESS )
2601 goto exit;
2602 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2603 if( status != PSA_SUCCESS )
2604 goto exit;
2605
2606exit:
2607 if( status == PSA_SUCCESS )
2608 status = psa_hash_abort( &operation );
2609 else
2610 psa_hash_abort( &operation );
2611 return( status );
2612}
2613
2614psa_status_t psa_hash_compare( psa_algorithm_t alg,
2615 const uint8_t *input, size_t input_length,
2616 const uint8_t *hash, size_t hash_length )
2617{
2618 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2620
2621 status = psa_hash_setup( &operation, alg );
2622 if( status != PSA_SUCCESS )
2623 goto exit;
2624 status = psa_hash_update( &operation, input, input_length );
2625 if( status != PSA_SUCCESS )
2626 goto exit;
2627 status = psa_hash_verify( &operation, hash, hash_length );
2628 if( status != PSA_SUCCESS )
2629 goto exit;
2630
2631exit:
2632 if( status == PSA_SUCCESS )
2633 status = psa_hash_abort( &operation );
2634 else
2635 psa_hash_abort( &operation );
2636 return( status );
2637}
2638
Gilles Peskineeb35d782019-01-22 17:56:16 +01002639psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2640 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002641{
2642 if( target_operation->alg != 0 )
2643 return( PSA_ERROR_BAD_STATE );
2644
2645 switch( source_operation->alg )
2646 {
2647 case 0:
2648 return( PSA_ERROR_BAD_STATE );
2649#if defined(MBEDTLS_MD2_C)
2650 case PSA_ALG_MD2:
2651 mbedtls_md2_clone( &target_operation->ctx.md2,
2652 &source_operation->ctx.md2 );
2653 break;
2654#endif
2655#if defined(MBEDTLS_MD4_C)
2656 case PSA_ALG_MD4:
2657 mbedtls_md4_clone( &target_operation->ctx.md4,
2658 &source_operation->ctx.md4 );
2659 break;
2660#endif
2661#if defined(MBEDTLS_MD5_C)
2662 case PSA_ALG_MD5:
2663 mbedtls_md5_clone( &target_operation->ctx.md5,
2664 &source_operation->ctx.md5 );
2665 break;
2666#endif
2667#if defined(MBEDTLS_RIPEMD160_C)
2668 case PSA_ALG_RIPEMD160:
2669 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2670 &source_operation->ctx.ripemd160 );
2671 break;
2672#endif
2673#if defined(MBEDTLS_SHA1_C)
2674 case PSA_ALG_SHA_1:
2675 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2676 &source_operation->ctx.sha1 );
2677 break;
2678#endif
2679#if defined(MBEDTLS_SHA256_C)
2680 case PSA_ALG_SHA_224:
2681 case PSA_ALG_SHA_256:
2682 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2683 &source_operation->ctx.sha256 );
2684 break;
2685#endif
2686#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002687#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002688 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002689#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002690 case PSA_ALG_SHA_512:
2691 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2692 &source_operation->ctx.sha512 );
2693 break;
2694#endif
2695 default:
2696 return( PSA_ERROR_NOT_SUPPORTED );
2697 }
2698
2699 target_operation->alg = source_operation->alg;
2700 return( PSA_SUCCESS );
2701}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002702
2703
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002704/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705/* MAC */
2706/****************************************************************/
2707
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002708static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709 psa_algorithm_t alg,
2710 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002711 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002712 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002715 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002717 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002718 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002719
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2721 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002722 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002724 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002725 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726 mode = MBEDTLS_MODE_STREAM;
2727 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 case PSA_ALG_CTR:
2729 mode = MBEDTLS_MODE_CTR;
2730 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002731 case PSA_ALG_CFB:
2732 mode = MBEDTLS_MODE_CFB;
2733 break;
2734 case PSA_ALG_OFB:
2735 mode = MBEDTLS_MODE_OFB;
2736 break;
2737 case PSA_ALG_CBC_NO_PADDING:
2738 mode = MBEDTLS_MODE_CBC;
2739 break;
2740 case PSA_ALG_CBC_PKCS7:
2741 mode = MBEDTLS_MODE_CBC;
2742 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002743 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744 mode = MBEDTLS_MODE_CCM;
2745 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002746 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747 mode = MBEDTLS_MODE_GCM;
2748 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002749 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2750 mode = MBEDTLS_MODE_CHACHAPOLY;
2751 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 default:
2753 return( NULL );
2754 }
2755 }
2756 else if( alg == PSA_ALG_CMAC )
2757 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758 else
2759 return( NULL );
2760
2761 switch( key_type )
2762 {
2763 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002764 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765 break;
2766 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002767 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2768 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002770 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002772 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002773 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2774 * but two-key Triple-DES is functionally three-key Triple-DES
2775 * with K1=K3, so that's how we present it to mbedtls. */
2776 if( key_bits == 128 )
2777 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778 break;
2779 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002780 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781 break;
2782 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002783 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002785 case PSA_KEY_TYPE_CHACHA20:
2786 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2787 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 default:
2789 return( NULL );
2790 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002791 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002792 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793
Jaeden Amero23bbb752018-06-26 14:16:54 +01002794 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2795 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796}
2797
Gilles Peskinea05219c2018-11-16 16:02:56 +01002798#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002799static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002800{
Gilles Peskine2d277862018-06-18 15:41:12 +02002801 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002802 {
2803 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002804 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002805 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002806 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002807 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002808 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002809 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002810 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002811 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002812 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002813 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002814 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002815 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002816 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002817 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002818 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002819 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002820 return( 128 );
2821 default:
2822 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002823 }
2824}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002825#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002826
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002827/* Initialize the MAC operation structure. Once this function has been
2828 * called, psa_mac_abort can run and will do the right thing. */
2829static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2830 psa_algorithm_t alg )
2831{
2832 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2833
2834 operation->alg = alg;
2835 operation->key_set = 0;
2836 operation->iv_set = 0;
2837 operation->iv_required = 0;
2838 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002839 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002840
2841#if defined(MBEDTLS_CMAC_C)
2842 if( alg == PSA_ALG_CMAC )
2843 {
2844 operation->iv_required = 0;
2845 mbedtls_cipher_init( &operation->ctx.cmac );
2846 status = PSA_SUCCESS;
2847 }
2848 else
2849#endif /* MBEDTLS_CMAC_C */
2850#if defined(MBEDTLS_MD_C)
2851 if( PSA_ALG_IS_HMAC( operation->alg ) )
2852 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002853 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2854 operation->ctx.hmac.hash_ctx.alg = 0;
2855 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856 }
2857 else
2858#endif /* MBEDTLS_MD_C */
2859 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002860 if( ! PSA_ALG_IS_MAC( alg ) )
2861 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862 }
2863
2864 if( status != PSA_SUCCESS )
2865 memset( operation, 0, sizeof( *operation ) );
2866 return( status );
2867}
2868
Gilles Peskine01126fa2018-07-12 17:04:55 +02002869#if defined(MBEDTLS_MD_C)
2870static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2871{
Gilles Peskine3f108122018-12-07 18:14:53 +01002872 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002873 return( psa_hash_abort( &hmac->hash_ctx ) );
2874}
2875#endif /* MBEDTLS_MD_C */
2876
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2878{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002881 /* The object has (apparently) been initialized but it is not
2882 * in use. It's ok to call abort on such an object, and there's
2883 * nothing to do. */
2884 return( PSA_SUCCESS );
2885 }
2886 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002887#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002888 if( operation->alg == PSA_ALG_CMAC )
2889 {
2890 mbedtls_cipher_free( &operation->ctx.cmac );
2891 }
2892 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895 if( PSA_ALG_IS_HMAC( operation->alg ) )
2896 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002898 }
2899 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901 {
2902 /* Sanity check (shouldn't happen: operation->alg should
2903 * always have been initialized to a valid value). */
2904 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905 }
Moran Peker41deec42018-04-04 15:43:05 +03002906
Gilles Peskine8c9def32018-02-08 10:02:12 +01002907 operation->alg = 0;
2908 operation->key_set = 0;
2909 operation->iv_set = 0;
2910 operation->iv_required = 0;
2911 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002913
Gilles Peskine8c9def32018-02-08 10:02:12 +01002914 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002915
2916bad_state:
2917 /* If abort is called on an uninitialized object, we can't trust
2918 * anything. Wipe the object in case it contains confidential data.
2919 * This may result in a memory leak if a pointer gets overwritten,
2920 * but it's too late to do anything about this. */
2921 memset( operation, 0, sizeof( *operation ) );
2922 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923}
2924
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002925#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002926static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002927 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002928 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002929 const mbedtls_cipher_info_t *cipher_info )
2930{
Janos Follath24eed8d2019-11-22 13:21:35 +00002931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002932
2933 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002934
2935 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2936 if( ret != 0 )
2937 return( ret );
2938
2939 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002940 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941 key_bits );
2942 return( ret );
2943}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002944#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945
Gilles Peskine248051a2018-06-20 16:09:38 +02002946#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2948 const uint8_t *key,
2949 size_t key_length,
2950 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002952 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002953 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002954 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002955 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002956 psa_status_t status;
2957
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002958 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2959 * overflow below. This should never trigger if the hash algorithm
2960 * is implemented correctly. */
2961 /* The size checks against the ipad and opad buffers cannot be written
2962 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2963 * because that triggers -Wlogical-op on GCC 7.3. */
2964 if( block_size > sizeof( ipad ) )
2965 return( PSA_ERROR_NOT_SUPPORTED );
2966 if( block_size > sizeof( hmac->opad ) )
2967 return( PSA_ERROR_NOT_SUPPORTED );
2968 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969 return( PSA_ERROR_NOT_SUPPORTED );
2970
Gilles Peskined223b522018-06-11 18:12:58 +02002971 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002972 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002973 status = psa_hash_compute( hash_alg, key, key_length,
2974 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002976 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002977 }
Gilles Peskine96889972018-07-12 17:07:03 +02002978 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2979 * but it is permitted. It is common when HMAC is used in HKDF, for
2980 * example. Don't call `memcpy` in the 0-length because `key` could be
2981 * an invalid pointer which would make the behavior undefined. */
2982 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002983 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002984
Gilles Peskined223b522018-06-11 18:12:58 +02002985 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2986 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002987 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002988 ipad[i] ^= 0x36;
2989 memset( ipad + key_length, 0x36, block_size - key_length );
2990
2991 /* Copy the key material from ipad to opad, flipping the requisite bits,
2992 * and filling the rest of opad with the requisite constant. */
2993 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002994 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2995 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002996
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002998 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002999 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000
Gilles Peskine01126fa2018-07-12 17:04:55 +02003001 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003002
3003cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03003004 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003005
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003006 return( status );
3007}
Gilles Peskine248051a2018-06-20 16:09:38 +02003008#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003009
Gilles Peskine89167cb2018-07-08 20:12:23 +02003010static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003011 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003012 psa_algorithm_t alg,
3013 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003016 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003018 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003019 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003020 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003021 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003023 /* A context must be freshly initialized before it can be set up. */
3024 if( operation->alg != 0 )
3025 {
3026 return( PSA_ERROR_BAD_STATE );
3027 }
3028
Gilles Peskined911eb72018-08-14 15:18:45 +02003029 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003030 if( status != PSA_SUCCESS )
3031 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003032 if( is_sign )
3033 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034
Gilles Peskine28f8f302019-07-24 13:30:31 +02003035 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003036 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003037 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003038 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003039
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003041 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003042 {
3043 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003044 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003045 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003047 if( cipher_info == NULL )
3048 {
3049 status = PSA_ERROR_NOT_SUPPORTED;
3050 goto exit;
3051 }
3052 operation->mac_size = cipher_info->block_size;
3053 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3054 status = mbedtls_to_psa_error( ret );
3055 }
3056 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003059 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003060 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003061 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003062 if( hash_alg == 0 )
3063 {
3064 status = PSA_ERROR_NOT_SUPPORTED;
3065 goto exit;
3066 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003067
3068 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3069 /* Sanity check. This shouldn't fail on a valid configuration. */
3070 if( operation->mac_size == 0 ||
3071 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3072 {
3073 status = PSA_ERROR_NOT_SUPPORTED;
3074 goto exit;
3075 }
3076
Gilles Peskine8e338702019-07-30 20:06:31 +02003077 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003078 {
3079 status = PSA_ERROR_INVALID_ARGUMENT;
3080 goto exit;
3081 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003082
Gilles Peskine01126fa2018-07-12 17:04:55 +02003083 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003084 slot->data.key.data,
3085 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003086 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003087 }
3088 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003090 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003091 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003092 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003094
Gilles Peskined911eb72018-08-14 15:18:45 +02003095 if( truncated == 0 )
3096 {
3097 /* The "normal" case: untruncated algorithm. Nothing to do. */
3098 }
3099 else if( truncated < 4 )
3100 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003101 /* A very short MAC is too short for security since it can be
3102 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3103 * so we make this our minimum, even though 32 bits is still
3104 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003105 status = PSA_ERROR_NOT_SUPPORTED;
3106 }
3107 else if( truncated > operation->mac_size )
3108 {
3109 /* It's impossible to "truncate" to a larger length. */
3110 status = PSA_ERROR_INVALID_ARGUMENT;
3111 }
3112 else
3113 operation->mac_size = truncated;
3114
Gilles Peskinefbfac682018-07-08 20:51:54 +02003115exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003116 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003118 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003120 else
3121 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122 operation->key_set = 1;
3123 }
3124 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125}
3126
Gilles Peskine89167cb2018-07-08 20:12:23 +02003127psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003128 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003129 psa_algorithm_t alg )
3130{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003131 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003132}
3133
3134psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003135 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003136 psa_algorithm_t alg )
3137{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003138 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003139}
3140
Gilles Peskine8c9def32018-02-08 10:02:12 +01003141psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3142 const uint8_t *input,
3143 size_t input_length )
3144{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003145 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003147 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003148 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003149 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003150 operation->has_input = 1;
3151
Gilles Peskine8c9def32018-02-08 10:02:12 +01003152#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003153 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003154 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003155 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3156 input, input_length );
3157 status = mbedtls_to_psa_error( ret );
3158 }
3159 else
3160#endif /* MBEDTLS_CMAC_C */
3161#if defined(MBEDTLS_MD_C)
3162 if( PSA_ALG_IS_HMAC( operation->alg ) )
3163 {
3164 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3165 input_length );
3166 }
3167 else
3168#endif /* MBEDTLS_MD_C */
3169 {
3170 /* This shouldn't happen if `operation` was initialized by
3171 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003172 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003173 }
3174
Gilles Peskinefbfac682018-07-08 20:51:54 +02003175 if( status != PSA_SUCCESS )
3176 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003177 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003178}
3179
Gilles Peskine01126fa2018-07-12 17:04:55 +02003180#if defined(MBEDTLS_MD_C)
3181static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3182 uint8_t *mac,
3183 size_t mac_size )
3184{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003185 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003186 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3187 size_t hash_size = 0;
3188 size_t block_size = psa_get_hash_block_size( hash_alg );
3189 psa_status_t status;
3190
Gilles Peskine01126fa2018-07-12 17:04:55 +02003191 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3192 if( status != PSA_SUCCESS )
3193 return( status );
3194 /* From here on, tmp needs to be wiped. */
3195
3196 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3197 if( status != PSA_SUCCESS )
3198 goto exit;
3199
3200 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3201 if( status != PSA_SUCCESS )
3202 goto exit;
3203
3204 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3205 if( status != PSA_SUCCESS )
3206 goto exit;
3207
Gilles Peskined911eb72018-08-14 15:18:45 +02003208 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3209 if( status != PSA_SUCCESS )
3210 goto exit;
3211
3212 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003213
3214exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003215 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003216 return( status );
3217}
3218#endif /* MBEDTLS_MD_C */
3219
mohammad16036df908f2018-04-02 08:34:15 -07003220static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003221 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003222 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003223{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003224 if( ! operation->key_set )
3225 return( PSA_ERROR_BAD_STATE );
3226 if( operation->iv_required && ! operation->iv_set )
3227 return( PSA_ERROR_BAD_STATE );
3228
Gilles Peskine8c9def32018-02-08 10:02:12 +01003229 if( mac_size < operation->mac_size )
3230 return( PSA_ERROR_BUFFER_TOO_SMALL );
3231
Gilles Peskine8c9def32018-02-08 10:02:12 +01003232#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003233 if( operation->alg == PSA_ALG_CMAC )
3234 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003235 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3236 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3237 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003238 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003239 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003240 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003241 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003242 else
3243#endif /* MBEDTLS_CMAC_C */
3244#if defined(MBEDTLS_MD_C)
3245 if( PSA_ALG_IS_HMAC( operation->alg ) )
3246 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003247 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003248 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003249 }
3250 else
3251#endif /* MBEDTLS_MD_C */
3252 {
3253 /* This shouldn't happen if `operation` was initialized by
3254 * a setup function. */
3255 return( PSA_ERROR_BAD_STATE );
3256 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257}
3258
Gilles Peskineacd4be32018-07-08 19:56:25 +02003259psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3260 uint8_t *mac,
3261 size_t mac_size,
3262 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003263{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003264 psa_status_t status;
3265
Jaeden Amero252ef282019-02-15 14:05:35 +00003266 if( operation->alg == 0 )
3267 {
3268 return( PSA_ERROR_BAD_STATE );
3269 }
3270
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003271 /* Fill the output buffer with something that isn't a valid mac
3272 * (barring an attack on the mac and deliberately-crafted input),
3273 * in case the caller doesn't check the return status properly. */
3274 *mac_length = mac_size;
3275 /* If mac_size is 0 then mac may be NULL and then the
3276 * call to memset would have undefined behavior. */
3277 if( mac_size != 0 )
3278 memset( mac, '!', mac_size );
3279
Gilles Peskine89167cb2018-07-08 20:12:23 +02003280 if( ! operation->is_sign )
3281 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003282 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003283 }
mohammad16036df908f2018-04-02 08:34:15 -07003284
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003285 status = psa_mac_finish_internal( operation, mac, mac_size );
3286
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003287 if( status == PSA_SUCCESS )
3288 {
3289 status = psa_mac_abort( operation );
3290 if( status == PSA_SUCCESS )
3291 *mac_length = operation->mac_size;
3292 else
3293 memset( mac, '!', mac_size );
3294 }
3295 else
3296 psa_mac_abort( operation );
3297 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003298}
3299
Gilles Peskineacd4be32018-07-08 19:56:25 +02003300psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3301 const uint8_t *mac,
3302 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003303{
Gilles Peskine828ed142018-06-18 23:25:51 +02003304 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003305 psa_status_t status;
3306
Jaeden Amero252ef282019-02-15 14:05:35 +00003307 if( operation->alg == 0 )
3308 {
3309 return( PSA_ERROR_BAD_STATE );
3310 }
3311
Gilles Peskine89167cb2018-07-08 20:12:23 +02003312 if( operation->is_sign )
3313 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003314 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003315 }
3316 if( operation->mac_size != mac_length )
3317 {
3318 status = PSA_ERROR_INVALID_SIGNATURE;
3319 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003320 }
mohammad16036df908f2018-04-02 08:34:15 -07003321
3322 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003323 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003324 if( status != PSA_SUCCESS )
3325 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003326
3327 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3328 status = PSA_ERROR_INVALID_SIGNATURE;
3329
3330cleanup:
3331 if( status == PSA_SUCCESS )
3332 status = psa_mac_abort( operation );
3333 else
3334 psa_mac_abort( operation );
3335
Gilles Peskine3f108122018-12-07 18:14:53 +01003336 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003337
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003338 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003339}
3340
3341
Gilles Peskine20035e32018-02-03 22:44:14 +01003342
Gilles Peskine20035e32018-02-03 22:44:14 +01003343/****************************************************************/
3344/* Asymmetric cryptography */
3345/****************************************************************/
3346
Gilles Peskine2b450e32018-06-27 15:42:46 +02003347#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003348/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003349 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003350static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3351 size_t hash_length,
3352 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003353{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003354 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003355 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003356 *md_alg = mbedtls_md_get_type( md_info );
3357
3358 /* The Mbed TLS RSA module uses an unsigned int for hash length
3359 * parameters. Validate that it fits so that we don't risk an
3360 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003361#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003362 if( hash_length > UINT_MAX )
3363 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003364#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003365
3366#if defined(MBEDTLS_PKCS1_V15)
3367 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3368 * must be correct. */
3369 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3370 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003371 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003372 if( md_info == NULL )
3373 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003374 if( mbedtls_md_get_size( md_info ) != hash_length )
3375 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003376 }
3377#endif /* MBEDTLS_PKCS1_V15 */
3378
3379#if defined(MBEDTLS_PKCS1_V21)
3380 /* PSS requires a hash internally. */
3381 if( PSA_ALG_IS_RSA_PSS( alg ) )
3382 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003383 if( md_info == NULL )
3384 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003385 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003386#endif /* MBEDTLS_PKCS1_V21 */
3387
Gilles Peskine61b91d42018-06-08 16:09:36 +02003388 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003389}
3390
Gilles Peskine2b450e32018-06-27 15:42:46 +02003391static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3392 psa_algorithm_t alg,
3393 const uint8_t *hash,
3394 size_t hash_length,
3395 uint8_t *signature,
3396 size_t signature_size,
3397 size_t *signature_length )
3398{
3399 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003401 mbedtls_md_type_t md_alg;
3402
3403 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3404 if( status != PSA_SUCCESS )
3405 return( status );
3406
Gilles Peskine630a18a2018-06-29 17:49:35 +02003407 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003408 return( PSA_ERROR_BUFFER_TOO_SMALL );
3409
3410#if defined(MBEDTLS_PKCS1_V15)
3411 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3412 {
3413 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3414 MBEDTLS_MD_NONE );
3415 ret = mbedtls_rsa_pkcs1_sign( rsa,
3416 mbedtls_ctr_drbg_random,
3417 &global_data.ctr_drbg,
3418 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003419 md_alg,
3420 (unsigned int) hash_length,
3421 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003422 signature );
3423 }
3424 else
3425#endif /* MBEDTLS_PKCS1_V15 */
3426#if defined(MBEDTLS_PKCS1_V21)
3427 if( PSA_ALG_IS_RSA_PSS( alg ) )
3428 {
3429 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3430 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3431 mbedtls_ctr_drbg_random,
3432 &global_data.ctr_drbg,
3433 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003434 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003435 (unsigned int) hash_length,
3436 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003437 signature );
3438 }
3439 else
3440#endif /* MBEDTLS_PKCS1_V21 */
3441 {
3442 return( PSA_ERROR_INVALID_ARGUMENT );
3443 }
3444
3445 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003446 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003447 return( mbedtls_to_psa_error( ret ) );
3448}
3449
3450static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3451 psa_algorithm_t alg,
3452 const uint8_t *hash,
3453 size_t hash_length,
3454 const uint8_t *signature,
3455 size_t signature_length )
3456{
3457 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003459 mbedtls_md_type_t md_alg;
3460
3461 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3462 if( status != PSA_SUCCESS )
3463 return( status );
3464
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003465 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3466 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003467
3468#if defined(MBEDTLS_PKCS1_V15)
3469 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3470 {
3471 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3472 MBEDTLS_MD_NONE );
3473 ret = mbedtls_rsa_pkcs1_verify( rsa,
3474 mbedtls_ctr_drbg_random,
3475 &global_data.ctr_drbg,
3476 MBEDTLS_RSA_PUBLIC,
3477 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003478 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003479 hash,
3480 signature );
3481 }
3482 else
3483#endif /* MBEDTLS_PKCS1_V15 */
3484#if defined(MBEDTLS_PKCS1_V21)
3485 if( PSA_ALG_IS_RSA_PSS( alg ) )
3486 {
3487 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3488 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3489 mbedtls_ctr_drbg_random,
3490 &global_data.ctr_drbg,
3491 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003492 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003493 (unsigned int) hash_length,
3494 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003495 signature );
3496 }
3497 else
3498#endif /* MBEDTLS_PKCS1_V21 */
3499 {
3500 return( PSA_ERROR_INVALID_ARGUMENT );
3501 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003502
3503 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3504 * the rest of the signature is invalid". This has little use in
3505 * practice and PSA doesn't report this distinction. */
3506 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3507 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003508 return( mbedtls_to_psa_error( ret ) );
3509}
3510#endif /* MBEDTLS_RSA_C */
3511
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003512#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003513/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3514 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3515 * (even though these functions don't modify it). */
3516static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3517 psa_algorithm_t alg,
3518 const uint8_t *hash,
3519 size_t hash_length,
3520 uint8_t *signature,
3521 size_t signature_size,
3522 size_t *signature_length )
3523{
Janos Follath24eed8d2019-11-22 13:21:35 +00003524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003526 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003527 mbedtls_mpi_init( &r );
3528 mbedtls_mpi_init( &s );
3529
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003530 if( signature_size < 2 * curve_bytes )
3531 {
3532 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3533 goto cleanup;
3534 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003535
Gilles Peskinea05219c2018-11-16 16:02:56 +01003536#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003537 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3538 {
3539 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3540 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3541 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003542 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3543 &ecp->d, hash,
3544 hash_length, md_alg,
3545 mbedtls_ctr_drbg_random,
3546 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003547 }
3548 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003549#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003550 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003551 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003552 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3553 hash, hash_length,
3554 mbedtls_ctr_drbg_random,
3555 &global_data.ctr_drbg ) );
3556 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003557
3558 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3559 signature,
3560 curve_bytes ) );
3561 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3562 signature + curve_bytes,
3563 curve_bytes ) );
3564
3565cleanup:
3566 mbedtls_mpi_free( &r );
3567 mbedtls_mpi_free( &s );
3568 if( ret == 0 )
3569 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003570 return( mbedtls_to_psa_error( ret ) );
3571}
3572
3573static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3574 const uint8_t *hash,
3575 size_t hash_length,
3576 const uint8_t *signature,
3577 size_t signature_length )
3578{
Janos Follath24eed8d2019-11-22 13:21:35 +00003579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003580 mbedtls_mpi r, s;
3581 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3582 mbedtls_mpi_init( &r );
3583 mbedtls_mpi_init( &s );
3584
3585 if( signature_length != 2 * curve_bytes )
3586 return( PSA_ERROR_INVALID_SIGNATURE );
3587
3588 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3589 signature,
3590 curve_bytes ) );
3591 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3592 signature + curve_bytes,
3593 curve_bytes ) );
3594
Steven Cooremanacda8342020-07-24 23:09:52 +02003595 /* Check whether the public part is loaded. If not, load it. */
3596 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3597 {
3598 MBEDTLS_MPI_CHK(
3599 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3600 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3601 }
3602
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003603 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3604 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003605
3606cleanup:
3607 mbedtls_mpi_free( &r );
3608 mbedtls_mpi_free( &s );
3609 return( mbedtls_to_psa_error( ret ) );
3610}
3611#endif /* MBEDTLS_ECDSA_C */
3612
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003613psa_status_t psa_sign_hash( psa_key_handle_t handle,
3614 psa_algorithm_t alg,
3615 const uint8_t *hash,
3616 size_t hash_length,
3617 uint8_t *signature,
3618 size_t signature_size,
3619 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003620{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003621 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003622 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003623#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3624 const psa_drv_se_t *drv;
3625 psa_drv_se_context_t *drv_context;
3626#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003627
3628 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003629 /* Immediately reject a zero-length signature buffer. This guarantees
3630 * that signature must be a valid pointer. (On the other hand, the hash
3631 * buffer can in principle be empty since it doesn't actually have
3632 * to be a hash.) */
3633 if( signature_size == 0 )
3634 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003635
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003636 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003637 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003638 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003639 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003640 {
3641 status = PSA_ERROR_INVALID_ARGUMENT;
3642 goto exit;
3643 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003644
Gilles Peskineedc64242019-08-07 21:05:07 +02003645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3646 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3647 {
3648 if( drv->asymmetric == NULL ||
3649 drv->asymmetric->p_sign == NULL )
3650 {
3651 status = PSA_ERROR_NOT_SUPPORTED;
3652 goto exit;
3653 }
3654 status = drv->asymmetric->p_sign( drv_context,
3655 slot->data.se.slot_number,
3656 alg,
3657 hash, hash_length,
3658 signature, signature_size,
3659 signature_length );
3660 }
3661 else
3662#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003663#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003664 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003665 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003666 mbedtls_rsa_context rsa;
3667 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3668
3669 status = psa_load_rsa_representation( slot,
3670 &rsa );
3671 if( status != PSA_SUCCESS )
3672 goto exit;
3673
3674 status = psa_rsa_sign( &rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003675 alg,
3676 hash, hash_length,
3677 signature, signature_size,
3678 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003679
3680 mbedtls_rsa_free( &rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003681 }
3682 else
3683#endif /* defined(MBEDTLS_RSA_C) */
3684#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003685 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003686 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003687#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003688 if(
3689#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3690 PSA_ALG_IS_ECDSA( alg )
3691#else
3692 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3693#endif
3694 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003695 {
3696 mbedtls_ecp_keypair ecp;
3697 status = psa_load_ecp_representation( slot, &ecp );
3698 if( status != PSA_SUCCESS )
3699 goto exit;
3700 status = psa_ecdsa_sign( &ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003701 alg,
3702 hash, hash_length,
3703 signature, signature_size,
3704 signature_length );
Steven Cooremanacda8342020-07-24 23:09:52 +02003705 mbedtls_ecp_keypair_free( &ecp );
3706 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003707 else
3708#endif /* defined(MBEDTLS_ECDSA_C) */
3709 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003710 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003711 }
itayzafrir5c753392018-05-08 11:18:38 +03003712 }
3713 else
3714#endif /* defined(MBEDTLS_ECP_C) */
3715 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003716 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003717 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003718
3719exit:
3720 /* Fill the unused part of the output buffer (the whole buffer on error,
3721 * the trailing part on success) with something that isn't a valid mac
3722 * (barring an attack on the mac and deliberately-crafted input),
3723 * in case the caller doesn't check the return status properly. */
3724 if( status == PSA_SUCCESS )
3725 memset( signature + *signature_length, '!',
3726 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003727 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003728 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003729 /* If signature_size is 0 then we have nothing to do. We must not call
3730 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003731 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003732}
3733
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003734psa_status_t psa_verify_hash( psa_key_handle_t handle,
3735 psa_algorithm_t alg,
3736 const uint8_t *hash,
3737 size_t hash_length,
3738 const uint8_t *signature,
3739 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003740{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003741 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003742 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003743#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3744 const psa_drv_se_t *drv;
3745 psa_drv_se_context_t *drv_context;
3746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003747
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003748 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003749 if( status != PSA_SUCCESS )
3750 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003751
Gilles Peskineedc64242019-08-07 21:05:07 +02003752#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3753 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3754 {
3755 if( drv->asymmetric == NULL ||
3756 drv->asymmetric->p_verify == NULL )
3757 return( PSA_ERROR_NOT_SUPPORTED );
3758 return( drv->asymmetric->p_verify( drv_context,
3759 slot->data.se.slot_number,
3760 alg,
3761 hash, hash_length,
3762 signature, signature_length ) );
3763 }
3764 else
3765#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003766#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003767 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003769 mbedtls_rsa_context rsa;
3770 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3771
3772 status = psa_load_rsa_representation( slot, &rsa );
3773 if( status != PSA_SUCCESS )
3774 return status;
3775
3776 status = psa_rsa_verify( &rsa,
3777 alg,
3778 hash, hash_length,
3779 signature, signature_length );
3780 mbedtls_rsa_free( &rsa );
3781 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003782 }
3783 else
3784#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003785#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003786 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003787 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003788#if defined(MBEDTLS_ECDSA_C)
3789 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003790 {
3791 mbedtls_ecp_keypair ecp;
3792 status = psa_load_ecp_representation( slot, &ecp );
3793 if( status != PSA_SUCCESS )
3794 return status;
3795 status = psa_ecdsa_verify( &ecp,
3796 hash, hash_length,
3797 signature, signature_length );
3798 mbedtls_ecp_keypair_free( &ecp );
3799 return status;
3800 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003801 else
3802#endif /* defined(MBEDTLS_ECDSA_C) */
3803 {
3804 return( PSA_ERROR_INVALID_ARGUMENT );
3805 }
itayzafrir5c753392018-05-08 11:18:38 +03003806 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003807 else
3808#endif /* defined(MBEDTLS_ECP_C) */
3809 {
3810 return( PSA_ERROR_NOT_SUPPORTED );
3811 }
3812}
3813
Gilles Peskine072ac562018-06-30 00:21:29 +02003814#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3815static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3816 mbedtls_rsa_context *rsa )
3817{
3818 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3819 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3820 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3821 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3822}
3823#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3824
Gilles Peskinec5487a82018-12-03 18:08:14 +01003825psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003826 psa_algorithm_t alg,
3827 const uint8_t *input,
3828 size_t input_length,
3829 const uint8_t *salt,
3830 size_t salt_length,
3831 uint8_t *output,
3832 size_t output_size,
3833 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003834{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003835 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003836 psa_status_t status;
3837
Darryl Green5cc689a2018-07-24 15:34:10 +01003838 (void) input;
3839 (void) input_length;
3840 (void) salt;
3841 (void) output;
3842 (void) output_size;
3843
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003844 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003845
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003846 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3847 return( PSA_ERROR_INVALID_ARGUMENT );
3848
Gilles Peskine28f8f302019-07-24 13:30:31 +02003849 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003850 if( status != PSA_SUCCESS )
3851 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003852 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3853 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003854 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855
3856#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003857 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003859 mbedtls_rsa_context rsa;
3860 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3861
3862 status = psa_load_rsa_representation( slot, &rsa );
3863 if( status != PSA_SUCCESS )
3864 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003866 if( output_size < mbedtls_rsa_get_len( &rsa ) )
3867 {
3868 mbedtls_rsa_free( &rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003869 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003870 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003872 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003874 ret = mbedtls_rsa_pkcs1_encrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003875 mbedtls_ctr_drbg_random,
3876 &global_data.ctr_drbg,
3877 MBEDTLS_RSA_PUBLIC,
3878 input_length,
3879 input,
3880 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 }
3882 else
3883#endif /* MBEDTLS_PKCS1_V15 */
3884#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003885 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003887 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3888 ret = mbedtls_rsa_rsaes_oaep_encrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003889 mbedtls_ctr_drbg_random,
3890 &global_data.ctr_drbg,
3891 MBEDTLS_RSA_PUBLIC,
3892 salt, salt_length,
3893 input_length,
3894 input,
3895 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896 }
3897 else
3898#endif /* MBEDTLS_PKCS1_V21 */
3899 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003900 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 return( PSA_ERROR_INVALID_ARGUMENT );
3902 }
3903 if( ret == 0 )
Steven Cooremana01795d2020-07-24 22:48:15 +02003904 *output_length = mbedtls_rsa_get_len( &rsa );
3905
3906 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003907 return( mbedtls_to_psa_error( ret ) );
3908 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003910#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911 {
3912 return( PSA_ERROR_NOT_SUPPORTED );
3913 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003914}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915
Gilles Peskinec5487a82018-12-03 18:08:14 +01003916psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003917 psa_algorithm_t alg,
3918 const uint8_t *input,
3919 size_t input_length,
3920 const uint8_t *salt,
3921 size_t salt_length,
3922 uint8_t *output,
3923 size_t output_size,
3924 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003926 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003927 psa_status_t status;
3928
Darryl Green5cc689a2018-07-24 15:34:10 +01003929 (void) input;
3930 (void) input_length;
3931 (void) salt;
3932 (void) output;
3933 (void) output_size;
3934
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003935 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003937 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3938 return( PSA_ERROR_INVALID_ARGUMENT );
3939
Gilles Peskine28f8f302019-07-24 13:30:31 +02003940 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003941 if( status != PSA_SUCCESS )
3942 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003943 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 return( PSA_ERROR_INVALID_ARGUMENT );
3945
3946#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003947 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003949 mbedtls_rsa_context rsa;
3950 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3951
3952 status = psa_load_rsa_representation( slot, &rsa );
3953 if( status != PSA_SUCCESS )
3954 return status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956
Steven Cooremana01795d2020-07-24 22:48:15 +02003957 if( input_length != mbedtls_rsa_get_len( &rsa ) )
3958 {
3959 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003961 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962
3963#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003964 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003966 ret = mbedtls_rsa_pkcs1_decrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003967 mbedtls_ctr_drbg_random,
3968 &global_data.ctr_drbg,
3969 MBEDTLS_RSA_PRIVATE,
3970 output_length,
3971 input,
3972 output,
3973 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003974 }
3975 else
3976#endif /* MBEDTLS_PKCS1_V15 */
3977#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003978 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003980 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3981 ret = mbedtls_rsa_rsaes_oaep_decrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003982 mbedtls_ctr_drbg_random,
3983 &global_data.ctr_drbg,
3984 MBEDTLS_RSA_PRIVATE,
3985 salt, salt_length,
3986 output_length,
3987 input,
3988 output,
3989 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990 }
3991 else
3992#endif /* MBEDTLS_PKCS1_V21 */
3993 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003994 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 return( PSA_ERROR_INVALID_ARGUMENT );
3996 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003997
Steven Cooremana01795d2020-07-24 22:48:15 +02003998 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003999 return( mbedtls_to_psa_error( ret ) );
4000 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004002#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003 {
4004 return( PSA_ERROR_NOT_SUPPORTED );
4005 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004006}
Gilles Peskine20035e32018-02-03 22:44:14 +01004007
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004008
4009
mohammad1603503973b2018-03-12 15:59:30 +02004010/****************************************************************/
4011/* Symmetric cryptography */
4012/****************************************************************/
4013
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004014/* Initialize the cipher operation structure. Once this function has been
4015 * called, psa_cipher_abort can run and will do the right thing. */
4016static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4017 psa_algorithm_t alg )
4018{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004019 if( ! PSA_ALG_IS_CIPHER( alg ) )
4020 {
4021 memset( operation, 0, sizeof( *operation ) );
4022 return( PSA_ERROR_INVALID_ARGUMENT );
4023 }
4024
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004025 operation->alg = alg;
4026 operation->key_set = 0;
4027 operation->iv_set = 0;
4028 operation->iv_required = 1;
4029 operation->iv_size = 0;
4030 operation->block_size = 0;
4031 mbedtls_cipher_init( &operation->ctx.cipher );
4032 return( PSA_SUCCESS );
4033}
4034
Gilles Peskinee553c652018-06-04 16:22:46 +02004035static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004036 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004037 psa_algorithm_t alg,
4038 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004039{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004040 int ret = 0;
4041 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004042 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004043 size_t key_bits;
4044 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004045 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4046 PSA_KEY_USAGE_ENCRYPT :
4047 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004048
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004049 /* A context must be freshly initialized before it can be set up. */
4050 if( operation->alg != 0 )
4051 {
4052 return( PSA_ERROR_BAD_STATE );
4053 }
4054
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004055 status = psa_cipher_init( operation, alg );
4056 if( status != PSA_SUCCESS )
4057 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004058
Gilles Peskine28f8f302019-07-24 13:30:31 +02004059 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004060 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004061 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004062 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004063
Gilles Peskine8e338702019-07-30 20:06:31 +02004064 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004065 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004066 {
4067 status = PSA_ERROR_NOT_SUPPORTED;
4068 goto exit;
4069 }
mohammad1603503973b2018-03-12 15:59:30 +02004070
mohammad1603503973b2018-03-12 15:59:30 +02004071 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004072 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004073 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004074
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004075#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004076 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004077 {
4078 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004079 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004080 memcpy( keys, slot->data.key.data, 16 );
4081 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004082 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4083 keys,
4084 192, cipher_operation );
4085 }
4086 else
4087#endif
4088 {
4089 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004090 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004091 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004092 }
Moran Peker41deec42018-04-04 15:43:05 +03004093 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004094 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004095
mohammad16038481e742018-03-18 13:57:31 +02004096#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004097 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004098 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004099 case PSA_ALG_CBC_NO_PADDING:
4100 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4101 MBEDTLS_PADDING_NONE );
4102 break;
4103 case PSA_ALG_CBC_PKCS7:
4104 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4105 MBEDTLS_PADDING_PKCS7 );
4106 break;
4107 default:
4108 /* The algorithm doesn't involve padding. */
4109 ret = 0;
4110 break;
4111 }
4112 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004113 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004114#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4115
mohammad1603503973b2018-03-12 15:59:30 +02004116 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004117 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004118 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004119 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004120 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004121 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004122 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004123#if defined(MBEDTLS_CHACHA20_C)
4124 else
4125 if( alg == PSA_ALG_CHACHA20 )
4126 operation->iv_size = 12;
4127#endif
mohammad1603503973b2018-03-12 15:59:30 +02004128
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004129exit:
4130 if( status == 0 )
4131 status = mbedtls_to_psa_error( ret );
4132 if( status != 0 )
4133 psa_cipher_abort( operation );
4134 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004135}
4136
Gilles Peskinefe119512018-07-08 21:39:34 +02004137psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004138 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004139 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004140{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004141 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004142}
4143
Gilles Peskinefe119512018-07-08 21:39:34 +02004144psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004145 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004146 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004147{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004149}
4150
Gilles Peskinefe119512018-07-08 21:39:34 +02004151psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004152 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004153 size_t iv_size,
4154 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004155{
itayzafrir534bd7c2018-08-02 13:56:32 +03004156 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004158 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004159 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004160 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004161 }
Moran Peker41deec42018-04-04 15:43:05 +03004162 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004163 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004164 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004165 goto exit;
4166 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004167 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4168 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004169 if( ret != 0 )
4170 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004171 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004172 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004173 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004174
mohammad16038481e742018-03-18 13:57:31 +02004175 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004176 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004177
Moran Peker395db872018-05-31 14:07:14 +03004178exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004179 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004180 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004181 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004182}
4183
Gilles Peskinefe119512018-07-08 21:39:34 +02004184psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004185 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004186 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004187{
itayzafrir534bd7c2018-08-02 13:56:32 +03004188 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004190 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004191 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004192 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004193 }
Moran Pekera28258c2018-05-29 16:25:04 +03004194 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004195 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004196 status = PSA_ERROR_INVALID_ARGUMENT;
4197 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004198 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004199 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4200 status = mbedtls_to_psa_error( ret );
4201exit:
4202 if( status == PSA_SUCCESS )
4203 operation->iv_set = 1;
4204 else
mohammad1603503973b2018-03-12 15:59:30 +02004205 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004206 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004207}
4208
Gilles Peskinee553c652018-06-04 16:22:46 +02004209psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4210 const uint8_t *input,
4211 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004212 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004213 size_t output_size,
4214 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004215{
itayzafrir534bd7c2018-08-02 13:56:32 +03004216 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004218 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004219
4220 if( operation->alg == 0 )
4221 {
4222 return( PSA_ERROR_BAD_STATE );
4223 }
4224
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004225 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004226 {
4227 /* Take the unprocessed partial block left over from previous
4228 * update calls, if any, plus the input to this call. Remove
4229 * the last partial block, if any. You get the data that will be
4230 * output in this call. */
4231 expected_output_size =
4232 ( operation->ctx.cipher.unprocessed_len + input_length )
4233 / operation->block_size * operation->block_size;
4234 }
4235 else
4236 {
4237 expected_output_size = input_length;
4238 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004239
Gilles Peskine89d789c2018-06-04 17:17:16 +02004240 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004241 {
4242 status = PSA_ERROR_BUFFER_TOO_SMALL;
4243 goto exit;
4244 }
mohammad160382759612018-03-12 18:16:40 +02004245
mohammad1603503973b2018-03-12 15:59:30 +02004246 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004247 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004248 status = mbedtls_to_psa_error( ret );
4249exit:
4250 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004251 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004252 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004253}
4254
Gilles Peskinee553c652018-06-04 16:22:46 +02004255psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4256 uint8_t *output,
4257 size_t output_size,
4258 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004259{
David Saadab4ecc272019-02-14 13:48:10 +02004260 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004261 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004262 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004263
mohammad1603503973b2018-03-12 15:59:30 +02004264 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004265 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004266 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004267 }
4268 if( operation->iv_required && ! operation->iv_set )
4269 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004270 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004271 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004272
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004273 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004274 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4275 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004276 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004277 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004278 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004279 }
4280
Janos Follath315b51c2018-07-09 16:04:51 +01004281 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4282 temp_output_buffer,
4283 output_length );
4284 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004285 {
Janos Follath315b51c2018-07-09 16:04:51 +01004286 status = mbedtls_to_psa_error( cipher_ret );
4287 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004288 }
Janos Follath315b51c2018-07-09 16:04:51 +01004289
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004290 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004291 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004292 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004293 memcpy( output, temp_output_buffer, *output_length );
4294 else
4295 {
Janos Follath315b51c2018-07-09 16:04:51 +01004296 status = PSA_ERROR_BUFFER_TOO_SMALL;
4297 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004298 }
mohammad1603503973b2018-03-12 15:59:30 +02004299
Gilles Peskine3f108122018-12-07 18:14:53 +01004300 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004301 status = psa_cipher_abort( operation );
4302
4303 return( status );
4304
4305error:
4306
4307 *output_length = 0;
4308
Gilles Peskine3f108122018-12-07 18:14:53 +01004309 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004310 (void) psa_cipher_abort( operation );
4311
4312 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004313}
4314
Gilles Peskinee553c652018-06-04 16:22:46 +02004315psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4316{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004317 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004318 {
4319 /* The object has (apparently) been initialized but it is not
4320 * in use. It's ok to call abort on such an object, and there's
4321 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004322 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004323 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004324
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004325 /* Sanity check (shouldn't happen: operation->alg should
4326 * always have been initialized to a valid value). */
4327 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4328 return( PSA_ERROR_BAD_STATE );
4329
mohammad1603503973b2018-03-12 15:59:30 +02004330 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004331
Moran Peker41deec42018-04-04 15:43:05 +03004332 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004333 operation->key_set = 0;
4334 operation->iv_set = 0;
4335 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004336 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004337 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004338
Moran Peker395db872018-05-31 14:07:14 +03004339 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004340}
4341
Gilles Peskinea0655c32018-04-30 17:06:50 +02004342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004343
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004344
mohammad16035955c982018-04-26 00:53:03 +03004345/****************************************************************/
4346/* AEAD */
4347/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004348
Gilles Peskineedf9a652018-08-17 18:11:56 +02004349typedef struct
4350{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004351 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004352 const mbedtls_cipher_info_t *cipher_info;
4353 union
4354 {
4355#if defined(MBEDTLS_CCM_C)
4356 mbedtls_ccm_context ccm;
4357#endif /* MBEDTLS_CCM_C */
4358#if defined(MBEDTLS_GCM_C)
4359 mbedtls_gcm_context gcm;
4360#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004361#if defined(MBEDTLS_CHACHAPOLY_C)
4362 mbedtls_chachapoly_context chachapoly;
4363#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004364 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004365 psa_algorithm_t core_alg;
4366 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004367 uint8_t tag_length;
4368} aead_operation_t;
4369
Gilles Peskine30a9e412019-01-14 18:36:12 +01004370static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004371{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004372 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004373 {
4374#if defined(MBEDTLS_CCM_C)
4375 case PSA_ALG_CCM:
4376 mbedtls_ccm_free( &operation->ctx.ccm );
4377 break;
4378#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004379#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004380 case PSA_ALG_GCM:
4381 mbedtls_gcm_free( &operation->ctx.gcm );
4382 break;
4383#endif /* MBEDTLS_GCM_C */
4384 }
4385}
4386
4387static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004388 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004389 psa_key_usage_t usage,
4390 psa_algorithm_t alg )
4391{
4392 psa_status_t status;
4393 size_t key_bits;
4394 mbedtls_cipher_id_t cipher_id;
4395
Gilles Peskine28f8f302019-07-24 13:30:31 +02004396 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397 if( status != PSA_SUCCESS )
4398 return( status );
4399
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004400 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004401
4402 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004403 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404 &cipher_id );
4405 if( operation->cipher_info == NULL )
4406 return( PSA_ERROR_NOT_SUPPORTED );
4407
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004408 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004409 {
4410#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004411 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4412 operation->core_alg = PSA_ALG_CCM;
4413 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004414 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4415 * The call to mbedtls_ccm_encrypt_and_tag or
4416 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004417 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004418 return( PSA_ERROR_INVALID_ARGUMENT );
4419 mbedtls_ccm_init( &operation->ctx.ccm );
4420 status = mbedtls_to_psa_error(
4421 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004422 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004423 (unsigned int) key_bits ) );
4424 if( status != 0 )
4425 goto cleanup;
4426 break;
4427#endif /* MBEDTLS_CCM_C */
4428
4429#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004430 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4431 operation->core_alg = PSA_ALG_GCM;
4432 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004433 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4434 * The call to mbedtls_gcm_crypt_and_tag or
4435 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004436 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004437 return( PSA_ERROR_INVALID_ARGUMENT );
4438 mbedtls_gcm_init( &operation->ctx.gcm );
4439 status = mbedtls_to_psa_error(
4440 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004441 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004442 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004443 if( status != 0 )
4444 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445 break;
4446#endif /* MBEDTLS_GCM_C */
4447
Gilles Peskine26869f22019-05-06 15:25:00 +02004448#if defined(MBEDTLS_CHACHAPOLY_C)
4449 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4450 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4451 operation->full_tag_length = 16;
4452 /* We only support the default tag length. */
4453 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4454 return( PSA_ERROR_NOT_SUPPORTED );
4455 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4456 status = mbedtls_to_psa_error(
4457 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004458 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004459 if( status != 0 )
4460 goto cleanup;
4461 break;
4462#endif /* MBEDTLS_CHACHAPOLY_C */
4463
Gilles Peskineedf9a652018-08-17 18:11:56 +02004464 default:
4465 return( PSA_ERROR_NOT_SUPPORTED );
4466 }
4467
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004468 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4469 {
4470 status = PSA_ERROR_INVALID_ARGUMENT;
4471 goto cleanup;
4472 }
4473 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004474
Gilles Peskineedf9a652018-08-17 18:11:56 +02004475 return( PSA_SUCCESS );
4476
4477cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004478 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004479 return( status );
4480}
4481
Gilles Peskinec5487a82018-12-03 18:08:14 +01004482psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004483 psa_algorithm_t alg,
4484 const uint8_t *nonce,
4485 size_t nonce_length,
4486 const uint8_t *additional_data,
4487 size_t additional_data_length,
4488 const uint8_t *plaintext,
4489 size_t plaintext_length,
4490 uint8_t *ciphertext,
4491 size_t ciphertext_size,
4492 size_t *ciphertext_length )
4493{
mohammad16035955c982018-04-26 00:53:03 +03004494 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004495 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004496 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004497
mohammad1603f08a5502018-06-03 15:05:47 +03004498 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004499
Gilles Peskinec5487a82018-12-03 18:08:14 +01004500 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004501 if( status != PSA_SUCCESS )
4502 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004503
Gilles Peskineedf9a652018-08-17 18:11:56 +02004504 /* For all currently supported modes, the tag is at the end of the
4505 * ciphertext. */
4506 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4507 {
4508 status = PSA_ERROR_BUFFER_TOO_SMALL;
4509 goto exit;
4510 }
4511 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004512
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004513#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004514 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004515 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004516 status = mbedtls_to_psa_error(
4517 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4518 MBEDTLS_GCM_ENCRYPT,
4519 plaintext_length,
4520 nonce, nonce_length,
4521 additional_data, additional_data_length,
4522 plaintext, ciphertext,
4523 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004524 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004525 else
4526#endif /* MBEDTLS_GCM_C */
4527#if defined(MBEDTLS_CCM_C)
4528 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004529 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004530 status = mbedtls_to_psa_error(
4531 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4532 plaintext_length,
4533 nonce, nonce_length,
4534 additional_data,
4535 additional_data_length,
4536 plaintext, ciphertext,
4537 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004538 }
mohammad16035c8845f2018-05-09 05:40:09 -07004539 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004540#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004541#if defined(MBEDTLS_CHACHAPOLY_C)
4542 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4543 {
4544 if( nonce_length != 12 || operation.tag_length != 16 )
4545 {
4546 status = PSA_ERROR_NOT_SUPPORTED;
4547 goto exit;
4548 }
4549 status = mbedtls_to_psa_error(
4550 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4551 plaintext_length,
4552 nonce,
4553 additional_data,
4554 additional_data_length,
4555 plaintext,
4556 ciphertext,
4557 tag ) );
4558 }
4559 else
4560#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004561 {
mohammad1603554faad2018-06-03 15:07:38 +03004562 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004563 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004564
Gilles Peskineedf9a652018-08-17 18:11:56 +02004565 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4566 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004567
Gilles Peskineedf9a652018-08-17 18:11:56 +02004568exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004569 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004570 if( status == PSA_SUCCESS )
4571 *ciphertext_length = plaintext_length + operation.tag_length;
4572 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004573}
4574
Gilles Peskineee652a32018-06-01 19:23:52 +02004575/* Locate the tag in a ciphertext buffer containing the encrypted data
4576 * followed by the tag. Return the length of the part preceding the tag in
4577 * *plaintext_length. This is the size of the plaintext in modes where
4578 * the encrypted data has the same size as the plaintext, such as
4579 * CCM and GCM. */
4580static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4581 const uint8_t *ciphertext,
4582 size_t ciphertext_length,
4583 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004584 const uint8_t **p_tag )
4585{
4586 size_t payload_length;
4587 if( tag_length > ciphertext_length )
4588 return( PSA_ERROR_INVALID_ARGUMENT );
4589 payload_length = ciphertext_length - tag_length;
4590 if( payload_length > plaintext_size )
4591 return( PSA_ERROR_BUFFER_TOO_SMALL );
4592 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004593 return( PSA_SUCCESS );
4594}
4595
Gilles Peskinec5487a82018-12-03 18:08:14 +01004596psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004597 psa_algorithm_t alg,
4598 const uint8_t *nonce,
4599 size_t nonce_length,
4600 const uint8_t *additional_data,
4601 size_t additional_data_length,
4602 const uint8_t *ciphertext,
4603 size_t ciphertext_length,
4604 uint8_t *plaintext,
4605 size_t plaintext_size,
4606 size_t *plaintext_length )
4607{
mohammad16035955c982018-04-26 00:53:03 +03004608 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004609 aead_operation_t operation;
4610 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004611
Gilles Peskineee652a32018-06-01 19:23:52 +02004612 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004613
Gilles Peskinec5487a82018-12-03 18:08:14 +01004614 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004615 if( status != PSA_SUCCESS )
4616 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004617
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004618 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4619 ciphertext, ciphertext_length,
4620 plaintext_size, &tag );
4621 if( status != PSA_SUCCESS )
4622 goto exit;
4623
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004624#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004625 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004626 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004627 status = mbedtls_to_psa_error(
4628 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4629 ciphertext_length - operation.tag_length,
4630 nonce, nonce_length,
4631 additional_data,
4632 additional_data_length,
4633 tag, operation.tag_length,
4634 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004635 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004636 else
4637#endif /* MBEDTLS_GCM_C */
4638#if defined(MBEDTLS_CCM_C)
4639 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004640 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004641 status = mbedtls_to_psa_error(
4642 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4643 ciphertext_length - operation.tag_length,
4644 nonce, nonce_length,
4645 additional_data,
4646 additional_data_length,
4647 ciphertext, plaintext,
4648 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004649 }
mohammad160339574652018-06-01 04:39:53 -07004650 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004651#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004652#if defined(MBEDTLS_CHACHAPOLY_C)
4653 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4654 {
4655 if( nonce_length != 12 || operation.tag_length != 16 )
4656 {
4657 status = PSA_ERROR_NOT_SUPPORTED;
4658 goto exit;
4659 }
4660 status = mbedtls_to_psa_error(
4661 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4662 ciphertext_length - operation.tag_length,
4663 nonce,
4664 additional_data,
4665 additional_data_length,
4666 tag,
4667 ciphertext,
4668 plaintext ) );
4669 }
4670 else
4671#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004672 {
mohammad1603554faad2018-06-03 15:07:38 +03004673 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004674 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004675
Gilles Peskineedf9a652018-08-17 18:11:56 +02004676 if( status != PSA_SUCCESS && plaintext_size != 0 )
4677 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004678
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004680 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004681 if( status == PSA_SUCCESS )
4682 *plaintext_length = ciphertext_length - operation.tag_length;
4683 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004684}
4685
Gilles Peskinea0655c32018-04-30 17:06:50 +02004686
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004687
Gilles Peskine20035e32018-02-03 22:44:14 +01004688/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004689/* Generators */
4690/****************************************************************/
4691
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004692#define HKDF_STATE_INIT 0 /* no input yet */
4693#define HKDF_STATE_STARTED 1 /* got salt */
4694#define HKDF_STATE_KEYED 2 /* got key */
4695#define HKDF_STATE_OUTPUT 3 /* output started */
4696
Gilles Peskinecbe66502019-05-16 16:59:18 +02004697static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004698 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004699{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4701 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004702 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004704}
4705
4706
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708{
4709 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004711 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004712 {
4713 /* The object has (apparently) been initialized but it is not
4714 * in use. It's ok to call abort on such an object, and there's
4715 * nothing to do. */
4716 }
4717 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004718#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004719 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004720 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 mbedtls_free( operation->ctx.hkdf.info );
4722 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004723 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004724 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004726 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004727 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004728 if( operation->ctx.tls12_prf.seed != NULL )
4729 {
4730 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4731 operation->ctx.tls12_prf.seed_length );
4732 mbedtls_free( operation->ctx.tls12_prf.seed );
4733 }
4734
4735 if( operation->ctx.tls12_prf.label != NULL )
4736 {
4737 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4738 operation->ctx.tls12_prf.label_length );
4739 mbedtls_free( operation->ctx.tls12_prf.label );
4740 }
4741
4742 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4743
4744 /* We leave the fields Ai and output_block to be erased safely by the
4745 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004746 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004747 else
4748#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749 {
4750 status = PSA_ERROR_BAD_STATE;
4751 }
Janos Follath083036a2019-06-11 10:22:26 +01004752 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753 return( status );
4754}
4755
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757 size_t *capacity)
4758{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004760 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004762 return PSA_ERROR_BAD_STATE;
4763 }
4764
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004766 return( PSA_SUCCESS );
4767}
4768
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004769psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004770 size_t capacity )
4771{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004773 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004775 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004777 return( PSA_SUCCESS );
4778}
4779
Gilles Peskinebef7f142018-07-12 17:22:21 +02004780#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004782 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004783static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004784 psa_algorithm_t hash_alg,
4785 uint8_t *output,
4786 size_t output_length )
4787{
4788 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4789 psa_status_t status;
4790
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004791 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4792 return( PSA_ERROR_BAD_STATE );
4793 hkdf->state = HKDF_STATE_OUTPUT;
4794
Gilles Peskinebef7f142018-07-12 17:22:21 +02004795 while( output_length != 0 )
4796 {
4797 /* Copy what remains of the current block */
4798 uint8_t n = hash_length - hkdf->offset_in_block;
4799 if( n > output_length )
4800 n = (uint8_t) output_length;
4801 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4802 output += n;
4803 output_length -= n;
4804 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004805 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004806 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004807 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004808 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004810 * object was corrupted or if this function is called directly
4811 * inside the library. */
4812 if( hkdf->block_number == 0xff )
4813 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004814
4815 /* We need a new block */
4816 ++hkdf->block_number;
4817 hkdf->offset_in_block = 0;
4818 status = psa_hmac_setup_internal( &hkdf->hmac,
4819 hkdf->prk, hash_length,
4820 hash_alg );
4821 if( status != PSA_SUCCESS )
4822 return( status );
4823 if( hkdf->block_number != 1 )
4824 {
4825 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4826 hkdf->output_block,
4827 hash_length );
4828 if( status != PSA_SUCCESS )
4829 return( status );
4830 }
4831 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4832 hkdf->info,
4833 hkdf->info_length );
4834 if( status != PSA_SUCCESS )
4835 return( status );
4836 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4837 &hkdf->block_number, 1 );
4838 if( status != PSA_SUCCESS )
4839 return( status );
4840 status = psa_hmac_finish_internal( &hkdf->hmac,
4841 hkdf->output_block,
4842 sizeof( hkdf->output_block ) );
4843 if( status != PSA_SUCCESS )
4844 return( status );
4845 }
4846
4847 return( PSA_SUCCESS );
4848}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004849
Janos Follath7742fee2019-06-17 12:58:10 +01004850static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4851 psa_tls12_prf_key_derivation_t *tls12_prf,
4852 psa_algorithm_t alg )
4853{
4854 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4855 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004856 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4857 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004858
Janos Follath7742fee2019-06-17 12:58:10 +01004859 /* We can't be wanting more output after block 0xff, otherwise
4860 * the capacity check in psa_key_derivation_output_bytes() would have
4861 * prevented this call. It could happen only if the operation
4862 * object was corrupted or if this function is called directly
4863 * inside the library. */
4864 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004865 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004866
4867 /* We need a new block */
4868 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004869 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004870
4871 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4872 *
4873 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4874 *
4875 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4876 * HMAC_hash(secret, A(2) + seed) +
4877 * HMAC_hash(secret, A(3) + seed) + ...
4878 *
4879 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004880 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004881 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004882 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004883 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004884 * is currently extracted as `output_block` and where i is
4885 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004886 */
4887
Janos Follathea29bfb2019-06-19 12:21:20 +01004888 /* Save the hash context before using it, to preserve the hash state with
4889 * only the inner padding in it. We need this, because inner padding depends
4890 * on the key (secret in the RFC's terminology). */
4891 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4892 if( status != PSA_SUCCESS )
4893 goto cleanup;
4894
4895 /* Calculate A(i) where i = tls12_prf->block_number. */
4896 if( tls12_prf->block_number == 1 )
4897 {
4898 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4899 * the variable seed and in this instance means it in the context of the
4900 * P_hash function, where seed = label + seed.) */
4901 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4902 tls12_prf->label, tls12_prf->label_length );
4903 if( status != PSA_SUCCESS )
4904 goto cleanup;
4905 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4906 tls12_prf->seed, tls12_prf->seed_length );
4907 if( status != PSA_SUCCESS )
4908 goto cleanup;
4909 }
4910 else
4911 {
4912 /* A(i) = HMAC_hash(secret, A(i-1)) */
4913 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4914 tls12_prf->Ai, hash_length );
4915 if( status != PSA_SUCCESS )
4916 goto cleanup;
4917 }
4918
4919 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4920 tls12_prf->Ai, hash_length );
4921 if( status != PSA_SUCCESS )
4922 goto cleanup;
4923 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4924 if( status != PSA_SUCCESS )
4925 goto cleanup;
4926
4927 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4928 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4929 tls12_prf->Ai, hash_length );
4930 if( status != PSA_SUCCESS )
4931 goto cleanup;
4932 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4933 tls12_prf->label, tls12_prf->label_length );
4934 if( status != PSA_SUCCESS )
4935 goto cleanup;
4936 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4937 tls12_prf->seed, tls12_prf->seed_length );
4938 if( status != PSA_SUCCESS )
4939 goto cleanup;
4940 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4941 tls12_prf->output_block, hash_length );
4942 if( status != PSA_SUCCESS )
4943 goto cleanup;
4944 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4945 if( status != PSA_SUCCESS )
4946 goto cleanup;
4947
Janos Follath7742fee2019-06-17 12:58:10 +01004948
4949cleanup:
4950
Janos Follathea29bfb2019-06-19 12:21:20 +01004951 cleanup_status = psa_hash_abort( &backup );
4952 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4953 status = cleanup_status;
4954
4955 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004956}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004957
Janos Follath844eb0e2019-06-19 12:10:49 +01004958static psa_status_t psa_key_derivation_tls12_prf_read(
4959 psa_tls12_prf_key_derivation_t *tls12_prf,
4960 psa_algorithm_t alg,
4961 uint8_t *output,
4962 size_t output_length )
4963{
4964 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4965 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4966 psa_status_t status;
4967 uint8_t offset, length;
4968
4969 while( output_length != 0 )
4970 {
4971 /* Check if we have fully processed the current block. */
4972 if( tls12_prf->left_in_block == 0 )
4973 {
4974 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4975 alg );
4976 if( status != PSA_SUCCESS )
4977 return( status );
4978
4979 continue;
4980 }
4981
4982 if( tls12_prf->left_in_block > output_length )
4983 length = (uint8_t) output_length;
4984 else
4985 length = tls12_prf->left_in_block;
4986
4987 offset = hash_length - tls12_prf->left_in_block;
4988 memcpy( output, tls12_prf->output_block + offset, length );
4989 output += length;
4990 output_length -= length;
4991 tls12_prf->left_in_block -= length;
4992 }
4993
4994 return( PSA_SUCCESS );
4995}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004996#endif /* MBEDTLS_MD_C */
4997
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004998psa_status_t psa_key_derivation_output_bytes(
4999 psa_key_derivation_operation_t *operation,
5000 uint8_t *output,
5001 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005002{
5003 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005005
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005006 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005007 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005009 return PSA_ERROR_BAD_STATE;
5010 }
5011
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005013 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005015 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005017 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005018 goto exit;
5019 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005021 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005023 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005024 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5025 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005028 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005029 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005031
Gilles Peskinebef7f142018-07-12 17:22:21 +02005032#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005033 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005034 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005035 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005036 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005037 output, output_length );
5038 }
Janos Follathadbec812019-06-14 11:05:39 +01005039 else
Janos Follathadbec812019-06-14 11:05:39 +01005040 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005042 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005045 output_length );
5046 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005047 else
5048#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005049 {
5050 return( PSA_ERROR_BAD_STATE );
5051 }
5052
5053exit:
5054 if( status != PSA_SUCCESS )
5055 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005056 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 * This allows us to differentiate between exhausted operations and
5058 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5059 * operations. */
5060 psa_algorithm_t alg = operation->alg;
5061 psa_key_derivation_abort( operation );
5062 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005063 memset( output, '!', output_length );
5064 }
5065 return( status );
5066}
5067
Gilles Peskine08542d82018-07-19 17:05:42 +02005068#if defined(MBEDTLS_DES_C)
5069static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5070{
5071 if( data_size >= 8 )
5072 mbedtls_des_key_set_parity( data );
5073 if( data_size >= 16 )
5074 mbedtls_des_key_set_parity( data + 8 );
5075 if( data_size >= 24 )
5076 mbedtls_des_key_set_parity( data + 16 );
5077}
5078#endif /* MBEDTLS_DES_C */
5079
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005080static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005081 psa_key_slot_t *slot,
5082 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084{
5085 uint8_t *data = NULL;
5086 size_t bytes = PSA_BITS_TO_BYTES( bits );
5087 psa_status_t status;
5088
Gilles Peskine8e338702019-07-30 20:06:31 +02005089 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005090 return( PSA_ERROR_INVALID_ARGUMENT );
5091 if( bits % 8 != 0 )
5092 return( PSA_ERROR_INVALID_ARGUMENT );
5093 data = mbedtls_calloc( 1, bytes );
5094 if( data == NULL )
5095 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5096
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005097 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005098 if( status != PSA_SUCCESS )
5099 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005100#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005101 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005102 psa_des_set_key_parity( data, bytes );
5103#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005104 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005105
5106exit:
5107 mbedtls_free( data );
5108 return( status );
5109}
5110
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005111psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005113 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005114{
5115 psa_status_t status;
5116 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005117 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005118
5119 /* Reject any attempt to create a zero-length key so that we don't
5120 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5121 if( psa_get_key_bits( attributes ) == 0 )
5122 return( PSA_ERROR_INVALID_ARGUMENT );
5123
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005124 if( ! operation->can_output_key )
5125 return( PSA_ERROR_NOT_PERMITTED );
5126
Gilles Peskinedf179142019-07-15 22:02:14 +02005127 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5128 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5130 if( driver != NULL )
5131 {
5132 /* Deriving a key in a secure element is not implemented yet. */
5133 status = PSA_ERROR_NOT_SUPPORTED;
5134 }
5135#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005136 if( status == PSA_SUCCESS )
5137 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005138 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005139 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005141 }
5142 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005143 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005144 if( status != PSA_SUCCESS )
5145 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005146 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005147 *handle = 0;
5148 }
5149 return( status );
5150}
5151
Gilles Peskineeab56e42018-07-12 17:12:33 +02005152
5153
5154/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005155/* Key derivation */
5156/****************************************************************/
5157
Gilles Peskine969c5d62019-01-16 15:53:06 +01005158static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005160 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005161{
Janos Follath5fe19732019-06-20 15:09:30 +01005162 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5163 * initialisers for this union leave some bytes unspecified.) */
5164 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5165
Gilles Peskine969c5d62019-01-16 15:53:06 +01005166 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005167#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005168 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5169 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5170 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005171 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005172 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005173 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5174 if( hash_size == 0 )
5175 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005176 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5177 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005178 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179 {
5180 return( PSA_ERROR_NOT_SUPPORTED );
5181 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005183 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005184 }
5185#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005188}
5189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005191 psa_algorithm_t alg )
5192{
5193 psa_status_t status;
5194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005195 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005196 return( PSA_ERROR_BAD_STATE );
5197
Gilles Peskine6843c292019-01-18 16:44:49 +01005198 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5199 return( PSA_ERROR_INVALID_ARGUMENT );
5200 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005201 {
5202 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 }
5205 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5206 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 else
5210 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005211
5212 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215}
5216
5217#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005218static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005219 psa_algorithm_t hash_alg,
5220 psa_key_derivation_step_t step,
5221 const uint8_t *data,
5222 size_t data_length )
5223{
5224 psa_status_t status;
5225 switch( step )
5226 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005227 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005228 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005230 status = psa_hmac_setup_internal( &hkdf->hmac,
5231 data, data_length,
5232 hash_alg );
5233 if( status != PSA_SUCCESS )
5234 return( status );
5235 hkdf->state = HKDF_STATE_STARTED;
5236 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005237 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005238 /* If no salt was provided, use an empty salt. */
5239 if( hkdf->state == HKDF_STATE_INIT )
5240 {
5241 status = psa_hmac_setup_internal( &hkdf->hmac,
5242 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005243 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005244 if( status != PSA_SUCCESS )
5245 return( status );
5246 hkdf->state = HKDF_STATE_STARTED;
5247 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005248 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005249 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005250 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5251 data, data_length );
5252 if( status != PSA_SUCCESS )
5253 return( status );
5254 status = psa_hmac_finish_internal( &hkdf->hmac,
5255 hkdf->prk,
5256 sizeof( hkdf->prk ) );
5257 if( status != PSA_SUCCESS )
5258 return( status );
5259 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5260 hkdf->block_number = 0;
5261 hkdf->state = HKDF_STATE_KEYED;
5262 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005263 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005264 if( hkdf->state == HKDF_STATE_OUTPUT )
5265 return( PSA_ERROR_BAD_STATE );
5266 if( hkdf->info_set )
5267 return( PSA_ERROR_BAD_STATE );
5268 hkdf->info_length = data_length;
5269 if( data_length != 0 )
5270 {
5271 hkdf->info = mbedtls_calloc( 1, data_length );
5272 if( hkdf->info == NULL )
5273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5274 memcpy( hkdf->info, data, data_length );
5275 }
5276 hkdf->info_set = 1;
5277 return( PSA_SUCCESS );
5278 default:
5279 return( PSA_ERROR_INVALID_ARGUMENT );
5280 }
5281}
Janos Follathb03233e2019-06-11 15:30:30 +01005282
Janos Follathf08e2652019-06-13 09:05:41 +01005283static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5284 const uint8_t *data,
5285 size_t data_length )
5286{
5287 if( prf->state != TLS12_PRF_STATE_INIT )
5288 return( PSA_ERROR_BAD_STATE );
5289
Janos Follathd6dce9f2019-07-04 09:11:38 +01005290 if( data_length != 0 )
5291 {
5292 prf->seed = mbedtls_calloc( 1, data_length );
5293 if( prf->seed == NULL )
5294 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005295
Janos Follathd6dce9f2019-07-04 09:11:38 +01005296 memcpy( prf->seed, data, data_length );
5297 prf->seed_length = data_length;
5298 }
Janos Follathf08e2652019-06-13 09:05:41 +01005299
5300 prf->state = TLS12_PRF_STATE_SEED_SET;
5301
5302 return( PSA_SUCCESS );
5303}
5304
Janos Follath81550542019-06-13 14:26:34 +01005305static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5306 psa_algorithm_t hash_alg,
5307 const uint8_t *data,
5308 size_t data_length )
5309{
5310 psa_status_t status;
5311 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5312 return( PSA_ERROR_BAD_STATE );
5313
5314 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5315 if( status != PSA_SUCCESS )
5316 return( status );
5317
5318 prf->state = TLS12_PRF_STATE_KEY_SET;
5319
5320 return( PSA_SUCCESS );
5321}
5322
Janos Follath6660f0e2019-06-17 08:44:03 +01005323static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5324 psa_tls12_prf_key_derivation_t *prf,
5325 psa_algorithm_t hash_alg,
5326 const uint8_t *data,
5327 size_t data_length )
5328{
5329 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005330 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5331 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005332
5333 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5334 return( PSA_ERROR_INVALID_ARGUMENT );
5335
5336 /* Quoting RFC 4279, Section 2:
5337 *
5338 * The premaster secret is formed as follows: if the PSK is N octets
5339 * long, concatenate a uint16 with the value N, N zero octets, a second
5340 * uint16 with the value N, and the PSK itself.
5341 */
5342
Janos Follath40e13932019-06-26 13:22:29 +01005343 *cur++ = ( data_length >> 8 ) & 0xff;
5344 *cur++ = ( data_length >> 0 ) & 0xff;
5345 memset( cur, 0, data_length );
5346 cur += data_length;
5347 *cur++ = pms[0];
5348 *cur++ = pms[1];
5349 memcpy( cur, data, data_length );
5350 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005351
Janos Follath40e13932019-06-26 13:22:29 +01005352 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005353
5354 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5355 return( status );
5356}
5357
Janos Follath63028dd2019-06-13 09:15:47 +01005358static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5359 const uint8_t *data,
5360 size_t data_length )
5361{
5362 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5363 return( PSA_ERROR_BAD_STATE );
5364
Janos Follathd6dce9f2019-07-04 09:11:38 +01005365 if( data_length != 0 )
5366 {
5367 prf->label = mbedtls_calloc( 1, data_length );
5368 if( prf->label == NULL )
5369 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005370
Janos Follathd6dce9f2019-07-04 09:11:38 +01005371 memcpy( prf->label, data, data_length );
5372 prf->label_length = data_length;
5373 }
Janos Follath63028dd2019-06-13 09:15:47 +01005374
5375 prf->state = TLS12_PRF_STATE_LABEL_SET;
5376
5377 return( PSA_SUCCESS );
5378}
5379
Janos Follathb03233e2019-06-11 15:30:30 +01005380static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5381 psa_algorithm_t hash_alg,
5382 psa_key_derivation_step_t step,
5383 const uint8_t *data,
5384 size_t data_length )
5385{
Janos Follathb03233e2019-06-11 15:30:30 +01005386 switch( step )
5387 {
Janos Follathf08e2652019-06-13 09:05:41 +01005388 case PSA_KEY_DERIVATION_INPUT_SEED:
5389 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005390 case PSA_KEY_DERIVATION_INPUT_SECRET:
5391 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005392 case PSA_KEY_DERIVATION_INPUT_LABEL:
5393 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005394 default:
5395 return( PSA_ERROR_INVALID_ARGUMENT );
5396 }
5397}
Janos Follath6660f0e2019-06-17 08:44:03 +01005398
5399static psa_status_t psa_tls12_prf_psk_to_ms_input(
5400 psa_tls12_prf_key_derivation_t *prf,
5401 psa_algorithm_t hash_alg,
5402 psa_key_derivation_step_t step,
5403 const uint8_t *data,
5404 size_t data_length )
5405{
5406 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005407 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005408 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5409 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005410 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005411
5412 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5413}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005414#endif /* MBEDTLS_MD_C */
5415
Gilles Peskineb8965192019-09-24 16:21:10 +02005416/** Check whether the given key type is acceptable for the given
5417 * input step of a key derivation.
5418 *
5419 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5420 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5421 * Both secret and non-secret inputs can alternatively have the type
5422 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5423 * that the input was passed as a buffer rather than via a key object.
5424 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005425static int psa_key_derivation_check_input_type(
5426 psa_key_derivation_step_t step,
5427 psa_key_type_t key_type )
5428{
5429 switch( step )
5430 {
5431 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005432 if( key_type == PSA_KEY_TYPE_DERIVE )
5433 return( PSA_SUCCESS );
5434 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005435 return( PSA_SUCCESS );
5436 break;
5437 case PSA_KEY_DERIVATION_INPUT_LABEL:
5438 case PSA_KEY_DERIVATION_INPUT_SALT:
5439 case PSA_KEY_DERIVATION_INPUT_INFO:
5440 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005441 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5442 return( PSA_SUCCESS );
5443 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005444 return( PSA_SUCCESS );
5445 break;
5446 }
5447 return( PSA_ERROR_INVALID_ARGUMENT );
5448}
5449
Janos Follathb80a94e2019-06-12 15:54:46 +01005450static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005451 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005452 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005453 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005454 const uint8_t *data,
5455 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005456{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005457 psa_status_t status;
5458 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5459
5460 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005461 if( status != PSA_SUCCESS )
5462 goto exit;
5463
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005464#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005465 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005467 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005468 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005469 step, data, data_length );
5470 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005471 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005472 {
Janos Follathb03233e2019-06-11 15:30:30 +01005473 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5474 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5475 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005476 }
5477 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5478 {
5479 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5480 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5481 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005482 }
5483 else
5484#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005485 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005487 return( PSA_ERROR_BAD_STATE );
5488 }
5489
Gilles Peskine224b0d62019-09-23 18:13:17 +02005490exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005491 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005493 return( status );
5494}
5495
Janos Follath51f4a0f2019-06-14 11:35:55 +01005496psa_status_t psa_key_derivation_input_bytes(
5497 psa_key_derivation_operation_t *operation,
5498 psa_key_derivation_step_t step,
5499 const uint8_t *data,
5500 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005501{
Gilles Peskineb8965192019-09-24 16:21:10 +02005502 return( psa_key_derivation_input_internal( operation, step,
5503 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005504 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005505}
5506
Janos Follath51f4a0f2019-06-14 11:35:55 +01005507psa_status_t psa_key_derivation_input_key(
5508 psa_key_derivation_operation_t *operation,
5509 psa_key_derivation_step_t step,
5510 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005511{
5512 psa_key_slot_t *slot;
5513 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005514
Gilles Peskine28f8f302019-07-24 13:30:31 +02005515 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005516 PSA_KEY_USAGE_DERIVE,
5517 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005518 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005519 {
5520 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005522 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005523
5524 /* Passing a key object as a SECRET input unlocks the permission
5525 * to output to a key object. */
5526 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5527 operation->can_output_key = 1;
5528
Janos Follathb80a94e2019-06-12 15:54:46 +01005529 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005530 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005531 slot->data.key.data,
5532 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005533}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005534
5535
5536
5537/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005538/* Key agreement */
5539/****************************************************************/
5540
Gilles Peskinea05219c2018-11-16 16:02:56 +01005541#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005542static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5543 size_t peer_key_length,
5544 const mbedtls_ecp_keypair *our_key,
5545 uint8_t *shared_secret,
5546 size_t shared_secret_size,
5547 size_t *shared_secret_length )
5548{
Steven Cooremanacda8342020-07-24 23:09:52 +02005549 mbedtls_ecp_keypair their_key;
5550 psa_key_slot_t their_key_slot;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005551 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005552 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005553 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005554 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005555 mbedtls_ecdh_init( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005556 memset(&their_key_slot, 0, sizeof(their_key_slot));
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005557
Steven Cooremanacda8342020-07-24 23:09:52 +02005558 /* Creating ephemeral key slot for import purposes */
5559 their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
5560 their_key_slot.data.key.data = (uint8_t*) peer_key;
5561 their_key_slot.data.key.bytes = peer_key_length;
5562
5563 status = psa_load_ecp_representation( &their_key_slot, &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005564 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005565 goto exit;
5566
Jaeden Amero97271b32019-01-10 19:38:51 +00005567 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005568 mbedtls_ecdh_get_params( &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005569 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005570 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005571 status = mbedtls_to_psa_error(
5572 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5573 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005574 goto exit;
5575
Jaeden Amero97271b32019-01-10 19:38:51 +00005576 status = mbedtls_to_psa_error(
5577 mbedtls_ecdh_calc_secret( &ecdh,
5578 shared_secret_length,
5579 shared_secret, shared_secret_size,
5580 mbedtls_ctr_drbg_random,
5581 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005582 if( status != PSA_SUCCESS )
5583 goto exit;
5584 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5585 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005586
5587exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005588 if( status != PSA_SUCCESS )
5589 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005590 mbedtls_ecdh_free( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005591 mbedtls_ecp_keypair_free( &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005592 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005593}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005594#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005595
Gilles Peskine01d718c2018-09-18 12:01:02 +02005596#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5597
Gilles Peskine0216fe12019-04-11 21:23:21 +02005598static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5599 psa_key_slot_t *private_key,
5600 const uint8_t *peer_key,
5601 size_t peer_key_length,
5602 uint8_t *shared_secret,
5603 size_t shared_secret_size,
5604 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005605{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005606 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005607 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005608#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005609 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005610 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005611 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005612 mbedtls_ecp_keypair ecp;
5613 psa_status_t status = psa_load_ecp_representation( private_key, &ecp );
5614 if( status != PSA_SUCCESS )
5615 return status;
5616 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
5617 &ecp,
5618 shared_secret, shared_secret_size,
5619 shared_secret_length );
5620 mbedtls_ecp_keypair_free( &ecp );
5621 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005622#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005623 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005624 (void) private_key;
5625 (void) peer_key;
5626 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005627 (void) shared_secret;
5628 (void) shared_secret_size;
5629 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005630 return( PSA_ERROR_NOT_SUPPORTED );
5631 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005632}
5633
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005634/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005635 * to potentially free embedded data structures and wipe confidential data.
5636 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005637static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005638 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005639 psa_key_slot_t *private_key,
5640 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005641 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005642{
5643 psa_status_t status;
5644 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5645 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005646 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005647
5648 /* Step 1: run the secret agreement algorithm to generate the shared
5649 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005650 status = psa_key_agreement_raw_internal( ka_alg,
5651 private_key,
5652 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005653 shared_secret,
5654 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005655 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005656 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005657 goto exit;
5658
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005659 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005660 * the shared secret. A shared secret is permitted wherever a key
5661 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005662 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005663 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005664 shared_secret,
5665 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005666
Gilles Peskine12313cd2018-06-20 00:20:32 +02005667exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005668 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005669 return( status );
5670}
5671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005672psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005673 psa_key_derivation_step_t step,
5674 psa_key_handle_t private_key,
5675 const uint8_t *peer_key,
5676 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005677{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005678 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005679 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005680 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005681 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005682 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005683 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005684 if( status != PSA_SUCCESS )
5685 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005687 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005688 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005689 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005691 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005692}
5693
Gilles Peskinebe697d82019-05-16 18:00:41 +02005694psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5695 psa_key_handle_t private_key,
5696 const uint8_t *peer_key,
5697 size_t peer_key_length,
5698 uint8_t *output,
5699 size_t output_size,
5700 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005701{
5702 psa_key_slot_t *slot;
5703 psa_status_t status;
5704
5705 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5706 {
5707 status = PSA_ERROR_INVALID_ARGUMENT;
5708 goto exit;
5709 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005710 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005711 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005712 if( status != PSA_SUCCESS )
5713 goto exit;
5714
5715 status = psa_key_agreement_raw_internal( alg, slot,
5716 peer_key, peer_key_length,
5717 output, output_size,
5718 output_length );
5719
5720exit:
5721 if( status != PSA_SUCCESS )
5722 {
5723 /* If an error happens and is not handled properly, the output
5724 * may be used as a key to protect sensitive data. Arrange for such
5725 * a key to be random, which is likely to result in decryption or
5726 * verification errors. This is better than filling the buffer with
5727 * some constant data such as zeros, which would result in the data
5728 * being protected with a reproducible, easily knowable key.
5729 */
5730 psa_generate_random( output, output_size );
5731 *output_length = output_size;
5732 }
5733 return( status );
5734}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005735
5736
5737/****************************************************************/
5738/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005739/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005740
Gilles Peskine4c317f42018-07-12 01:24:09 +02005741psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005742 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005743{
Janos Follath24eed8d2019-11-22 13:21:35 +00005744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005745 GUARD_MODULE_INITIALIZED;
5746
Gilles Peskinef181eca2019-08-07 13:49:00 +02005747 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5748 {
5749 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5750 output,
5751 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5752 if( ret != 0 )
5753 return( mbedtls_to_psa_error( ret ) );
5754 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5755 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5756 }
5757
Gilles Peskinee59236f2018-01-27 23:32:46 +01005758 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5759 return( mbedtls_to_psa_error( ret ) );
5760}
5761
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005762#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5763#include "mbedtls/entropy_poll.h"
5764
Gilles Peskine7228da22019-07-15 11:06:15 +02005765psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005766 size_t seed_size )
5767{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005768 if( global_data.initialized )
5769 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005770
5771 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5772 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5773 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5774 return( PSA_ERROR_INVALID_ARGUMENT );
5775
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005776 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005777}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005778#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005779
Gilles Peskinee56e8782019-04-26 17:34:02 +02005780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5781static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5782 size_t domain_parameters_size,
5783 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005784{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005785 size_t i;
5786 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787
Gilles Peskinee56e8782019-04-26 17:34:02 +02005788 if( domain_parameters_size == 0 )
5789 {
5790 *exponent = 65537;
5791 return( PSA_SUCCESS );
5792 }
5793
5794 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5795 * support values that fit in a 32-bit integer, which is larger than
5796 * int on just about every platform anyway. */
5797 if( domain_parameters_size > sizeof( acc ) )
5798 return( PSA_ERROR_NOT_SUPPORTED );
5799 for( i = 0; i < domain_parameters_size; i++ )
5800 acc = ( acc << 8 ) | domain_parameters[i];
5801 if( acc > INT_MAX )
5802 return( PSA_ERROR_NOT_SUPPORTED );
5803 *exponent = acc;
5804 return( PSA_SUCCESS );
5805}
5806#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5807
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005808static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005809 psa_key_slot_t *slot, size_t bits,
5810 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811{
Gilles Peskine8e338702019-07-30 20:06:31 +02005812 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005813
Gilles Peskinee56e8782019-04-26 17:34:02 +02005814 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815 return( PSA_ERROR_INVALID_ARGUMENT );
5816
Gilles Peskinee59236f2018-01-27 23:32:46 +01005817 if( key_type_is_raw_bytes( type ) )
5818 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005819 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005820
5821 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005822 if( status != PSA_SUCCESS )
5823 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005824
5825 /* Allocate memory for the key */
5826 slot->data.key.bytes = PSA_BITS_TO_BYTES( bits );
5827 slot->data.key.data = mbedtls_calloc( 1, slot->data.key.bytes );
5828 if( slot->data.key.data == NULL )
5829 {
5830 slot->data.key.bytes = 0;
5831 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5832 }
5833
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005834 status = psa_generate_random( slot->data.key.data,
5835 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005836 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005837 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005838
5839 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005840#if defined(MBEDTLS_DES_C)
5841 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005842 psa_des_set_key_parity( slot->data.key.data,
5843 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005844#endif /* MBEDTLS_DES_C */
5845 }
5846 else
5847
5848#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005849 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005851 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005853 int exponent;
5854 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005855 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5856 return( PSA_ERROR_NOT_SUPPORTED );
5857 /* Accept only byte-aligned keys, for the same reasons as
5858 * in psa_import_rsa_key(). */
5859 if( bits % 8 != 0 )
5860 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005861 status = psa_read_rsa_exponent( domain_parameters,
5862 domain_parameters_size,
5863 &exponent );
5864 if( status != PSA_SUCCESS )
5865 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005866 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5867 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005868 mbedtls_ctr_drbg_random,
5869 &global_data.ctr_drbg,
5870 (unsigned int) bits,
5871 exponent );
5872 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005874
5875 /* Make sure to always have an export representation available */
5876 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5877
5878 slot->data.key.data = mbedtls_calloc( 1, bytes );
5879 if( slot->data.key.data == NULL )
5880 {
5881 mbedtls_rsa_free( &rsa );
5882 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005883 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005884
5885 status = psa_export_rsa_key( type,
5886 &rsa,
5887 slot->data.key.data,
5888 bytes,
5889 &slot->data.key.bytes );
5890 mbedtls_rsa_free( &rsa );
5891 if( status != PSA_SUCCESS )
5892 {
5893 psa_remove_key_data_from_memory( slot );
5894 return( status );
5895 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005896 }
5897 else
5898#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5899
5900#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005901 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005902 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005903 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005904 mbedtls_ecp_group_id grp_id =
5905 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005906 const mbedtls_ecp_curve_info *curve_info =
5907 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005908 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005910 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005911 return( PSA_ERROR_NOT_SUPPORTED );
5912 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5913 return( PSA_ERROR_NOT_SUPPORTED );
5914 if( curve_info->bit_size != bits )
5915 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005916 mbedtls_ecp_keypair_init( &ecp );
5917 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005918 mbedtls_ctr_drbg_random,
5919 &global_data.ctr_drbg );
5920 if( ret != 0 )
5921 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005922 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005923 return( mbedtls_to_psa_error( ret ) );
5924 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005925
5926
5927 /* Make sure to always have an export representation available */
5928 size_t bytes = PSA_BITS_TO_BYTES( bits );
5929 slot->data.key.data = mbedtls_calloc( 1, bytes );
5930 if( slot->data.key.data == NULL )
5931 {
5932 mbedtls_ecp_keypair_free( &ecp );
5933 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5934 }
5935 slot->data.key.bytes = bytes;
5936 psa_status_t status = mbedtls_to_psa_error(
5937 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5938
5939 mbedtls_ecp_keypair_free( &ecp );
5940 if( status != PSA_SUCCESS )
5941 {
5942 psa_remove_key_data_from_memory( slot );
5943 return( status );
5944 }
5945 return( PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005946 }
5947 else
5948#endif /* MBEDTLS_ECP_C */
5949
5950 return( PSA_ERROR_NOT_SUPPORTED );
5951
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005952 return( PSA_SUCCESS );
5953}
Darryl Green0c6575a2018-11-07 16:05:30 +00005954
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005955psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005956 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005957{
5958 psa_status_t status;
5959 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005960 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005961
Gilles Peskine0f84d622019-09-12 19:03:13 +02005962 /* Reject any attempt to create a zero-length key so that we don't
5963 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5964 if( psa_get_key_bits( attributes ) == 0 )
5965 return( PSA_ERROR_INVALID_ARGUMENT );
5966
Gilles Peskinedf179142019-07-15 22:02:14 +02005967 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5968 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005969 if( status != PSA_SUCCESS )
5970 goto exit;
5971
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5973 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005974 {
Gilles Peskine11792082019-08-06 18:36:36 +02005975 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5976 size_t pubkey_length = 0; /* We don't support this feature yet */
5977 if( drv->key_management == NULL ||
5978 drv->key_management->p_generate == NULL )
5979 {
5980 status = PSA_ERROR_NOT_SUPPORTED;
5981 goto exit;
5982 }
5983 status = drv->key_management->p_generate(
5984 psa_get_se_driver_context( driver ),
5985 slot->data.se.slot_number, attributes,
5986 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005987 }
Gilles Peskine11792082019-08-06 18:36:36 +02005988 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005989#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005990 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005991 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005992 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005993 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005994 }
Gilles Peskine11792082019-08-06 18:36:36 +02005995
5996exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005997 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005998 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005999 if( status != PSA_SUCCESS )
6000 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006001 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006002 *handle = 0;
6003 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006004 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006005}
6006
6007
Gilles Peskinee59236f2018-01-27 23:32:46 +01006008
6009/****************************************************************/
6010/* Module setup */
6011/****************************************************************/
6012
Gilles Peskine5e769522018-11-20 21:59:56 +01006013psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6014 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6015 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6016{
6017 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6018 return( PSA_ERROR_BAD_STATE );
6019 global_data.entropy_init = entropy_init;
6020 global_data.entropy_free = entropy_free;
6021 return( PSA_SUCCESS );
6022}
6023
Gilles Peskinee59236f2018-01-27 23:32:46 +01006024void mbedtls_psa_crypto_free( void )
6025{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006026 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006027 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6028 {
6029 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006030 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006031 }
6032 /* Wipe all remaining data, including configuration.
6033 * In particular, this sets all state indicator to the value
6034 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006035 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006037 /* Unregister all secure element drivers, so that we restart from
6038 * a pristine state. */
6039 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006040#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006041}
6042
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006043#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6044/** Recover a transaction that was interrupted by a power failure.
6045 *
6046 * This function is called during initialization, before psa_crypto_init()
6047 * returns. If this function returns a failure status, the initialization
6048 * fails.
6049 */
6050static psa_status_t psa_crypto_recover_transaction(
6051 const psa_crypto_transaction_t *transaction )
6052{
6053 switch( transaction->unknown.type )
6054 {
6055 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6056 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006057 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006058 * is implemented.
6059 * https://github.com/ARMmbed/mbed-crypto/issues/218
6060 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006061 default:
6062 /* We found an unsupported transaction in the storage.
6063 * We don't know what state the storage is in. Give up. */
6064 return( PSA_ERROR_STORAGE_FAILURE );
6065 }
6066}
6067#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6068
Gilles Peskinee59236f2018-01-27 23:32:46 +01006069psa_status_t psa_crypto_init( void )
6070{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006071 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072 const unsigned char drbg_seed[] = "PSA";
6073
Gilles Peskinec6b69072018-11-20 21:42:52 +01006074 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006075 if( global_data.initialized != 0 )
6076 return( PSA_SUCCESS );
6077
Gilles Peskine5e769522018-11-20 21:59:56 +01006078 /* Set default configuration if
6079 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6080 if( global_data.entropy_init == NULL )
6081 global_data.entropy_init = mbedtls_entropy_init;
6082 if( global_data.entropy_free == NULL )
6083 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006084
Gilles Peskinec6b69072018-11-20 21:42:52 +01006085 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006086 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006087#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6088 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6089 /* The PSA entropy injection feature depends on using NV seed as an entropy
6090 * source. Add NV seed as an entropy source for PSA entropy injection. */
6091 mbedtls_entropy_add_source( &global_data.entropy,
6092 mbedtls_nv_seed_poll, NULL,
6093 MBEDTLS_ENTROPY_BLOCK_SIZE,
6094 MBEDTLS_ENTROPY_SOURCE_STRONG );
6095#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006096 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006097 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006098 status = mbedtls_to_psa_error(
6099 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6100 mbedtls_entropy_func,
6101 &global_data.entropy,
6102 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6103 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006105 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106
Gilles Peskine66fb1262018-12-10 16:29:04 +01006107 status = psa_initialize_key_slots( );
6108 if( status != PSA_SUCCESS )
6109 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006110
Gilles Peskined9348f22019-10-01 15:22:29 +02006111#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6112 status = psa_init_all_se_drivers( );
6113 if( status != PSA_SUCCESS )
6114 goto exit;
6115#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6116
Gilles Peskine4b734222019-07-24 15:56:31 +02006117#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006118 status = psa_crypto_load_transaction( );
6119 if( status == PSA_SUCCESS )
6120 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006121 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6122 if( status != PSA_SUCCESS )
6123 goto exit;
6124 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006125 }
6126 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6127 {
6128 /* There's no transaction to complete. It's all good. */
6129 status = PSA_SUCCESS;
6130 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006131#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006132
Gilles Peskinec6b69072018-11-20 21:42:52 +01006133 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134 global_data.initialized = 1;
6135
6136exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006137 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006138 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006139 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006140}
6141
6142#endif /* MBEDTLS_PSA_CRYPTO_C */